core.utils.string

ViUR utility functions regarding string processing.

Module Contents

Functions

random([length])

Return a string containing random characters of given length.

escape(val[, max_length, maxLength])

Quotes special characters from a string and removes "\0".

unescape(val)

Unquotes characters formerly escaped by escape.

is_prefix(name, prefix[, delimiter])

Utility function to check if a given name matches a prefix,

Attributes

__STRING_ESCAPE_MAPPING

__STRING_ESCAPE_TRANSTAB

__STRING_UNESCAPE_MAPPING

core.utils.string.random(length=13)

Return a string containing random characters of given length. It’s safe to use this string in URLs or HTML. Because we use the secrets module it could be used for security purposes as well

Parameters:

length (int) – The desired length of the generated string.

Returns:

A string with random characters of the given length.

Return type:

str

core.utils.string.__STRING_ESCAPE_MAPPING
core.utils.string.__STRING_ESCAPE_TRANSTAB
core.utils.string.__STRING_UNESCAPE_MAPPING
core.utils.string.escape(val, max_length=254, maxLength=None)

Quotes special characters from a string and removes “\0”. It shall be used to prevent XSS injections in data.

Parameters:
  • val (str) – The value to be escaped.

  • max_length (int | None) – Cut-off after max_length characters. None or 0 means “unlimited”.

  • maxLength (int | None) –

Returns:

The quoted string.

Return type:

str

core.utils.string.unescape(val)

Unquotes characters formerly escaped by escape.

Parameters:
  • val (str) – The value to be unescaped.

  • max_length – Optional cut-off after max_length characters. A value of None or 0 means “unlimited”.

Returns:

The unquoted string.

Return type:

str

core.utils.string.is_prefix(name, prefix, delimiter='.')

Utility function to check if a given name matches a prefix, which defines a specialization, delimited by delimiter.

In ViUR, modules, bones, renders, etc. provide a kind or handler to classify or subclassify the specific object. To securitly check for a specific type, it is either required to ask for the exact type or if its prefixed by a path delimited normally by dots.

Example:

Parameters:
  • name (str) –

  • prefix (str) –

  • delimiter (str) –

Return type:

bool