lib.utils

This library contails the Utile-class for SmartHomeNG.

New helper-functions are going to be implemented in this library.

class lib.utils.Utils[Quellcode]

Bases: object

static check_hashed_password(pwd_to_check, hashed_pwd)[Quellcode]

Check if given plaintext password matches the hashed password An empty password is always rejected

Parameter:
  • pwd_to_check (str) – plaintext password to check

  • hashed_pwd (str) – hashed password

Rückgabe:

True: password matches, False: password does not match

Rückgabetyp:

bool

static create_hash(plaintext)[Quellcode]

Create hash (currently sha512) for given plaintext value

Parameter:

plaintext (str) – plaintext

Rückgabe:

hash of plaintext, lowercase letters

Rückgabetyp:

str

static execute_subprocess(commandline, wait=True)[Quellcode]

Executes a subprocess via a shell and returns the output written to stdout by this process as a string

static get_local_ipv4_address()[Quellcode]

Get local ipv4 address of the interface with the default gateway. Return ‚127.0.0.1‘ if no suitable interface is found NOTE: if more than one IP addresses are available and no external gateway is configured, thie method returns one of the configured addresses, but not deterministically.

Rückgabe:

IPv4 address as a string

Rückgabetyp:

string

static get_local_ipv6_address()[Quellcode]

Get local ipv6 address of the interface with the default gateway. Return ‚::1‘ if no suitable interface is found

Rückgabe:

IPv6 address as a string

Rückgabetyp:

string

static get_type(var)[Quellcode]

returns the type of the passed variable

Parameter:

var – Variable to get the type of

Rückgabe:

type of the var

Rückgabetyp:

str

static is_float(string)[Quellcode]

Checks if a string is a float.

Parameter:

string (str) – String to check.

Rückgabe:

True if a cast to float works, false otherwise.

Rückgabetyp:

bool

static is_hash(value)[Quellcode]

Check if value is a valid hash (currently sha512) value

Parameter:

value (str) – value to check

Rückgabe:

True: given value can be a sha512 hash, False: given value can not be a sha512 hash

Rückgabetyp:

bool

static is_hostname(string)[Quellcode]

Checks if a string is a valid hostname

The hostname has is checked to have a valid format

Parameter:

string (str) – String to check

Rückgabe:

True if a hostname, false otherwise.

Rückgabetyp:

bool

static is_int(string)[Quellcode]

Checks if a string is a integer.

Parameter:

string (str) – String to check.

Rückgabe:

True if a cast to int works, false otherwise.

Rückgabetyp:

bool

static is_ip(string)[Quellcode]

FUTURE: Checks if a string is a valid ip-address (v4 or v6) ACTUAL: redirects to ipv4 only check for backwards compatibility

Parameter:

string (str) – String to check

Rückgabe:

True if an ip, false otherwise.

Rückgabetyp:

bool

static is_ipv4(string)[Quellcode]

Checks if a string is a valid ip-address (v4)

Parameter:

string (str) – String to check

Rückgabe:

True if an ip, false otherwise.

Rückgabetyp:

bool

static is_ipv6(string)[Quellcode]

Checks if a string is a valid ip-address (v6)

Parameter:

string (str) – String to check

Rückgabe:

True if an ipv6, false otherwise.

Rückgabetyp:

bool

static is_knx_groupaddress(groupaddress)[Quellcode]

Checks if the passed string is a valid knx goup address

The checked format is:

main group (0-31 = 5 bits) middle group (0-7 = 3 bits) subgroup (0-255 = 8 bits)

Parameter:

groupaddress (str) – String to check

Rückgabe:

True if a groupaddress can be recognized, false otherwise.

Rückgabetyp:

bool

static is_mac(mac)[Quellcode]

Validates a MAC address

Parameter:

mac – MAC address

Rückgabe:

True if value is a MAC

Rückgabetyp:

bool

static is_timeframe(string)[Quellcode]

Checks if a string is a timeframe. A timeframe consists of a number and an optional unit identifier (e.g. 2h, 30m, …). Unit identifiers are: i for minutes, h for hours, d for days, w for weeks, m for months, y for years. If omitted milliseconds are assumed.

Parameter:

string (str) – String to check.

Rückgabe:

True if a timeframe can be recognized, false otherwise.

Rückgabetyp:

bool

static string_to_list(string)[Quellcode]

Convert a string to a list

If the parameter is not of type str, the parameter gest returned unchanged

If parameter string is - a list, it gets returned unchanged - a simple datatype other than string, it gets returned unchanged - an empty string, it gets returned unchanged - a non-empty string, it gets converted to a list of len=1 - format [<str>,<str>], it gets converted to a list

Parameter:

string (str) – string to convert

Rückgabe:

list of unchanged value

static strip_quotes(string)[Quellcode]

If a string contains quotes as first and last character, this function returns the string without quotes, otherwise the string is returned unchanged

Parameter:

string (str) – string to check for quotes

Rückgabe:

string with quotes stripped

Rückgabetyp:

str

static strip_quotes_fromlist(string)[Quellcode]

If a string representation of a list contains quotes as first and last character of a list entry, this function returns the string representation without the qoutes, otherwise the string is returned unchanged

Parameter:

string (str) – string representation of a list to check for quotes

Rückgabe:

string representation with square quotes stripped

Rückgabetyp:

str

static strip_square_brackets(string)[Quellcode]

If a string contains square brackets as first and last character, this function returns the string without the brackets, otherwise the string is returned unchanged

Parameter:

string (str) – string to check for square brackets

Rückgabe:

string with square brackets stripped

Rückgabetyp:

str

static to_bool(value, default='exception')[Quellcode]

Converts a value to boolean.

Raises exception if value is a string and can’t be converted and if no default value is given Case is ignored. These string values are allowed: - True: ‚True‘, „1“, „true“, „yes“, „y“, „t“, „on“ - False: „“, „0“, „faLse“, „no“, „n“, „f“, „off“ Non-string values are passed to bool constructor.

:param value : value to convert :type default: :param default: optional, value to return if value can not be parsed, if default is not set this method throws an exception :type value: str, object, int, … :type value: str, object, int, …

Rückgabe:

True if cant be converted and is true, False otherwise.

Rückgabetyp:

bool

static to_timeframe(value)[Quellcode]

Converts a timeframe value to milliseconds. See is_timeframe() method. The special value ‚now‘ is supported for the current time.

:param value : value to convert :type value: str, int, …

Rückgabe:

True if cant be converted and is true, False otherwise.

Rückgabetyp:

bool

class lib.utils.Version[Quellcode]

Bases: object

static check_list(versl)[Quellcode]
classmethod compare(v1, v2, operator)[Quellcode]

Compare two version numbers and return if the condition is met

Parameter:
  • v1 (str or list of int) –

  • v2 (str or list of int) –

  • operator (str) –

Rückgabe:

true if condition is met

Rückgabetyp:

bool

classmethod format(vers)[Quellcode]
classmethod to_list(vers)[Quellcode]

Split version number to list and get rid of non-numeric parts

Parameter:

vers

Rückgabe:

version as list

Rückgabetyp:

list

classmethod to_string(versl)[Quellcode]
lib.utils.execute_subprocess(commandline, wait=True)[Quellcode]

Executes a subprocess via a shell and returns the output written to stdout by this process as a string

lib.utils.get_python_version()[Quellcode]
lib.utils.running_virtual()[Quellcode]

Return if we run in a virtual environment (venv or virtualenv).