probator.utils

class probator.utils.NotificationContact(type, value)

Bases: object

probator.utils.deprecated(msg)

Marks a function / method as deprecated.

Takes one argument, a message to be logged with information on future usage of the function or alternative methods to call.

Parameters:msg (str) – Deprecation message to be logged
Returns:callable
probator.utils.get_hash(data)

Return the SHA256 hash of an object

>>> get_hash('my-test-string')
'56426297313df008f6ac9d4554b0724a2b3d39a7134bf2d9aede21ec680dd9c4'
Parameters:data – Object to hash
Returns:SHA256 hash of the object
probator.utils.is_truthy(value, default=False)

Evaluate a value for truthiness

>>> is_truthy('Yes')
True
>>> is_truthy('False')
False
>>> is_truthy(1)
True
Parameters:
  • value (Any) – Value to evaluate
  • default (bool) – Optional default value, if the input does not match the true or false values
Returns:

True if a truthy value is passed, else False

probator.utils.validate_email(email, partial_match=False)

Perform email address validation

>>> validate_email('user@domain.tld')
True
>>> validate_email('John Doe <user@domain.tld')
False
>>> validate_email('John Doe <user@domain.tld', partial_match=True)
True
Parameters:
  • email (str) – Email address to match
  • partial_match (bool) – If False (default), the entire string must be a valid email address. If true, any valid email address in the string will trigger a valid response
Returns:

True if the value contains an email address, else False

probator.utils.get_template(template)

Return a Jinja2 template by filename

Parameters:template (str) – Name of the template to return
Returns:A Jinja2 Template object
probator.utils.parse_bucket_info(domain)

Parse a domain name to gather the bucket name and region for an S3 bucket. Returns a tuple (bucket_name, bucket_region) if a valid domain name, else None

>>> parse_bucket_info('www.domain.tld.s3-website-us-west-2.amazonaws.com')
('www.domain.tld', 'us-west-2')
Parameters:domain (str) – Domain name to parse
Returns:str,`None`
Return type:list of str
probator.utils.to_utc_date(date)

Convert a datetime object from local to UTC format

>>> from datetime import datetime
>>> d = datetime(2017, 8, 15, 18, 24, 31)
>>> to_utc_date(d)
datetime.datetime(2017, 8, 16, 1, 24, 31)
Parameters:date (datetime) – Input datetime object
Returns:datetime
probator.utils.isoformat(date)

Convert a datetime object to a ISO 8601 formatted string, with added None type handling

>>> from datetime import datetime
>>> d = datetime(2017, 8, 15, 18, 24, 31)
>>> isoformat(d)
'2017-08-15T18:24:31'
Parameters:date (datetime) – Input datetime object
Returns:str
probator.utils.generate_password(length=32)

Generate a cryptographically secure random string to use for passwords

Parameters:length (int) – Length of password, defaults to 32 characters
Returns:Randomly generated string
probator.utils.generate_csrf_token()

Return a randomly generated string for use as CSRF Tokens

Returns:str
probator.utils.hash_password(password)

Return an argon2 hashed version of the password provided

password: Password to hash
Returns:String representing the hashed password
probator.utils.generate_jwt_token(user, authsys, **kwargs)

Generate a new JWT token, with optional extra information. Any data provided in **kwargs will be added into the token object for auth specific usage

Parameters:
  • user (User) – User object to generate token for
  • authsys (str) – The auth system for which the token was generated
  • **kwargs (dict) – Any optional items to add to the token
Returns:

Encoded JWT token

probator.utils.get_jwt_key_data()

Returns the data for the JWT private key used for encrypting the user login token as a string object

Returns:str
probator.utils.has_access(user, required_roles, match_all=True)

Check if the user meets the role requirements. If mode is set to AND, all the provided roles must apply

Parameters:
  • user (User) – User object
  • required_roles (list of str) – List of roles that the user must have applied
  • match_all (bool) – If true, all the required_roles must be applied to the user, else any one match will return True
Returns:

bool

probator.utils.merge_lists(*args)

Merge an arbitrary number of lists into a single list and dedupe it

Parameters:*args – Two or more lists
Returns:A deduped merged list of all the provided lists as a single list
probator.utils.to_camelcase(in_str)

Converts a string from snake_case to camelCase

>>> to_camelcase('convert_to_camel_case')
'convertToCamelCase'
Parameters:in_str (str) – String to convert
Returns:String formatted as camelCase
probator.utils.from_camelcase(in_str)

Converts a string from camelCase to snake_case

>>> from_camelcase('convertToPythonicCase')
'convert_to_pythonic_case'
Parameters:in_str (str) – String to convert
Returns:String formatted as snake_case
probator.utils.get_resource_id(prefix, *data)

Returns a unique ID based on the SHA256 hash of the provided data. The input data is flattened and sorted to ensure identical hashes are generated regardless of the order of the input. Values must be of types str, int or float, any other input type will raise a ValueError

>>> get_resource_id('ec2', 'lots', 'of', 'data')
'ec2-1d21940125214123'
>>> get_resource_id('ecs', 'foo', ['more', 'data', 'here', 2, 3])
'ecs-e536b036ea6fd463'
>>> get_resource_id('ecs', ['more'], 'data', 'here', [[2], 3], 'foo')
'ecs-e536b036ea6fd463'
Parameters:
  • prefix (str) – Key prefix
  • *data (str, int, float, list, tuple) – Data used to generate a unique ID
Returns:

str

probator.utils.parse_date(date_string, ignoretz=True)

Parse a string as a date. If the string fails to parse, None will be returned instead

>>> parse_date('2017-08-15T18:24:31')
datetime.datetime(2017, 8, 15, 18, 24, 31)
Parameters:
  • date_string (str) – Date in string format to parse
  • ignoretz (bool) – If set True, ignore time zones and return a naive datetime object.
Returns:

datetime, None

probator.utils.get_user_data_configuration()

Retrieve and update the application configuration with information from the user-data

Returns:None
probator.utils.read_config()

Attempts to read the application configuration file and will raise a FileNotFoundError if the configuration file is not found. Returns the folder where the configuration file was loaded from, and a Munch (dict-like object) containing the configuration

Configuration file paths searched, in order:
  • ~/.probator/config.json’
  • ./config.json
  • /usr/local/etc/probator/config.json
Returns:str, dict
probator.utils.flatten(data)

Returns a flattened version of a list.

Courtesy of https://stackoverflow.com/a/12472564

Parameters:data (tuple or list) – Input data
Returns:list
probator.utils.send_notification(*, subsystem, recipients, subject, body_html, body_text)

Method to send a notification. A plugin may use only part of the information, but all fields are required.

Parameters:
  • subsystem (str) – Name of the subsystem originating the notification
  • recipients (list of NotificationContact) – List of recipients
  • subject (str) – Subject / title of the notification
  • body_html (str) – HTML formatted version of the message
  • body_text (str) – Text formatted version of the message
Returns:

None

probator.utils.diff(a, b)

Return the difference between two strings

Will return a human-readable difference between two strings. See https://docs.python.org/3/library/difflib.html#difflib.Differ for more information about the output format

Parameters:
  • a (str) – Original string
  • b (str) – New string
Returns:

str

probator.utils.limit_value(*, value, min_value, max_value)

Limit a value to be between min_value and max_value

>>> limit_value(value=17, min_value=0, max_value=100)
17
>>> limit_value(value=231, min_value=0, max_value=100)
100
>>> limit_value(value=-16, min_value=0, max_value=100)
0
Parameters:
  • value (int or float) – The value to limit
  • min_value (int or float) – The minimum allowed value
  • max_value (int or float) – The maximum allowed value
Returns:

int or float