koris.util package

Submodules

koris.util.hue module

Hue

fancy colors

# magic! taken from https://github.com/s0md3v/hue

koris.util.hue.info(s, prefix='[!] ', key=33)
koris.util.hue.que(s, prefix='[?] ', key=34)
koris.util.hue.bad(s, prefix='[-] ', key=31)
koris.util.hue.good(s, prefix='[+] ', key=32)
koris.util.hue.run(s, prefix='[~] ', key=37)
koris.util.hue.green(s, prefix='', key=32)
koris.util.hue.lightgreen(s, prefix='', key=92)
koris.util.hue.grey(s, prefix='', key=37)
koris.util.hue.black(s, prefix='', key=30)
koris.util.hue.red(s, prefix='', key=31)
koris.util.hue.lightred(s, prefix='', key=91)
koris.util.hue.cyan(s, prefix='', key=36)
koris.util.hue.lightcyan(s, prefix='', key=96)
koris.util.hue.blue(s, prefix='', key=34)
koris.util.hue.lightblue(s, prefix='', key=94)
koris.util.hue.purple(s, prefix='', key=35)
koris.util.hue.yellow(s, prefix='', key=93)
koris.util.hue.white(s, prefix='', key=97)
koris.util.hue.lightpurple(s, prefix='', key=95)
koris.util.hue.orange(s, prefix='', key=33)
koris.util.hue.bg(s, prefix='', key=';7')
koris.util.hue.bold(s, prefix='', key=';1')
koris.util.hue.italic(s, prefix='', key='3')
koris.util.hue.under(s, prefix='', key='4')
koris.util.hue.strike(s, prefix='', key='09')

koris.util.util module

General purpose utilities

class koris.util.util.KorisVersionCheck(html_string)[source]

Bases: object

check the version published in the koris docs

check_is_latest(current_version)[source]

compare the published version on the docs to the current_version

class koris.util.util.TitleParser[source]

Bases: html.parser.HTMLParser

parse <title></title> from a given HTML page.

handle_data(data)[source]
handle_starttag(tag, attributes)[source]

handle the attributes of the page

koris.util.util.get_kubeconfig_yaml(master_uri, ca_cert, username, client_cert, client_key, encode=False)[source]

format a kube configuration file

koris.util.util.get_logger(name, level=20)[source]

return a logging.Logger instance which can be used in each module

koris.util.util.host_names[source]

format host names

koris.util.util.k8s_version_validation(version)[source]

Checks if a specified Kubernetes version is valid.

Parameters:version (str) – The version string to be checked
Returns:True if version is valid.
koris.util.util.name_validation(name)[source]

Validates a name that will be used as an instance name. Each name should conform to the following convention: not too long (maximum 244 characters) only ASCII-letters, numbers and dashes

Parameters:name (str) – The name to be checked
Returns:Name if valid, Exits with Status code 2 if name is invalid.
koris.util.util.retry(exceptions, tries=4, delay=3, backoff=2, logger=None)[source]

Retry calling the decorated function using an exponential backoff.

Parameters:
  • exceptions – The exception to check. may be a tuple of exceptions to check.
  • tries – Number of times to try (not retry) before giving up.
  • delay – Initial delay between retries in seconds.
  • backoff – Backoff multiplier (e.g. value of 2 will double the delay each retry).
  • logger – Logger to use. If None, print.

koris.util.logger module

This module defines logging capabilities for koris.

class koris.util.logger.Logger(name)[source]

Bases: object

This class provides logging capabilities.

This class is a singleton that returns as proxy instance of logging.Logger.

Before using, make sure to set Logger.LOG_LEVEL to the desired level.

The different levels are:

* 0 - quiet (no output)
* 1 - error
* 2 - warning
* 3 - info
* 4 - debug

A logger initiated with a specific level will print everything below (except 0) but not above.

All functions except for :meth`.Logger.question` support f-, %-, and format-Style formatting.

Example

>>> log = Logger(__name__)
>>> log.info("hello world")
[~] hello world
>>> log.info("%s %s", "hello", "world")
[~] hello world
>>> a, b = "hello", "world"
>>> log.info(f"{a} {b}")
[~] hello world
>>> log.info("{} {}".format(b, a))
[~] world hello
LOG_LEVEL

The log level to be used across the application.

Type:int
Parameters:name (str) – The name of the logger.
LOG_LEVEL = 3
debug(msg, *args, color=True, **kwargs)[source]

Logs a message on debug level.

If color is True, will be logged in grey with the current timestamp in brackets as prefix, else in plain.

Example

>>> log.debug("test")
[20190426-155611] test
>>> log.debug("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored.
error(msg, *args, color=True, **kwargs)[source]

Logs a message on error level.

If color is True, will be logged in red with [-], else in plain.

Example

>>> log.error("test")
[-] test
>>> log.error("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored.
info(msg, *args, color=True, **kwargs)[source]

Logs a message on info level.

If color is True, will be logged in grey with [~], else in plain.

Example

>>> log.info("test")
[~] test
>>> log.info("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored.
level

Returns the Python log level equivalent.

Returns:The Python loglevel equivalent or None if logger not instantiated.
static question(msg, color=True)[source]

Outputs a question.

Questions are unaffected by the log level and always printed.

This function does not support the %-formatting syntax.

If color is True, will be logged in green with [+], else in plain.

Example

>>> log.question("test")
[?] test
>>> log.question("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored
success(msg, *args, color=True, **kwargs)[source]

Indicates a success.

Messages are printend on info level.

If color is True, will be logged in green with [+], else in plain.

Example

>>> log.info("test")
[~] test
>>> log.success("test")
[+] test
>>> log.success("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored
warn(msg, *args, color=True, **kwargs)[source]

Convenience function to log on warning level.

Will just call Logger.warning().

Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored.
warning(msg, *args, color=True, **kwargs)[source]

Logs a message on warning level.

If color is True, will be logged in yellow with [!], else in plain.

Example

>>> log.warning("test")
[!] test
>>> log.warning("test", color=False)
test
Parameters:
  • msg (str) – The message to be logged.
  • color (bool) – If the message should be colored.
class koris.util.logger.Singleton[source]

Bases: type

Metaclass to implement the Singleton pattern.

This Metaclass implements the Singleton pattern. This should only be used logging purposes to avoid introducing mutable global state into the application.

Metaclasses are classes that instantiate other classes. Everytime a new class (any class in Python) is instantiated, it checks what the Metaclass of that specific class is, then executes it with certain parameters.

In our case, the metaclass holds a dictionary that keeps track of all the instances that have been created by the Singleton Metaclass. Everytime we instantiate or call let’s say koris.util.logger.Logger, whose Metaclass is the Singleton class, we check if we have already have such an instance. If yes, that one is returned. If not, we create such an instance, add it to the _instances dict and then return it. Subsequent calls will then only return one instance, which is always the same object with the same ID.

For more information about the pattern, see Eli’s Post and Stack Overflow.

Example

>>> log1 = Logger(__name__)
>>> log2 = Logger(__name__)
>>> log1.info("hello")
[~] hello
>>> log2.info("world")
[~] world
>>> log3 = Logger("koris")
>>> id(log1) == id(log2) == id(log3)
True
koris.util.logger.get_logger(name)[source]

Returns a Python logger.

Right now, only a single handler which logs to STDOUT can be added to a logger. This is because if multiple calls with the same name would add duplicate handlers to a logger, which lead to extra prints.

Parameters:name (str) – The name of the Logger.
Returns:A Python Logger.
koris.util.logger.set_level(logger, level)[source]

Sets the logging level.

See Python Logging Levels for more information on how the koris levels relate to the original Python levels.

Parameters:
  • logger – A Python logger object.
  • level (int) – The logging level.
Raises:

ValueError if log level is unsupported.

Module contents