package

stow.manager

Important

All Manager objects express the public interface defined below, so only the Manager interface is listed for reference.

You should not (and cannot) initialise this class.

For extending the class for bespoke managers, you should import and extend either the LocalManager or the RemoteManager. Information on how to do that can be found here.

abstract class

Manager()

Bases
stow.manager.abstract_methods.AbstractManager abc.ABC stow.manager.class_methods.ClassMethodManager stow.class_interfaces.ManagerInterface stow.manager.reloader.ManagerSeralisable

Manager Abstract base class - expressed the interface of a Manager which governs a storage option and allows extraction and placement of files in that storage container

Classes
  • ABCMeta Metaclass for defining Abstract Base Classes (ABCs).
Methods
  • __getitem__(path) (artefact) Fetch an artefact from the manager. In the event that it hasn't been cached, look it up on the underlying implementation and return a newly created object. If it doesn't exist raise an error
  • abspath(artefact) (str) Return a normalized absolute version of the path or artefact given.
  • basename(artefact) (str) Return the base name of an artefact or path. This is the second element of the pair returned by passing path to the function split().
  • commonpath(paths) (str) Return the longest common sub-path of each pathname in the sequence paths
  • commonprefix(paths) (str) Return the longest common string literal for a collection of path/artefacts
  • cp(source, destination, overwrite) (Artefact) Copy the artefacts at the source location to the provided destination location. Overwriting items at the destination.
  • dirname(artefact) (str) Return the directory name of path or artefact. Preserve the protocol of the path if a protocol is given
  • exists(artefact) (bool) Return true if the given artefact is a member of the manager, or the path is correct for the manager and it leads to a File or Directory.
  • expanduser(path) (str) On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user’s home directory.
  • expandvars(path) (str) Return the argument with environment variables expanded. Substrings of the form $name or ${name} are replaced by the value of environment variable name. Malformed variable names and references to non-existing variables are left unchanged.
  • extension(artefact) (str) Return the extension of an artefact or path.
  • get(source, destination, overwrite) (typing.Union[typing.Any, bytes]) Get a remote artefact from the storage option and write it to the destination path given.
  • getatime(artefact) (timestamp) Get the accessed time for the artefact as a UTC timestamp
  • getctime(artefact) (timestamp) Get the created time for the artefact as a UTC timestamp
  • getmtime(artefact) (timestamp) Get the modified time for the artefact as a UTC timestamp
  • isabs(path) (bool) Return True if path is an absolute pathname. On Unix, that means it begins with a slash, on Windows that it begins with a (back)slash after chopping off a potential drive letter.
  • isdir(artefact) (Bool) Check if the artefact provided is a directory
  • isfile(artefact) (Bool) Check if the artefact provided is a file
  • islink(artefact) (Bool) Check if the artefact provided is a link
  • ismount(artefact) (Bool) Check if the artefact provided is a link
  • join(*paths, separator, joinAbsolutes) (str) Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
  • lexists(artefact) (bool) Return true if the given artefact is a member of the manager, or the path is correct for the manager and it leads to a File or Directory.
  • localise(artefact) (str) Localise an artefact, ensure that there is an absolute path that reaches this artefact. For local artefacts this will be the direct abspath. Remote managers will get the artefact, and pass back the path to this local version.
  • ls(art, recursive) ({Artefact}) List contents of the directory path/artefact given.
  • md5(path) TODO
  • mkdir(path, ignoreExists, overwrite) (Directory) Make a directory at the location of the path provided. By default - do nothing in the event that the location is already a directory object.
  • mv(source, destination, overwrite) (Artefact) Copy the artefacts at the source location to the provided destination location. Overwriting items at the destination.
  • name(artefact) (str) Return the name of an artefact or path (basename without extension).
  • normcase(path) (str) Normalize the case of a pathname. On Windows, convert all characters in the pathname to lowercase, and also convert forward slashes to backward slashes. On other operating systems, return the path unchanged.
  • normpath(path) (str) Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B.
  • open(artefact, mode, **kwargs) (io.IOBase) Open a file and create a stream to that file. Expose interface of open
  • put(source, destination, overwrite) (Artefact) Put a local artefact onto the remote at the location given.
  • realpath(path) (str) Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).
  • relpath(path, start) (str) Return a relative filepath to path either from the current directory or from an optional start directory
  • rm(artefact, recursive) Remove an artefact from the manager using the artefact object or its relative path. If its a directory, remove it if it is empty, or all of its contents if recursive has been set to true.
  • samefile(artefact1, artefact2) (bool) Check if provided artefacts are represent the same data on disk
  • sameopenfile(handle1, handle2) (bool) Return True if the file descriptors fp1 and fp2 refer to the same file.
  • samestat(artefact1, artefact2) (bool) Check if provided artefacts are represent the same data on disk
  • split(artefact) (dirname, basename) Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that.
  • splitdrive(path) (drive, path) Split the pathname path into a pair (drive, tail) where drive is either a mount point or the empty string.
  • splitext(artefact) (root, ext) Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period.
  • submanager(uri) (SubManager) Create a submanager at the given uri which shall behave like a conventional manager, however, its actions shall be relative to the given uri and shall update the main manager.
  • sync(source, destination, overwrite, delete) Put artefacts in the source location into the destination location if they have more recently been editted
  • toConfig() (dict) Generate a config which can be unpacked into the connect interface to initialise this manager. To be used to seralise and de-seralise a manager object.
classmethod

abspath(artefact)

Return a normalized absolute version of the path or artefact given.

Parameters
  • artefact (Artefact or str) The path or object whose path is to be made absolute and returned
Returns (str)

the absolute path of the artefact provided

Raises
  • ValueError Cannot make a remote artefact object's path absolute
classmethod

basename(artefact)

Return the base name of an artefact or path. This is the second element of the pair returned by passing path to the function split().

Parameters
  • artefact (Artefact or str) The path or object whose path is to have its base name extracted
Returns (str)

the basename

classmethod

name(artefact)

Return the name of an artefact or path (basename without extension).

Parameters
  • artefact (Artefact or str) The path or object whose path is to have its base name extracted
Returns (str)

the name e.g. /hello/there.txt => there

classmethod

extension(artefact)

Return the extension of an artefact or path.

Parameters
  • artefact (Artefact or str) The path or object whose path is to have its base name extracted
Returns (str)

the extension e.g. /hello/there.txt => txt

classmethod

commonpath(paths)

Return the longest common sub-path of each pathname in the sequence paths

Examples

commonpath(["/foo/bar", "/foo/ban/pip"]) == "/foo"

Parameters
  • paths (iterable of Artefact or str) Artefact/paths who will have their paths comparied to find a common path
Returns (str)

A valid owning directory path that is the shared owning directory for all paths

Raises
  • ValueError If there is no crossover at all
classmethod

commonprefix(paths)

Return the longest common string literal for a collection of path/artefacts

Examples

commonpath(["/foo/bar", "/foo/ban/pip"]) == "/foo/ba"

Parameters
  • paths (iterable of Artefact or str) Artefact/paths who will have their paths comparied to find a common path
Returns (str)

A string that all paths startwith (may be empty string)

classmethod

dirname(artefact)

Return the directory name of path or artefact. Preserve the protocol of the path if a protocol is given

Parameters
  • artefact (Artefact or str) The artefact or path whose directory path is to be returned
Returns (str)

The directory path for the holding directory of the artefact

staticmethod

expanduser(path)

On Unix and Windows, return the argument with an initial component of ~ or ~user replaced by that user’s home directory.

On Unix, an initial ~ is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module pwd. An initial ~user is looked up directly in the password directory.

On Windows, USERPROFILE will be used if set, otherwise a combination of HOMEPATH and HOMEDRIVE will be used. An initial ~user is handled by stripping the last directory component from the created user path derived above.

If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.

Parameters
  • path (str) the path which may contain a home variable indicator to be expanded
Returns (str)

A path with the home path factored in - if applicable

staticmethod

expandvars(path)

Return the argument with environment variables expanded. Substrings of the form $name or ${name} are replaced by the value of environment variable name. Malformed variable names and references to non-existing variables are left unchanged.

On Windows, %name% expansions are supported in addition to $name and ${name}.

Parameters
  • path (str) A path which might contain variables to be expanded
Returns (str)

A string with any environment variables added

staticmethod

isabs(path) → bool

Return True if path is an absolute pathname. On Unix, that means it begins with a slash, on Windows that it begins with a (back)slash after chopping off a potential drive letter.

Parameters
  • path (str) the path to be checked for being absolute
classmethod

join(*paths, separator='/', joinAbsolutes=False)

Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty. If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

Protocols/drive letters are perserved in the event that an absolute is passed in.

Parameters
  • *paths (iterable of str) segments of a path to be joined together
  • separator (optional) The character to be used to join the path segments
  • joinAbsolutes (bool, optional) Whether to stick to normal behaviour continue from absolute paths or join them in series
Returns (str)

A joined path

staticmethod

normcase(path)

Normalize the case of a pathname. On Windows, convert all characters in the pathname to lowercase, and also convert forward slashes to backward slashes. On other operating systems, return the path unchanged.

Parameters
  • path (str) path to normalise
Returns (str)

the path normalised

staticmethod

normpath(path)

Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B.

Parameters
  • path (str) the path whose to be
Returns (str)

The path transformed

classmethod

realpath(path)

Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path (if they are supported by the operating system).

Parameters
  • path (str) the path to have symbolic links corrected
Returns (str)

the path with the symbolic links corrected

classmethod

relpath(path, start='.') → str

Return a relative filepath to path either from the current directory or from an optional start directory

Parameters
  • path (str) the path to be made relative
  • start (optional) the location to become relative to
classmethod

samefile(artefact1, artefact2)

Check if provided artefacts are represent the same data on disk

Parameters
  • artefact1 (Artefact or str) An artefact object or path
  • artefact2 (Artefact or str) An artefact object or path
Returns (bool)

True if the artefacts are the same

staticmethod

sameopenfile(handle1, handle2) → bool

Return True if the file descriptors fp1 and fp2 refer to the same file.

classmethod

samestat(artefact1, artefact2)

Check if provided artefacts are represent the same data on disk

Parameters
  • artefact1 (Artefact or str) An artefact object or path
  • artefact2 (Artefact or str) An artefact object or path
Returns (bool)

True if the artefacts are the same

classmethod

split(artefact)

Split the pathname path into a pair, (head, tail) where tail is the last pathname component and head is everything leading up to that.

Parameters
  • artefact (Artefact or str) the artefact to be split
Returns (dirname, basename)

the split parts of the artefact

classmethod

splitdrive(path)

Split the pathname path into a pair (drive, tail) where drive is either a mount point or the empty string.

Parameters
  • path (str) the path whose mount point/drive is to be removed
Returns (drive, path)

tuple with drive string separated from the path

classmethod

splitext(artefact)

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period.

Parameters
  • artefact (Artefact or str) the artefact to have the extension extracted
Returns (root, ext)

The root path without the extension and the extension

staticmethod

md5(path)

TODO

class

ABCMeta(name, bases, namespace, **kwargs)

Metaclass for defining Abstract Base Classes (ABCs).

Use this metaclass to create an ABC. An ABC can be subclassed directly, and then acts as a mix-in class. You can also register unrelated concrete classes (even built-in classes) and unrelated ABCs as 'virtual subclasses' -- these and their descendants will be considered subclasses of the registering ABC by the built-in issubclass() function, but the registering ABC won't show up in their MRO (Method Resolution Order) nor will method implementations defined by the registering ABC be callable (not even via super()).

Methods
  • __instancecheck__(cls, instance) Override for isinstance(instance, cls).
  • __subclasscheck__(cls, subclass) Override for issubclass(subclass, cls).
  • register(cls, subclass) Register a virtual subclass of an ABC.
staticmethod
register(cls, subclass)

Register a virtual subclass of an ABC.

Returns the subclass, to allow usage as a class decorator.

staticmethod
__instancecheck__(cls, instance)

Override for isinstance(instance, cls).

staticmethod
__subclasscheck__(cls, subclass)

Override for issubclass(subclass, cls).

abstract method

localise(artefact)

Localise an artefact, ensure that there is an absolute path that reaches this artefact. For local artefacts this will be the direct abspath. Remote managers will get the artefact, and pass back the path to this local version.

A path is still returned even if the artefact doesn't exist. It will be the responsibility of the calling method to handle what is localised.

Parameters
  • artefact (Artefact or str) The artefact path or artefact object
Yields (str)

The abspath for the artefact

abstract method

toConfig()

Generate a config which can be unpacked into the connect interface to initialise this manager. To be used to seralise and de-seralise a manager object.

NOTE Defaulted values or environment variables are not guaranteed to be saved

Returns (dict)

A dictionary of the kwargs of the init of the manager

method

__getitem__(path)

Fetch an artefact from the manager. In the event that it hasn't been cached, look it up on the underlying implementation and return a newly created object. If it doesn't exist raise an error

Parameters
  • managerPath The manager relative path to fine the artefact with
Returns (artefact)

The artefact at the provided location path

Raises
  • ArtefactNotFound In the event that the path does not exist
method

isfile(artefact)

Check if the artefact provided is a file

Parameters
  • artefact (Artefact or str) The artefact to be checked
Returns (Bool)

True or False in answer to the question

method

isdir(artefact)

Check if the artefact provided is a directory

Parameters
  • artefact (Artefact or str) The artefact to be checked
Returns (Bool)

True or False in answer to the question

method

ismount(artefact)

Check if the artefact provided is a link

Will check for local managers but will default to False for remote managers

Parameters
  • artefact (Artefact or str) The artefact to be checked
Returns (Bool)

True or False in answer to the question

method

getctime(artefact)

Get the created time for the artefact as a UTC timestamp

Parameters
  • artefact (Artefact or str) the artefact whose creation datetime is to be returned
Returns (timestamp)

a float timestamp of creation time if manager holds such information else None

Raises
  • ArtefactNotFound If there is no artefact at the location
method

getmtime(artefact)

Get the modified time for the artefact as a UTC timestamp

Parameters
  • artefact (Artefact or str) the artefact whose modified datetime is to be returned
Returns (timestamp)

a float timestamp of modified time if manager holds such information else None

Raises
  • ArtefactNotFound If there is no artefact at the location
method

getatime(artefact)

Get the accessed time for the artefact as a UTC timestamp

Parameters
  • artefact (Artefact or str) the artefact whose accessed datetime is to be returned
Returns (timestamp)

a float timestamp of accessed time if manager holds such information else None

Raises
  • ArtefactNotFound If there is no artefact at the location
method

exists(artefact)

Return true if the given artefact is a member of the manager, or the path is correct for the manager and it leads to a File or Directory.

Does not handle protocols

Parameters
  • artefact (Artefact or str) Artefact or path whose existence is to be checked
Returns (bool)

True if artefact exists else False

method

lexists(artefact)

Return true if the given artefact is a member of the manager, or the path is correct for the manager and it leads to a File or Directory.

Does not handle protocols

Parameters
  • artefact (Artefact or str) Artefact or path whose existence is to be checked
Returns (bool)

True if artefact exists else False

method

get(source, destination=None, overwrite=False)

Get a remote artefact from the storage option and write it to the destination path given.

Parameters
  • source (Artefact/str) The remote's file object or its path
  • destination (str) The local path for the artefact to be written to
Returns (typing.Union[typing.Any, bytes])

Return user defined response for get if file written to destination else return bytes if no destination given

method

put(source, destination, overwrite=False) → Artefact

Put a local artefact onto the remote at the location given.

Parameters
  • overwrite (bool) Whether to accept the overwriting of a target destination when it is a directory
  • src_local (str) The path to the local artefact that is to be put on the remote
  • dest_remote (Artefact/str) A file object to overwrite or the relative path to a destination on the remote
method

cp(source, destination, overwrite=False)

Copy the artefacts at the source location to the provided destination location. Overwriting items at the destination.

Parameters
  • source (Artefact or str) source path or artefact
  • destination (Artefact or str) destination path or artefact
  • overwrite (bool, optional) Whether to overwrite directories my move
Returns (Artefact)

The destination artefact object

method

mv(source, destination, overwrite=False)

Copy the artefacts at the source location to the provided destination location. Overwriting items at the destination.

Parameters
  • source (Artefact or str) source path or artefact
  • destination (Artefact or str) destination path or artefact
  • overwrite (bool, optional) Whether to overwrite directories my move
Returns (Artefact)

The destination artefact object (source object updated if source was on manager originally)

method

rm(artefact, recursive=False)

Remove an artefact from the manager using the artefact object or its relative path. If its a directory, remove it if it is empty, or all of its contents if recursive has been set to true.

Parameters
  • artefact (typing.Union[Artefact, str]) the object which is to be deleted
  • recursive (bool) whether to accept the deletion of a directory which has contents
method

ls(art='/', recursive=False)

List contents of the directory path/artefact given.

Parameters
  • art (Directory/str) The Directory artefact or the relpath to the directory to be listed
  • recursive (bool) Return subdirectory contents as well
Returns ({Artefact})

The artefact objects which are within the directory

method

mkdir(path, ignoreExists=True, overwrite=False)

Make a directory at the location of the path provided. By default - do nothing in the event that the location is already a directory object.

Parameters
  • path (str) Relpath to the location where a directory is to be created
  • ignoreExists (bool) Whether to do nothing if a directory already exists
  • overwrite (bool) Whether to overwrite the directory with an empty directory
Returns (Directory)

The directory at the given location - it may have been created as per the call

Raises
  • OperationNotPermitted In the event that you try to overwrite a directory that already exists without passing the overwrite flag
method

sync(source, destination, overwrite=False, delete=False)

Put artefacts in the source location into the destination location if they have more recently been editted

Parameters
  • source (Directory) source directory artefact
  • destination (Directory) destination directory artefact on the manager
  • delete (bool, optional) Togger the deletion of artefacts that are members of the destination which do not conflict with the source.
Raises
  • ArtefactNotFound In the event that the source directory doesn't exist
generator

open(artefact, mode='r', **kwargs)

Open a file and create a stream to that file. Expose interface of open

Parameters
  • artefact (File or str) The object that represents the file (or path to the file) to be openned by this manager
  • mode (str, optional) The open method
  • kwargs kwargs to be passed to the interface of open
Yields (io.IOBase)

An IO object depending on the mode for interacting with the file

method

submanager(uri)

Create a submanager at the given uri which shall behave like a conventional manager, however, its actions shall be relative to the given uri and shall update the main manager.

If a manager exists at the uri specified already, then it is returned.

Parameters
  • uri (str) The uri of the target location for the manager to be setup. If the uri does not exist, a directory shall be created. If it exists, the manager shall require it be a Directory object
Returns (SubManager)

A sub manager at the given uri

Raises
  • ArtefactTypeError if there exists an object at the location which isn't a directory
  • ValueError Raised if uri is top level directory