aeidon.util

Source: aeidon/util.py

Miscellaneous functions.

Functions

aeidon.util.affirm(value)

Raise aeidon.AffirmationError if value evaluates to False.

aeidon.util.atomic_open(path, mode='w', *args, **kwargs)

A context manager for atomically writing a file.

The file is written to a temporary file on the same filesystem, flushed and fsynced and then renamed to replace the existing file. This should (probably) be atomic on any Unix system. On Windows, it should (probably) be atomic if using Python 3.3 or greater.

aeidon.util.chardet_available()

Return True if chardet module is available.

aeidon.util.compare_versions(x, y)

Compare version strings x and y.

Used version number formats are MAJOR.MINOR, MAJOR.MINOR.PATCH and MAJOR.MINOR.PATCH.DATE/REVISION, where all items are integers. Return 1 if x newer, 0 if equal or -1 if y newer.

aeidon.util.connect(observer, observable, signal, *args)

Connect observable‘s signal to observer‘s callback method.

If observable is a string, it should be an attribute of observer. If observable is not a string it should be the same as observer.

aeidon.util.detect_format(path, encoding)

Detect and return format of subtitle file at path.

Raise IOError if reading fails. Raise UnicodeError if decoding fails. Raise aeidon.FormatError if unable to detect format. Return an aeidon.formats enumeration item.

aeidon.util.detect_newlines(path)

Detect and return the newline type of file at path or None.

aeidon.util.enchant_and_dicts_available()

Return True if enchant and dictionaries are available.

aeidon.util.enchant_available()

Return True if enchant module is available.

aeidon.util.flatten(lst)

Return a shallow version of lst.

>>> aeidon.util.flatten([1, 2, 3, [4, 5, [6]]])
[1, 2, 3, 4, 5, 6]
aeidon.util.get_chardet_version()

Return chardet version number as string or None.

aeidon.util.get_default_encoding(fallback='utf_8')

Return the locale encoding or fallback.

aeidon.util.get_default_newline()

Return system default newline as aeidon.newlines item.

aeidon.util.get_enchant_version()

Return enchant version number as string or None.

aeidon.util.get_encoding_alias(encoding)

Return proper Python alias for encoding.

aeidon.util.get_ranges(lst)

Return a list of ranges in list of integers.

>>> aeidon.util.get_ranges([1, 2, 3, 5, 6, 7, 9, 11, 12])
[[1, 2, 3], [5, 6, 7], [9], [11, 12]]
aeidon.util.get_template_header(format)

Read and return the template header for format.

Raise IOError if reading global header file fails. Raise UnicodeError if decoding global header file fails.

aeidon.util.get_unique(lst, keep_last=False)

Return lst with duplicates removed.

aeidon.util.install_module(name, obj)

Install obj‘s module into the aeidon namespace.

Typical call is of form:

aeidon.util.install_module("foo", lambda: None)
aeidon.util.last(iterator)

Return the last value from iterator or None.

aeidon.util.makedirs(directory)

Recursively make directory if it does not exist.

Raise OSError if unsuccessful.

aeidon.util.normalize_newlines(text)

Convert all newlines in text to “n”.

aeidon.util.path_to_uri(path)

Convert local filepath to URI.

aeidon.util.print_read_io(exc_info, path)

Print IOError message to standard error.

aeidon.util.print_read_unicode(exc_info, path, encoding)

Print UnicodeError message to standard error.

aeidon.util.print_write_io(exc_info, path)

Print IOError message to standard error.

aeidon.util.print_write_unicode(exc_info, path, encoding)

Print UnicodeError message to standard error.

aeidon.util.read(path, encoding=None, fallback='utf_8', quiet=False)

Read file at path and return text.

fallback should be None to try only encoding. Raise IOError if reading fails. Raise UnicodeError if decoding fails.

aeidon.util.readlines(path, encoding=None, fallback='utf_8', quiet=False)

Read file at path and return lines.

fallback should be None to try only encoding. Raise IOError if reading fails. Raise UnicodeError if decoding fails.

aeidon.util.replace_extension(path, format)

Replace possible extension in path with that of format.

aeidon.util.shell_quote(path)

Quote and escape path for shell use.

aeidon.util.silent(*exceptions)

Try to execute body, ignoring exceptions.

aeidon.util.start_process(command, **kwargs)

Start command as a new background subprocess.

command and kwargs are passed to subprocess.Popen. Raise aeidon.ProcessError if something goes wrong. Return subprocess.Popen instance.

aeidon.util.title_to_lower_case(title_name)

Convert title case name to lower case with underscores.

aeidon.util.uri_to_path(uri)

Convert uri to local filepath.

aeidon.util.write(path, text, encoding=None, fallback='utf_8', quiet=False)

Write text to file at path.

fallback should be None to try only encoding. Raise IOError if writing fails. Raise UnicodeError if encoding fails.

aeidon.util.writelines(path, lines, encoding=None, fallback='utf_8', quiet=False)

Write lines of text to file at path.

fallback should be None to try only encoding. Raise IOError if writing fails. Raise UnicodeError if encoding fails.