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ückgabetyp:

bool

Rückgabe:

True: password matches, False: password does not match

static create_hash(plaintext)[Quellcode]

Create hash (currently sha512) for given plaintext value

Parameter:

plaintext (str) – plaintext

Rückgabetyp:

str

Rückgabe:

hash of plaintext, lowercase letters

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_all_addresses_for_addressfamily(af)[Quellcode]

Get all addresses for an address-family as a list

https://stackoverflow.com/questions/270745/how-do-i-determine-all-of-my-ip-addresses-when-i-have-multiple-nics/274644#274644

Rückgabetyp:

list

Rückgabe:

addresses

static get_all_local_ipv4_addresses()[Quellcode]

Get ipv4 addresses of all interfaces as a list

Rückgabetyp:

list

Rückgabe:

ipv4 addresses

static get_all_local_ipv6_addresses()[Quellcode]

Get ipv6 addresses of all interfaces as a list

Rückgabetyp:

list

Rückgabe:

ipv6 addresses

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ückgabetyp:

bool

Rückgabe:

True if a cast to float works, false otherwise.

static is_hash(value)[Quellcode]

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

Parameter:

value (str) – value to check

Rückgabetyp:

bool

Rückgabe:

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

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ückgabetyp:

bool

Rückgabe:

True if a cast to int works, false otherwise.

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ückgabetyp:

str

Rückgabe:

string with quotes stripped

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: bool :param default: optional, value to return if value can not be parsed, if default is not set this method throws an exception

Rückgabetyp:

bool

Rückgabe:

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

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ückgabetyp:

int

Rückgabe:

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

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).