opath.opath

Object encapsulation of directory/file trees

Functions

deepcopy(x[, memo, _nil]) Deep copy operation on arbitrary Python objects.

Classes

ChainList List-like class that collects attributes and applies functions but functions like a list in every other regard.
ODir(name[, push_up, check_attr]) A directory object
OFile(name[, push_up, check_attr]) A file object
OPath(name[, push_up, check_attr]) A generic path
ObjChain([push_up, check_attr]) A tree-like class for chaining commands and attributes together with special root/head handling.
Path
class opath.opath.ODir(name, push_up=True, check_attr=True)[source]

Bases: opath.opath.OPath

A directory object

<<<<<<< HEAD:magicdir/magic_dir.py
Initializer for MagicPath
Initializer for OPath

>>>>>>> refactoring:opath/opath.py

param name:basename of the path
type name:str
param push_up:default of whether to ‘push’ access of this path to the root path node
type push_up:boolean
param check_attr:
 default to validate attributes. For example ‘this is not valid’ is not a valid attribute since it contains spaces but ‘this_is_a_valid_attribute’ is a valid attribute.
type check_attr:
 boolean
_add(attr, child, push_up=None, check_attr=None)

Adds child node to this node.

Parameters:
  • attr (str) – name to use to reference the child
  • child (ObjChain) – child node to add
  • push_up (boolean) – whether to add the child node to the root node. If True, the child will be able to be accessed from the root node.
  • check_attr (boolean or None) – if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
Returns:

the child node

Return type:

ObjChain

_add_grandchild(child)

Adds a node to the roots grandchildren

_create_and_add_child(attr, with_attributes=None, push_up=None, check_attr=None)

Copy this node and adds the node as a child

param attr:name of the new node
type attr:str
param with_attributes:
 attribute to apply to the new node
type with_attributes:
 dict
param push_up:whether to push the new node to root.
type push_up:boolean
<<<<<<< HEAD:magicdir/magicchain.py
param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will

default to defaults defined on initialization :type check_attr: boolean|None

param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
type check_attr:
 boolean or None
return:the newly added child node
rtype:ObjChain
_create_child(with_attributes=None)

Create a new copy node with with a set of attributes

Parameters:with_attributes (dict) – list of attributes to apply to child
Returns:child node
Return type:ObjChain
_opts(**opts)

Returns the options dictionary. If passed opts has a None value, default option is used.

_remove_child(attr)

Removes a child from this node

Parameters:attr (str) – the attribute name of the node
Returns:the removed child, else return None
Return type:ObjChain or None
_sanitize_identifier(iden)

Validates the identifier to ensure it is not a reserve keyword used in python (‘which’, ‘in’, ‘class’, ‘else’, etc.). Other strings such as ‘something.else’ that cannot be translated into an attribute are also disallowed.

_update_grandchildren(push_up)

Updates accessible children

_validate_attr(attr, push_up=None)

Validates the attribute name for a node and checks if that attribute (i) already exists or (ii) already exists in the root attributes (if push_up=True)

param attr: attribute name for potential node :type attr: str :param push_up: whether to validate if the attribute exists in the list of root attributes :type push_up: boolean :return: None :rtype: None

abspath

Absolute path of this location

abspaths

Returns all absolute paths to all directories (includes parent_dir).

Returns:directory absolute paths in this directory (inclusive)
Return type:list of PosixPath
add(name, attr=None, push_up=None, check_attr=None)[source]

Adds a new directory to the directory tree.

Parameters:
  • name (basestring) – name of the new directory
  • attr (basestring) – attribute to use to access this directory. Defaults to name.
  • push_up (boolean) – whether to ‘push’ attribute to the root, where it can be accessed
  • check_attr – if True, will raise exception if attr is not a valid attribute. If None, value will

default to defaults defined on initialization :type check_attr: boolean|None :return: new directory :rtype: ODir

add_file(name, attr=None, push_up=None, check_attr=False)[source]

Adds a new file to the directory tree.

Parameters:
  • name (basestring) – name of the new file
  • attr (basestring) – attribute to use to access this directory. Defaults to name.
  • push_up (boolean) – whether to ‘push’ attribute to the root, where it can be accessed
  • check_attr – if True, will raise exception if attr is not a valid attribute. If None, value will

default to defaults defined on initialization :type check_attr: boolean|None :return: new directory :rtype: ODir

all_exists()[source]

Whether all directories in the tree exist.

Returns:directory tree exists
Return type:boolean
ancestors(include_self=False)

All ancestral nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of ancestor nodes (list of ObjChain instances)
Return type:list
attr

The name for this node from the parent’s reference.

children

This nodes descendent notes

cpdirs(new_parent)[source]

Copies the directory tree to a new location. Returns the root of the newly copied tree.

Parameters:new_parent (basestring or PosixPath or Path object) – path of new parent directory
Returns:copied directory
Return type:ODir
descendents(include_self=False)

All descendent nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of descendent nodes (list of ObjChain instances)
Return type:list
dir

The parent directory of this location

dirs

Recursively returns all directories ODir of this directory. Does not include parent directories.

Returns:list of ODir
Return type:list
dump_json(filename, mode, data, *args, **json_kwargs)[source]

Dump data to json

exists()[source]

Whether the directory exists

files

Recursively returns all files OFile of this directory. Does not include parent directories.

Returns:list of OFiles
Return type:list
get(attr)

Short for getattr(self, attr)

has(attr)

Short for hasattr(self, attr)

is_root()

Whether this node is the root/head node

list_dirs()[source]

List immediate directories in this directory

list_files()[source]

List immediate files in this directory

load_json(filename, mode, **kwargs)[source]

Load data from a json

ls()[source]

Lists the files that exist in directory

mkdirs()[source]
Creates all directories in the directory tree. Existing directories are ignored.
Returns:self
Return type:ODir
mvdirs(new_parent)[source]

Moves the directory. If this directory has a parent connection, the connection will be broken and this directory will become a root directory. The original root will be left in place but will no longer have access to the moved directories.

open_file(filename, mode, **kwargs)[source]

Open a file at this location

parent

This nodes parent

path

Path of this location

paths

Returns all paths to all directories (includes parent_dir).

Returns:directory paths in this directory (inclusive)
Return type:list of PosixPath
read_file(filename, mode, **kwargs)[source]

Read a file at this location

relpath

Relative path of this location

relpaths

Returns all paths to all directories (includes parent_dir).

Returns:directory paths in this directory (inclusive)
Return type:list of PosixPath
remove_parent()

Remove parent from this path

rmdirs()[source]
Recursively removes all files and directories of this directory (inclusive)
Returns:self
Return type:ODir
root

The root/head node

set_dir(path)

Set the parent directory

show(print_files=False, indent=4, max_level=None, level=0, list_missing=True)

Recursively print the file structure

Parameters:
  • print_files (boolean) – whether to print files
  • indent (int) – number of spaces per indent
  • max_level (int) – max level depth to print
  • level (int) – start at level
  • list_missing (boolean) – whether to annotate files that do not exist
Returns:

None

Return type:

None

write_file(filename, mode, data, **kwargs)[source]

Write a file at this location

class opath.opath.OFile(name, push_up=True, check_attr=True)[source]

Bases: opath.opath.OPath

A file object

<<<<<<< HEAD:magicdir/magic_dir.py
Initializer for MagicPath
Initializer for OPath

>>>>>>> refactoring:opath/opath.py

param name:basename of the path
type name:str
param push_up:default of whether to ‘push’ access of this path to the root path node
type push_up:boolean
param check_attr:
 default to validate attributes. For example ‘this is not valid’ is not a valid attribute since it contains spaces but ‘this_is_a_valid_attribute’ is a valid attribute.
type check_attr:
 boolean
_add(attr, child, push_up=None, check_attr=None)

Adds child node to this node.

Parameters:
  • attr (str) – name to use to reference the child
  • child (ObjChain) – child node to add
  • push_up (boolean) – whether to add the child node to the root node. If True, the child will be able to be accessed from the root node.
  • check_attr (boolean or None) – if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
Returns:

the child node

Return type:

ObjChain

_add_grandchild(child)

Adds a node to the roots grandchildren

_create_and_add_child(attr, with_attributes=None, push_up=None, check_attr=None)

Copy this node and adds the node as a child

param attr:name of the new node
type attr:str
param with_attributes:
 attribute to apply to the new node
type with_attributes:
 dict
param push_up:whether to push the new node to root.
type push_up:boolean
<<<<<<< HEAD:magicdir/magicchain.py
param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will

default to defaults defined on initialization :type check_attr: boolean|None

param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
type check_attr:
 boolean or None
return:the newly added child node
rtype:ObjChain
_create_child(with_attributes=None)

Create a new copy node with with a set of attributes

Parameters:with_attributes (dict) – list of attributes to apply to child
Returns:child node
Return type:ObjChain
_opts(**opts)

Returns the options dictionary. If passed opts has a None value, default option is used.

_remove_child(attr)

Removes a child from this node

Parameters:attr (str) – the attribute name of the node
Returns:the removed child, else return None
Return type:ObjChain or None
_sanitize_identifier(iden)

Validates the identifier to ensure it is not a reserve keyword used in python (‘which’, ‘in’, ‘class’, ‘else’, etc.). Other strings such as ‘something.else’ that cannot be translated into an attribute are also disallowed.

_update_grandchildren(push_up)

Updates accessible children

_validate_attr(attr, push_up=None)

Validates the attribute name for a node and checks if that attribute (i) already exists or (ii) already exists in the root attributes (if push_up=True)

param attr: attribute name for potential node :type attr: str :param push_up: whether to validate if the attribute exists in the list of root attributes :type push_up: boolean :return: None :rtype: None

abspath

Absolute path of this location

ancestors(include_self=False)

All ancestral nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of ancestor nodes (list of ObjChain instances)
Return type:list
attr

The name for this node from the parent’s reference.

children

This nodes descendent notes

descendents(include_self=False)

All descendent nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of descendent nodes (list of ObjChain instances)
Return type:list
dir

The parent directory of this location

dump_json(data, mode='w', **json_kwargs)[source]

Dump data as a json

exists()[source]

Whether the file exists

get(attr)

Short for getattr(self, attr)

has(attr)

Short for hasattr(self, attr)

is_root()

Whether this node is the root/head node

load_json(mode='r', **json_kwargs)[source]

Load data from json

open(mode='r', **kwargs)[source]

Opens a file for reading or writing

parent

This nodes parent

path

Path of this location

read(mode='r', **kwargs)[source]

Read data from a file

relpath

Relative path of this location

remove_parent()

Remove parent from this path

rm()[source]

Removes file if it exists.

root

The root/head node

set_dir(path)

Set the parent directory

show(print_files=False, indent=4, max_level=None, level=0, list_missing=True)

Recursively print the file structure

Parameters:
  • print_files (boolean) – whether to print files
  • indent (int) – number of spaces per indent
  • max_level (int) – max level depth to print
  • level (int) – start at level
  • list_missing (boolean) – whether to annotate files that do not exist
Returns:

None

Return type:

None

write(data, mode='w', **kwargs)[source]

Write data to a file

class opath.opath.OPath(name, push_up=True, check_attr=True)[source]

Bases: opath.objchain.ObjChain

A generic path

<<<<<<< HEAD:magicdir/magic_dir.py
Initializer for MagicPath
Initializer for OPath

>>>>>>> refactoring:opath/opath.py

param name:basename of the path
type name:str
param push_up:default of whether to ‘push’ access of this path to the root path node
type push_up:boolean
param check_attr:
 default to validate attributes. For example ‘this is not valid’ is not a valid attribute since it contains spaces but ‘this_is_a_valid_attribute’ is a valid attribute.
type check_attr:
 boolean
_add(attr, child, push_up=None, check_attr=None)

Adds child node to this node.

Parameters:
  • attr (str) – name to use to reference the child
  • child (ObjChain) – child node to add
  • push_up (boolean) – whether to add the child node to the root node. If True, the child will be able to be accessed from the root node.
  • check_attr (boolean or None) – if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
Returns:

the child node

Return type:

ObjChain

_add_grandchild(child)

Adds a node to the roots grandchildren

_create_and_add_child(attr, with_attributes=None, push_up=None, check_attr=None)

Copy this node and adds the node as a child

param attr:name of the new node
type attr:str
param with_attributes:
 attribute to apply to the new node
type with_attributes:
 dict
param push_up:whether to push the new node to root.
type push_up:boolean
<<<<<<< HEAD:magicdir/magicchain.py
param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will

default to defaults defined on initialization :type check_attr: boolean|None

param check_attr:
 if True, will raise exception if attr is not a valid attribute. If None, value will default to defaults defined on initialization
type check_attr:
 boolean or None
return:the newly added child node
rtype:ObjChain
_create_child(with_attributes=None)

Create a new copy node with with a set of attributes

Parameters:with_attributes (dict) – list of attributes to apply to child
Returns:child node
Return type:ObjChain
_opts(**opts)

Returns the options dictionary. If passed opts has a None value, default option is used.

_remove_child(attr)

Removes a child from this node

Parameters:attr (str) – the attribute name of the node
Returns:the removed child, else return None
Return type:ObjChain or None
_sanitize_identifier(iden)

Validates the identifier to ensure it is not a reserve keyword used in python (‘which’, ‘in’, ‘class’, ‘else’, etc.). Other strings such as ‘something.else’ that cannot be translated into an attribute are also disallowed.

_update_grandchildren(push_up)

Updates accessible children

_validate_attr(attr, push_up=None)

Validates the attribute name for a node and checks if that attribute (i) already exists or (ii) already exists in the root attributes (if push_up=True)

param attr: attribute name for potential node :type attr: str :param push_up: whether to validate if the attribute exists in the list of root attributes :type push_up: boolean :return: None :rtype: None

abspath

Absolute path of this location

ancestors(include_self=False)

All ancestral nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of ancestor nodes (list of ObjChain instances)
Return type:list
attr

The name for this node from the parent’s reference.

children

This nodes descendent notes

descendents(include_self=False)

All descendent nodes

Parameters:include_self (boolean) – Whether to include this node in the return list
Returns:list of descendent nodes (list of ObjChain instances)
Return type:list
dir

The parent directory of this location

get(attr)

Short for getattr(self, attr)

has(attr)

Short for hasattr(self, attr)

is_root()

Whether this node is the root/head node

parent

This nodes parent

path

Path of this location

relpath

Relative path of this location

remove_parent()[source]

Remove parent from this path

root

The root/head node

set_dir(path)[source]

Set the parent directory

show(print_files=False, indent=4, max_level=None, level=0, list_missing=True)[source]

Recursively print the file structure

Parameters:
  • print_files (boolean) – whether to print files
  • indent (int) – number of spaces per indent
  • max_level (int) – max level depth to print
  • level (int) – start at level
  • list_missing (boolean) – whether to annotate files that do not exist
Returns:

None

Return type:

None