encommon.utils package#
Subpackages#
Submodules#
encommon.utils.common module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
encommon.utils.files module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.utils.files.append_text(path: str | Path, content: str) None [source]#
Append the provided text content into provided file path.
Example#
>>> path.exists() False >>> append_text(path, 'foo') >>> append_text(path, 'foo') >>> read_text(path) 'foofoo'
- param path:
Complete or relative path to the text file.
- param content:
Content that will be written to the file.
- encommon.utils.files.read_text(path: str | Path) str [source]#
Read the text content from within the provided file path.
Example#
>>> path.exists() False >>> save_text(path, 'foo') 'foo' >>> read_text(path) 'foo'
- param path:
Complete or relative path to the text file.
- returns:
Text content that was read from the file path.
- encommon.utils.files.save_text(path: str | Path, content: str) str [source]#
Save the provided text content to the provided file path.
Example#
>>> path.exists() False >>> save_text(path, 'foo') 'foo' >>> read_text(path) 'foo'
- param path:
Complete or relative path to the text file.
- param content:
Content that will be written to the file.
- returns:
Text content that was read from the file path.
encommon.utils.match module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.utils.match.fuzz_match(values: str | list[str], patterns: str | list[str]) bool [source]#
Determine whether or not values are included by patterns.
Example#
>>> rgxp_match('one', ['on[a-z]', 'two']) True
Example#
>>> rgxp_match('uno', ['on[a-z]', 'two']) False
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- returns:
Boolean indicating outcome from the operation.
- encommon.utils.match.rgxp_match(values: str | list[str], patterns: str | list[str], complete: bool = False) bool [source]#
Determine whether or not values are included by patterns.
Example#
>>> rgxp_match('one', ['on*', 'two']) True
Example#
>>> rgxp_match('uno', ['on*', 'two']) False
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- param complete:
Whether or not whole string must match.
- returns:
Boolean indicating outcome from the operation.
encommon.utils.paths module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.utils.paths.resolve_path(path: str | Path, replace: REPLACE | None = None) Path [source]#
Resolve the provided path replacing the magic keywords.
Example#
>>> resolve_path('/foo/bar') PosixPath('/foo/bar')
- param path:
Complete or relative path for processing.
- param replace:
Optional values to replace in the path.
- returns:
New resolved filesystem path object instance.
- encommon.utils.paths.resolve_paths(paths: PATHABLE, replace: REPLACE | None = None) tuple[Path, ...] [source]#
Resolve the provided paths replacing the magic keywords.
Note
This will remove duplicative paths from the returned.
Example#
>>> resolve_paths(['/foo/bar']) (PosixPath('/foo/bar'),)
- param paths:
Complete or relative paths for processing.
- param replace:
Optional values to replace in the path.
- returns:
New resolved filesystem path object instances.
- encommon.utils.paths.stats_path(path: str | Path, replace: REPLACE | None = None, ignore: list[str] | None = None) dict[str, stat_result] [source]#
Collect stats object for the complete or relative path.
Example#
>>> replace = {str(path): '/'} >>> stats = stats_path(path, replace) >>> stats['/hello.txt'].st_size 12
- param path:
Complete or relative path for enumeration.
- param replace:
Optional values to replace in the path.
- param ignore:
Paths matching these patterns are ignored.
- returns:
Metadata for files recursively found in path.
encommon.utils.sample module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.utils.sample.load_sample(path: str | ~pathlib.Path, content: ~typing.Any | None = None, update: bool = False, *, default: ~typing.Callable[[~typing.Any], str] = <class 'str'>, replace: dict[str, ~typing.Any] | None = None) str [source]#
Load the sample file and compare using provided content.
Example#
>>> content = {'one': 'two'} >>> load_sample(sample, content) '{\n "one": "two"\n}'
Example#
>>> load_sample(sample) '{\n "one": "two"\n}'
- param path:
Complete or relative path for sample file.
- param update:
Determine whether the sample is updated.
- param content:
Content that will be processed as JSON.
- param default:
Callable used when stringifying values.
- param replace:
Optional values to replace in the file.
- returns:
Content after processing using JSON functions.
- encommon.utils.sample.prep_sample(content: ~typing.Any, *, default: ~typing.Callable[[~typing.Any], str] = <class 'str'>, replace: dict[str, ~typing.Any] | None = None, indent: int | None = 2) str [source]#
Return the content after processing as the sample value.
Example#
>>> prep_sample(['one', 'two']) '[\n "one",\n "two"\n]'
Example#
>>> from ..types import Empty >>> prep_sample({'one': Empty}) '{\n "one": "Empty"\n}'
- param content:
Content that will be processed as JSON.
- param default:
Callable used when stringifying values.
- param replace:
Optional values to replace in the file.
- returns:
Content after processing as the sample value.
- encommon.utils.sample.read_sample(sample: str, *, replace: dict[str, Any] | None = None, prefix: bool = True) str [source]#
Return the content after processing as the sample value.
- Parameters:
sample – Content that will be processed as sample.
replace – Optional values to replace in the file.
prefix – Determine whether or not prefix is added.
- Returns:
Content after processing as the sample value.
- encommon.utils.sample.rvrt_sample(sample: str, *, replace: dict[str, Any] | None = None) str [source]#
Return the content after processing as the sample value.
- Parameters:
sample – Content that will be processed as sample.
replace – Optional values to replace in the file.
- Returns:
Content after processing as the sample value.
encommon.utils.stdout module#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- class encommon.utils.stdout.ArrayColors(label: int = 37, key: int = 97, colon: int = 37, hyphen: int = 37, bool: int = 93, none: int = 33, str: int = 92, num: int = 93, times: int = 96, empty: int = 36, other: int = 91)[source]#
Bases:
object
Colors used to colorize the provided array like object.
- encommon.utils.stdout.array_ansi(source: list[Any] | tuple[Any, ...] | set[Any] | dict[Any, Any] | BaseModel | DataclassInstance, *, indent: int = 0, colors: ArrayColors = ArrayColors(label=37, key=97, colon=37, hyphen=37, bool=93, none=33, str=92, num=93, times=96, empty=36, other=91)) str [source]#
Print the ANSI colorized iterable to the standard output.
Note
This massive function should be refactored, possibly into a class with methods where there are functions.
Example#
>>> array_ansi({'foo': 'bar'}) "\x1b[0;97mfoo\x1b[0;37m:\x1b[0;0m...
- param source:
Value in supported and iterable formats.
- param indent:
How many levels for initial indentation.
- param colors:
Determine colors used with different types.
- returns:
ANSI colorized string using inline directives.
- encommon.utils.stdout.kvpair_ansi(key: str, value: Any) str [source]#
Process and colorize keys and values for standard output.
Example#
>>> kvpair_ansi('k', 'v') '\x1b[0;90mk\x1b[0;37m="\x1b[0;0m...
- param key:
String value to use for the key name portion.
- param value:
String value to use for the value portion.
- returns:
ANSI colorized string using inline directives.
- encommon.utils.stdout.make_ansi(string: str) str [source]#
Parse the string and replace directives with ANSI codes.
Example#
>>> make_ansi('<c91>ERROR<c0>') '\x1b[0;91mERROR\x1b[0;0m'
- param string:
String containing directives to replace.
- returns:
Provided string with the directives replaced.
- encommon.utils.stdout.print_ansi(string: str = '', method: Literal['stdout', 'print'] = 'stdout', output: bool = True) str [source]#
Print the ANSI colorized string to the standard output.
Example#
>>> print_ansi('<c91>ERROR<c0>') '\x1b[0;91mERROR\x1b[0;0m'
- param string:
String processed using inline directives.
- param method:
Which method for standard output is used.
- param output:
Whether or not hte output should be print.
- returns:
ANSI colorized string using inline directives.
Module contents#
Functions and routines associated with Enasis Network Common Library.
This file is part of Enasis Network software eco-system. Distribution is permitted, for more information consult the project license file.
- encommon.utils.append_text(path: str | Path, content: str) None [source]#
Append the provided text content into provided file path.
Example#
>>> path.exists() False >>> append_text(path, 'foo') >>> append_text(path, 'foo') >>> read_text(path) 'foofoo'
- param path:
Complete or relative path to the text file.
- param content:
Content that will be written to the file.
- encommon.utils.array_ansi(source: list[Any] | tuple[Any, ...] | set[Any] | dict[Any, Any] | BaseModel | DataclassInstance, *, indent: int = 0, colors: ArrayColors = ArrayColors(label=37, key=97, colon=37, hyphen=37, bool=93, none=33, str=92, num=93, times=96, empty=36, other=91)) str [source]#
Print the ANSI colorized iterable to the standard output.
Note
This massive function should be refactored, possibly into a class with methods where there are functions.
Example#
>>> array_ansi({'foo': 'bar'}) "\x1b[0;97mfoo\x1b[0;37m:\x1b[0;0m...
- param source:
Value in supported and iterable formats.
- param indent:
How many levels for initial indentation.
- param colors:
Determine colors used with different types.
- returns:
ANSI colorized string using inline directives.
- encommon.utils.fuzz_match(values: str | list[str], patterns: str | list[str]) bool [source]#
Determine whether or not values are included by patterns.
Example#
>>> rgxp_match('one', ['on[a-z]', 'two']) True
Example#
>>> rgxp_match('uno', ['on[a-z]', 'two']) False
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- returns:
Boolean indicating outcome from the operation.
- encommon.utils.kvpair_ansi(key: str, value: Any) str [source]#
Process and colorize keys and values for standard output.
Example#
>>> kvpair_ansi('k', 'v') '\x1b[0;90mk\x1b[0;37m="\x1b[0;0m...
- param key:
String value to use for the key name portion.
- param value:
String value to use for the value portion.
- returns:
ANSI colorized string using inline directives.
- encommon.utils.load_sample(path: str | ~pathlib.Path, content: ~typing.Any | None = None, update: bool = False, *, default: ~typing.Callable[[~typing.Any], str] = <class 'str'>, replace: dict[str, ~typing.Any] | None = None) str [source]#
Load the sample file and compare using provided content.
Example#
>>> content = {'one': 'two'} >>> load_sample(sample, content) '{\n "one": "two"\n}'
Example#
>>> load_sample(sample) '{\n "one": "two"\n}'
- param path:
Complete or relative path for sample file.
- param update:
Determine whether the sample is updated.
- param content:
Content that will be processed as JSON.
- param default:
Callable used when stringifying values.
- param replace:
Optional values to replace in the file.
- returns:
Content after processing using JSON functions.
- encommon.utils.make_ansi(string: str) str [source]#
Parse the string and replace directives with ANSI codes.
Example#
>>> make_ansi('<c91>ERROR<c0>') '\x1b[0;91mERROR\x1b[0;0m'
- param string:
String containing directives to replace.
- returns:
Provided string with the directives replaced.
- encommon.utils.prep_sample(content: ~typing.Any, *, default: ~typing.Callable[[~typing.Any], str] = <class 'str'>, replace: dict[str, ~typing.Any] | None = None, indent: int | None = 2) str [source]#
Return the content after processing as the sample value.
Example#
>>> prep_sample(['one', 'two']) '[\n "one",\n "two"\n]'
Example#
>>> from ..types import Empty >>> prep_sample({'one': Empty}) '{\n "one": "Empty"\n}'
- param content:
Content that will be processed as JSON.
- param default:
Callable used when stringifying values.
- param replace:
Optional values to replace in the file.
- returns:
Content after processing as the sample value.
- encommon.utils.print_ansi(string: str = '', method: Literal['stdout', 'print'] = 'stdout', output: bool = True) str [source]#
Print the ANSI colorized string to the standard output.
Example#
>>> print_ansi('<c91>ERROR<c0>') '\x1b[0;91mERROR\x1b[0;0m'
- param string:
String processed using inline directives.
- param method:
Which method for standard output is used.
- param output:
Whether or not hte output should be print.
- returns:
ANSI colorized string using inline directives.
- encommon.utils.read_sample(sample: str, *, replace: dict[str, Any] | None = None, prefix: bool = True) str [source]#
Return the content after processing as the sample value.
- Parameters:
sample – Content that will be processed as sample.
replace – Optional values to replace in the file.
prefix – Determine whether or not prefix is added.
- Returns:
Content after processing as the sample value.
- encommon.utils.read_text(path: str | Path) str [source]#
Read the text content from within the provided file path.
Example#
>>> path.exists() False >>> save_text(path, 'foo') 'foo' >>> read_text(path) 'foo'
- param path:
Complete or relative path to the text file.
- returns:
Text content that was read from the file path.
- encommon.utils.resolve_path(path: str | Path, replace: REPLACE | None = None) Path [source]#
Resolve the provided path replacing the magic keywords.
Example#
>>> resolve_path('/foo/bar') PosixPath('/foo/bar')
- param path:
Complete or relative path for processing.
- param replace:
Optional values to replace in the path.
- returns:
New resolved filesystem path object instance.
- encommon.utils.resolve_paths(paths: PATHABLE, replace: REPLACE | None = None) tuple[Path, ...] [source]#
Resolve the provided paths replacing the magic keywords.
Note
This will remove duplicative paths from the returned.
Example#
>>> resolve_paths(['/foo/bar']) (PosixPath('/foo/bar'),)
- param paths:
Complete or relative paths for processing.
- param replace:
Optional values to replace in the path.
- returns:
New resolved filesystem path object instances.
- encommon.utils.rgxp_match(values: str | list[str], patterns: str | list[str], complete: bool = False) bool [source]#
Determine whether or not values are included by patterns.
Example#
>>> rgxp_match('one', ['on*', 'two']) True
Example#
>>> rgxp_match('uno', ['on*', 'two']) False
- param values:
Value or values to enumerate for matching.
- param patterns:
Patterns which values can match any one.
- param complete:
Whether or not whole string must match.
- returns:
Boolean indicating outcome from the operation.
- encommon.utils.rvrt_sample(sample: str, *, replace: dict[str, Any] | None = None) str [source]#
Return the content after processing as the sample value.
- Parameters:
sample – Content that will be processed as sample.
replace – Optional values to replace in the file.
- Returns:
Content after processing as the sample value.
- encommon.utils.save_text(path: str | Path, content: str) str [source]#
Save the provided text content to the provided file path.
Example#
>>> path.exists() False >>> save_text(path, 'foo') 'foo' >>> read_text(path) 'foo'
- param path:
Complete or relative path to the text file.
- param content:
Content that will be written to the file.
- returns:
Text content that was read from the file path.
- encommon.utils.stats_path(path: str | Path, replace: REPLACE | None = None, ignore: list[str] | None = None) dict[str, stat_result] [source]#
Collect stats object for the complete or relative path.
Example#
>>> replace = {str(path): '/'} >>> stats = stats_path(path, replace) >>> stats['/hello.txt'].st_size 12
- param path:
Complete or relative path for enumeration.
- param replace:
Optional values to replace in the path.
- param ignore:
Paths matching these patterns are ignored.
- returns:
Metadata for files recursively found in path.