The synaps.utils Module

abspath(s)
align_metrictime(timestamp, resolution=60)

Align timestamp of metric for statistics

>>> align_metrictime(35.0, 60.0)
60.0
>>> align_metrictime(60.0, 60.0)
120.0
>>> align_metrictime(150.0, 60.0)
180.0
>>> align_metrictime(150.1, 60.0)
180.0       
cleanup_file_locks()

clean up stale locks left behind by process failures

The lockfile module, used by @synchronized, can leave stale lockfiles behind after process failure. These locks can cause process hangs at startup, when a process deadlocks on a lock which will never be unlocked.

Intended to be called at service startup.

datetime_to_timestamp(dt)
default_flagfile(filename='synaps.conf', args=None)
delete_if_exists(pathname)

delete a file, but ignore file not found error

dict_to_aws(pydict)
dumps(value)
execute(*cmd, **kwargs)

Helper method to execute command with optional retry.

:cmd Passed to subprocess.Popen. :process_input Send to opened process. :check_exit_code Defaults to 0. Raise exception.ProcessExecutionError

unless program exits with this code.
:delay_on_retry True | False. Defaults to True. If set to True, wait a
short amount of time before retrying.

:attempts How many times to retry cmd. :run_as_root True | False. Defaults to False. If set to True,

the command is prefixed by the command specified in the root_helper FLAG.

:raises exception.Error on receiving unknown arguments :raises exception.ProcessExecutionError

extract_member_dict(aws_dict, key='member')

it will convert from

{‘member’: {‘1’: {‘name’: u’member1’, ‘value’: u’value1’}},
{‘2’: {‘name’: u’member2’, ‘value’: u’value2’}}}

to

{u’member1’: u’value1’, u’member2’: u’value2’}

this is useful for processing dimension.

extract_member_list(aws_list, key='member')

ex) if key is ‘member’, it will convert from

{‘member’: {‘1’: ‘something1’,
‘2’: ‘something2’, ‘3’: ‘something3’}}

to

[‘something1’, ‘something2’, ‘something3’]

find_config(config_path)

Find a configuration file using the given hint.

Parameters:config_path – Full or relative path to the config.
Returns:Full path of the config, if it exists.
Raises :synaps.exception.ConfigNotFound
gen_uuid()
generate_metric_key(project_id, namespace, metric_name, dimensions)
get_python_novaclient()
import_class(import_str)

Returns a class from a string including module and class.

import_object(import_str)

Returns an object including a module or module and class.

isotime(at=None)

Returns iso formatted utcnow.

monkey_patch()

If the Flags.monkey_patch set as True, this function patches a decorator for all functions in specified modules. You can set decorators for each modules using FLAGS.monkey_patch_modules. The format is “Module path:Decorator function”. Example: ‘nova.api.ec2.cloud:nova.notifier.api.notify_decorator’

Parameters of the decorator is as follows. (See nova.notifier.api.notify_decorator)

name - name of the function function - object of the function

pack_dimensions(dimensions)
parse_groupnotification_action(action)
parse_instance_action(instance_action)
parse_server_string(server_str)

Parses the given server_string and returns a list of host and port. If it’s not a combination of host part and port, the port element is a null string. If the input is invalid expression, return a null list.

parse_strtime(timestr, fmt='%Y-%m-%dT%H:%M:%S.%f')

Turn a formatted time back into a datetime.

prefix_end(buf)
str_to_timestamp(timestr, fmt='%Y-%m-%dT%H:%M:%S.%f')
strcmp_const_time(s1, s2)

Constant-time string comparison.

Params s1:the first string
Params s2:the second string
Returns:True if the strings are equal.

This function takes two strings and compares them. It is intended to be used when doing a comparison for authentication purposes to help guard against timing attacks.

strtime(at=None, fmt='%Y-%m-%dT%H:%M:%S.%f')

Returns formatted utcnow.

strtime_trunk(at=None)

Returns formatted utcnow.

tempdir(*args, **kwds)
to_ascii(utf8)
to_default_unit(value, unit)
to_primitive(value, convert_instances=False, level=0)

Convert a complex object into primitives.

Handy for JSON serialization. We can optionally handle instances, but since this is a recursive function, we could have cyclical data structures.

To handle cyclical data structures we could track the actual objects visited in a set, but not all objects are hashable. Instead we just track the depth of the object inspections and don’t go too deep.

Therefore, convert_instances=True is lossy ... be aware.

to_unit(value, unit)
utcnow()

Overridable version of utils.utcnow.

utcnow_ts()

Timestamp version of our utcnow function.

utf8(value)

Try to turn a string into utf-8 if possible.

Code is directly from the utf8 function in http://github.com/facebook/tornado/blob/master/tornado/escape.py

validate_email(email)
validate_groupnotification_action(action)
validate_instance_action(instance_action)
validate_international_phonenumber(number)

International Telecommunication Union ITU-T Rec. E.123 (02/2001)

Notation for national and international telephone numbers, e-mail addresses and web addresses

xhtml_escape(value)

Escapes a string so it is valid within XML or XHTML.

Previous topic

The synaps.service Module

Next topic

The synaps.version Module

This Page