sysutils - CSTBox system level utilities¶
CSTBox system level utilities.
-
pycstbox.sysutils.
str_2_bool
(s)¶ Parses a boolean value provided as a string and returns it.
Accepted values for True:
true, t, yes, y, 1Any other value is converted as False
-
pycstbox.sysutils.
parse_period
(s)¶ Parses a period provided as a string and returns the equivalent number of seconds.
The accepted format is:
<nn> [ ‘s’ | ‘m’ | ‘h’ ]with:
<nn> : a positive integer valueThe suffix (if provided) indicates the units, with the following convention:
‘s’ : seconds ‘m’ : minutes ‘h’ : hoursIf not provided, units are defaulted to seconds.
Returns: the corresponding number of seconds. If the parameter is None or an empty string, 0 is return. Raises ValueError: if input string is not valid
-
class
pycstbox.sysutils.
LocalTimeUnits
(hour, minute, second)¶ -
hour
¶ Alias for field number 0
-
minute
¶ Alias for field number 1
-
second
¶ Alias for field number 2
-
-
pycstbox.sysutils.
human_friendly_delay_format
(secs, lang='en')¶ Returns a human friendly formatted string corresponding to the number of seconds.
Multiples (minutes, hours,...) will be used to render the value the friendliest possible.
Parameters: - secs (int) – the delay in seconds
- lang (str) – language code
Returns: a string such as “2 hours 12 minutes 5 seconds”
-
pycstbox.sysutils.
parse_time_of_day
(s)¶ Parses a string representing a (possibly abbreviated) time of day and returns the corresponding datetime.time instance.
Parsed string must be expressed in 24 hours format, and must contains at least hours and minutes. If not present, seconds are defaulted to 0. Fields are separated by colons.
Parameters: s (str) – the string to be parsed Returns: the corresponding time of day Return type: datetime.time Raise: ValueError if the parsed string is not a valid time of day
-
pycstbox.sysutils.
time_in_span
(t, start, end)¶ Returns if a given time of day is in the span defined by two bounds.
The following rule are applied, depending on t0 is before or after t1 : * t0 < t1 : returns True if t0 <= t <= t1 * t0 > t1 : returns True if t >+ t0 or t <= t1
The second case corresponds to an overnight time span.
Parameters: - t (datetime.time) – tested time of day
- start (datetime.time) – span starting time of day
- end (datetime.time) – span ending time of day
Returns: True if t in the time span
Return type: boolean
Raises ValueError: if any of the parameters is invalid of of t0 == t1
-
pycstbox.sysutils.
to_milliseconds
(ts)¶ Returns the milliseconds equivalent of a given time stamp.
If the parameter is a datetime instance, the result is the number of milliseconds elapsed from the epoch. The passed datetime is supposed to be naive or UTC.
If it is provided as an integer, it is just returned as is since supposed to be already converted.
Parameters: ts (datetime.datetime) – time stamp Returns: equivalent milliseconds from Epoch Return type: long
-
pycstbox.sysutils.
tod_to_num
(dt)¶ Returns a numeric version of a time of day, using the formula : result = 10000 * hour + 100 * minute + second + microsecond / 1000000.
Parameters: dt – a datetime of time Returns: the numeric “equivalent” of the time of day Return type: float
-
pycstbox.sysutils.
day_start_time
(day)¶ Returns the naive datetime of the beginning of a given day (i.e. hour, minute,... set to 0)
Parameters: day (datetime.date) – the day for which we want the start time Returns: the very first moment of the given day (as a UTC date time) Return type: datetime
-
pycstbox.sysutils.
day_end_time
(day)¶ Returns the naive datetime of the beginning of a given day (i.e. 1 epsilon time before the beginning of next day)
Parameters: day (datetime.date) – the day for which we want the end time Returns: the very last second of the given day (as a naive date time) Return type: datetime.datetime
-
pycstbox.sysutils.
day_bounds
(day)¶ Parameters: day (datetime.date) – the day for which we want the bounds Returns: a tuple containing the start and end times of the given day as naive datetime Return type: tuple of [datetime.datetime]
-
pycstbox.sysutils.
ts_to_datetime
(msecs, tz=<UTC>)¶ Returns a non naive datetime from the equivalent milliseconds count.
If not specified, the time zone is set to UTC.
The function is tolerant and accepts an already converted datetime or date. If it is not naive, it is adjusted to the requested time zone. If it is naive, its time zone is set to the supplied one.
Parameters: - msecs (int) – input time, supposed to be a number of milliseconds
- datetime.tzinfo – the timezone of the returned datetime
Returns: the equivalent UTC datetime
Return type: datetime.datetime
-
pycstbox.sysutils.
string_to_lines
(s)¶ Given a string containing lines separated by newlines, returns the equivalent list of strings, stripping the spaces in excess at both ends.
Parameters: s (str) – the concatenated lines as a string Returns: the concatenated lines as a list Return type: list of [str]
-
class
pycstbox.sysutils.
ServiceInformation
(descr, core, running)¶ Service descriptor namedtuple.
-
descr
¶ service short description
-
core
¶ flag telling if it is a core service (not user manageable) or not
-
running
¶ flag telling if the service is currently active or not
-
core
Alias for field number 1
-
descr
Alias for field number 0
-
running
Alias for field number 2
-
-
class
pycstbox.sysutils.
ServiceProperties
(descr, core)¶ Service properties namedtuple.
Is a subset of
ServiceInformation
-
core
¶ Alias for field number 1
-
descr
¶ Alias for field number 0
-
-
class
pycstbox.sysutils.
ServicesManager
¶ Mimics what is provided by the Linux ‘service’ command, applied to the CSTBox context.
-
known_services
¶ Returns the list of services currently installed.
Service names are returned in their shortened form, without the “cstbox-” prefix.
-
running_services
()¶ Returns the list of currently running CSTBox services.
Same remark as for known_services() method applies here with respect to the returned names.
-
get_service_info
(svc_name=None)¶ Returns the information about a given service, or about all installed services.
The returned information are packed in a ServiceInformation named tuple,
If a service name is passed, the result is a single tuple. If non no parameter is passed, the result is a dictionary containing the information about all installed services, keyed by the service name.
Parameters: svc_name (str) – the name of the service which information is asked for. If not given, information about all installed services are returned Returns: a ServiceInformation instance for the requested service, or a dictionary ServiceInformation instances for a parameterless call Raises ValueError: if a given service is requested but does not exist
-
start
(svc_name)¶ Starts a given service.
Parameters: svc_name (str) – the name of the service Raises ServicesManagerError: if the ‘service start’ command fails
-
stop
(svc_name)¶ Stops a given service.
Parameters: svc_name (str) – the name of the service Raises ServicesManagerError: if the ‘service stop’ command fails
-
restart
(svc_name)¶ Restarts a given service.
Parameters: svc_name (str) – the name of the service Raises ServicesManagerError: if the ‘service restart’ command fails
-
static
application_layer_restart
()¶ Restarts only the application layer services.
-
static
cstbox_restart
()¶ Restarts all CSTBox services, including the core ones.
-
static
system_reboot
()¶ Reboots the whole Linux system.
-
-
pycstbox.sysutils.
get_services_manager
()¶ Poor man’s singleton implementation, since it does not forbid to use ServicesManager() in your code.
But we are all adults here, and the ServicesManager is not intended to be used by application integrators. Don’t go with over-design if we can do without ;)
-
exception
pycstbox.sysutils.
ServicesManagerError
¶ Specialized error for CSTBox services management.
-
pycstbox.sysutils.
checked_dir
(path)¶ Internal helper for checking if the given path exists and is a directory.
If not, raises a ValueError exception. If yes, return the corresponding absolute path.
-
pycstbox.sysutils.
to_unicode
(s)¶ Converts a string to unicode if needed.