Package autodesk_toxik :: Package utils :: Module utils
[frames] | no frames]

Module utils

Common utilities.

This module provides common utilities helpful in writing scripts, used in our example scripts. Functions to parse the command-line with commonly used options and a base class for implementing an interactive shell-like interface.

Classes
  CmdFix
Improvement upon the cmd module of Python-2.1.1.
  Cmd
  Argument
(see ArgumentParser.)
  ArgumentParser
Extension of optparse.OptionParser which adds the notion of required arguments (this is a variant of an example of 'Extending optparse' from www.python.org).
Functions
bool
floatEqual(f1, f2, epsilon=1e-6)
Returns: True if the two given floats are equal within the specified epsilon.
list
toListIfNot(obj)
Converts the given object to a list if it's not already one.
set
toSetIfNot(obj)
Puts the given object(s) in a set if it's not already one.
set
peekSet(s)
Extracts an arbitrary element in the set without removing it from the set.
bool
matchesWildcardStr(s, w)
Returns: True if the string s matches the wildcard string w.
 
safeMakeDirs(path)
Creates a directory tree only if it doesn't already exists.
str
getClosestExistingDir(path)
Walks back up the given path until it finds an existing directory.
 
hardLinkOrCopy(src, dst)
str
getFileExtension(path)
Returns: The file extension of the given path without the dot.
str
getBasenameNoExt(path)
Gets the basename of the given file or directory path without its extension.
bool
isPathAbsolute(path)
Platform independent os.path.isabs().
str
absolutizePath(path)
Platform independent os.path.abspath().
str
normalizePath(path)
Platform independent os.path.normpath(): it recognizes both forward and backward slashes and the result contains only forward slashes.
str
normalizePathAndCase(path)
Platform independent os.path.normpath() (see normalizePath) followed by a os.path.normcase().
str
normalizePathSeparators(path)
Changes all backward slashes to forward slashes in the given path which work on all platforms (including in Windows UNC paths).
 
touch(path)
Touches a file.
bool
compareFiles(path1, path2)
Compares two files.
bool
compareTrees(dir1, dir2)
Compares two directory trees recursively
 
rmtree(folder, ignoreErrors=False)
Remove tree.
tuple
parseSubCommand(args, commands)
Utility function that parses the arguments expecting a subcommand with its own set of arguments.
 
bgTaskProgress(num, denum)
Function used to indicate progress to a Composite parent running a script as a background task.
 
bgTaskProgressPercent(percent)
Function used to indicate progress to a Composite parent running a script as a background task.
Function Details

floatEqual(f1, f2, epsilon=1e-6)

 
Parameters:
  • f1 (float)
  • f2 (float)
  • epsilon (float)
Returns: bool
True if the two given floats are equal within the specified epsilon.

Note: the epsilon is absolute (not scaled to the float values passed in).

toListIfNot(obj)

 

Converts the given object to a list if it's not already one. Strings are not considered a list of characters by this function.

Parameters:
  • obj - Object.
Returns: list
A list containing the given object.

toSetIfNot(obj)

 

Puts the given object(s) in a set if it's not already one.

Parameters:
  • obj - Object.
Returns: set
A set containing the given object(s).

peekSet(s)

 

Extracts an arbitrary element in the set without removing it from the set.

Returns: set
None if the set is empty.

matchesWildcardStr(s, w)

 
Parameters:
  • s (str)
  • w (str)
Returns: bool
True if the string s matches the wildcard string w.

safeMakeDirs(path)

 

Creates a directory tree only if it doesn't already exists.

Parameters:
  • path (str) - Path of the directory tree to create.

getClosestExistingDir(path)

 

Walks back up the given path until it finds an existing directory.

Parameters:
  • path (str) - Path.
Returns: str
An existing directory that parents the given path. On platforms with multiple root directories (Windows drives), the empty string can be returned if the given path is under a root directory that doesn't exist. The empty string is also returned if given a relative file path.

hardLinkOrCopy(src, dst)

 
Parameters:
  • src (str) - Source file path.
  • dst (str) - Destination file path.

getFileExtension(path)

 
Parameters:
  • path (str)
Returns: str
The file extension of the given path without the dot.

getBasenameNoExt(path)

 

Gets the basename of the given file or directory path without its extension.

Parameters:
  • path (str) - Path.
Returns: str
The basename of the file or directory without the extension.

isPathAbsolute(path)

 

Platform independent os.path.isabs().

Parameters:
  • path (str)
Returns: bool
True if the given path is absolute.

absolutizePath(path)

 

Platform independent os.path.abspath(). And like os.path.abspath(), it normalizes the given path, but in a platform independent manner; using utils.normalizePath().

Parameters:
  • path (str)
Returns: str
The absolute path corresponding to the given path.

normalizePath(path)

 

Platform independent os.path.normpath(): it recognizes both forward and backward slashes and the result contains only forward slashes.

Parameters:
  • path (str)
Returns: str
The normalized path.

normalizePathAndCase(path)

 

Platform independent os.path.normpath() (see normalizePath) followed by a os.path.normcase().

Parameters:
  • path (str)
Returns: str
The normalized path.

normalizePathSeparators(path)

 

Changes all backward slashes to forward slashes in the given path which work on all platforms (including in Windows UNC paths).

Parameters:
  • path (str)
Returns: str
The normalized path.

touch(path)

 

Touches a file.

Parameters:
  • path (str)

compareFiles(path1, path2)

 

Compares two files.

Parameters:
  • path1 (str)
  • path2 (str)
Returns: bool
True if the content of the given files is identical.

compareTrees(dir1, dir2)

 

Compares two directory trees recursively

Parameters:
  • dir1 (str)
  • dir2 (str)
Returns: bool
True if all files contained under dir1 are present and identical to the files under dir2.

Note: This function is not bidirectional, that is: compareTrees( dir1, dir2 ) is not necessarily equal to compareTrees( dir2, dir1 ).

rmtree(folder, ignoreErrors=False)

 

Remove tree.

Parameters:
  • folder (str)
  • ignoreErrors (bool) - If set to True, errors will be ignored (i.e. no exception will be thrown).
Raises:
  • Exception - If an error occured (and ignoreErrors is set to False).

parseSubCommand(args, commands)

 

Utility function that parses the arguments expecting a subcommand with its own set of arguments. Expects a dict of command names to command functions/objects.

Parameters:
  • args (list of str)
  • commands (dict of command names to command functions/objects)
Returns: tuple
A pair of the command function object and a list of arguments for the command.

bgTaskProgress(num, denum)

 

Function used to indicate progress to a Composite parent running a script as a background task.

Parameters:
  • num (int)
  • denum (int)