Utility functions for manipulating or working with text.
Class |
|
Splits byte data chunks into lines of text separated by newline. |
Class |
|
Helper class to write scripts with indentation. |
Function |
|
Build a safe shell command line from the given arguments. |
Function |
|
Nicely join a set of identifiers. |
Function |
|
Generates regular expression from DNS name globbing pattern. |
Function |
|
Encode a list in a way parsable by UnescapeAndSplit. |
Function |
|
Filters empty lines and comments from a line-based string. |
Function |
|
Formats a dictionary as "key=value" parameters. |
Function |
|
Formats a number as an ordinal in the English language. |
Function |
|
Formats seconds for easier reading. |
Function |
|
Formats a time value. |
Function |
|
Formats an incoming number of MiB with the appropriate unit. |
Function |
|
Generates a random secret. |
Function |
|
Verifies is the given word is safe from the shell's p.o.v. |
Function |
|
Try to match a name against a list. |
Function |
|
Normalizes and check if a MAC address is valid and contains six octets. |
Function |
|
Normalizes a potential MAC address prefix (three octets). |
Function |
|
Tries to extract number and scale from the given string. |
Function |
|
Return a 'safe' version of a source string. |
Function |
|
Out of a list of shell comands construct a single one. |
Function |
|
Quotes shell argument according to POSIX. |
Function |
|
Quotes a list of shell arguments. |
Function |
|
Truncate string and add ellipsis if needed. |
Function |
|
Split and unescape a string based on a given separator. |
Function | _ |
Helper function for DnsNameGlobPattern . |
Function | _ |
Checks a MAC address using a regular expression. |
Function | _ |
Builds a regular expression for verifying MAC addresses. |
Constant | _ASCII |
Undocumented |
Constant | _MAC |
Undocumented |
Constant | _MAC |
Undocumented |
Constant | _MAC |
Undocumented |
Constant | _PARSEUNIT |
Undocumented |
Constant | _SHELL |
Undocumented |
Constant | _SHELLPARAM |
Undocumented |
Build a safe shell command line from the given arguments.
This function will check all arguments in the args list so that they are valid shell parameters (i.e. they don't contain shell metacharacters). If everything is ok, it will return the result of template % args.
Parameters | |
template:str | the string holding the template for the string formatting |
*args | Undocumented |
Returns | |
str | the expanded command line |
Nicely join a set of identifiers.
Parameters | |
names | set, list or tuple |
Returns | |
a string with the formatted results |
Generates regular expression from DNS name globbing pattern.
A DNS name globbing pattern (e.g. *.site) is converted to a regular expression. Escape sequences or ranges (e.g. [a-z]) are not supported.
Matching always starts at the leftmost part. An asterisk (*) matches all characters except the dot (.) separating DNS name parts. A question mark (?) matches a single character except the dot (.).
Parameters | |
pattern:string | DNS name globbing pattern |
Returns | |
string | Regular expression |
Encode a list in a way parsable by UnescapeAndSplit.
Parameters | |
slist:list of strings | the strings to be encoded |
sep | Undocumented |
Returns | |
string | the encoding of the list oas a string |
Filters empty lines and comments from a line-based string.
Whitespace is also removed from the beginning and end of all lines.
Parameters | |
text:string | Input string |
Returns | |
list | Undocumented |
Formats a dictionary as "key=value" parameters.
The keys are sorted to have a stable order.
Parameters | |
data:dict | Undocumented |
Returns | |
list of string | Undocumented |
Formats a number as an ordinal in the English language.
E.g. the number 1 becomes "1st", 22 becomes "22nd".
Parameters | |
value:integer | Number |
Returns | |
string | Undocumented |
Formats seconds for easier reading.
Parameters | |
secs:number | Number of seconds |
Returns | |
string | Formatted seconds (e.g. "2d 9h 19m 49s") |
Formats a time value.
Parameters | |
val:float or None | Timestamp as returned by time.time() (seconds since Epoch, 1970-01-01 00:00:00 UTC) |
usecs | Undocumented |
Returns | |
a string value or N/A if we don't have a valid timestamp |
Formats an incoming number of MiB with the appropriate unit.
Parameters | |
value:int | integer representing the value in MiB (1048576) |
units:char | the type of formatting we should do:
|
roman | Undocumented |
Returns | |
str | the formatted value (with suffix) |
Generates a random secret.
This will generate a pseudo-random secret returning an hex string (so that it can be used where an ASCII string is needed).
Parameters | |
numbytes | the number of bytes which will be represented by the returned string (defaulting to 20, the length of a SHA1 hash) |
Returns | |
str | an hex representation of the pseudo-random sequence |
Verifies is the given word is safe from the shell's p.o.v.
This means that we can pass this to a command via the shell and be sure that it doesn't alter the command line and is passed as such to the actual command.
Note that we are overly restrictive here, in order to be on the safe side.
Parameters | |
word:str | the word to check |
Returns | |
boolean | True if the word is 'safe' |
Try to match a name against a list.
This function will try to match a name like test1 against a list like ['test1.example.com', 'test2.example.com', ...]. Against this list, 'test1' as well as 'test1.example' will match, but not 'test1.ex'. A multiple match will be considered as no match at all (e.g. 'test1' against ['test1.example.com', 'test1.example.org']), except when the key fully matches an entry (e.g. 'test1' against ['test1', 'test1.example.com']).
Parameters | |
key:str | the name to be searched |
name | the list of strings against which to search the key |
case | whether to provide a case-sensitive match |
Returns | |
None or str | None if there is no match or if there are multiple matches, otherwise the element from the list which matches |
Normalizes and check if a MAC address is valid and contains six octets.
Checks whether the supplied MAC address is formally correct. Accepts colon-separated format only. Normalize it to all lower case.
Parameters | |
mac:string | MAC address to be validated |
Returns | |
string | Normalized and validated MAC address |
Raises | |
errors.OpPrereqError | If the MAC address isn't valid |
Normalizes a potential MAC address prefix (three octets).
Checks whether the supplied string is a valid MAC address prefix consisting of three colon-separated octets. The result is normalized to all lower case.
Parameters | |
mac:string | Prefix to be validated |
Returns | |
string | Normalized and validated prefix |
Raises | |
errors.OpPrereqError | If the MAC address prefix isn't valid |
Tries to extract number and scale from the given string.
Input must be in the format NUMBER+ [DOT NUMBER+] SPACE* [UNIT]. If no unit is specified, it defaults to MiB. Return value is always an int in MiB.
Return a 'safe' version of a source string.
This function mangles the input string and returns a version that should be safe to display/encode as ASCII. To this end, we first convert it to ASCII using the 'backslashreplace' encoding which should get rid of any non-ASCII chars, and then we process it through a loop copied from the string repr sources in the python; we don't use string_escape anymore since that escape single quotes and backslashes too, and that is too much; and that escaping is not stable, i.e. string_escape(string_escape(x)) != string_escape(x).
Parameters | |
text:str or unicode | input data |
Returns | |
str | a safe version of text |
Quotes shell argument according to POSIX.
Parameters | |
value:str | the argument to be quoted |
Returns | |
str | the quoted value |
Quotes a list of shell arguments.
Parameters | |
args:list | list of arguments to be quoted |
Returns | |
str | the quoted arguments concatenated with spaces |
Truncate string and add ellipsis if needed.
Parameters | |
text:string | Text |
length:integer | Desired length |
Returns | |
string | Truncated text |
Split and unescape a string based on a given separator.
This function splits a string based on a separator where the separator itself can be escape in order to be an element of the elements. The escaping rules are (assuming coma being the separator):
- a plain , separates the elements
- a sequence \\\\, (double backslash plus comma) is handled as a backslash plus a separator comma
- a sequence \, (backslash plus comma) is handled as a non-separator comma
Parameters | |
text:string | the separator |
sep:string | Undocumented |
Returns | |
string | a list of strings |
Helper function for DnsNameGlobPattern
.
Returns regular expression pattern for parts of the pattern.
Checks a MAC address using a regular expression.
Parameters | |
check | Compiled regular expression as returned by re.compile |
mac:string | MAC address to be validated |
msg:string | Error message (%s will be replaced with MAC address) |