Operating System helpers

mom.os

module:mom.os
synopsis:Operating system specific functions.
module:mom.os.path
synopsis:Directory walking, listing, and path sanitizing functions.

Functions

mom.os.path.get_dir_walker(recursive, topdown=True, followlinks=False)

Returns a recursive or a non-recursive directory walker.

Parameters:recursiveTrue produces a recursive walker; False produces a non-recursive walker.
Returns:A walker function.
mom.os.path.walk(dir_pathname, recursive=True, topdown=True, followlinks=False)

Walks a directory tree optionally recursively. Works exactly like os.walk() only adding the recursive argument.

Parameters:
  • dir_pathname – The directory to traverse.
  • recursiveTrue for walking recursively through the directory tree; False otherwise.
  • topdown – Please see the documentation for os.walk()
  • followlinks – Please see the documentation for os.walk()
mom.os.path.listdir(dir_pathname, recursive=True, topdown=True, followlinks=False)

Enlists all items using their absolute paths in a directory, optionally non-recursively.

Parameters:
  • dir_pathname – The directory to traverse.
  • recursiveTrue (default) for walking recursively through the directory tree; False otherwise.
  • topdown – Please see the documentation for os.walk()
  • followlinks – Please see the documentation for os.walk()
mom.os.path.list_directories(dir_pathname, recursive=True, topdown=True, followlinks=False)

Enlists all the directories using their absolute paths within the specified directory, optionally non-recursively.

Parameters:
  • dir_pathname – The directory to traverse.
  • recursiveTrue (default) for walking recursively through the directory tree; False otherwise.
  • topdown – Please see the documentation for os.walk()
  • followlinks – Please see the documentation for os.walk()
mom.os.path.list_files(dir_pathname, recursive=True, topdown=True, followlinks=False)

Enlists all the files using their absolute paths within the specified directory, optionally recursively.

Parameters:
  • dir_pathname – The directory to traverse.
  • recursiveTrue for walking recursively through the directory tree; False otherwise.
  • topdown – Please see the documentation for os.walk()
  • followlinks – Please see the documentation for os.walk()
mom.os.path.absolute_path(path)

Returns the absolute path for the given path and normalizes the path.

Parameters:path – Path for which the absolute normalized path will be found.
Returns:Absolute normalized path.
mom.os.path.real_absolute_path(path)

Returns the real absolute normalized path for the given path.

Parameters:path – Path for which the real absolute normalized path will be found.
Returns:Real absolute normalized path.
mom.os.path.parent_dir_path(path)

Returns the parent directory path.

Parameters:path – Path for which the parent directory will be obtained.
Returns:Parent directory path.
module:mom.os.patterns
synopsis:Wildcard pattern matching and filtering functions for paths.

Functions

mom.os.patterns.match_path(pathname, included_patterns=None, excluded_patterns=None, case_sensitive=True)

Matches a pathname against a set of acceptable and ignored patterns.

Parameters:
  • pathname – A pathname which will be matched against a pattern.
  • included_patterns – Allow filenames matching wildcard patterns specified in this list. If no pattern is specified, the function treats the pathname as a match_path.
  • excluded_patterns – Ignores filenames matching wildcard patterns specified in this list. If no pattern is specified, the function treats the pathname as a match_path.
  • case_sensitiveTrue if matching should be case-sensitive; False otherwise.
Returns:

True if the pathname matches; False otherwise.

Raises:

ValueError if included patterns and excluded patterns contain the same pattern.

mom.os.patterns.match_path_against(pathname, patterns, case_sensitive=True)

Determines whether the pathname matches any of the given wildcard patterns, optionally ignoring the case of the pathname and patterns.

Parameters:
  • pathname – A path name that will be matched against a wildcard pattern.
  • patterns – A list of wildcard patterns to match_path the filename against.
  • case_sensitiveTrue if the matching should be case-sensitive; False otherwise.
Returns:

True if the pattern matches; False otherwise.

mom.os.patterns.match_any_paths(pathnames, included_patterns=None, excluded_patterns=None, case_sensitive=True)

Matches from a set of paths based on acceptable patterns and ignorable patterns.

Parameters:
  • pathnames – A list of path names that will be filtered based on matching and ignored patterns.
  • included_patterns – Allow filenames matching wildcard patterns specified in this list. If no pattern list is specified, [“*”] is used as the default pattern, which matches all files.
  • excluded_patterns – Ignores filenames matching wildcard patterns specified in this list. If no pattern list is specified, no files are ignored.
  • case_sensitiveTrue if matching should be case-sensitive; False otherwise.
Returns:

True if any of the paths matches; False otherwise.

mom.os.patterns.filter_paths(pathnames, included_patterns=None, excluded_patterns=None, case_sensitive=True)

Filters from a set of paths based on acceptable patterns and ignorable patterns.

Parameters:
  • pathnames – A list of path names that will be filtered based on matching and ignored patterns.
  • included_patterns – Allow filenames matching wildcard patterns specified in this list. If no pattern list is specified, [“*”] is used as the default pattern, which matches all files.
  • excluded_patterns – Ignores filenames matching wildcard patterns specified in this list. If no pattern list is specified, no files are ignored.
  • case_sensitiveTrue if matching should be case-sensitive; False otherwise.
Returns:

A list of pathnames that matched the allowable patterns and passed through the ignored patterns.