commandio package
Submodules
commandio.command module
Command module for UNIX command line interactions.
Creates a command object for UNIX command line binaries/programs/applications to be executed. |
|
Exception intended for unment dependencies |
- class commandio.command.Command(command, env=None)[source]
Creates a command object for UNIX command line binaries/programs/applications to be executed. Primary use and use-cases are intended for the subprocess module and its associated classes (i.e. Popen/call/run).
The input argument is a command (string).
Note
The specified command used must be in the system path variable.
- Usage example:
>>> echo = Command("echo 'Hi! I have arrived!'") >>> echo Command(command="echo 'Hi! I have arrived!'", env=None) >>> >>> echo.run() (0, None, None) Hi! I have arrived! # Output is directed to the shell
- Parameters
- check_dependency(raise_exc=True)[source]
Checks the dependency of some command line executable. Should the dependency not be met, then an exception is raised. Check the system path should problems arise and ensure that the executable of interest is installed and is in the system path.
- Usage example:
>>> figlet = Command("figlet python") >>> figlet.check_dependency() # Raises exception if not in system path
- Parameters
raise_exc (
bool) – If true, an exception is raised if the dependency is not in the system path. Defaults to False.- Raises
DependencyError – Dependency error exception is raised if the dependency is not met. Defaults to
- Return type
- Returns
Returns True if dependency is met, OR raises exception (if
raise_excis True)/ returns False otherwise.
- run(log=None, debug=False, dryrun=False, stdout=None, shell=False, raise_exc=True)[source]
Uses python’s built-in
subprocessmodule to execute (run) a command from the command line.The standard output and error can optionally be written to file.
Note
The contents of the
stdoutoutput file will be empty ifshellis set toTrue.shellshould remainFalseas setting it toTrueexposes risks for the injection of malicious code.
- Usage example:
>>> echo = Command("echo 'Hi! I have arrived!'") >>> echo Command(command="echo 'Hi! I have arrived!'", env=None) >>> >>> # Run/execute command >>> echo.run() (0, '', '')
- Parameters
debug (
bool) – Sets logging function verbosity to DEBUG level.dryrun (
bool) – Dry run – does not run task. Command is recorded to the log file.stdout (
Optional[str]) – Output file to write standard output to.shell (
bool) – Use shell to execute command.raise_exec – If true, raises
RuntimeErrorexception if the return code of the command is not 0 - otherwise no exception is raised with non-0 return codes.
- Raises
RuntimeError – Exception that is raised if the return code of the command is not 0 and the
raise_excargument is set toTrue.- Return type
- Returns
Return code for command execution (
int).Standard output writtent to file should the ‘stdout’ option be used (
str).Standard error writtent to file should the ‘stdout’ option be used (
str).
- class commandio.command.Command(command, env=None)[source]
Bases:
objectCreates a command object for UNIX command line binaries/programs/applications to be executed. Primary use and use-cases are intended for the subprocess module and its associated classes (i.e. Popen/call/run).
The input argument is a command (string).
Note
The specified command used must be in the system path variable.
- Usage example:
>>> echo = Command("echo 'Hi! I have arrived!'") >>> echo Command(command="echo 'Hi! I have arrived!'", env=None) >>> >>> echo.run() (0, None, None) Hi! I have arrived! # Output is directed to the shell
- Parameters
- check_dependency(raise_exc=True)[source]
Checks the dependency of some command line executable. Should the dependency not be met, then an exception is raised. Check the system path should problems arise and ensure that the executable of interest is installed and is in the system path.
- Usage example:
>>> figlet = Command("figlet python") >>> figlet.check_dependency() # Raises exception if not in system path
- Parameters
raise_exc (
bool) – If true, an exception is raised if the dependency is not in the system path. Defaults to False.- Raises
DependencyError – Dependency error exception is raised if the dependency is not met. Defaults to
- Return type
- Returns
Returns True if dependency is met, OR raises exception (if
raise_excis True)/ returns False otherwise.
- run(log=None, debug=False, dryrun=False, stdout=None, shell=False, raise_exc=True)[source]
Uses python’s built-in
subprocessmodule to execute (run) a command from the command line.The standard output and error can optionally be written to file.
Note
The contents of the
stdoutoutput file will be empty ifshellis set toTrue.shellshould remainFalseas setting it toTrueexposes risks for the injection of malicious code.
- Usage example:
>>> echo = Command("echo 'Hi! I have arrived!'") >>> echo Command(command="echo 'Hi! I have arrived!'", env=None) >>> >>> # Run/execute command >>> echo.run() (0, '', '')
- Parameters
debug (
bool) – Sets logging function verbosity to DEBUG level.dryrun (
bool) – Dry run – does not run task. Command is recorded to the log file.stdout (
Optional[str]) – Output file to write standard output to.shell (
bool) – Use shell to execute command.raise_exec – If true, raises
RuntimeErrorexception if the return code of the command is not 0 - otherwise no exception is raised with non-0 return codes.
- Raises
RuntimeError – Exception that is raised if the return code of the command is not 0 and the
raise_excargument is set toTrue.- Return type
- Returns
Return code for command execution (
int).Standard output writtent to file should the ‘stdout’ option be used (
str).Standard error writtent to file should the ‘stdout’ option be used (
str).
commandio.enums module
Enums module for the commandio package.
Log level enumerators. |
commandio.fileio module
File IO methods, functions and operations for the commandio package.
This class creates a |
- class commandio.fileio.File(src, ext='', assert_exists=False)[source]
This class creates a
Fileobject that encapsulates a number of methods and properites for file and filename handling, and file manipulation.File object base class that inherits from the
IOBaseObjabstract base class.- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... file.write_txt("some text") ... >>> # or >>> >>> file = File("file_name.txt") >>> file "file_name.txt"
- Parameters
- copy(dst)[source]
Copies file to some source destination.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... new_file: str = file.copy("file2.txt") ... >>> new_file "/abs/path/to/file2.txt" >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.copy("file2.txt") "/abs/path/to/file2.txt"
- file_parts(ext='')[source]
Similar to MATLAB’s
fileparts, this function splits a file and its path into its constituent parts:file path
filename
extension
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... print(file.file_parts()) ... ("path/to/file", "filename", ".txt") >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.file_parts() ("path/to/file", "filename", ".txt")
- read(remove_newline=False)[source]
Reads input file text.
- Usage example:
>>> from typing import Lines >>> >>> # Using class object as context manager >>> with File("file_name.txt") as file: ... lines: List[str] = file.read(remove_newline=True) ... >>> lines ['These are the file contents','new lines are separate elements'] >>> >>> # or >>> >>> file = File("file_name.txt") >>> lines: List[str] = file.read(remove_newline=True) >>> lines ['These are the file contents','new lines are separate elements']
- remove()[source]
Removes file.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.remove() ... >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.remove()
- Return type
- rm_ext(ext='')[source]
Removes file extension from the file.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... print(file.rm_ext()) ... "file_name" >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.rm_ext() "file_name"
- touch()[source]
Creates an empty file.
This class method is analagous to UNIX’s
touchcommand.- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... >>> # or >>> >>> file = File("file_name.txt") >>> file.touch()
- Return type
- write(txt='')[source]
Writes/appends text to file.
Note
Text written to file is ALWAYS
utf-8encoded.- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.write("<Text to be written>") ... >>> # or >>> >>> file = File("file_name.txt") >>> file.write("<Text to be written>")
- class commandio.fileio.File(src, ext='', assert_exists=False)[source]
Bases:
IOBaseObjThis class creates a
Fileobject that encapsulates a number of methods and properites for file and filename handling, and file manipulation.File object base class that inherits from the
IOBaseObjabstract base class.- src
Input file.
- ext
File extension of input file. If no extension is provided, it is inferred.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... file.write_txt("some text") ... >>> # or >>> >>> file = File("file_name.txt") >>> file "file_name.txt"
- Parameters
- copy(dst)[source]
Copies file to some source destination.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... new_file: str = file.copy("file2.txt") ... >>> new_file "/abs/path/to/file2.txt" >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.copy("file2.txt") "/abs/path/to/file2.txt"
- file_parts(ext='')[source]
Similar to MATLAB’s
fileparts, this function splits a file and its path into its constituent parts:file path
filename
extension
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... print(file.file_parts()) ... ("path/to/file", "filename", ".txt") >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.file_parts() ("path/to/file", "filename", ".txt")
- read(remove_newline=False)[source]
Reads input file text.
- Usage example:
>>> from typing import Lines >>> >>> # Using class object as context manager >>> with File("file_name.txt") as file: ... lines: List[str] = file.read(remove_newline=True) ... >>> lines ['These are the file contents','new lines are separate elements'] >>> >>> # or >>> >>> file = File("file_name.txt") >>> lines: List[str] = file.read(remove_newline=True) >>> lines ['These are the file contents','new lines are separate elements']
- remove()[source]
Removes file.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.remove() ... >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.remove()
- Return type
- rm_ext(ext='')[source]
Removes file extension from the file.
- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... print(file.rm_ext()) ... "file_name" >>> >>> # or >>> >>> file = File("file_name.txt") >>> file.rm_ext() "file_name"
- touch()[source]
Creates an empty file.
This class method is analagous to UNIX’s
touchcommand.- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.touch() ... >>> # or >>> >>> file = File("file_name.txt") >>> file.touch()
- Return type
- write(txt='')[source]
Writes/appends text to file.
Note
Text written to file is ALWAYS
utf-8encoded.- Usage example:
>>> # Using class object as context manager >>> with File("file_name.txt") as file: ... file.write("<Text to be written>") ... >>> # or >>> >>> file = File("file_name.txt") >>> file.write("<Text to be written>")
commandio.iobase module
Abstract base class file IO methods, functions and operations for the commandio package.
IO abstract base class ( |
- class commandio.iobase.IOBaseObj(src)[source]
IO abstract base class (
ABC) object that encapsulates methods related to file and directory manipulation.This
ABCcannot be directly instantiated, and MUST used by a child/sub-class that inherits from this class. Additionally, the ``copy` class method (shown in abstract methods) MUST be overwritten when inheriting from this class.- Abstract methods:
copy: Copies a file or recursively copies a directory using
copyandcopytreefromshutil. This method may need to be implemented differently should other aspects of the data need to be preserved (i.e. needing to copy the file metadata with the file).- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ...
- Parameters
src (
str) – Input string that represents a file or directory.
- abspath(follow_sym_links=False)[source]
Returns the absolute file path.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.abspath()) ... "abspath/to/file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.abspath() "abspath/to/file_namt.txt"
- basename()[source]
Retrieves file or directory basename.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.basename()) ... "file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.basename() "file_namt.txt"
- abstract copy(dst)[source]
Copies file or recursively copies a directory to some destination.
This method is an abstract method AND MUST be overwritten when the parent class
IOBaseObjis being inherited from. This allows for the definition of different types of copy methods (e.g. deep vs shallow copies of a file, or the copying of file metadata.)- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... new_file: str = file.copy("file2.txt") ... print(new_file) ... "/abs/path/to/file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.copy("file2.txt") "/abs/path/to/file2.txt"
- dirname()[source]
Retrieves file or directory basename.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.dirname()) ... "/abs/path/to" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.dirname() "/abs/path/to"
- exists()[source]
Tests if a file or directory exists.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.exists()) ... False >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.exists() False
- Return type
- Returns
Returns
Trueif the file or directory exists andFalseotherwise.
- join(*args)[source]
Joins directory or dirname of a file with additional pathname components.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.join("file2.txt")) ... "/abs/path/to/dirname/file1/file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.join("file2.txt") "/abs/path/to/dirname/file1/file2.txt"
- Parameters
*args – Variable length argument list.
- Returns
New file path with the specified directories.
- Return type
- move(dst)[source]
Renames/moves a file/directory.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.move("file2.txt")) ... "file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.move("file2.txt") "file2.txt"
- relpath(dst)[source]
Returns the relative file path to some destination.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.relpath('new_dir/file2.txt')) ... "../file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.relpath('new_dir/file2.txt') "../file_namt.txt"
- sym_link(dst, relative=False)[source]
Creates a symbolic link with an absolute or relative file path.
Note
If a directory is used as the input object, then the linked destination is returned.
This WILL NOT WORK on Windows platforms
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... linked_file: str = file.sym_link("file2.txt") ... print(linked_file) ... "file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.sym_link("file2.txt") "file2.txt"
- Parameters
- Return type
- Returns
String that reprents the absolute path of the sym linked file path.
- class commandio.iobase.IOBaseObj(src)[source]
Bases:
ABCIO abstract base class (
ABC) object that encapsulates methods related to file and directory manipulation.This
ABCcannot be directly instantiated, and MUST used by a child/sub-class that inherits from this class. Additionally, the ``copy` class method (shown in abstract methods) MUST be overwritten when inheriting from this class.- src
Input string that represents a file or directory.
- Abstract methods:
copy: Copies a file or recursively copies a directory using
copyandcopytreefromshutil. This method may need to be implemented differently should other aspects of the data need to be preserved (i.e. needing to copy the file metadata with the file).- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ...
- Parameters
src (
str) – Input string that represents a file or directory.
- abspath(follow_sym_links=False)[source]
Returns the absolute file path.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.abspath()) ... "abspath/to/file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.abspath() "abspath/to/file_namt.txt"
- basename()[source]
Retrieves file or directory basename.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.basename()) ... "file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.basename() "file_namt.txt"
- abstract copy(dst)[source]
Copies file or recursively copies a directory to some destination.
This method is an abstract method AND MUST be overwritten when the parent class
IOBaseObjis being inherited from. This allows for the definition of different types of copy methods (e.g. deep vs shallow copies of a file, or the copying of file metadata.)- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... new_file: str = file.copy("file2.txt") ... print(new_file) ... "/abs/path/to/file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.copy("file2.txt") "/abs/path/to/file2.txt"
- dirname()[source]
Retrieves file or directory basename.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.dirname()) ... "/abs/path/to" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.dirname() "/abs/path/to"
- exists()[source]
Tests if a file or directory exists.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.exists()) ... False >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.exists() False
- Return type
- Returns
Returns
Trueif the file or directory exists andFalseotherwise.
- join(*args)[source]
Joins directory or dirname of a file with additional pathname components.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.join("file2.txt")) ... "/abs/path/to/dirname/file1/file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.join("file2.txt") "/abs/path/to/dirname/file1/file2.txt"
- Parameters
*args – Variable length argument list.
- Returns
New file path with the specified directories.
- Return type
- move(dst)[source]
Renames/moves a file/directory.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.move("file2.txt")) ... "file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.move("file2.txt") "file2.txt"
- relpath(dst)[source]
Returns the relative file path to some destination.
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC method ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... print(file.relpath('new_dir/file2.txt')) ... "../file_namt.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.relpath('new_dir/file2.txt') "../file_namt.txt"
- sym_link(dst, relative=False)[source]
Creates a symbolic link with an absolute or relative file path.
Note
If a directory is used as the input object, then the linked destination is returned.
This WILL NOT WORK on Windows platforms
- Usage example:
>>> # Initialize child class and inherit >>> # from IOBaseObj ABC >>> class SomeFileClass(IOBaseObj): ... def __init__(self, src: str): ... super().__init__(src) ... ... # Overwrite IOBaseObj ABC methods ... def copy(self, dst: str): ... return super().copy(dst) ... >>> # Using class object as context manager >>> with SomeFileClass("file_name.txt") as file: ... linked_file: str = file.sym_link("file2.txt") ... print(linked_file) ... "file2.txt" >>> >>> # OR >>> file = SomeFileClass("file_name.txt") >>> file.sym_link("file2.txt") "file2.txt"
- Parameters
- Return type
- Returns
String that reprents the absolute path of the sym linked file path.
commandio.logutil module
Logging utility module.
Convenience class that creates a log file object for logging purposes. |
- class commandio.logutil.LogFile(log_file='', print_to_screen=False, format_log_str=False, use_root_logger=False, level='info', exc_info=False, stack_info=False, stacklevel=1)[source]
Convenience class that creates a log file object for logging purposes.
- Usage examples:
>>> log = LogFile("file.log",False) >>> log "file.log"
- Parameters
file – Log filename (need not exist at runtime).
print_to_screen (
bool) – If true, prints output to standard output (stdout) as well.format_log_str (
bool) – If true, this formats the logging information with more detail.use_root_logger (
bool) – If true, ALL information is written to a single log file.level (
str) – Logging level. Options include: *info*debug*critical*error*warningexc_info (
bool) – Adds exception information to the logging message.stack_info (
bool) – Adds stack information to the logging message.stacklevel (
int) – If greater than 1, the corresponding number of stack frames are skipped when computing the line number and function name set in theLogRecordfor the logging event.
- critical(msg, use_header=False)[source]
Write critical messages/information to file.
- Usage example:
>>> log = LogFile("file.log") >>> log.critical("<str>")
- debug(msg='', use_header=False)[source]
Writes debug information to file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.debug("<str>")
- error(msg='', use_header=False)[source]
Writes error information to file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.error("<str>")
- info(msg='', use_header=False)[source]
Writes information to log file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.info("<str>")
- class commandio.logutil.LogFile(log_file='', print_to_screen=False, format_log_str=False, use_root_logger=False, level='info', exc_info=False, stack_info=False, stacklevel=1)[source]
Bases:
FileConvenience class that creates a log file object for logging purposes.
- log_file
Log filename.
- Usage examples:
>>> log = LogFile("file.log",False) >>> log "file.log"
- Parameters
file – Log filename (need not exist at runtime).
print_to_screen (
bool) – If true, prints output to standard output (stdout) as well.format_log_str (
bool) – If true, this formats the logging information with more detail.use_root_logger (
bool) – If true, ALL information is written to a single log file.level (
str) – Logging level. Options include: *info*debug*critical*error*warningexc_info (
bool) – Adds exception information to the logging message.stack_info (
bool) – Adds stack information to the logging message.stacklevel (
int) – If greater than 1, the corresponding number of stack frames are skipped when computing the line number and function name set in theLogRecordfor the logging event.
- critical(msg, use_header=False)[source]
Write critical messages/information to file.
- Usage example:
>>> log = LogFile("file.log") >>> log.critical("<str>")
- debug(msg='', use_header=False)[source]
Writes debug information to file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.debug("<str>")
- error(msg='', use_header=False)[source]
Writes error information to file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.error("<str>")
- info(msg='', use_header=False)[source]
Writes information to log file.
- Usage examples:
>>> log = LogFile("file.log") >>> log.info("<str>")
commandio.tmpdir module
Temporary working directory module for the commandio package.
Temporary directory class that creates (random) temporary directories and files given a parent directory. |
- class commandio.tmpdir.TmpDir(src, use_cwd=False, cleanup=True)[source]
Temporary directory class that creates (random) temporary directories and files given a parent directory.
This class inherits methods from the
WorkDirbase class.- Usage example:
>>> with TmpDir("/path/to/temporary_directory",False) as tmp_dir: ... cwd = os.getcwd() ... print("My temporary directory") ... os.chdir(tmp_dir.src) ... # do more stuff ... os.chdir(cwd) ... >>> # or >>> >>> tmp_dir = TmpDir("/path/to/temporary_directory") >>> tmp_dir "/path/to/temporary_directory" >>> tmp_dir.rmdir(rm_parent=False)
- class commandio.tmpdir.TmpDir(src, use_cwd=False, cleanup=True)[source]
Bases:
WorkDirTemporary directory class that creates (random) temporary directories and files given a parent directory.
This class inherits methods from the
WorkDirbase class.- parent_dir
Parent directory of the specified temproary directory.
- Usage example:
>>> with TmpDir("/path/to/temporary_directory",False) as tmp_dir: ... cwd = os.getcwd() ... print("My temporary directory") ... os.chdir(tmp_dir.src) ... # do more stuff ... os.chdir(cwd) ... >>> # or >>> >>> tmp_dir = TmpDir("/path/to/temporary_directory") >>> tmp_dir "/path/to/temporary_directory" >>> tmp_dir.rmdir(rm_parent=False)
- Parameters
commandio.tmpfile module
Temporary file module for the commandio package.
Creates and manipulates temporary files via inheritance from the |
- class commandio.tmpfile.TmpFile(tmp_dir='', tmp_file='', ext='')[source]
Creates and manipulates temporary files via inheritance from the
Fileobject base class.- file
Temporary file name.
- Usage example:
>>> tmp_directory = TmpDir("/path/to/temporary_directory") >>> >>> temp_file = TmpFile(tmp_directory.tmp_dir, ... ext="txt") ... >>> temp_file "/path/to/temporary_directory/temporary_file.txt"
- class commandio.tmpfile.TmpFile(tmp_dir='', tmp_file='', ext='')[source]
Bases:
FileCreates and manipulates temporary files via inheritance from the
Fileobject base class.- file
Temporary file name.
- Usage example:
>>> tmp_directory = TmpDir("/path/to/temporary_directory") >>> >>> temp_file = TmpFile(tmp_directory.tmp_dir, ... ext="txt") ... >>> temp_file "/path/to/temporary_directory/temporary_file.txt"
- Parameters
commandio.util module
Utility module for the commandio package.
Decorator function that times some operation and writes that time to a log file object. |
- commandio.util.timeops(log=None)[source]
Decorator function that times some operation and writes that time to a log file object.
- Usage example:
>>> from logutil import LogFile >>> log = LogFile('my_log_file.log') >>> >>> @timeops(log) >>> def my_func(args*, log): ... for i in args: ... log.log(f"This is an arg: {i}") ... return None ... >>> # The length of time to complete the operation >>> # should be written to the log file. >>> myfunc(args*, log)
commandio.workdir module
Working directory module for the commandio package.
Working directory base class that instantiates |
- class commandio.workdir.WorkDir(src, use_cwd=False)[source]
Working directory base class that instantiates
WorkDirobjects that creates and manipulates working directories.- Usage example:
>>> # Using class object as context manager >>> ## Create work directory , then clean-up (remove it) >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... work.rmdir(rm_parent=False) ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work "/path/to/working_directory" >>> work.rmdir(rm_parent=False)
- Parameters
- copy(dst)[source]
Recursively copies a directory to some destination.
- Usage example:
>>> # Using class object as context manager >>> with WorkDir("/path/to/working_directory") as work_dir: ... work: str = work_dir.copy("/path/to/new/directory") ... >>> work "/path/to/new/directory" >>> >>> # or >>> >>> work = WorkDir("/path/to/working_directory") >>> work.copy("/path/to/new/directory") "/path/to/new/directory"
- mkdir()[source]
Makes/creates the working directory.
This class method is analogous to UNIX’s
mkdir -pcommand and option combination.- Usage example:
>>> # Using class object as context manager >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work "/path/to/working_directory"
- Return type
- rmdir(rm_parent=False)[source]
Removes working directory, and the parent directory if indicated to do so.
This class method is analogous to UNIX’s
rm -rfcommand and option combination.- Usage example:
>>> # Using class object as context manager >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... work.rmdir(rm_parent=False) ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work.rmdir(rm_parent=False)
- class commandio.workdir.WorkDir(src, use_cwd=False)[source]
Bases:
IOBaseObjWorking directory base class that instantiates
WorkDirobjects that creates and manipulates working directories.- parent_dir
Parent directory.
- Usage example:
>>> # Using class object as context manager >>> ## Create work directory , then clean-up (remove it) >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... work.rmdir(rm_parent=False) ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work "/path/to/working_directory" >>> work.rmdir(rm_parent=False)
- Parameters
- copy(dst)[source]
Recursively copies a directory to some destination.
- Usage example:
>>> # Using class object as context manager >>> with WorkDir("/path/to/working_directory") as work_dir: ... work: str = work_dir.copy("/path/to/new/directory") ... >>> work "/path/to/new/directory" >>> >>> # or >>> >>> work = WorkDir("/path/to/working_directory") >>> work.copy("/path/to/new/directory") "/path/to/new/directory"
- mkdir()[source]
Makes/creates the working directory.
This class method is analogous to UNIX’s
mkdir -pcommand and option combination.- Usage example:
>>> # Using class object as context manager >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work "/path/to/working_directory"
- Return type
- rmdir(rm_parent=False)[source]
Removes working directory, and the parent directory if indicated to do so.
This class method is analogous to UNIX’s
rm -rfcommand and option combination.- Usage example:
>>> # Using class object as context manager >>> with WorkDir(src="/path/to/working_directory", use_cwd=False) as work: ... work.mkdir() ... work.rmdir(rm_parent=False) ... >>> # or >>> >>> work = WorkDir(src="/path/to/working_directory", ... use_cwd=False) >>> work.mkdir() >>> work.rmdir(rm_parent=False)
Module contents
The commandio package contains modules for reading/writing files,
logging, and running commands on the command line.
Command module for UNIX command line interactions. |
|
Enums module for the |
|
File IO methods, functions and operations for the |
|
Abstract base class file IO methods, functions and operations for the |
|
Logging utility module. |
|
Temporary working directory module for the |
|
Temporary file module for the |
|
Utility module for the |
|
Working directory module for the |