pathnavigator package
Submodules
pathnavigator.pathnavigator module
- class pathnavigator.pathnavigator.PathNavigator(root_dir, load_nested_directories=True, show_tree=True)
Bases:
Folder
A class to manage the root folder and recursively load its nested structure (subfolders and files).
- dir()
Returns the full path to this folder.
- ls()
Prints the contents (subfolders and files) of the folder.
- remove(name)
Removes a file or subfolder from the folder and deletes it from the filesystem.
- mkdir(*args)
Creates a subdirectory in the current folder and updates the internal structure.
- reload()
Reloads the entire folder structure from the filesystem.
Examples
>>> pn = PathNavigator('/path/to/root') >>> pn.mkdir('folder1', 'folder2') # make a subfolder under the root >>> pn.folder1.dir() # returns the full path to folder1. >>> pn.folder1.ls() # prints the contents (subfolders and files) of folder1. >>> pn.folder1.file1 # returns the full path to file1. >>> pn.remove('folder1') # removes a file or subfolder from the folder and deletes it from the filesystem.
- reload()
Reload the entire folder structure from the root directory.
Examples
>>> pn = PathNavigator('/path/to/root') >>> pn.reload()
pathnavigator.folder module
- class pathnavigator.folder.Folder(name, parent_path, _pn_object=None, subfolders=<factory>, files=<factory>, _pn_converter=<factory>)
Bases:
object
A class to represent a folder in the filesystem and manage subfolders and files.
- name
The name of the folder.
- Type:
str
- parent_path
The path of the parent folder.
- Type:
str
- subfolders
A dictionary of subfolder names (keys) and Folder objects (values).
- Type:
dict
- files
A dictionary of file names (keys) and their paths (values).
- Type:
dict
- __getattr__(item)
Allows access to subfolders and files as attributes. Replaces ‘_’ with spaces.
- ls()
Prints the contents (subfolders and files) of the folder.
- join(*args)
Joins the current folder path with additional path components.
- set_shortcut(name, filename=None)
Adds a shortcut to this folder (or file) using the Shortcut manager.
- remove(name)
Removes a file or subfolder from the folder and deletes it from the filesystem.
- mkdir(*args)
Creates a subdirectory in the current folder and updates the internal structure.
- add_to_sys_path(method='insert', index=1)
Adds the directory to the system path.
- tree(level=-1, limit_to_directories=False, length_limit=1000, level_length_limit=1000)
Prints a visual tree structure of the folder and its contents.
- add_to_sys_path(method='insert', index=1)
Adds the directory to the system path.
- Parameters:
method (str, optional) – The method to use for adding the path to the system path. Options are ‘insert’ (default) or ‘append’.
index (int, optional) – The index at which to insert the path if method is ‘insert’. Default is 1.
- Raises:
ValueError – If the method is not ‘insert’ or ‘append’.
Examples
>>> folder = Folder('/path/to/folder') >>> folder.add_to_sys_path() Inserted /path/to/folder at index 1 in system path.
>>> folder.add_to_sys_path(method='append') Appended /path/to/folder to system path.
>>> folder.add_to_sys_path(method='invalid') Invalid method: invalid. Use 'insert' or 'append'.
- chdir()
Set this directory as working directory.
Examples
>>> folder.chdir()
-
files:
Dict
[str
,str
]
- get(filename=None)
Get the full path of a file in the current folder.
- Parameters:
filename (str) – The name of the file to get. If None, returns the full path of the folder. Default is None. If the file does not exist, returns None.
- Returns:
The full path to the file.
- Return type:
str
Examples
>>> folder = Folder(name="root") >>> folder.get("file1") '/home/user/root/file1'
- get_path_str(filename=None)
Get the full path of a file in the current folder as a string.
- Parameters:
filename (str) – The name of the file to get. If None, returns the full path of the folder. Default is None. If the file does not exist, returns None.
- Returns:
The full path to the file as a string.
- Return type:
str
Examples
>>> folder = Folder(name="root") >>> folder.get_path_str("file1") '/home/user/root/file1'
- join(*args)
Join the current folder path with additional path components.
- Parameters:
args (str) – Path components to join with the current folder path.
- Returns:
The full path after joining the current folder path with the provided components.
- Return type:
str
Examples
>>> folder = Folder(name="root") >>> folder.join("subfolder", "file.txt") '/home/user/root/subfolder/file.txt'
- ls()
Print the contents of the folder, including subfolders and files.
Prints subfolders first, followed by files.
Examples
>>> folder = Folder(name="root") >>> folder.subfolders['sub1'] = Folder("sub1") >>> folder.files['file1'] = "/path/to/file1" >>> folder.ls() Contents of '/root': Subfolders: [Dir] sub1 Files: [File] file1
- mkdir(*args)
Create a directory inside the current folder and update the internal structure.
- Parameters:
args (str) – Path components for the new directory relative to the current folder.
Examples
>>> folder = Folder(name="root") >>> folder.mkdir("new_subfolder") >>> folder.subfolders['new_subfolder'] Folder(name='new_subfolder', parent_path='/root', subfolders={}, files={})
-
name:
str
-
parent_path:
Path
- remove(name)
Remove a file or subfolder from the folder and delete it from the filesystem.
- Parameters:
name (str) – The name of the file or folder to remove, replacing underscores with spaces if needed.
Examples
>>> folder = Folder(name="root") >>> folder.subfolders['sub1'] = Folder("sub1") >>> folder.files['file1'] = "/path/to/file1" >>> folder.remove('sub1') Subfolder 'sub1' has been removed from '/root' >>> folder.remove('file1') File 'file1' has been removed from '/root'
- set_shortcut(name, filename=None)
Add a shortcut to this folder using the Shortcut manager.
- Parameters:
name (str) – The name of the shortcut to add.
Examples
>>> folder = Folder(name="root") >>> folder.set_shortcut("my_folder") Shortcut 'my_folder' added for path '/root'
-
subfolders:
Dict
[str
,Any
]
- tree(level=-1, limit_to_directories=False, length_limit=1000, level_length_limit=1000)
Given a directory Path object print a visual tree structure
pathnavigator.shortcut module
- class pathnavigator.shortcut.Shortcut
Bases:
object
A class to manage shortcuts to specific paths and access them as attributes.
- add(name, path, overwrite=False)
Adds a new shortcut as an attribute.
- remove(name)
Removes an existing shortcut by name and deletes the attribute.
- ls()
Lists all shortcuts (attributes).
- to_json(filename)
Returns all shortcuts as a JSON string and saves it to a file.
- to_dict()
Returns all shortcuts as a dictionary.
- load_dict(data, overwrite=False)
Loads shortcuts from a dictionary.
- load_json(filename, overwrite=False)
Loads shortcuts from a JSON file.
- add(name, path, overwrite=False)
Add a new shortcut as an attribute.
- Parameters:
name (str) – The name of the shortcut.
path (str) – The path that the shortcut refers to.
overwrite (bool, optional) – Whether to overwrite an existing shortcut. Default is False.
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.my_folder '/path/to/folder'
- get(name)
Retrieve the path of a shortcut.
- Parameters:
name (str) – The name of the shortcut.
- Returns:
The path associated with the shortcut.
- Return type:
str
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.get("my_folder") '/path/to/folder'
- load_dict(data, overwrite=False)
Load shortcuts from a dictionary.
- Parameters:
data (dict) – A dictionary where keys are shortcut names and values are paths.
overwrite (bool, optional) – Whether to overwrite existing shortcuts. Default is False.
Examples
>>> shortcut = Shortcut() >>> shortcut.load_dict({"project": "/path/to/project", "data": "/path/to/data"})
- load_json(filename, overwrite=False)
Load shortcuts from a JSON file.
- Parameters:
filename (str) – The name of the file containing the shortcuts in JSON format.
overwrite (bool, optional) – Whether to overwrite existing shortcuts. Default is False.
Examples
>>> shortcut = Shortcut() >>> shortcut.load_json("shortcuts.json")
- ls()
List all shortcuts and their paths.
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.ls() Shortcuts: my_folder -> /path/to/folder
- remove(name)
Remove a shortcut by name and delete its attribute.
- Parameters:
name (str) – The name of the shortcut to remove.
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.remove("my_folder") >>> hasattr(shortcut, "my_folder") False
- to_dict(to_str=False)
Return all shortcuts as a dictionary.
- Parameters:
to_str (bool, optional) – Whether to convert all Path objects to strings. Default is False.
- Returns:
A dictionary representation of all shortcuts.
- Return type:
dict
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.to_dict() {'my_folder': '/path/to/folder'}
- to_json(filename)
Return all shortcuts as a JSON string and save it to a file.
- Parameters:
filename (str) – The name of the file where the JSON string will be saved.
- Returns:
A JSON string representation of all shortcuts.
- Return type:
str
Examples
>>> shortcut = Shortcut() >>> shortcut.add("my_folder", "/path/to/folder") >>> shortcut.to_json("shortcuts.json") '{"my_folder": "/path/to/folder"}'
pathnavigator.att_name_convertor module
- class pathnavigator.att_name_convertor.AttributeNameConverter(_pn_org_to_valid_name=<factory>, _pn_valid_name_to_org=<factory>)
Bases:
object
A class to convert original names to valid attribute names and store the mapping.
- to_valid_name(name)
Convert the original name to a valid attribute name.
- get(name)
Get the valid attribute name for the given original name.
- get(name)
Get the valid attribute name for the given original name.
- Parameters:
name (str) – The original name to get the valid attribute name for.
- Return type:
str
- to_valid_name(name)
Convert the original name to a valid attribute name.
- Return type:
str