util¶
Common utility (static) functions.
-
yael.util.
VP_PATTERN_HW
= <_sre.SRE_Pattern object at 0x24d7a30>¶ pattern to match viewport value height=H, width=W
-
yael.util.
VP_PATTERN_WH
= <_sre.SRE_Pattern object at 0x24d7f40>¶ pattern to match viewport value width=W, height=H
-
yael.util.
clip_time_seconds
(string)[source]¶ Convert the given clip time string in seconds (possibly with decimal digits).
Parameters: string (str) – the clip time string to be converted Returns: the clip time in seconds Return type: float
-
yael.util.
directory_size
(path)[source]¶ Compute the total size, in bytes, of all the files in the filesystem tree rooted at the given directory.
Parameters: path (str) – the path of the root directory Returns: the total size in bytes of the subtree rooted at path Type: integer
-
yael.util.
is_valid
(obj, allowed_class, single=True)[source]¶ If single is True, return True if obj is an instance of allowed_class or None.
If single is False, return True if obj is a list (possibly, empty) of instances of allowed_class.
Parameters: - obj (object or list) – the object to be checked
- allowed_class (class) – the allowed class
- single (bool) – if True, obj must be a single object
Returns: whether the given object is valid
Return type: bool
-
yael.util.
list_all_files
(path)[source]¶ List all files in the filesystem tree rooted at the given directory.
Parameters: path (str) – the path to the root directory Returns: the list of files in the subtree rooted at path Type: list of str
-
yael.util.
norm_join
(path1, path2)[source]¶ Join the two given paths and normalize the result.
Parameters: - path1 (str) – prefix path
- path2 (str) – suffix path
Returns: the join of the two paths, normalized
Return type: str
-
yael.util.
norm_join_parent
(path1, path2)[source]¶ Join the parent directory of path1 with path2 and normalize the result.
Parameters: - path1 (str) – prefix path
- path2 (str) – suffix path
Returns: the join of the parent directory of path1 with path2, and normalize (e.g., “a/../b/c” => “b/c”) the result
Return type: str
-
yael.util.
obfuscate_data
(data, key, algorithm)[source]¶ Obfuscate/deobfuscate data with the given key and algorithm.
Parameters: - data (bytes) – the data to be obfuscated/deobfuscated
- key (str) – the string to be used as the obfuscation key
- algorithm (str) – the algorithm to be used (“adobe” or “idpf”)
Return type: bytes
-
yael.util.
parse_viewport_string
(string)[source]¶ Parse the given viewport value and return the corresponding dictionary {“width”: W, “height”: H}.
Parameters: string (str) – a viewport value Returns: {“width”: W, “height”: H} or None if string is not valid Return type: dict
-
yael.util.
query_xpath
(obj, query, args, nsp, required=None, formatted_query=None)[source]¶ Perform an xpath query on an XML (lxml) node obj.
The query template will be formatted using args and the namespaces nsp. (This works only in Python 2.6+.)
If required is not None and the result is empty, raise an exception.
If formatted_query is passed, use it instead of formatting query with args. (Useful if working in Python <2.6.)
Parameters: - obj (object) – the XML (lxml) node object
- query (str) – a string template to be formatted with args
- args (list of str) – a list of arguments to format the query
- nsp (dict) – namespace dictionary, mapping prefixes to namespace strings
- required (str) – required element
- formatted_query (str) – a pre-formatted query
Returns: the matched XML node objects
Return type: list of object
-
yael.util.
safe_first
(lis)[source]¶ Return the first element of the list, dealing safely with None or empty arguments.
Parameters: lis (list of object) – a list of objects or values Returns: the first element of the list (or None if lis is None or empty) Return type: object or value
-
yael.util.
safe_len
(lis)[source]¶ Return the number of elements of the list, dealing safely with non-list arguments.
Parameters: lis (list of object) – a list of objects or values Returns: the length of the list (or -1 if lis is not a list) Return type: int
-
yael.util.
safe_strip
(string)[source]¶ Strip the given string, dealing safely with None arguments.
Parameters: string (str) – the string to be stripped Returns: the stripped string (or None if string is None) Return type: str
-
yael.util.
split_reference
(string)[source]¶ Split the given reference (BASE#F) and return a dictionary {“base”: BASE, “fragment”: F}.
If there is no fragment, return {“base”: BASE}.
If the string is None, return {}.
Parameters: string (str) – a reference Returns: a dictionary containing base and fragment id Return type: dict