rattail.files

File Utilities

This module contains various utility functions for use with the filesystem.

rattail.files.count_lines(path, encoding=None)[source]

Counts the number of lines in a text file. Some attempt is made to ensure cross-platform compatibility.

Parameters:
  • path (string) – Path to the file.
  • encoding – Optional encoding to be used when opening the file for reading. This is passed directly to io.open().
Returns:

Number of lines in the file.

Return type:

integer

rattail.files.creation_time(path)[source]

Returns the “naive” (i.e. not timezone-aware) creation timestamp for a file.

Parameters:path (string) – Path to the file.
Returns:The creation timestamp for the file.
Return type:datetime.datetime instance
rattail.files.locking_copy(src, destdir, timeout=None)[source]

Implements a “locking” version of the standard library’s shutil.copy() function.

This exists to provide a more atomic method for copying a file into a folder which is being watched by a file monitor. The assumption is that the monitor is configured to watch for file “locks” and therefore only process files once they have had their locks removed. See also The Profile “watch_locks” Option.

Parameters:
  • src (string) – Path to the source file.
  • destdir (string) – Path to the destination directory.
rattail.files.locking_copy_old(src, dst, timeout=None)[source]

Implements a “locking” version of the standard library’s shutil.copy() function.

This exists to provide a more atomic method for copying a file into a folder which is being watched by a file monitor. The assumption is that the monitor is configured to watch for file “locks” and therefore only process files once they have had their locks removed. See also The Profile “watch_locks” Option.

Parameters:
  • src (string) – Path to the source file.
  • dst (string) – Path to the destination file (or directory).
  • timeout (float) –

    Number of seconds to wait for the file lock to clear, if it already exists. This value may be specified as an integer or float, or string (which will be coerced to a float).

    Note

    There is no default value for the timeout, which means that by default, the function will wait indefinitely for the lock to clear.

rattail.files.overwriting_move(src, dst)[source]

Convenience function which is equivalent to shutil.move(), except it will cause the destination file to be overwritten if it exists.

rattail.files.resource_path(path)[source]

Obtain a resource file path, extracting the resource and/or coercing the path as necessary.

Parameters:path (string) – May be either a package resource specifier, or a regular file path.
Returns:Absolute file path to the resource.
Return type:string

If path is a package resource specifier, and the package containing it is a zipped egg, then the resource will be extracted and the resultant filename will be returned.

rattail.files.temp_path(suffix=u'.tmp', prefix=u'rattail.', **kwargs)[source]

Returns a path for a temporary file.

This is a convenience function which wraps tempfile.mkstemp(). The meaning of the arguments is the same.