system

class lib.system.MethodRegister
does_process(filename)

Check if the filename can be processed by any of the registered methods.

Parameter:filename (string) – filename
get_methods(extension)

Get all methods registered for an extension.

Parameter:extension (string) – file extension
Returns:list of methods registered for the extension
Return type:list
register(extensions, method)

Register one method for multiple extensions. If the method is None, it will cancel the registration.

Parameters:
  • extensions (list of strings) – list of file extensions
  • method – method to open a file
  • method – function
unregister_extension(extension)

Unregister an extension from all methods.

Parameter:extensions (list of strings) – list of file extensions
unregister_method(method)

Unregister a method from all extensions.

Parameters:
  • method – method to open a file
  • method – function
class lib.system.TempFile(suffix='', path=None)
close(force_remove=True, dest='')

It is important to call this method when finished with the temporary file.

Parameters:
  • force_remove (boolean) – Remove temporary file and raise IOError when it does not exist anymore. Set to False to allow for processes that: - delete the temporary files themselves - might have failed to produce an output file
  • dest (string) – This is eg used for thumbnails in order to rename them to their proper location
lib.system.call(args, **options)

Runs a shell command and returns it’s exit code. This also is an obligatory alternative to subprocess.call as that won’t work with py2exe.

Parameter:args (tuple of strings) – the command to be executed in the shell
Returns:command exit code
Return type:integer
lib.system.call_out_err(args, **options)

Runs a shell command and captures the output.

Parameter:args (tuple of strings) – the command to be executed in the shell
Returns:stdout, stderr and return code
Return type:tuple of (str, str, int)

The options will be passed to the subprocess.Popen

>>> call_out_err(['echo', 'world'])
('world\n', '', 0)
lib.system.call_out_err_temp(args, input=None, output=None, needs_temp=None, output_ext=None, **options)

Same function as call_out_err() but hacks around problematic unicode filenames with temp files. This will probably only be needed by certain Windows programs.

We can’t use hard links as on windows that only works for NTFS.

Parameters:
  • args (tuple of strings) – the command to be executed in the shell
  • input (list of str or unicode) – input files which could be converted to temp files
  • output (list of str or unicode) – output files which could be converted to temp files
  • needs_temp (function) – function(stderr, stdout, err) which evaluates if it is worth to convert to temp files or not
  • output_ext (None) – extension of generated output file which is not listed in the command (e.g. necessary for dcraw)
Returns:

stdout, stderr and return code

Return type:

tuple of (str, str, int)

lib.system.ensure_path(path)

Ensure a path exists, create all not existing paths.

It raises an OSError, if an invalid path is specified.

Parameter:path (str) – the absolute folder path (not relative!)
lib.system.file_extension(uri)
lib.system.filename_to_title(filename)

Converts a filename to a title. It replaces dashes with spaces and converts every first character to uppercase.

Parameter:filename (str) – an absolute or relative path
Returns:titled version of the filename
Return type:bool
>>> filename_to_title('~/highlight_mask.png')
'Highlight Mask'
lib.system.find_command(text)

Find command in text

Parameter:text (string) – command line
Returns:text
Return type:text
>>> find_command('convert image.jpg image.jpg')
'convert'
>>> find_command('"/my apps/convert" image.jpg image.jpg')
'"/my apps/convert"'
>>> find_command('/my apps/convert image.jpg image.jpg')
'/my'
lib.system.find_exe(executable, use_which=True, raise_exception=False)

Finds an executable binary. Returns None if the binary can not be found.

This method need some extra love for Windows and Mac.

Parameters:
  • executable (string) – binary which will be used as a plugin (eg imagemagick)
  • quote (bool) – quote the path if it contains spaces
  • use_which (bool) – use the command which on non windows platforms
  • raise_exception (bool) – raise exception if not found
Returns:

absolute path to the binary

Return type:

string or None

lib.system.find_in(filename, paths)

Finds a filename in a list of paths.

Parameters:
  • filename (str) – filename
  • paths (list of strings) – paths
Returns:

found filename with path or None

Return type:

string or None

lib.system.fix_path(path)
lib.system.fix_quotes(text)

Fix quotes for a command line parameter. Only surround by quotes if a space is present in the filename.

Parameter:text (string) – command line parameter
Returns:text with quotes if needed
Return type:string
>>> fix_quotes('blender')
'blender'
>>> fix_quotes('/my programs/blender')
'"/my programs/blender"'
lib.system.has_ext(path, ext)
lib.system.is_file(path)

Checks wether a path is a valid local or remote file.

Parameter:path (str) – the path which has to be checked
Returns:True if path is a valid local or remote file, False otherwise
Return type:bool
>>> is_file('http://www.foo.com/logo.png')
True
>>> is_file('ftp://foo.com/logo.png')
True
>>> is_file('/etc/fstap')
False
lib.system.is_www_file(url)

Checks whether a file is remote (http or ftp).

Parameter:url (str) – file path or url
Returns:True if remote, False if local
Return type:bool
>>> is_www_file('http://www.foo.com/logo.png')
True
>>> is_www_file('ftp://foo.com/logo.png')
True
>>> is_www_file('logo.png')
False
lib.system.reexec_with_pythonw(py_file=None)
‘pythonw’ needs to be called for any wxPython app to run from the command line on Mac Os X if python version < 2.5.
lib.system.replace_ext(filename, ext)
lib.system.set_bin_paths(paths=None)

Initializes where binaries can be found.

Parameter:paths (list of strings) – list of paths where binaries might be found
lib.system.shell_cache(args, cache='', key=None, validate=None, **options)

Runs a shell command and captures the output. It uses a caching system so that cached results don’t need to run a subprocess anymore. The results are cached by sys.platform

Parameters:
  • args (tuple of strings) – the command to be executed in the shell
  • cache (string) – the filename of the cache file
  • key – normally the result is stored in as cache_dict[args], but this can overwritten by
  • validate – a validate (eg mtime) to validate the cache result
Returns:

stdout and stdout

Return type:

typle of strings

lib.system.start(path)

Open a file or browse a folder.

Parameter:path (string) – location of the file
lib.system.title(text)

Turns a text in a title

Parameter:text (str) – text
Returns:title
Return type:str
>>> title('hello_world')
'Hello World'
lib.system.wrap(text, fill=70)

Previous topic

safe

Next topic

thumbnail