globalfunctions¶
Global common functions.
-
aeneas.globalfunctions.absolute_path(path, from_file)[source]¶ Return the absolute path of a file or directory, specified as
pathrelative to (the parent directory of)from_file.This method is intented to be called with
__file__as second argument.If
pathisNone, returnNone.Example:
path="res/foo.bar" from_file="/abc/def/ghi.py" => "/abc/def/res/foo.bar"
Parameters: - path (string) – the file path
- from_file (string) – the reference file
Return type: string
-
aeneas.globalfunctions.bundle_directory()[source]¶ Return the absolute path of the bundle directory if running from a frozen binary; otherwise return
None.Return type: string
-
aeneas.globalfunctions.can_run_c_extension(name=None)[source]¶ Determine whether the given Python C extension loads correctly.
If
nameisNone, tests all Python C extensions, and returnTrueif and only if all load correctly.Parameters: name (string) – the name of the Python C extension to test Return type: bool
-
aeneas.globalfunctions.close_file_handler(handler)[source]¶ Safely close the given file handler.
Parameters: handler (object) – the file handler (as returned by tempfile)
-
aeneas.globalfunctions.config_dict_to_string(dictionary)[source]¶ Convert a given config dictionary
dictionary[key_1] = value_1 dictionary[key_2] = value_2 ... dictionary[key_n] = value_n
into the corresponding string
key_1=value_1|key_2=value_2|...|key_n=value_n
Parameters: dictionary (dict) – the config dictionary Return type: string
-
aeneas.globalfunctions.config_string_to_dict(string, result=None)[source]¶ Convert a given configuration string
key_1=value_1|key_2=value_2|...|key_n=value_n
into the corresponding dictionary
dictionary[key_1] = value_1 dictionary[key_2] = value_2 ... dictionary[key_n] = value_n
Parameters: string (string) – the configuration string Return type: dict
-
aeneas.globalfunctions.config_txt_to_string(string)[source]¶ Convert the contents of a TXT config file into the corresponding configuration string
key_1=value_1|key_2=value_2|...|key_n=value_n
Leading and trailing blank characters will be stripped and empty lines (after stripping) will be ignored.
Parameters: string (string) – the contents of a TXT config file Return type: string
-
aeneas.globalfunctions.config_xml_to_dict(contents, result, parse_job=True)[source]¶ Convert the contents of a XML config file into the corresponding dictionary
dictionary[key_1] = value_1 dictionary[key_2] = value_2 ... dictionary[key_n] = value_n
Parameters: - contents (bytes) – the XML configuration contents
- parse_job (bool) – if
True, parse the job properties; ifFalse, parse the tasks properties
Return type: dict (
parse_job=True) or list of dict (parse_job=False)
-
aeneas.globalfunctions.copytree(source_directory, destination_directory, ignore=None)[source]¶ Recursively copy the contents of a source directory into a destination directory. Both directories must exist.
This function does not copy the root directory
source_directoryintodestination_directory.Since
shutil.copytree(src, dst)requiresdstnot to exist, we cannot use for our purposes.Code adapted from http://stackoverflow.com/a/12686557
Parameters: - source_directory (string) – the source directory, already existing
- destination_directory (string) – the destination directory, already existing
-
aeneas.globalfunctions.custom_tmp_dir()[source]¶ Return the path of the temporary directory to use.
On POSIX OSes (Linux and OS X), return the value of
TMP_PATH_DEFAULT_POSIX(e.g.,/tmp/).On non-POSIX OSes, return the value of
TMP_PATH_DEFAULT_NONPOSIX(i.e.,None), so thattempfilewill use the directory specified by the environment/userTMPorTEMPvariable.Return type: string
-
aeneas.globalfunctions.datetime_string(time_zone=False)[source]¶ Return a string representing the current date and time, in
YYYY-MM-DDThh:mm:ssorYYYY-MM-DDThh:mm:ss+hh:mmformatParameters: time_zone (boolean) – if True, add the time zone offset.Return type: string
-
aeneas.globalfunctions.delete_directory(path)[source]¶ Safely delete a directory.
Parameters: path (string) – the file path
-
aeneas.globalfunctions.delete_file(handler, path)[source]¶ Safely delete file.
Parameters: - handler (object) – the file handler (as returned by tempfile)
- path (string) – the file path
-
aeneas.globalfunctions.directory_exists(path)[source]¶ Return
Trueif the givenpathstring points to an existing directory.Parameters: path (string) – the file path Return type: bool
-
aeneas.globalfunctions.ensure_parent_directory(path, ensure_parent=True)[source]¶ Ensures the parent directory exists.
Parameters: - path (string) – the path of the file
- ensure_parent (bool) – if
True, ensure the parent directory ofpathexists; ifFalse, ensurepathexists
Raises: OSError: if the path cannot be created
-
aeneas.globalfunctions.file_can_be_read(path)[source]¶ Return
Trueif the file at the givenpathcan be read.Parameters: path (string) – the file path Return type: bool New in version 1.4.0.
-
aeneas.globalfunctions.file_can_be_written(path)[source]¶ Return
Trueif a file can be written at the givenpath.Parameters: path (string) – the file path Return type: bool Warning
This function will attempt to open the given
pathin write mode, possibly destroying the file previously existing there.New in version 1.4.0.
-
aeneas.globalfunctions.file_exists(path)[source]¶ Return
Trueif the givenpathstring points to an existing file.Parameters: path (string) – the file path Return type: bool
-
aeneas.globalfunctions.file_extension(path)[source]¶ Return the file extension.
Examples:
/foo/bar.baz => baz None => None
Parameters: path (string) – the file path Return type: string
-
aeneas.globalfunctions.file_name_without_extension(path)[source]¶ Return the file name without extension.
Examples:
/foo/bar.baz => bar /foo/bar => bar None => None
Parameters: path (string) – the file path Return type: string
-
aeneas.globalfunctions.file_size(path)[source]¶ Return the size, in bytes, of the file at the given
path. Return-1if the file does not exist or cannot be read.Parameters: path (string) – the file path Return type: int
-
aeneas.globalfunctions.fix_slash(path)[source]¶ On non-POSIX OSes, change the slashes in
pathfor loading in the browser.Example:
c:\abc\def => c:/abc/def
Parameters: path (string) – the path Return type: string
-
aeneas.globalfunctions.human_readable_number(number, suffix='')[source]¶ Format the given number into a human-readable string.
Code adapted from http://stackoverflow.com/a/1094933
Parameters: - number (variant) – the number (int or float)
- suffix (string) – the unit of the number
Return type: string
-
aeneas.globalfunctions.is_bytes(string)[source]¶ Return
Trueif the given string is a sequence of bytes.Parameters: string (variant) – the string to test Return type: bool
-
aeneas.globalfunctions.is_osx()[source]¶ Return
Trueif running on Mac OS X (Darwin).Return type: bool
-
aeneas.globalfunctions.is_py2_narrow_build()[source]¶ Return
Trueif running on a Python 2 narrow build.Return type: bool
-
aeneas.globalfunctions.is_unicode(string)[source]¶ Return
Trueif the given string is a sequence of Unicode code points.Parameters: string (variant) – the string to test Return type: bool
-
aeneas.globalfunctions.is_utf8_encoded(bstring)[source]¶ Return
Trueif the given byte string can be decoded into a Unicode string using the UTF-8 decoder.Parameters: bstring (bytes) – the string to test Return type: bool
-
aeneas.globalfunctions.mimetype_from_path(path)[source]¶ Return a mimetype from the file extension.
Parameters: path (string) – the file path Return type: string
-
aeneas.globalfunctions.norm_join(prefix, suffix)[source]¶ Join
prefixandsuffixpaths and return the resulting path, normalized.Parameters: - prefix (string) – the prefix path
- suffix (string) – the suffix path
Return type: string
-
aeneas.globalfunctions.object_to_bytes(obj)[source]¶ Return a sequence of bytes from the given object.
Parameters: obj (object) – the object Return type: bytes
-
aeneas.globalfunctions.object_to_unicode(obj)[source]¶ Return a sequence of Unicode code points from the given object.
Parameters: obj (object) – the object Return type: string
-
aeneas.globalfunctions.pairs_to_dict(pairs, result=None)[source]¶ Convert a given list of
key=valuestrings["key_1=value_1", "key_2=value_2", ..., "key_n=value_n"]
into the corresponding dictionary
dictionary[key_1] = value_1 dictionary[key_2] = value_2 ... dictionary[key_n] = value_n
Parameters: pairs (list) – the list of key=value strings Return type: dict
-
aeneas.globalfunctions.print_error(msg, color=True)[source]¶ Print an error message.
Parameters: - msg (string) – the message
- color (bool) – if
True, print with POSIX color
-
aeneas.globalfunctions.print_info(msg, color=True)[source]¶ Print an info message.
Parameters: - msg (string) – the message
- color (bool) – if
True, print with POSIX color
-
aeneas.globalfunctions.print_success(msg, color=True)[source]¶ Print a success message.
Parameters: - msg (string) – the message
- color (bool) – if
True, print with POSIX color
-
aeneas.globalfunctions.print_warning(msg, color=True)[source]¶ Print a warning message.
Parameters: - msg (string) – the message
- color (bool) – if
True, print with POSIX color
-
aeneas.globalfunctions.read_file_bytes(input_file_path)[source]¶ Read the file at the given file path and return its contents as a byte string, or
Noneif an error occurred.Parameters: input_file_path (string) – the file path Return type: bytes
-
aeneas.globalfunctions.relative_path(path, from_file)[source]¶ Return the relative path of a file or directory, specified as
pathrelative to (the parent directory of)from_file.This method is intented to be called with
__file__as second argument.The returned path is relative to the current working directory.
If
pathisNone, returnNone.Example:
path="res/foo.bar" from_file="/root/abc/def/ghi.py" cwd="/root" => "abc/def/res/foo.bar"
Parameters: - path (string) – the file path
- from_file (string) – the reference file
Return type: string
-
aeneas.globalfunctions.run_c_extension_with_fallback(log_function, extension, c_function, py_function, args, rconf)[source]¶ Run a function calling a C extension, falling back to a pure Python function if the former does not succeed.
Parameters: - log_function (function) – a logger function
- extension (string) – the name of the extension
- c_function (function) – the (Python) function calling the C extension
- py_function (function) – the (Python) function providing the fallback
- rconf (
aeneas.runtimeconfiguration.RuntimeConfiguration) – the runtime configuration
Return type: depends on the extension being called
Raises: RuntimeError: if both the C extension and the pure Python code did not succeed.
New in version 1.4.0.
-
aeneas.globalfunctions.safe_bytes(string)[source]¶ Safely convert the given string to a bytes string.
Parameters: string (variant) – the byte string or Unicode string to convert Return type: bytes
-
aeneas.globalfunctions.safe_float(string, default=None)[source]¶ Safely parse a string into a float.
On error return the
defaultvalue.Parameters: - string (string) – string value to be converted
- default (float) – default value to be used in case of failure
Return type: float
-
aeneas.globalfunctions.safe_get(dictionary, key, default_value, can_return_none=True)[source]¶ Safely perform a dictionary get, returning the default value if the key is not found.
Parameters: - dictionary (dict) – the dictionary
- key (string) – the key
- default_value (variant) – the default value to be returned
- can_return_none (bool) – if
True, the function can returnNone; otherwise, returndefault_valueeven if the dictionary lookup succeeded
Return type: variant
-
aeneas.globalfunctions.safe_int(string, default=None)[source]¶ Safely parse a string into an int.
On error return the
defaultvalue.Parameters: - string (string) – string value to be converted
- default (int) – default value to be used in case of failure
Return type: int
-
aeneas.globalfunctions.safe_print(msg)[source]¶ Safely print a given Unicode string to stdout, possibly replacing characters non-printable in the current stdout encoding.
Parameters: msg (string) – the message
-
aeneas.globalfunctions.safe_str(string)[source]¶ Safely return the given Unicode string from a
__str__function: as a byte string in Python 2, or as a Unicode string in Python 3.Parameters: string (string) – the string to return Return type: bytes or string
-
aeneas.globalfunctions.safe_unichr(codepoint)[source]¶ Safely return a Unicode string of length one, containing the Unicode character with given codepoint.
Parameters: codepoint (int) – the codepoint Return type: string
-
aeneas.globalfunctions.safe_unicode(string)[source]¶ Safely convert the given string to a Unicode string.
Parameters: string (variant) – the byte string or Unicode string to convert Return type: string
-
aeneas.globalfunctions.safe_unicode_stdin(string)[source]¶ Safely convert the given string to a Unicode string, decoding using
sys.stdin.encodingif needed.If running from a frozen binary,
utf-8encoding is assumed.Parameters: string (variant) – the byte string or Unicode string to convert Return type: string
-
aeneas.globalfunctions.split_url(url)[source]¶ Split the given URL
base#anchorinto(base, anchor), or(base, None)if no anchor is present.In case there are two or more
#characters, return only the first two tokens:a#b#c => (a, b).Parameters: url (string) – the url Return type: list of str
-
aeneas.globalfunctions.time_from_hhmmssmmm(string, decimal_separator='.')[source]¶ Parse the given
HH:MM:SS.mmmstring and return a time value.Parameters: - string (string) – the string to be parsed
- decimal_separator (string) – the decimal separator to be used
Return type:
-
aeneas.globalfunctions.time_from_srt(string)[source]¶ Parse the given
HH:MM:SS,mmmstring and return a time value.Parameters: string (string) – the string to be parsed Return type: TimeValue
-
aeneas.globalfunctions.time_from_ssmmm(string)[source]¶ Parse the given
SS.mmmstring and return a time value.Parameters: string (string) – the string to be parsed Return type: TimeValue
-
aeneas.globalfunctions.time_from_ttml(string)[source]¶ Parse the given
SS.mmmsstring (TTML values have an “s” suffix, e.g.1.234s) and return a time value.Parameters: string (string) – the string to be parsed Return type: TimeValue
-
aeneas.globalfunctions.time_to_hhmmssmmm(time_value, decimal_separator='.')[source]¶ Format the given time value into a
HH:MM:SS.mmmstring.Examples:
12 => 00:00:12.000 12.345 => 00:00:12.345 12.345432 => 00:00:12.345 12.345678 => 00:00:12.346 83 => 00:01:23.000 83.456 => 00:01:23.456 83.456789 => 00:01:23.456 3600 => 01:00:00.000 3612.345 => 01:00:12.345
Parameters: - time_value (float) – a time value, in seconds
- decimal_separator (string) – the decimal separator, default
.
Return type: string
-
aeneas.globalfunctions.time_to_srt(time_value)[source]¶ Format the given time value into a
HH:MM:SS,mmmstring, as used in the SRT format.Examples:
12 => 00:00:12,000 12.345 => 00:00:12,345 12.345432 => 00:00:12,345 12.345678 => 00:00:12,346 83 => 00:01:23,000 83.456 => 00:01:23,456 83.456789 => 00:01:23,456 3600 => 01:00:00,000 3612.345 => 01:00:12,345
Parameters: time_value (float) – a time value, in seconds Return type: string
-
aeneas.globalfunctions.time_to_ssmmm(time_value)[source]¶ Format the given time value into a
SS.mmmstring.Examples:
12 => 12.000 12.345 => 12.345 12.345432 => 12.345 12.345678 => 12.346
Parameters: time_value (float) – a time value, in seconds Return type: string
-
aeneas.globalfunctions.time_to_ttml(time_value)[source]¶ Format the given time value into a
SS.mmmsstring (TTML values have an “s” suffix, e.g.1.234s).Examples:
12 => 12.000s 12.345 => 12.345s 12.345432 => 12.345s 12.345678 => 12.346s
Parameters: time_value (float) – a time value, in seconds Return type: string
-
aeneas.globalfunctions.tmp_directory(root=None)[source]¶ Return the path of a temporary directory created by
tempfile.Parameters: root (string) – path to the root temporary directory; if None, the default temporary directory will be used insteadReturn type: string
-
aeneas.globalfunctions.tmp_file(suffix='', root=None)[source]¶ Return a (handler, path) tuple for a temporary file with given suffix created by
tempfile.Parameters: - suffix (string) – the suffix (e.g., the extension) of the file
- root (string) – path to the root temporary directory;
if
None, the default temporary directory will be used instead
Return type: tuple