Filesystem manipulation

The components of datreant.core documented here are those designed for working directly with filesystem objects, namely directories and files.

Tree

The class datreant.core.Tree is an interface to a directory in the filesystem.

class datreant.core.Tree(dirpath, limbs=None)

A directory.

abspath

Absolute path of self.path.

attach(*limbname)

Attach limbs by name to this Tree.

children

A View of all files and directories in this Tree.

Includes hidden files and directories.

discover(dirpath='.', depth=None, treantdepth=None)

Find all Treants within given directory, recursively.

Parameters:
  • dirpath (string, Tree) – Directory within which to search for Treants. May also be an existing Tree.
  • depth (int) – Maximum directory depth to tolerate while traversing in search of Treants. None indicates no depth limit.
  • treantdepth (int) – Maximum depth of Treants to tolerate while traversing in search of Treants. None indicates no Treant depth limit.
Returns:

found – Bundle of found Treants.

Return type:

Bundle

draw(depth=None, hidden=False)

Print an ASCII-fied visual of the tree.

Parameters:
  • depth (int) – Maximum directory depth to display. None indicates no limit.
  • hidden (bool) – If False, do not show hidden files; hidden directories are still shown if they contain non-hidden files or directories.
exists

Check existence of this path in filesystem.

glob(pattern)

Return a View of all child Leaves and Trees matching given globbing pattern.

Arguments:
pattern

globbing pattern to match files and directories with

hidden

A View of the hidden files and directories in this Tree.

leaves

A View of the files in this Tree.

Hidden files are not included.

limbs

A set giving the names of this Tree’s attached limbs.

make()

Make the directory if it doesn’t exist. Equivalent to makedirs().

Returns:This Tree.
Return type:Tree
makedirs()

Make all directories along path that do not currently exist.

Returns:
tree

this tree

name

Basename for this path.

parent

Parent directory for this path.

path

Filesystem path as a pathlib.Path.

relpath

Relative path of self.path from current working directory.

treants

Bundle of all Treants found within this Tree.

This does not return a Treant for a bare state file found within this Tree. In effect this gives the same result as Bundle(self.trees).

trees

A View of the directories in this Tree.

Hidden directories are not included.

Leaf

The class datreant.core.Leaf is an interface to a file in the filesystem.

class datreant.core.Leaf(filepath)

A file in the filesystem.

abspath

Absolute path.

exists

Check existence of this path in filesystem.

make()

Make the file if it doesn’t exit. Equivalent to touch().

Returns:
leaf

this leaf

makedirs()

Make all directories along path that do not currently exist.

Returns:
leaf

this leaf

name

Basename for this path.

parent

Parent directory for this path.

path

Filesystem path as a pathlib.Path.

read(size=None)

Read file, or up to size in bytes.

Arguments:
size

extent of the file to read, in bytes

relpath

Relative path from current working directory.

touch()

Make file if it doesn’t exist.

View

The class datreant.core.View is an ordered set of Trees and Leaves. It allows for convenient operations on its members as a collective, as well as providing mechanisms for filtering and subselection.

class datreant.core.View(*vegs, **kwargs)

An ordered set of Trees and Leaves.

Parameters:vegs (Tree, Leaf, or list) – Trees and/or Leaves to be added, which may be nested lists of Trees and Leaves. Trees and Leaves can be given as either objects or paths.
abspaths

List of absolute paths for the members in this View.

add(*vegs)

Add any number of members to this collection.

Arguments:
vegs

Trees or Leaves to add; lists, tuples, or other Views with Trees or Leaves will also work; strings giving a path (existing or not) also work, since these are what define Trees and Leaves

attach(*aggtreelimbname)

Attach aggtreelimbs by name to this View. Attaches corresponding limb to any member Trees.

children

A View of all children within the member Trees.

exists

List giving existence of each member as a boolean.

glob(pattern)

Return a View of all child Leaves and Trees of members matching given globbing pattern.

Parameters:pattern (string) – globbing pattern to match files and directories with
globfilter(pattern)

Return a View of members that match by name the given globbing pattern.

Parameters:pattern (string) – globbing pattern to match member names with
hidden

A View of the hidden files and directories within the member Trees.

leaves

A View of the files within the member Trees.

Hidden files are not included.

limbs

A set giving the names of this collection’s attached limbs.

make()

Make the Trees and Leaves in this View if they don’t already exist.

Returns:This View.
Return type:View
map(function, processes=1, **kwargs)

Apply a function to each member, perhaps in parallel.

A pool of processes is created for processes > 1; for example, with 40 members and processes=4, 4 processes will be created, each working on a single member at any given time. When each process completes work on a member, it grabs another, until no members remain.

kwargs are passed to the given function when applied to each member

Parameters:
  • function (function) – Function to apply to each member. Must take only a single Treant instance as input, but may take any number of keyword arguments.
  • processes (int) – How many processes to use. If 1, applies function to each member in member order in serial.
Returns:

results – List giving the result of the function for each member, in member order. If the function returns None for each member, then only None is returned instead of a list.

Return type:

list

memberleaves

A View giving only members that are Leaves (or subclasses).

membertrees

A View giving only members that are Trees (or subclasses).

names

List the basenames for the members in this View.

relpaths

List of relative paths from the current working directory for the members in this View.

treants

A Bundle of all existing Treants among the Trees and Leaves in this View.

trees

A View of directories within the member Trees.

Hidden directories are not included.