Various useful functions.
Convert a timestamp using strptime and the format: %Y%m%dT%H:%M:%S.
Because various datetime formats are used, the following exceptions are handled:
Parameters: | timestamp (str) – The string containing the formatted date. |
---|---|
Returns: | The date. |
Return type: | datetime.datetime |
>>> datetime_tuple('20130226T06:23:12')
datetime.datetime(2013, 2, 26, 8, 23, 12)
Split an input into a list of arguments, return the number of the argument selected by pos.
If the position searched is outside the string, or in a space between words, then it will return the position of an hypothetical new argument.
See the doctests of the two methods for example behaviors.
Parameters: | |
---|---|
Return type: | int |
>>> find_argument_quoted(4, 'toto titi tata')
3
>>> find_argument_quoted(4, '"toto titi" tata')
0
>>> find_argument_quoted(8, '"toto" "titi tata"')
1
>>> find_argument_quoted(8, '"toto" "titi tata')
1
>>> find_argument_quoted(3, '"toto" "titi tata')
0
>>> find_argument_quoted(18, '"toto" "titi tata" ')
2
>>> find_argument_unquoted(2, 'toto titi tata')
0
>>> find_argument_unquoted(3, 'toto titi tata')
0
>>> find_argument_unquoted(6, 'toto titi tata')
1
>>> find_argument_unquoted(4, 'toto titi tata')
3
>>> find_argument_unquoted(25, 'toto titi tata')
3
Check if a message is delayed or not.
Parameters: | message (sleekxmpp.Message) – The message to check. |
---|---|
Returns: | A tuple containing (True, the datetime) or (False, None) |
Return type: | tuple |
Construct a string from a dict containing the “user gaming” informations. (for now, only use address and name)
Parameters: | infos (dict) – The informations |
---|---|
Returns: | The formatted string |
Return type: | str |
Contruct a string from a dict created from an “User tune” event.
Parameters: | infos (dict) – The informations |
---|---|
Returns: | The formatted string |
Return type: | str |
Convert the content of a file to base64
Parameters: | path (str) – The path of the file to convert. |
---|---|
Returns: | A tuple of (encoded data, mime type, sha1 hash) if the file exists and does not exceeds the upper size limit of 16384. |
Returns: | (None, None, error message) if it fails |
Return type: | tuple |
Returns a detailed and well formated string containing informations about the operating system
Return type: | str |
---|
Runs a command and returns its output.
Parameters: | command (str) – The command to run. |
---|---|
Returns: | The output or None |
Return type: | str |
Check if command is in the $PATH or not.
Parameters: | |
---|---|
Returns: | True if the command is found, the command path if the command is found and return_abs_path is True, otherwise False. |
Do the reverse operation of parse_str_to_secs().
Parse a number of seconds to a human-readable string. The string has the form XdXhXmXs. 0 units are removed.
Parameters: | duration (int) – The duration, in seconds. |
---|---|
Returns: | A formatted string containing the duration. |
Return type: | str |
>>> parse_secs_to_str(3601)
'1h1s'
Parse a string of with a number of d, h, m, s.
Parameters: | duration (str) – The formatted string. |
---|---|
Returns: | The number of seconds represented by the string |
Return type: | int |
>>> parse_str_to_secs("1d3m1h")
90180
Construct a sleekxmpp.JID object from a string.
Used to avoid tracebacks during is stringprep fails (fall back to a JID with an empty string).
Split a string correctly according to the quotes around the elements.
Parameters: | st (str) – The string to split. |
---|---|
Returns: | A list of the different of the string. |
Return type: | list |
>>> shell_split('"sdf 1" "toto 2"')
['sdf 1', 'toto 2']
>>> shell_split('toto "titi"')
['toto', 'titi']
>>> shell_split('toto ""')
['toto', '']
>>> shell_split('"toto titi" toto ""')
['toto titi', 'toto', '']
>>> shell_split('toto "titi')
['toto', 'titi']