Reference¶
nodeps¶
NoDeps Helpers and Utils Module.
- nodeps.getstdout(func, *args, ansi=False, new=True, **kwargs)[source]¶
Redirect stdout for func output and remove ansi and/or new line.
- class nodeps.EnumLower(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
EnumEnumLower class.
- class nodeps.Color(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
_Colorclick.secho()andclick.style()foreground color wrapper class.- BLACK = 'black'¶
might be a gray
- BLUE = 'blue'¶
- CYAN = 'cyan'¶
- GREEN = 'green'¶
- MAGENTA = 'magenta'¶
- RED = 'red'¶
- WHITE = 'white'¶
might be an grey
- YELLOW = 'yellow'¶
might be an orange
- BRIGHT_BLACK = 'bright_black'¶
- BRIGHT_BLUE = 'bright_blue'¶
- BRIGHT_CYAN = 'bright_cyan'¶
- BRIGHT_GREEN = 'bright_green'¶
- BRIGHT_MAGENTA = 'bright_magenta'¶
- BRIGHT_RED = 'bright_red'¶
- BRIGHT_WHITE = 'bright_white'¶
- BRIGHT_YELLOW = 'bright_yellow'¶
- RESET = 'reset'¶
bold, underline, etc.
- Type:
reset the color only, not styles
- style(text, bg=None, bold=None, dim=None, underline=None, overline=None, italic=None, blink=None, reverse=None, strikethrough=None, reset=True)[source]¶
Wrapper for
click.style()getting the fg color from the enum.Styles a text with ANSI styles and returns the new string.
- By default, the styling is self-contained which means that at the end of the string a
reset code is issued (this can be prevented by passing reset=False.
If the terminal supports it, color may also be specified as:
An integer in the interval [0, 255]. The terminal must support 8-bit/256-color mode.
An RGB tuple of three integers in [0, 255]. The terminal must support 24-bit/true-color mode
See click.style for more information.
- Parameters:
text (Any) – Text to apply style
bg (str | int | tuple[int, int, int] | None) – Background color (default: None)
bold (bool | None) – Bold text (default: None)
dim (bool | None) – Dim (default: None)
underline (bool | None) – Underline (default: None)
overline (bool | None) – Overline (default: None)
italic (bool | None) – Italic (default: None)
blink (bool | None) – Blink (default: None)
reverse (bool | None) – Reverse (default: None)
strikethrough (bool | None) – Strikethrough (default: None)
reset (bool) – Reset (default: True)
- Returns:
Formatted text
- Return type:
- class nodeps.Symbol(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
_Symbolclick.echo()andclick.style()wrapper class forSYMBOLS.Examples
>>> from nodeps import Symbol >>> >>> Symbol.OK() # OK >>> >>> Symbol.OK("Install") # OK Install >>> >>> Symbol.OK("Install", "Complete") # OK Install: Complete >>> >>> Symbol.OK("Install", "Complete", stderr=False) >>> >>> Symbol.OK("Debug", "Error", " |", stderr=False) >>> >>> Symbol.OK("Value", "2", " ==", file="/tmp/test.txt") >>> >>> Symbol.OK("Value", "2", newline=False) # OK Value: 2
- CRITICAL = '\x1b[31m\x1b[1m\x1b[5mCRITICAL\x1b[0m'¶
YELLOW (blink)
- Type:
symbol
- Type:
‘…’, color
- ERROR = '\x1b[31m\x1b[1mERROR\x1b[0m'¶
RED
- Type:
symbol
- Type:
‘✘’, color
- OK = '\x1b[32m\x1b[1mOK\x1b[0m'¶
GREEN
- Type:
symbol
- Type:
‘✔’, color
- NOTICE = '\x1b[36m\x1b[1mNOTICE\x1b[0m'¶
CYAN
- Type:
symbol
- Type:
‘‼’, color
- SUCCESS = '\x1b[34m\x1b[1mSUCCESS\x1b[0m'¶
BLUE
- Type:
symbol
- Type:
‘◉’, color
- VERBOSE = '\x1b[35m\x1b[1mVERBOSE\x1b[0m'¶
MAGENTA
- Type:
symbol
- Type:
‘+’, color
- WARNING = '\x1b[33m\x1b[1mWARNING\x1b[0m'¶
YELLOW
- Type:
symbol
- Type:
‘!’, color
- MINUS = '\x1b[31m\x1b[1mMINUS\x1b[0m'¶
RED
- Type:
letter
- Type:
‘-’, color
- MORE = '\x1b[35m\x1b[1mMORE\x1b[0m'¶
MAGENTA
- Type:
letter
- Type:
‘>, color
- MULTIPLY = '\x1b[34m\x1b[1mMULTIPLY\x1b[0m'¶
BLUE
- Type:
letter
- Type:
‘x’, color
- PLUS = '\x1b[31m\x1b[1mPLUS\x1b[0m'¶
GREEN
- Type:
letter
- Type:
‘+’, color
- WAIT = '\x1b[33m\x1b[1mWAIT\x1b[0m'¶
YELLOW (blink)
- Type:
symbol
- Type:
‘…’, color
- nodeps.logger(fmt='<level>{level: <8}</level> <red>|</red> <cyan>{name}</cyan> <red>|</red> <red>|</red> <level>{message}</level>')[source]¶
Returns a new logger.
Examples
>>> from nodeps import logger >>> >>> l = logger("<level>{level: <8}</level> <red>|</red> " ... "<cyan>{name}</cyan> <red>|</red> " ... "<blue><level>{message}</level></blue><red>:</red> " ... "<level>{extra[source]}</level> <red>-></red> " ... "<level>{extra[destination]}</level>") >>> l.info("test", source="source", destination="destination")
- nodeps.cache(func=Ellipsis)[source]¶
Caches previous calls to the function if object can be encoded.
Examples
>>> import asyncio >>> from typing import cast >>> from typing import Coroutine >>> from environs import Env as Environs >>> from collections import namedtuple >>> from nodeps import cache >>> >>> @cache ... def test(a): ... print(True) ... return a >>> >>> @cache ... async def test_async(a): ... print(True) ... return a >>> >>> test({}) True {} >>> test({}) {} >>> asyncio.run(cast(Coroutine, test_async({}))) True {} >>> asyncio.run(cast(Coroutine, test_async({}))) {} >>> test(Environs()) True <Env {}> >>> test(Environs()) <Env {}> >>> asyncio.run(cast(Coroutine, test_async(Environs()))) True <Env {}> >>> asyncio.run(cast(Coroutine, test_async(Environs()))) <Env {}> >>> >>> @cache ... class Test: ... def __init__(self, a): ... print(True) ... self.a = a ... ... @property ... @cache ... def prop(self): ... print(True) ... return self >>> >>> Test({}) True <....Test object at 0x...> >>> Test({}) <....Test object at 0x...> >>> Test({}).a {} >>> Test(Environs()).a True <Env {}> >>> Test(Environs()).prop True <....Test object at 0x...> >>> Test(Environs()).prop <....Test object at 0x...> >>> >>> Test = namedtuple('Test', 'a') >>> @cache ... class TestNamed(Test): ... __slots__ = () ... def __new__(cls, *args, **kwargs): ... print(True) ... return super().__new__(cls, *args, **kwargs) >>> >>> TestNamed({}) True TestNamed(a={}) >>> TestNamed({}) TestNamed(a={}) >>> @cache ... class TestNamed(Test): ... __slots__ = () ... def __new__(cls, *args, **kwargs): return super().__new__(cls, *args, **kwargs) ... def __init__(self): super().__init__() >>> TestNamed({}) Traceback (most recent call last): TypeError: __init__() takes 1 positional argument but 2 were given
- nodeps.ins(obj, *, _console=None, title=None, _help=False, methods=True, docs=False, private=True, dunder=False, sort=True, _all=False, value=True)[source]¶
Wrapper
rich.inspect()forrich._inspect.Inspect.Changing defaults to:
docs=False, methods=True, private=True.Inspect any Python object.
Examples
>>> from nodeps import ins >>> >>> # to see summarized info. >>> ins(ins) >>> # to not see methods. >>> ins(ins, methods=False) >>> # to see full (non-abbreviated) help. >>> ins(ins, help=True) >>> # to not see private attributes (single underscore). >>> ins(ins, private=False) >>> # to see attributes beginning with double underscore. >>> ins(ins, dunder=True) >>> # to see all attributes. >>> ins(ins, _all=True) '
- Parameters:
obj (Any) – An object to inspect.
_console (Console, optional) – Rich Console.
title (str, optional) – Title to display over inspect result, or None use type. Defaults to None.
_help (bool, optional) – Show full help text rather than just first paragraph. Defaults to False.
methods (bool, optional) – Enable inspection of callables. Defaults to False.
docs (bool, optional) – Also render doc strings. Defaults to True.
private (bool, optional) – Show private attributes (beginning with underscore). Defaults to False.
dunder (bool, optional) – Show attributes starting with double underscore. Defaults to False.
sort (bool, optional) – Sort attributes alphabetically. Defaults to True.
_all (bool, optional) – Show all attributes. Defaults to False.
value (bool, optional) – Pretty print value. Defaults to True.
- nodeps.is_terminal(self=None)[source]¶
Patch for rich console is terminal.
Examples
>>> import time >>> from rich.console import Console >>> from rich.json import JSON >>> from rich import print_json >>> >>> c = Console() >>> with c.status("Working...", spinner="material"): ... time.sleep(2) >>> >>> c.log(JSON('["foo", "bar"]')) >>> >>> print_json('["foo", "bar"]') >>> >>> c.log("Hello, World!") >>> c.print([1, 2, 3]) >>> c.print("[blue underline]Looks like a link") >>> c.print(locals()) >>> c.print("FOO", style="white on blue") >>> >>> blue_console = Console(style="white on blue") >>> blue_console.print("I'm blue. Da ba dee da ba di.") >>> >>> c.input("What is [i]your[/i] [bold red]name[/]? :smiley: ")
References
Test with: print(“[italic red]Hello[/italic red] World!”, locals())
rich.traceback.install(suppress=[click])To see the spinners: python -m rich.spinner To print json from the comamand line: python -m rich.json cats.json
Input: console.input(“What is [i]your[/i] [bold red]name[/]? :smiley: “)
- class nodeps.Repo(path=None, expand_vars=True, odbt=None, search_parent_directories=True)[source]¶
Bases:
objectDataclass Wrapper for
git.Repo.Represents a git repository and allows you to query references, gather commit information, generate diffs, create and clone repositories query the log.
‘working_tree_dir’ is the working tree directory, but will raise AssertionError if we are a bare repository.
Examples
>>> from nodeps.extras.repo import Repo >>> >>> repo = Repo() >>> repo.config_writer().set_value("user", "name", "root").release()
- Parameters:
- git: None¶
The Repo class manages communication with the Git binary.
It provides a convenient interface to calling the Git binary, such as in:
g = Repo( git_dir ) g.init() # calls 'git init' program rval = g.ls_files() # calls 'git ls-files' program
DebuggingSet the GIT_PYTHON_TRACE environment variable print each invocation of the command to stdout. Set its value to ‘full’ to see details about the returned values.
- working_dir: PathLike | AnyStr | IO | None = None¶
working directory of the git command, which is the working tree directory if available or the .git directory in case of bare repositories
- path: dataclasses.InitVar[Union[os.PathLike, AnyStr, IO[AnyStr], NoneType]] = None¶
File or Directory inside the git repository, the default with search_parent_directories
- odbt: dataclasses.InitVar[type[None]] = None¶
the path to either the root git directory or the bare git repo
- search_parent_directories: dataclasses.InitVar[bool] = True¶
if True, all parent directories will be searched for a valid repo as well.
- property git_config¶
Wrapper for
git.Repo.config_reader(), so it is already read and can be used.The configuration will include values from the system, user and repository configuration files.
Examples
>>> import nodeps >>> from nodeps import Repo, Path >>> >>> if not Path(nodeps.__file__).installed(): ... conf = Repo(__file__).git_config ... assert conf.has_section('remote "origin"') is True ... assert conf.has_option('remote "origin"', 'url') is True ... assert 'https://github.com/' in conf.get('remote "origin"', 'url') ... assert 'https://github.com/' in conf.get_value('remote "origin"', 'url', "")
- Returns:
GitConfigParser instance
- Return type:
GitConfigParser
- property origin_url¶
Git Origin URL.
Examples
>>> import nodeps >>> from nodeps import Repo, Path >>> >>> if not Path(nodeps.__file__).installed(): ... assert 'https://github.com' in Repo(nodeps.__file__).origin_url.geturl()
- property top¶
Repo Top Directory Path.
- nodeps.python_latest(start=None)[source]¶
Python latest version avaialble.
Examples
>>> import platform >>> from nodeps import python_latest >>> >>> v = platform.python_version() >>> if "rc" not in v: ... major_minor = v.rpartition(".")[0] ... assert python_latest(v).rpartition(".")[0] == major_minor ... assert python_latest(v).rpartition(".")[2] >= v.rpartition(".")[2] ... assert python_latest(major_minor).rpartition(".")[0] == major_minor ... assert python_latest(major_minor).rpartition(".")[2] >= v.rpartition(".")[2]
- nodeps.python_version()[source]¶
Major Minor Version
$PYTHON_VERSION,$PYTHON_REQUIRES,PYTHON_DEFAULT_VERSIONorsys.version.Examples
>>> import os >>> import platform >>> from nodeps import python_version >>> >>> v = python_version() >>> assert platform.python_version().startswith(v) >>> assert len(v.split(".")) == 2 >>> >>> os.environ["PYTHON_VERSION"] = "3.10" >>> assert python_version() == "3.10" >>> >>> os.environ["PYTHON_VERSION"] = "3.12-dev" >>> assert python_version() == "3.12-dev" >>> >>> os.environ["PYTHON_VERSION"] = "3.12.0b4" >>> assert python_version() == "3.12"
- Returns:
str
- Return type:
- nodeps.python_versions()[source]¶
Python versions avaialble.
Examples
>>> import platform >>> from nodeps import python_versions >>> >>> v = platform.python_version() >>> if not "rc" in v: ... assert v in python_versions()
- nodeps.request_x_api_key_json(url, key='')[source]¶
API request helper with API Key and returning json.
Examples
>>> from nodeps import request_x_api_key_json >>> >>> request_x_api_key_json("https://api.iplocation.net/?ip=8.8.8.8", "rn5ya4fp/tzI/mENxaAvxcMo8GMqmg7eMnCvUFLIV/s=") {'ip': '8.8.8.8', 'ip_number': '134744072', 'ip_version': 4, 'country_name': 'United States of America', 'country_code2': 'US', 'isp': 'Google LLC', 'response_code': '200', 'response_message': 'OK'}
- nodeps.IPYTHONConfigType¶
alias of
Config
- class nodeps.Chain(*maps, rv='unique', default=None)[source]¶
Bases:
ChainMapVariant of chain that allows direct updates to inner scopes and returns more than one value, not the first one.
Examples
>>> from nodeps import Chain >>> >>> class Test3: ... a = 2 >>> >>> class Test4: ... a = 2 >>> >>> Test1 = collections.namedtuple('Test1', 'a b') >>> Test2 = collections.namedtuple('Test2', 'a d') >>> test1 = Test1(1, 2) >>> test2 = Test2(3, 5) >>> test3 = Test3() >>> test4 = Test4() >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2))] >>> chain = Chain(*maps) >>> assert chain['a'] == [1, 2, 3, {'z': 1}, {'z': 2}] >>> chain = Chain(*maps, rv="first") >>> assert chain['a'] == 1 >>> chain = Chain(*maps, rv="all") >>> assert chain['a'] == [1, 2, 3, {'z': 1}, {'z': 1}, {'z': 2}] >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test2] >>> chain = Chain(*maps) >>> assert chain['a'] == [1, 2, 3, {'z': 1}, {'z': 2}] >>> chain = Chain(*maps, rv="first") >>> assert chain['a'] == 1 >>> chain = Chain(*maps, rv="all") >>> assert chain['a'] == [1, 2, 3, {'z': 1}, {'z': 1}, {'z': 2}, 1, 3] >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test2] >>> chain = Chain(*maps) >>> del chain['a'] >>> assert chain == Chain({'b': 2}, {'c': 3}, {'d': 4}, test1, test2) >>> assert chain['a'] == [1, 3] >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test2] >>> chain = Chain(*maps) >>> assert chain.delete('a') == Chain({'b': 2}, {'c': 3}, {'d': 4}, test1, test2) >>> assert chain.delete('a')['a'] == [1, 3] >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test2] >>> chain = Chain(*maps, rv="first") >>> del chain['a'] >>> del maps[0]['a'] Traceback (most recent call last): KeyError: >>> >>> assert chain['a'] == 2 >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test2] >>> chain = Chain(*maps, rv="first") >>> new = chain.delete('a') >>> del maps[0]['a'] Traceback (most recent call last): KeyError: >>> assert new.delete('a') >>> del maps[1]['a'] Traceback (most recent call last): KeyError: >>> >>> assert new['a'] == 3 >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test3] >>> chain = Chain(*maps) >>> del chain['a'] >>> assert chain[4] == [] >>> assert not hasattr(test3, 'a') >>> assert chain.set('a', 9) >>> assert chain['a'] == [9, 1] >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test4] >>> chain = Chain(*maps) >>> chain.set('j', 9) Chain({'a': 1, 'b': 2, 'j': 9}, {'a': 2, 'c': 3}, {'a': 3, 'd': 4}, {'a': {'z': 1}}, {'a': {'z': 1}}, {'a': {'z': 2}}, Test1(a=1, b=2), <....Test4 object at 0x...>) >>> assert [maps[0]['j']] == chain['j'] == [9] >>> chain.set('a', 10) Chain({'a': 10, 'b': 2, 'j': 9}, {'a': 10, 'c': 3}, {'a': 10, 'd': 4}, {'a': 10}, {'a': 10}, {'a': 10}, Test1(a=1, b=2), <....Test4 object at 0x...>) >>> # noinspection PyUnresolvedReferences >>> assert [maps[0]['a'], 1] == chain['a'] == [maps[7].a, 1] == [10, 1] # 1 from namedtuple >>> >>> maps = [dict(a=1, b=2), dict(a=2, c=3), dict(a=3, d=4), dict(a=dict(z=1)), dict(a=dict(z=1)), dict(a=dict(z=2)), test1, test4] >>> chain = Chain(*maps, rv="first") >>> chain.set('a', 9) Chain({'a': 9, 'b': 2}, {'a': 2, 'c': 3}, {'a': 3, 'd': 4}, {'a': {'z': 1}}, {'a': {'z': 1}}, {'a': {'z': 2}}, Test1(a=1, b=2), <....Test4 object at 0x...>) >>> assert maps[0]['a'] == chain['a'] == 9 >>> assert maps[1]['a'] == 2
Init.
- Parameters:
rv (ChainLiteral)
default (Any)
- maps: list[Iterable | NamedtupleMeta | MutableMapping] = []¶
- rv: ChainLiteral = 'unique'¶
- default: Any = None¶
- class nodeps.ColorLogger(fmt=None, datefmt=None, style='%', validate=True, *, defaults=None)[source]¶
Bases:
FormatterColor logger class.
Initialize the formatter with specified format strings.
Initialize the formatter either with the specified format string, or a default as described above. Allow for specialized date formatting with the optional datefmt argument. If datefmt is omitted, you get an ISO8601-like (or RFC 3339-like) format.
Use a style parameter of ‘%’, ‘{’ or ‘$’ to specify that you want to use one of %-formatting,
str.format()({}) formatting orstring.Templateformatting in your format string.Changed in version 3.2: Added the
styleparameter.- black = '\x1b[30m'¶
- blue = '\x1b[34m'¶
- cyan = '\x1b[36m'¶
- gr = '\x1b[32m'¶
- grey = '\x1b[38;21m'¶
- mg = '\x1b[35m'¶
- red = '\x1b[31;21m'¶
- red_bold = '\x1b[31;1m'¶
- reset = '\x1b[0m'¶
- white = '\x1b[37m'¶
- yellow = '\x1b[33;21m'¶
- fmt = '%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)'¶
- vertical = '\x1b[31;21m|\x1b[0m '¶
- FORMATS: ClassVar[dict[int, str]] = {10: '\x1b[38;21m%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)\x1b[0m', 20: '\x1b[36m%(levelname)8s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[36m%(name)s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[36m%(filename)s\x1b[0m:\x1b[36m%(lineno)d\x1b[0m \x1b[31;21m|\x1b[0m \x1b[32m%(extra)s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[36m%(message)s\x1b[0m', 30: '\x1b[33;21m%(levelname)8s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[33;21m%(name)s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[33;21m%(filename)s\x1b[0m:\x1b[33;21m%(lineno)d\x1b[0m \x1b[31;21m|\x1b[0m \x1b[32m%(repo)s\x1b[0m \x1b[31;21m|\x1b[0m \x1b[33;21m%(message)s\x1b[0m', 40: '\x1b[31;21m%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)\x1b[0m', 50: '\x1b[31;1m%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)\x1b[0m'}¶
- class nodeps.ConfigParser(defaults=None, dict_type=<class 'dict'>, allow_no_value=False, *, delimiters=('=', ':'), comment_prefixes=('#', ';'), inline_comment_prefixes=None, strict=True, empty_lines_in_values=True, default_section='DEFAULT', interpolation=<object object>, converters=<object object>)[source]¶
Bases:
ConfigParserConfig parser to get list from setup.cfg.
- class nodeps.dd(factory=None, *args, **kwargs)[source]¶
Bases:
defaultdictDefault Dict Helper Class.
Examples
>>> from nodeps import dd >>> >>> d = dd() >>> d dd(None, {}) >>> d[1] >>> d.get(1) >>> >>> d = dd({}) >>> d dd(None, {}) >>> d[1] >>> d.get(1) >>> >>> d = dd({}, a=1) >>> d dd(None, {'a': 1}) >>> d[1] >>> d.get(1) >>> >>> d = dd(dict) >>> d dd(<class 'dict'>, {}) >>> d.get(1) >>> d dd(<class 'dict'>, {}) >>> d[1] {} >>> d dd(<class 'dict'>, {1: {}}) >>> d = dd(tuple) >>> d dd(<class 'tuple'>, {}) >>> d[1] () >>> d.get(1) () >>> >>> d = dd(True) >>> d dd(True, {}) >>> d[1] True >>> d.get(1) True >>> >>> d = dd({1: 1}, a=1) >>> d dd(None, {1: 1, 'a': 1}) >>> d[1] 1 >>> d.get(1) 1 >>> >>> d = dd(list, {1: 1}, a=1) >>> d dd(<class 'list'>, {1: 1, 'a': 1}) >>> d[2] [] >>> d dd(<class 'list'>, {1: 1, 'a': 1, 2: []}) >>> >>> d = dd(True, {1: 1}, a=1) >>> d dd(True, {1: 1, 'a': 1}) >>> d.get('c') >>> d['c'] True
Init.
- Parameters:
factory (Union[Callable, Any])
args (Any)
kwargs (Any)
- class nodeps.dictsort(*args, **kwargs)[source]¶
Bases:
dict,MutableMapping[_KT,_VT]Dict Sort Class.
Examples
>>> from nodeps import dictsort >>> >>> d = dictsort(b=1, c=2, a=3) >>> assert d.sort() == dictsort({'a': 3, 'b': 1, 'c': 2})
Init.
- Parameters:
args (Any)
kwargs (Any)
- class nodeps.getter(attr, *attrs, default=None)[source]¶
Bases:
Callable[[Any],Any|tuple[Any, …]]Return a callable object that fetches the given attribute(s)/item(s) from its operand.
Examples
>>> from types import SimpleNamespace >>> from pickle import dumps, loads >>> from copy import deepcopy >>> from nodeps import getter >>> >>> test = SimpleNamespace(a='a', b='b') >>> assert getter('a b')(test) == (test.a, test.b) >>> assert getter('a c')(test) == (test.a, None) >>> dicts = getter('a c d', default={})(test) >>> assert dicts == (test.a, {}, {}) >>> assert id(dicts[1]) != id(dicts[2]) >>> assert getter('a')(test) == test.a >>> assert getter('a b', 'c')(test) == (test.a, test.b, None) >>> assert getter(['a', 'b'], 'c')(test) == (test.a, test.b, None) >>> assert getter(['a', 'b'])(test) == (test.a, test.b) >>> >>> test = dict(a='a', b='b') >>> assert getter('a b')(test) == (test['a'], test['b']) >>> assert getter('a c')(test) == (test['a'], None) >>> dicts = getter('a c d', default={})(test) >>> assert dicts == (test['a'], {}, {}) >>> assert id(dicts[1]) != id(dicts[2]) >>> assert getter('a')(test) == test['a'] >>> assert getter('a b', 'c')(test) == (test['a'], test['b'], None) >>> assert getter(['a', 'b'], 'c')(test) == (test['a'], test['b'], None) >>> assert getter(['a', 'b'])(test) == (test['a'], test['b']) >>> >>> test = SimpleNamespace(a='a', b='b') >>> test1 = SimpleNamespace(d='d', test=test) >>> assert getter('d test.a test.a.c test.c test.m.j.k')(test1) == (test1.d, test1.test.a, None, None, None) >>> assert getter('a c')(test1) == (None, None) >>> dicts = getter('a c d test.a', 'test.b', default={})(test1) >>> assert dicts == ({}, {}, test1.d, test1.test.a, test1.test.b) >>> assert id(dicts[1]) != id(dicts[2]) >>> assert getter('a')(test1) is None >>> assert getter('test.b')(test1) == test1.test.b >>> assert getter(['a', 'test.b'], 'c')(test1) == (None, test1.test.b, None) >>> assert getter(['a', 'a.b.c'])(test1) == (None, None) >>> >>> test = dict(a='a', b='b') >>> test1_dict = dict(d='d', test=test) >>> assert getter('d test.a test.a.c test.c test.m.j.k')(test1_dict) == getter('d test.a test.a.c test.c test.m.j.k')(test1) >>> assert getter('d test.a test.a.c test.c test.m.j.k')(test1_dict) == (test1_dict['d'], test1_dict['test']['a'], None, None, None) >>> assert getter('a c')(test1_dict) == (None, None) >>> dicts = getter('a c d test.a', 'test.b', default={})(test1_dict) >>> assert dicts == ({}, {}, test1_dict['d'], test1_dict['test']['a'], test1_dict['test']['b']) >>> assert id(dicts[1]) != id(dicts[2]) >>> assert getter('a')(test1_dict) is None >>> assert getter('test.b')(test1_dict) == test1_dict['test']['b'] >>> assert getter(['a', 'test.b'], 'c')(test1_dict) == (None, test1_dict['test']['b'], None) >>> assert getter(['a', 'a.b.c'])(test1_dict) == (None, None) >>> >>> encode = dumps(test1_dict) >>> test1_dict_decode = loads(encode) >>> assert id(test1_dict) != id(test1_dict_decode) >>> test1_dict_copy = deepcopy(test1_dict) >>> assert id(test1_dict) != id(test1_dict_copy) >>> >>> assert getter('d test.a test.a.c test.c test.m.j.k')(test1_dict_decode) == (test1_dict_decode['d'], test1_dict_decode['test']['a'], None, None, None) >>> assert getter('a c')(test1_dict_decode) == (None, None) >>> dicts = getter('a c d test.a', 'test.b', default={})(test1_dict_decode) >>> assert dicts == ({}, {}, test1_dict_decode['d'], test1_dict['test']['a'], test1_dict_decode['test']['b']) >>> assert id(dicts[1]) != id(dicts[2]) >>> assert getter('a')(test1_dict_decode) is None >>> assert getter('test.b')(test1_dict_decode) == test1_dict_decode['test']['b'] >>> assert getter(['a', 'test.b'], 'c')(test1_dict_decode) == (None, test1_dict_decode['test']['b'], None) >>> assert getter(['a', 'a.b.c'])(test1_dict_decode) == (None, None)
- The call returns:
getter(‘name’)(r): r.name/r[‘name’].
getter(‘name’, ‘date’)(r): (r.name, r.date)/(r[‘name’], r[‘date’]).
getter(‘name.first’, ‘name.last’)(r):(r.name.first, r.name.last)/(r[‘name.first’], r[‘name.last’]).
Init.
- class nodeps.LetterCounter(start='A')[source]¶
Bases:
objectLetter Counter generator function. This way, each time you call next() on the generator.
It will yield the next counter value. We will also remove the maximum counter check
Examples
>>> from nodeps import LetterCounter >>> >>> c = LetterCounter("Z") >>> assert c.increment() == 'AA'
Init.
- Parameters:
start (str)
- class nodeps.NamedtupleMeta[source]¶
Bases:
objectNamedtuple Metaclass.
Examples
>>> import collections >>> from nodeps import NamedtupleMeta >>> >>> named = collections.namedtuple('named', 'a', defaults=('a', )) >>> >>> assert isinstance(named(), NamedtupleMeta) == True >>> assert isinstance(named(), tuple) == True >>> >>> assert issubclass(named, NamedtupleMeta) == True >>> assert issubclass(named, tuple) == True
- class nodeps.Noset(name='')[source]¶
Bases:
objectMarker object for globals not initialized or other objects.
Examples
>>> from nodeps import NOSET >>> >>> name = Noset.__name__.lower() >>> assert str(NOSET) == f'<{name}>' >>> assert repr(NOSET) == f'<{name}>' >>> assert repr(Noset("test")) == f'<test>'
Init.
- Parameters:
name (str)
- class nodeps.GitStatus(base='', dirty=False, diverge=False, local='', pull=False, push=False, remote='')[source]¶
Bases:
objectGit SHA and status.
- class nodeps.Bump(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
-
Bump class.
- MAJOR = 'MAJOR'¶
- MINOR = 'MINOR'¶
- PATCH = 'PATCH'¶
- class nodeps.ProjectRepos(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
-
Options to show repos in Project class.
- DICT = '1'¶
- INSTANCES = '2'¶
- NAMES = '3'¶
- PATHS = '4'¶
- PY = '5'¶
- class nodeps.Env(parsed=True)[source]¶
Bases:
objectEnviron Class.
See Also: Environment variables
- If you need to use a workflow run’s URL from within a job, you can combine these environment variables:
$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID- If you generate a value in one step of a job, you can use the value in subsequent
stepsof the same job by assigning the value to an existing or new environment variable and then writing this to the
GITHUB_ENVenvironment file, see Commands.- If you want to pass a value from a step in one job in a
workflowto a step in another job in the workflow, you can define the value as a job output, see Syntax.
- Parameters:
parsed (dataclasses.InitVar[bool])
- config: CONFIG = None¶
var = Env()._config(“VAR”, default=True, cast=bool).
- Type:
Searches for settings.ini and .env cwd up. Usage
- GITHUB_ACTION: str | None = None¶
The name of the action currently running, or the id of a step.
For example, for an action,
__repo-owner_name-of-action-repo.GitHub removes special characters, and uses the name
__runwhen the current step runs a script without an id.If you use the same script or action more than once in the same job, the name will include a suffix that consists of the sequence number preceded by an underscore.
For example, the first script you run will have the name
__run, and the second script will be named__run_2.Similarly, the second invocation of
actions/checkoutwill beactionscheckout2.
- GITHUB_ACTION_PATH: Path | str | None = None¶
The path where an action is located. This property is only supported in composite actions.
You can use this path to access files located in the same repository as the action.
For example,
/home/runner/work/_actions/repo-owner/name-of-action-repo/v1.
- GITHUB_ACTION_REPOSITORY: str | None = None¶
For a step executing an action, this is the owner and repository name of the action.
For example,
actions/checkout.
- GITHUB_ACTIONS: bool | str | None = None¶
Always set to
truewhen GitHub Actions is running the workflow.You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
- GITHUB_ACTOR: str | None = None¶
The name of the person or app that initiated the workflow.
For example,
octocat.
- GITHUB_BASE_REF: str | None = None¶
The name of the base ref or target branch of the pull request in a workflow run.
This is only set when the event that triggers a workflow run is either
pull_requestorpull_request_target.For example,
main.
- GITHUB_ENV: Path | str | None = None¶
The path on the runner to the file that sets environment variables from workflow commands.
This file is unique to the current step and changes for each step in a job.
For example,
/home/runner/work/_temp/_runner_file_commands/set_env_87406d6e-4979-4d42-98e1-3dab1f48b13a.For more information, see Workflow commands for GitHub Actions.
- GITHUB_EVENT_NAME: str | None = None¶
The name of the event that triggered the workflow.
For example,
workflow_dispatch.
- GITHUB_EVENT_PATH: Path | str | None = None¶
The path to the file on the runner that contains the full event webhook payload.
For example,
/github/workflow/event.json.
- GITHUB_GRAPHQL_URL: ParseResult | str | None = None¶
Returns the GraphQL API URL.
For example:
https://api.github.com/graphql.
- GITHUB_HEAD_REF: str | None = None¶
The head ref or source branch of the pull request in a workflow run.
This property is only set when the event that triggers a workflow run is either
pull_requestorpull_request_target.For example,
feature-branch-1.
- GITHUB_PATH: Path | str | None = None¶
The path on the runner to the file that sets system PATH variables from workflow commands. This file is unique to the current step and changes for each step in a job.
For example,
/home/runner/work/_temp/_runner_file_commands/add_path_899b9445-ad4a-400c-aa89-249f18632cf5.For more information, see Workflow commands for GitHub Actions.
- GITHUB_REF: str | None = None¶
The branch or tag ref that triggered the workflow run.
For branches this is the format
refs/heads/<branch_name>, for tags it isrefs/tags/<tag_name>, and for pull requests it isrefs/pull/<pr_number>/merge.This variable is only set if a branch or tag is available for the event type.
For example,
refs/heads/feature-branch-1.
- GITHUB_REF_NAME: str | None = None¶
The branch or tag name that triggered the workflow run.
For example,
feature-branch-1.
- GITHUB_REF_PROTECTED: bool | str | None = None¶
trueif branch protections are configured for the ref that triggered the workflow run.
- GITHUB_REF_TYPE: str | None = None¶
The type of ref that triggered the workflow run.
Valid values are
branchortag.For example,
branch.
- GITHUB_REPOSITORY: str | None = None¶
The owner and repository name.
For example,
octocat/Hello-World.
- GITHUB_RETENTION_DAYS: str | None = None¶
The number of days that workflow run logs and artifacts are kept.
For example,
90.
- GITHUB_RUN_ATTEMPT: str | None = None¶
A unique number for each attempt of a particular workflow run in a repository.
This number begins at
1for the workflow run’s first attempt, and increments with each re-run.For example,
3.
- GITHUB_RUN_ID: str | None = None¶
A unique number for each workflow run within a repository.
This number does not change if you re-run the workflow run.
For example,
1658821493.
- GITHUB_RUN_NUMBER: str | None = None¶
A unique number for each run of a particular workflow in a repository.
This number begins at
1for the workflow’s first run, and increments with each new run. This number does not change if you re-run the workflow run.For example,
3.
- GITHUB_SERVER_URL: ParseResult | str | None = None¶
The URL of the GitHub Enterprise Cloud server.
For example:
https://github.com.
- GITHUB_SHA: str | None = None¶
The commit SHA that triggered the workflow.
The value of this commit SHA depends on the event that triggered the workflow. For more information, see Events that trigger workflows.
For example,
ffac537e6cbbf934b08745a378932722df287a53.
- GITHUB_WORKFLOW: Path | str | None = None¶
The name of the workflow.
For example,
My test workflow.If the workflow file doesn’t specify a name, the value of this variable is the full path of the workflow file in the repository.
- GITHUB_WORKSPACE: Path | str | None = None¶
The default working directory on the runner for steps, and the default location of your repository when using the checkout action.
For example,
/home/runner/work/my-repo-name/my-repo-name.
- RUNNER_ARCH: str | None = None¶
The architecture of the runner executing the job.
Possible values are
X86,X64,ARM, orARM64.For example,
X86.
- RUNNER_NAME: str | None = None¶
The name of the runner executing the job.
For example,
Hosted Agent.
- RUNNER_OS: str | None = None¶
The operating system of the runner executing the job.
Possible values are
Linux,Windows, ormacOS.For example,
Linux.
- RUNNER_TEMP: Path | str | None = None¶
The path to a temporary directory on the runner.
This directory is emptied at the beginning and end of each job.
Note that files will not be removed if the runner’s user account does not have permission to delete them.
For example,
_temp.
- RUNNER_TOOL_CACHE: str | None = None¶
The path to the directory containing preinstalled tools for GitHub-hosted runners.
For more information, see About GitHub-hosted runners.
For example,
C:/hostedtoolcache/windows.
- classmethod as_int(key, value='')[source]¶
Parse as int if environment variable should be forced to be parsed as int checking if:.
has value,
key in
Env._parse_as_intorkey ends with one of the items in
Env._parse_as_int_suffix.
- static parse_as_bool(variable='USER')[source]¶
Parses variable from environment 1 and 0 as bool instead of int.
- Parses:
bool: 1, 0, True, False, yes, no, on, off (case insensitive)
int: integer only numeric characters but 1 and 0 or SUDO_UID or SUDO_GID
ipaddress: ipv4/ipv6 address
url: if “//” or “@” is found it will be parsed as url
path: start with / or ~ or .
others as string
- Parameters:
variable (str) – variable name to parse from environment (default: USER)
- Return type:
bool | Path | ParseResult | IPv4Address | IPv6Address | int | str | None
Examples
>>> from nodeps import Path, MACOS, LOCAL >>> from nodeps import Env >>> >>> if MACOS and LOCAL: ... assert isinstance(Env.parse_as_bool(), str) >>> >>> os.environ['FOO'] = '1' >>> assert Env.parse_as_bool("FOO") is True >>> >>> os.environ['FOO'] = '0' >>> assert Env.parse_as_bool("FOO") is False >>> >>> os.environ['FOO'] = 'TrUe' >>> assert Env.parse_as_bool("FOO") is True >>> >>> os.environ['FOO'] = 'OFF' >>> assert Env.parse_as_bool("FOO") is False >>> >>> os.environ['FOO'] = '~/foo' >>> assert Env.parse_as_bool("FOO") == Path('~/foo') >>> >>> os.environ['FOO'] = '/foo' >>> assert Env.parse_as_bool("FOO") == Path('/foo') >>> >>> os.environ['FOO'] = './foo' >>> assert Env.parse_as_bool("FOO") == Path('./foo') >>> >>> os.environ['FOO'] = './foo' >>> assert Env.parse_as_bool("FOO") == Path('./foo') >>> >>> v = "https://github.com" >>> os.environ['FOO'] = v >>> assert Env.parse_as_bool("FOO").geturl() == v >>> >>> v = "git@github.com" >>> os.environ['FOO'] = v >>> assert Env.parse_as_bool("FOO").geturl() == v >>> >>> v = "0.0.0.0" >>> os.environ['FOO'] = v >>> assert Env.parse_as_bool("FOO").exploded == v >>> >>> os.environ['FOO'] = "::1" >>> assert Env.parse_as_bool("FOO").exploded.endswith(":0001") >>> >>> v = "2" >>> os.environ['FOO'] = v >>> assert Env.parse_as_bool("FOO") == int(v) >>> >>> v = "2.0" >>> os.environ['FOO'] = v >>> assert Env.parse_as_bool("FOO") == v >>> >>> del os.environ['FOO'] >>> assert Env.parse_as_bool("FOO") is None
- classmethod parse_as_int(name='USER')[source]¶
Parses variable from environment using
mreleaser.parse_str(),.except
SUDO_UIDorSUDO_GIDwhich are parsed as int instead of bool.- Parameters:
name (str) – variable name to parse from environment (default: USER)
- Return type:
bool | Path | ParseResult | IPv4Address | IPv6Address | int | str | None
Examples
>>> from nodeps import Path, MACOS, LOCAL >>> from nodeps import Env >>> >>> if MACOS and LOCAL: ... assert isinstance(Env.parse_as_int(), str) >>> >>> os.environ['FOO'] = '1' >>> assert Env.parse_as_int("FOO") is True >>> >>> os.environ['FOO'] = '0' >>> assert Env.parse_as_int("FOO") is False >>> >>> os.environ['FOO'] = 'TrUe' >>> assert Env.parse_as_int("FOO") is True >>> >>> os.environ['FOO'] = 'OFF' >>> assert Env.parse_as_int("FOO") is False >>> >>> os.environ['FOO'] = '~/foo' >>> assert Env.parse_as_int("FOO") == Path('~/foo') >>> >>> os.environ['FOO'] = '/foo' >>> assert Env.parse_as_int("FOO") == Path('/foo') >>> >>> os.environ['FOO'] = './foo' >>> assert Env.parse_as_int("FOO") == Path('./foo') >>> >>> os.environ['FOO'] = './foo' >>> assert Env.parse_as_int("FOO") == Path('./foo') >>> >>> v = "https://github.com" >>> os.environ['FOO'] = v >>> assert Env.parse_as_int("FOO").geturl() == v >>> >>> v = "git@github.com" >>> os.environ['FOO'] = v >>> assert Env.parse_as_int("FOO").geturl() == v >>> >>> v = "0.0.0.0" >>> os.environ['FOO'] = v >>> assert Env.parse_as_int("FOO").exploded == v >>> >>> os.environ['FOO'] = "::1" >>> assert Env.parse_as_int("FOO").exploded.endswith(":0001") >>> >>> v = "2" >>> os.environ['FOO'] = v >>> assert Env.parse_as_int("FOO") == int(v) >>> >>> v = "2.0" >>> os.environ['FOO'] = v >>> assert Env.parse_as_int("FOO") == v >>> >>> del os.environ['FOO'] >>> assert Env.parse_as_int("FOO") is None >>> >>> if not os.environ.get("CI"): ... assert isinstance(Env.parse_as_int("PATH"), str)
- class nodeps.EnvBuilder(system_site_packages=False, clear=False, symlinks=True, upgrade=True, with_pip=True, prompt='.', upgrade_deps=True, env_dir='venv')[source]¶
Bases:
EnvBuilderWrapper for
venv.EnvBuilder.Changed defaults for: prompt` symlinks and with_pip, adds env_dir to __init__ arguments.
Post install in
post_setup().This class exists to allow virtual environment creation to be customized. The constructor parameters determine the builder’s behaviour when called upon to create a virtual environment.
By default, the builder makes the system (global) site-packages dir *un*available to the created environment.
If invoked using the Python -m option, the default is to use copying on Windows platforms but symlinks elsewhere. If instantiated some other way, the default is to not use symlinks (changed with the wrapper to use symlinks always).
- Parameters:
- system_site_packages¶
bool If True, the system (global) site-packages dir is available to created environments.
- Type:
- clear¶
bool If True, delete the contents of the environment directory if it already exists, before environment creation.
- Type:
- symlinks¶
bool If True, attempt to symlink rather than copy files into virtual environment.
- Type:
- context¶
Simplenamespace The information for the environment creation request being processed.
- Type:
types.SimpleNamespace | None
- context: types.SimpleNamespace | None = None¶
- ensure_directories(env_dir=None)[source]¶
Create the directories for the environment.
- post_setup(context=None)[source]¶
Hook for post-setup modification of the venv.
Subclasses may install additional packages or scripts here, add activation shell scripts, etc.
- Parameters:
context (types.SimpleNamespace | None) – The information for the environment creation request being processed.
- Return type:
None
- nodeps.parse_str(data=None)[source]¶
Parses str or data.__str__().
- Parses:
bool: 1, 0, True, False, yes, no, on, off (case insensitive)
int: integer only numeric characters but 1 and 0
ipaddress: ipv4/ipv6 address
url: if “://” or “@” is found it will be parsed as url
path: if “.” or start with “/” or “~” or “.” and does contain “:”
others as string
- Parameters:
data (Any | None) – variable name to parse from environment (default: USER)
- Return type:
bool | GitUrl | Path | ParseResult | IPv4Address | IPv6Address | int | str | None
Examples
>>> import os >>> from nodeps import Path >>> from nodeps import parse_str >>> >>> assert parse_str() is None >>> >>> assert parse_str("1") is True >>> assert parse_str("0") is False >>> assert parse_str("TrUe") is True >>> assert parse_str("OFF") is False >>> >>> u = "https://github.com/user/repo" >>> assert parse_str(u).url == u >>> u = "git@github.com:user/repo" >>> assert parse_str(u).url == u >>> u = "https://github.com" >>> assert parse_str(u).geturl() == u >>> u = "git@github.com" >>> assert parse_str(u).geturl() == u >>> >>> assert parse_str("~/foo") == Path('~/foo') >>> assert parse_str("/foo") == Path('/foo') >>> assert parse_str("./foo") == Path('foo') >>> assert parse_str(".") == Path('.') >>> assert parse_str(Path()) == Path() >>> >>> assert parse_str("0.0.0.0").exploded == "0.0.0.0" >>> assert parse_str("::1").exploded.endswith(":0001") >>> >>> assert parse_str("2") == 2 >>> assert parse_str("2.0") == "2.0" >>> assert parse_str("/usr/share/man:") == "/usr/share/man:" >>> if not os.environ.get("CI"): ... assert isinstance(parse_str(os.environ.get("PATH")), str)
- exception nodeps.CalledProcessError(returncode=None, cmd=None, output=None, stderr=None, completed=None)[source]¶
Bases:
SubprocessErrorPatched
subprocess.CalledProcessError.Raised when run() and the process returns a non-zero exit status.
- cmd¶
The command that was run.
- Type:
AnyStr | os.PathLike[str] | os.PathLike[bytes] | collections.abc.Sequence[AnyStr | os.PathLike[str] | os.PathLike[bytes]]
- output¶
The output of the process.
- Type:
AnyStr | None
- stderr¶
The error output of the process.
- Type:
AnyStr | None
- completed¶
subprocess.CompletedProcessobject.- Type:
subprocess.CompletedProcess | None
Patched
subprocess.CalledProcessError.- Parameters:
cmd (AnyStr | PathLike[str] | PathLike[bytes] | Sequence[AnyStr | PathLike[str] | PathLike[bytes]]) – The command that was run.
returncode (int) – The exit code of the process.
output (AnyStr | None) – The output of the process.
stderr (AnyStr | None) – The error output of the process.
completed (CompletedProcess | None) –
subprocess.CompletedProcessobject.
- Return type:
None
Examples
>>> import subprocess >>> 3/0 Traceback (most recent call last): ZeroDivisionError: division by zero >>> subprocess.run(["ls", "foo"], capture_output=True, check=True) Traceback (most recent call last): errors.CalledProcessError: Return Code: 1 Command: ['ls', 'foo'] Stderr: b'ls: foo: No such file or directory\n' Stdout: b''
- completed: CompletedProcess | None¶
- exception nodeps.CmdError(process=None)[source]¶
Bases:
CalledProcessErrorRaised when run() and the process returns a non-zero exit status.
- Attribute:
process: The CompletedProcess object returned by run().
Init.
- Parameters:
process (CompletedProcess | None)
- Return type:
None
- exception nodeps.CommandNotFoundError[source]¶
Bases:
_NoDepsBaseErrorRaised when command is not found.
- exception nodeps.InvalidArgumentError[source]¶
Bases:
_NoDepsBaseErrorRaised when function is called with invalid argument.
- async nodeps.aiocmd(*args, **kwargs)[source]¶
Async Exec Command.
Examples
>>> import asyncio >>> from tempfile import TemporaryDirectory >>> from nodeps import Path, aiocmd >>> with TemporaryDirectory() as tmp: ... tmp = Path(tmp) ... rv = asyncio.run(aiocmd("git", "clone", "https://github.com/octocat/Hello-World.git", cwd=tmp)) ... assert rv.returncode == 0 ... assert (tmp / "Hello-World" / "README").exists()
- Parameters:
*args – command and args
**kwargs – subprocess.run kwargs
- Raises:
JetBrainsError –
- Returns:
None
- Return type:
- async nodeps.aiodmg(src, dest)[source]¶
Async Open dmg file and copy the app to dest.
Examples
>>> from nodeps import aiodmg >>> async def test(): ... await aiodmg("/tmp/JetBrains.dmg", "/tmp/JetBrains")
- async nodeps.aiogz(src, dest='.')[source]¶
Async ncompress .gz src to dest (default: current directory).
It will be uncompressed to the same directory name as src basename. Uncompressed directory will be under dest directory.
Examples
>>> import os >>> import tempfile >>> from nodeps import Path, aiogz, tardir >>> >>> cwd = Path.cwd() >>> with tempfile.TemporaryDirectory() as workdir: ... os.chdir(workdir) ... with tempfile.TemporaryDirectory() as compress: ... file = Path(compress) / "test.txt" ... _ = file.touch() ... compressed = tardir(compress) ... with tempfile.TemporaryDirectory() as uncompress: ... uncompressed = asyncio.run(aiogz(compressed, uncompress)) ... assert uncompressed.is_dir() ... assert Path(uncompressed).joinpath(file.name).exists() >>> os.chdir(cwd)
- Parameters:
- Returns:
Absolute Path of the Uncompressed Directory
- Return type:
- nodeps.allin(origin, destination)[source]¶
Checks all items in origin are in destination iterable.
Examples
>>> from nodeps import allin >>> from nodeps.variables.builtin import BUILTIN_CLASS >>> >>> class Int(int): ... pass >>> allin(tuple.__mro__, BUILTIN_CLASS) True >>> allin(Int.__mro__, BUILTIN_CLASS) False >>> allin('tuple int', 'bool dict int') False >>> allin('bool int', ['bool', 'dict', 'int']) True >>> allin(['bool', 'int'], ['bool', 'dict', 'int']) True
- nodeps.ami(user='root')[source]¶
Check if Current User is User in Argument (default: root).
Examples
>>> from nodeps import ami >>> from nodeps import USER >>> from nodeps import LOCAL >>> from nodeps import DOCKER >>> from nodeps import MACOS >>> >>> assert ami(USER) is True >>> if LOCAL and MACOS: ... assert ami() is False >>> if DOCKER: ... assert ami() is True
- nodeps.anyin(origin, destination)[source]¶
Checks any item in origin are in destination iterable and return the first found.
Examples
>>> from nodeps import anyin >>> from nodeps.variables.builtin import BUILTIN_CLASS >>> >>> class Int(int): ... pass >>> anyin(tuple.__mro__, BUILTIN_CLASS) <class 'tuple'> >>> assert anyin('tuple int', BUILTIN_CLASS) is None >>> anyin('tuple int', 'bool dict int') 'int' >>> anyin('tuple int', ['bool', 'dict', 'int']) 'int' >>> anyin(['tuple', 'int'], ['bool', 'dict', 'int']) 'int'
- nodeps.chdir(data=True)[source]¶
Change directory and come back to previous directory.
Examples
>>> from nodeps import Path >>> from nodeps import chdir >>> from nodeps import MACOS >>> >>> previous = Path.cwd() >>> new = Path('/usr') >>> with chdir(new) as (pr, ne): ... assert previous == pr ... assert new == ne ... assert ne == Path.cwd() >>> >>> if MACOS: ... new = Path('/bin/ls') ... with chdir(new) as (pr, ne): ... assert previous == pr ... assert new.parent == ne ... assert ne == Path.cwd() >>> >>> if MACOS: ... new = Path('/bin/foo') ... with chdir(new) as (pr, ne): ... assert previous == pr ... assert new.parent == ne ... assert ne == Path.cwd() >>> >>> with chdir() as (pr, ne): ... assert previous == pr ... if MACOS: ... assert "var" in str(ne) ... assert ne == Path.cwd()
- nodeps.cmd(*args, **kwargs)[source]¶
Exec Command.
Examples
>>> import tempfile >>> from nodeps import Path, cmd >>> >>> with tempfile.TemporaryDirectory() as tmp: ... rv = cmd("git", "clone", "https://github.com/octocat/Hello-World.git", tmp) ... assert rv.returncode == 0 ... assert (Path(tmp) / "README").exists()
- Parameters:
*args – command and args
**kwargs – subprocess.run kwargs
- Raises:
CmdError –
- Returns:
None
- Return type:
- nodeps.cmdrun(data, exc=False, lines=True, shell=True, py=False, pysite=True)[source]¶
Runs a cmd.
Examples
>>> from nodeps import CI >>> from nodeps import cmdrun >>> from nodeps import in_tox >>> >>> cmdrun('ls a') CompletedProcess(args='ls a', returncode=..., stdout=[], stderr=[...]) >>> cmdrun('ls a', shell=False, lines=False) CompletedProcess(args=['ls', 'a'], returncode=..., stdout='', stderr=...) >>> cmdrun('echo a', lines=False) # Extra '\' added to avoid docstring error. CompletedProcess(args='echo a', returncode=0, stdout='a\n', stderr='') >>> assert "venv" not in cmdrun("sysconfig", py=True, lines=False).stdout >>> if os.environ.get("VIRTUAL_ENV"): ... assert "venv" in cmdrun("sysconfig", py=True, pysite=False, lines=False).stdout
- Parameters:
data (Iterable) – command.
exc (bool) – raise exception.
lines (bool) – split lines so
\\nis removed from all lines (extra ‘' added to avoid docstring error).py (bool) – runs with python executable.
shell (bool) – expands shell variables and one line (shell True expands variables in shell).
pysite (bool) – run on site python if running on a VENV.
- Returns:
Completed process output.
- Return type:
- Raises:
CmdError –
- nodeps.cmdsudo(*args, user='root', **kwargs)[source]¶
Run Program with sudo if user is different that the current user.
- Parameters:
*args – command and args to run
user (str) – run as user (Default: False)
**kwargs – subprocess.run kwargs
- Returns:
CompletedProcess if the current user is not the same as user, None otherwise
- Return type:
CompletedProcess | None
- nodeps.command(*args, **kwargs)[source]¶
Exec Command with the following defaults compared to
subprocess.run().capture_output=True
text=True
check=True
Examples
>>> from nodeps import Path >>> import tempfile >>> with tempfile.TemporaryDirectory() as tmp: ... rv = command("git", "clone", "https://github.com/octocat/Hello-World.git", tmp) ... assert rv.returncode == 0 ... assert (Path(tmp) / ".git").exists()
- Parameters:
*args – command and args
**kwargs – subprocess.run kwargs
- Raises:
CmdError –
- Returns:
None
- Return type:
- nodeps.dict_sort(data, ordered=False, reverse=False)[source]¶
Order a dict based on keys.
Examples
>>> import platform >>> from collections import OrderedDict >>> from nodeps import dict_sort >>> >>> d = {"b": 2, "a": 1, "c": 3} >>> dict_sort(d) {'a': 1, 'b': 2, 'c': 3} >>> dict_sort(d, reverse=True) {'c': 3, 'b': 2, 'a': 1} >>> v = platform.python_version() >>> if "rc" not in v: ... # noinspection PyTypeHints ... assert dict_sort(d, ordered=True) == OrderedDict([('a', 1), ('b', 2), ('c', 3)])
- Parameters:
- Returns:
Dict sorted
- Return type:
Union[dict, collections.OrderedDict]
- nodeps.dmg(src, dest)[source]¶
Open dmg file and copy the app to dest.
Examples
>>> from nodeps import dmg >>> dmg("/tmp/JetBrains.dmg", "/tmp/JetBrains")
- nodeps.effect(apply, *args)[source]¶
Perform function on iterable.
Examples
>>> from types import SimpleNamespace >>> from nodeps import effect >>> simple = SimpleNamespace() >>> effect(lambda x: simple.__setattr__(x, dict()), 'a b', 'c') >>> assert simple.a == {} >>> assert simple.b == {} >>> assert simple.c == {}
- nodeps.elementadd(name, closing=False)[source]¶
Converts to HTML element.
Examples
>>> from nodeps import elementadd >>> >>> assert elementadd('light-black') == '<light-black>' >>> assert elementadd('light-black', closing=True) == '</light-black>' >>> assert elementadd(('green', 'bold',)) == '<green><bold>' >>> assert elementadd(('green', 'bold',), closing=True) == '</green></bold>'
- nodeps.elevate()[source]¶
Other https://github.com/netinvent/command_runner/blob/master/command_runner/elevate.py.
- nodeps.envsh(path='.env', missing_ok=False, override=True)[source]¶
Source
pathor ``path``relative to cwd upwards and return the resulting environment as a dictionary.- Parameters:
- Raises:
FileNotFoundError. –
- Returns:
Dict with updated values
- Return type:
- nodeps.exec_module_from_file(file, name=None)[source]¶
Executes module from file location.
Examples
>>> import nodeps >>> from nodeps import exec_module_from_file >>> m = exec_module_from_file(nodeps.__file__) >>> assert m.__name__ == nodeps.__name__
- Parameters:
- Returns:
Module instance
- Return type:
- nodeps.filterm(d, k=<function <lambda>>, v=<function <lambda>>)[source]¶
Filter Mutable Mapping.
Examples
>>> from nodeps import filterm >>> >>> assert filterm({'d':1}) == {'d': 1} >>> # noinspection PyUnresolvedReferences >>> assert filterm({'d':1}, lambda x: x.startswith('_')) == {} >>> # noinspection PyUnresolvedReferences >>> assert filterm({'d': 1, '_a': 2}, lambda x: x.startswith('_'), lambda x: isinstance(x, int)) == {'_a': 2}
- Returns:
Filtered dict with
- Parameters:
d (MutableMapping[_KT, _VT])
- Return type:
MutableMapping[_KT, _VT]
- nodeps.findfile(pattern, path=None)[source]¶
Find file with pattern.
Examples
>>> from pathlib import Path >>> import nodeps >>> from nodeps import findfile, MACOS, LOCAL >>> >>> if MACOS and LOCAL: ... assert Path(nodeps.__file__) in findfile("*.py")
- nodeps.findup(path=None, kind='is_file', name='.env', uppermost=False)[source]¶
Find up if name exists or is file or directory.
Examples
>>> import email >>> import email.mime >>> import nodeps >>> from nodeps import chdir, findup, parent, Path, Env >>> >>> >>> file = Path(email.mime.__file__) >>> >>> env = Env() >>> input_file = env.GITHUB_WORKSPACE if env.GITHUB_WORKSPACE else nodeps.__file__ >>> with chdir(parent(input_file)): ... pyproject_toml = findup(input_file, name="pyproject.toml") ... assert pyproject_toml.is_file() >>> >>> with chdir(parent(email.mime.__file__)): ... email_mime_py = findup(name="__init__.py") ... assert email_mime_py.is_file() ... assert email_mime_py == Path(email.mime.__file__) ... email_py = findup(name="__init__.py", uppermost=True) ... assert email_py.is_file() ... assert email_py == Path(email.__file__) >>> >>> assert findup(file, kind="exists", name="__init__.py") == file.parent / "__init__.py" >>> assert findup(file, name="__init__.py") == file.parent / "__init__.py" >>> assert findup(file, name="__init__.py", uppermost=True) == file.parent.parent / "__init__.py"
- Parameters:
- Returns:
Path if found.
- Return type:
Path | None
- nodeps.firstfound(data, apply)[source]¶
Returns first value in data if apply is True.
Examples
>>> from nodeps import firstfound >>> >>> assert firstfound([1, 2, 3], lambda x: x == 2) == 2 >>> assert firstfound([1, 2, 3], lambda x: x == 4) is None
- nodeps.flatten(data, recurse=False, unique=False, sort=True)[source]¶
Flattens an Iterable.
Examples
>>> from nodeps import flatten >>> >>> assert flatten([1, 2, 3, [1, 5, 7, [2, 4, 1, ], 7, 6, ]]) == [1, 2, 3, 1, 5, 7, [2, 4, 1], 7, 6] >>> assert flatten([1, 2, 3, [1, 5, 7, [2, 4, 1, ], 7, 6, ]], recurse=True) == [1, 1, 1, 2, 2, 3, 4, 5, 6, 7, 7] >>> assert flatten((1, 2, 3, [1, 5, 7, [2, 4, 1, ], 7, 6, ]), unique=True) == (1, 2, 3, 4, 5, 6, 7)
- nodeps.framesimple(data)[source]¶
Returns
nodeps.FrameSimple.Examples
>>> import inspect >>> from nodeps import Path >>> from nodeps import framesimple >>> >>> frameinfo = inspect.stack()[0] >>> finfo = framesimple(frameinfo) >>> ftype = framesimple(frameinfo.frame) >>> assert frameinfo.frame.f_code == finfo.code >>> assert frameinfo.frame == finfo.frame >>> assert frameinfo.filename == str(finfo.path) >>> assert frameinfo.lineno == finfo.lineno
- Returns:
- Parameters:
data (FrameInfo | FrameType | TracebackType)
- Return type:
FrameSimple | None
- nodeps.from_latin9(*args)[source]¶
Converts string from latin9 hex.
Examples
>>> from nodeps import from_latin9 >>> >>> from_latin9("f1") 'ñ' >>> >>> from_latin9("4a6f73e920416e746f6e696f205075e972746f6c6173204d6f6e7461f1e973") 'José Antonio Puértolas Montañés' >>> >>> from_latin9("f1", "6f") 'ño'
- Parameters:
args – strings to convert to latin9
- Returns:
str
- Return type:
- nodeps.fromiter(data, *args)[source]¶
Gets attributes from Iterable of objects and returns dict with.
Examples
>>> from types import SimpleNamespace as Simple >>> from nodeps import fromiter >>> >>> assert fromiter([Simple(a=1), Simple(b=1), Simple(a=2)], 'a', 'b', 'c') == {'a': [1, 2], 'b': [1]} >>> assert fromiter([Simple(a=1), Simple(b=1), Simple(a=2)], ('a', 'b', ), 'c') == {'a': [1, 2], 'b': [1]} >>> assert fromiter([Simple(a=1), Simple(b=1), Simple(a=2)], 'a b c') == {'a': [1, 2], 'b': [1]}
- Parameters:
data – object.
*args – attributes.
- Returns:
Tuple
- nodeps.getpths()[source]¶
Get list of pths under
sitedir.Examples
>>> from nodeps import getpths >>> >>> pths = getpths() >>> assert "distutils-precedence" in pths
- nodeps.getsitedir(index=2)[source]¶
Get site directory from stack if imported by
sitein a.pthfile orsysconfig.Examples
>>> from nodeps import getsitedir >>> assert "packages" in str(getsitedir())
- nodeps.group_user(data='root')[source]¶
Group and User for Name (id if name is str and vice versa).
Examples
>>> import os >>> import pathlib >>> >>> from nodeps import group_user >>> from nodeps import PW_USER, PW_ROOT, MACOS, LOCAL >>> >>> stat = pathlib.Path().stat() >>> gu = group_user() >>> assert gu.group.id == stat.st_gid and gu.user.id == stat.st_uid >>> gu = group_user(data=PW_USER.pw_uid) >>> actual_gname = gu.group.name >>> assert gu.user.name == PW_USER.pw_name >>> gu = group_user('root') >>> if MACOS and LOCAL: ... assert gu.group.id != stat.st_gid and gu.user.id == 0 >>> gu = group_user(data=0) >>> if MACOS and LOCAL: ... assert gu.group.name != actual_gname and gu.user.name == 'root'
- nodeps.gz(src, dest='.')[source]¶
Uncompress .gz src to dest (default: current directory).
It will be uncompressed to the same directory name as src basename. Uncompressed directory will be under dest directory.
Examples
>>> import os >>> import tempfile >>> from nodeps import Path, gz, tardir >>> cwd = Path.cwd() >>> with tempfile.TemporaryDirectory() as workdir: ... os.chdir(workdir) ... with tempfile.TemporaryDirectory() as compress: ... file = Path(compress) / "test.txt" ... _ = file.touch() ... compressed = tardir(compress) ... with tempfile.TemporaryDirectory() as uncompress: ... uncompressed = gz(compressed, uncompress) ... assert uncompressed.is_dir() ... assert Path(uncompressed).joinpath(file.name).exists() >>> os.chdir(cwd)
- Parameters:
- Returns:
Absolute Path of the Uncompressed Directory
- Return type:
- nodeps.indict(data, items=None, **kwargs)[source]¶
All item/kwargs pairs in flat dict.
Examples
>>> from nodeps import indict >>> from nodeps.variables.builtin import BUILTIN >>> >>> assert indict(BUILTIN, {'iter': iter}, credits=credits) is True >>> assert indict(BUILTIN, {'iter': 'fake'}) is False >>> assert indict(BUILTIN, {'iter': iter}, credits='fake') is False >>> assert indict(BUILTIN, credits='fake') is False
- Parameters:
data (MutableMapping) – dict to search.
items (MutableMapping | None) – key/value pairs.
**kwargs (Any) – key/value pairs.
- Returns:
True if all pairs in dict.
- Return type:
- nodeps.map_with_args(data, func, /, *args, pred=<function <lambda>>, split=' ', **kwargs)[source]¶
Apply pred/filter to data and map with args and kwargs.
Examples
>>> from nodeps import map_with_args >>> >>> # noinspection PyUnresolvedReferences >>> def f(i, *ar, **kw): ... return f'{i}: {[a(i) for a in ar]}, {", ".join([f"{k}: {v(i)}" for k, v in kw.items()])}' >>> map_with_args('0.1.2', f, int, list, pred=lambda x: x != '0', split='.', int=int, str=str) ["1: [1, ['1']], int: 1, str: 1", "2: [2, ['2']], int: 2, str: 2"]
- nodeps.mip()[source]¶
My Public IP.
Examples
>>> from nodeps import mip >>> >>> mip() '...............'
- Return type:
str | None
- nodeps.noexc(func, *args, default_=None, exc_=<class 'Exception'>, **kwargs)[source]¶
Execute function suppressing exceptions.
Examples
>>> from nodeps import noexc >>> assert noexc(dict(a=1).pop, 'b', default_=2, exc_=KeyError) == 2
- Parameters:
func (Callable[..., _T]) – callable.
*args (Any) – args.
default – default value if exception is raised.
exc – exception or exceptions.
**kwargs (Any) – kwargs.
default_ (Any)
exc_ (ExcType)
**kwargs
- Returns:
Function return.
- Return type:
Any
- nodeps.parent(path='/home/docs/checkouts/readthedocs.org/user_builds/nodeps/envs/latest/lib/python3.11/site-packages/nodeps/modules/functions.py', none=True)[source]¶
Parent if File or None if it does not exist.
Examples
>>> from nodeps import parent >>> >>> parent("/bin/ls") Path('/bin') >>> parent("/bin") Path('/bin') >>> parent("/bin/foo", none=False) Path('/bin') >>> parent("/bin/foo")
- nodeps.returncode(c, shell=True)[source]¶
Runs command in shell and returns returncode showing stdout and stderr.
No exception is raised
Examples
>>> from nodeps import returncode >>> >>> assert returncode("ls /bin/ls") == 0 >>> assert returncode("ls foo") in [1, 2]
- nodeps.sourcepath(data)[source]¶
Get path of object.
Examples
>>> import asyncio >>> from nodeps import Path >>> from nodeps import sourcepath >>> >>> finfo = inspect.stack()[0] >>> globs_locs = (finfo.frame.f_globals | finfo.frame.f_locals).copy() >>> assert sourcepath(sourcepath) == Path(__file__) >>> assert sourcepath(asyncio.__file__) == Path(asyncio.__file__) >>> assert sourcepath(dict(a=1)) == Path("{'a': 1}")
- nodeps.split_pairs(text)[source]¶
Split text in pairs for even length.
Examples
>>> from nodeps import split_pairs >>> >>> split_pairs("123456") [('1', '2'), ('3', '4'), ('5', '6')]
- Parameters:
text – text to split in pairs
- Returns:
text
- nodeps.stdout(shell, keepends=False, split=False, cwd=None)[source]¶
Return stdout of executing cmd in a shell or None if error.
Execute the string ‘cmd’ in a shell with ‘subprocess.getstatusoutput’ and return a stdout if success. The locale encoding is used to decode the output and process newlines.
A trailing newline is stripped from the output.
Examples
>>> from nodeps import stdout >>> >>> stdout("ls /bin/ls") '/bin/ls' >>> stdout("true") '' >>> stdout("ls foo") >>> stdout("ls /bin/ls", split=True) ['/bin/ls']
- Parameters:
- Returns:
Stdout or None if error.
- Return type:
- nodeps.stdquiet()[source]¶
Redirect stdout/stderr to StringIO objects to prevent console output from distutils commands.
- nodeps.suppress(func, *args, exception=<class 'Exception'>, **kwargs)[source]¶
Try and supress exception.
- Parameters:
func (Callable[P, T]) – function to call
*args (P.args) – args to pass to func
exception (ExcType | None) – exception to suppress (default: Exception)
**kwargs (P.kwargs) – kwargs to pass to func
- Returns:
result of func
- Return type:
T
- nodeps.syssudo(user='root')[source]¶
Rerun Program with sudo
sys.executableandsys.argvif user is different that the current user.- Parameters:
user (str) – run as user (Default: False)
- Returns:
CompletedProcess if the current user is not the same as user, None otherwise
- Return type:
CompletedProcess | None
- nodeps.tardir(src)[source]¶
Compress directory src to <basename src>.tar.gz in cwd.
Examples
>>> import os >>> import tempfile >>> from nodeps import Path, tardir >>> cwd = Path.cwd() >>> with tempfile.TemporaryDirectory() as workdir: ... os.chdir(workdir) ... with tempfile.TemporaryDirectory() as compress: ... file = Path(compress) / "test.txt" ... _ = file.touch() ... compressed = tardir(compress) ... with tempfile.TemporaryDirectory() as uncompress: ... uncompressed = gz(compressed, uncompress) ... assert uncompressed.is_dir() ... assert Path(uncompressed).joinpath(file.name).exists() >>> os.chdir(cwd)
- Parameters:
src (Path | AnyStr | PathLike[str] | PathLike[bytes] | IO) – directory to compress
- Raises:
FileNotFoundError – No such file or directory
ValueError – Can’t compress current working directory
- Returns:
Compressed Absolute File Path
- Return type:
- nodeps.tilde(path='.')[source]¶
Replaces $HOME with ~.
Examples
>>> from nodeps import Path, tilde >>> assert tilde(f"{Path.home()}/file") == f"~/file"
- nodeps.to_camel(text, replace=True)[source]¶
Convert to Camel.
Examples
>>> to_camel("__ignore_attr__") 'IgnoreAttr' >>> to_camel("__ignore_attr__", replace=False) '__Ignore_Attr__'
- nodeps.to_latin9(chars)[source]¶
Converts string to latin9 hex.
Examples
>>> from nodeps import AUTHOR >>> from nodeps import to_latin9 >>> >>> to_latin9("ñ") 'f1' >>> >>> to_latin9(AUTHOR) '4a6f73e920416e746f6e696f205075e972746f6c6173204d6f6e7461f1e973'
- nodeps.tomodules(obj, suffix=True)[source]¶
Converts Iterable to A.B.C.
Examples
>>> from nodeps import tomodules >>> assert tomodules('a b c') == 'a.b.c' >>> assert tomodules('a b c.py') == 'a.b.c' >>> assert tomodules('a/b/c.py') == 'a.b.c' >>> assert tomodules(['a', 'b', 'c.py']) == 'a.b.c' >>> assert tomodules('a/b/c.py', suffix=False) == 'a.b.c.py' >>> assert tomodules(['a', 'b', 'c.py'], suffix=False) == 'a.b.c.py'
- nodeps.urljson(data, rm=False)[source]¶
Url open json.
Examples
>>> import os >>> from nodeps import urljson >>> from nodeps import GIT >>> from nodeps import GITHUB_TOKEN >>> from nodeps import NODEPS_PROJECT_NAME >>> >>> if os.environ.get('GITHUB_TOKEN'): ... github = urljson(f"https://api.github.com/repos/{GIT}/{NODEPS_PROJECT_NAME}") ... assert github['name'] == NODEPS_PROJECT_NAME >>> >>> pypi = urljson(f"https://pypi.org/pypi/{NODEPS_PROJECT_NAME}/json") >>> assert pypi['info']['name'] == NODEPS_PROJECT_NAME
- nodeps.varname(index=2, lower=True, prefix=None, sep='_')[source]¶
Caller var name.
Examples
>>> from dataclasses import dataclass >>> from nodeps import varname >>> >>> def function() -> str: ... return varname() >>> >>> class ClassTest: ... def __init__(self): ... self.name = varname() ... ... @property ... def prop(self): ... return varname() ... ... # noinspection PyMethodMayBeStatic ... def method(self): ... return varname() >>> >>> @dataclass ... class DataClassTest: ... def __post_init__(self): ... self.name = varname() >>> >>> name = varname(1) >>> Function = function() >>> classtest = ClassTest() >>> method = classtest.method() >>> prop = classtest.prop >>> dataclasstest = DataClassTest() >>> >>> def test_var(): ... assert name == 'name' >>> >>> def test_function(): ... assert Function == function.__name__.lower() >>> >>> def test_class(): ... assert classtest.name == ClassTest.__name__.lower() >>> >>> def test_method(): ... assert classtest.method() == ClassTest.__name__.lower() ... assert method == 'method' >>> def test_property(): ... assert classtest.prop == ClassTest.__name__.lower() ... assert prop == 'prop' >>> def test_dataclass(): ... assert dataclasstest.name == DataClassTest.__name__.lower()
class A: def __init__(self): self.instance = varname() a = A() var = varname(1)
- Parameters:
index – index.
lower – lower.
prefix – prefix to add.
sep – split.
- Returns:
Var name.
- Return type:
Optional[str]
- nodeps.which(data='sudo', raises=False)[source]¶
Checks if cmd or path is executable.
Examples
>>> from nodeps import which >>> if which(): ... assert "sudo" in which() >>> assert which('/bin/ls') == '/bin/ls' >>> which("foo", raises=True) Traceback (most recent call last): nodeps.modules.errors.CommandNotFoundError: foo
- Attribute:
data: command or path. raises: raise exception if command not found
- nodeps.yield_if(data, pred=<function <lambda>>, split=' ', apply=None)[source]¶
Yield value if condition is met and apply function if predicate.
Examples
>>> from nodeps import yield_if >>> >>> assert list(yield_if([True, None])) == [True] >>> assert list(yield_if('test1.test2', pred=lambda x: x.endswith('2'), split='.')) == ['test2'] >>> assert list(yield_if('test1.test2', pred=lambda x: x.endswith('2'), split='.', apply=lambda x: x.removeprefix('test'))) == ['2'] >>> assert list(yield_if('test1.test2', pred=lambda x: x.endswith('2'), split='.', apply=(lambda x: x.removeprefix('test'), lambda x: int(x)))) == [2]
- nodeps.yield_last(data, split=' ')[source]¶
Yield value if condition is met and apply function if predicate.
Examples
>>> from nodeps import yield_last >>> >>> assert list(yield_last([True, None])) == [(False, True, None), (True, None, None)] >>> assert list(yield_last('first last')) == [(False, 'first', None), (True, 'last', None)] >>> assert list(yield_last('first.last', split='.')) == [(False, 'first', None), (True, 'last', None)] >>> assert list(yield_last(dict(first=1, last=2))) == [(False, 'first', 1), (True, 'last', 2)]
- class nodeps.GitUrl(data='', repo='')[source]¶
Bases:
objectParsed Git URL Helper Class.
- Parameters:
data (dataclasses.InitVar[str | nodeps.modules.path.Path | nodeps.modules.gh._SupportsWorkingDir | None])
repo (str)
- data¶
Url, path or user (to be used with name), default None for cwd. Does not have .git unless is git+file
- Type:
dataclasses.InitVar[str | nodeps.modules.path.Path | nodeps.modules.gh._SupportsWorkingDir | None]
Examples
>>> import nodeps >>> from nodeps import GitUrl >>> from nodeps import Path >>> from nodeps import NODEPS_PROJECT_NAME, CI >>> from nodeps import NODEPS_MODULE_PATH >>> >>> p = GitUrl() >>> p1 = GitUrl(nodeps.__file__) >>> p2 = GitUrl(repo=NODEPS_PROJECT_NAME) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('github.com', 'j5pu', 'nodeps', 'https', ['https'], 'github', '/j5pu/nodeps', 'j5pu/nodeps') >>> assert p2.url == p1.url == p.url == "https://github.com/j5pu/nodeps" >>> if not CI: ... assert NODEPS_MODULE_PATH == p1._path >>> >>> u = 'git@bitbucket.org:AaronO/some-repo.git' >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('bitbucket.org', 'AaronO', 'some-repo', 'ssh', ['ssh'], 'bitbucket', 'AaronO/some-repo.git', 'AaronO/some-repo') >>> assert p.normalized == u >>> assert p.url == u.removesuffix(".git") >>> assert p.ownerrepo == "AaronO/some-repo" >>> >>> u = "https://github.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('github.com', 'cpython', 'cpython', 'https', ['https'], 'github', '/cpython/cpython', 'cpython/cpython') >>> assert p.normalized == u + ".git" >>> assert p.url == u >>> >>> p1 = GitUrl(data="cpython", repo="cpython") >>> assert p == p1 >>> >>> u = "git+https://github.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('github.com', 'cpython', 'cpython', 'https', ['git', 'https'], 'github', '/cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2githttps ('https://github.com/cpython/cpython.git', 'git+https://github.com/cpython/cpython', 'git+https://github.com/cpython/cpython.git') >>> assert p.normalized == u.removeprefix("git+") + ".git" >>> assert p.url == u >>> assert p.url2githttps == u + ".git" >>> >>> u = "git+ssh://git@github.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('github.com', 'cpython', 'cpython', 'ssh', ['git', 'ssh'], 'github', '/cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2githttps ('git@github.com:cpython/cpython.git', 'git+ssh://git@github.com/cpython/cpython', 'git+https://github.com/cpython/cpython.git') >>> assert p.normalized == 'git@github.com:cpython/cpython.git' >>> assert p.url == u >>> assert p.url2gitssh == u + ".git" >>> >>> u = "git@github.com:cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('github.com', 'cpython', 'cpython', 'ssh', ['ssh'], 'github', 'cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2git ('git@github.com:cpython/cpython.git', 'git@github.com:cpython/cpython', 'git://github.com/cpython/cpython.git') >>> assert p.normalized == u + ".git" >>> assert p.url == u >>> >>> u = "https://domain.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('domain.com', 'cpython', 'cpython', 'https', ['https'], 'gitlab', '/cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2https ('https://domain.com/cpython/cpython.git', 'https://domain.com/cpython/cpython', 'https://domain.com/cpython/cpython.git') >>> assert p.normalized == u + ".git" >>> assert p.url == u >>> >>> u = "git+https://domain.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('domain.com', 'cpython', 'cpython', 'https', ['git', 'https'], 'gitlab', '/cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2githttps ('https://domain.com/cpython/cpython.git', 'git+https://domain.com/cpython/cpython', 'git+https://domain.com/cpython/cpython.git') >>> assert p.normalized == u.removeprefix("git+") + ".git" >>> assert p.url == u >>> assert p.url2githttps == u + ".git" >>> >>> u = "git+ssh://git@domain.com/cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('domain.com', 'cpython', 'cpython', 'ssh', ['git', 'ssh'], 'gitlab', '/cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2gitssh ('git@domain.com:cpython/cpython.git', 'git+ssh://git@domain.com/cpython/cpython', 'git+ssh://git@domain.com/cpython/cpython.git') >>> assert p.normalized == "git@domain.com:cpython/cpython.git" >>> assert p.url == u >>> assert p.url2gitssh == u + ".git" >>> >>> u = "git@domain.com:cpython/cpython" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('domain.com', 'cpython', 'cpython', 'ssh', ['ssh'], 'gitlab', 'cpython/cpython', 'cpython/cpython') >>> p.normalized, p.url, p.url2ssh ('git@domain.com:cpython/cpython.git', 'git@domain.com:cpython/cpython', 'git@domain.com:cpython/cpython.git') >>> assert p.normalized == u + ".git" >>> assert p.url == u >>> assert p.url2ssh == u + ".git" >>> >>> u = "git+file:///tmp/cpython.git" >>> p = GitUrl(u) >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('/tmp', '', 'cpython', 'file', ['git', 'file'], 'base', '/cpython.git', 'cpython') >>> p.normalized, p.url ('git+file:///tmp/cpython.git', 'git+file:///tmp/cpython.git') >>> >>> p = GitUrl("git+file:///tmp/cpython") >>> p.host, p.owner, p.repo, p.protocol, p.protocols, p.platform, p.pathname, p.ownerrepo ('/tmp', '', 'cpython', 'file', ['git', 'file'], 'base', '/cpython', 'cpython') >>> p.normalized, p.url ('git+file:///tmp/cpython.git', 'git+file:///tmp/cpython.git') >>> assert p.normalized == u >>> assert p.url == u
- data: dataclasses.InitVar[str | nodeps.modules.path.Path | nodeps.modules.gh._SupportsWorkingDir | None] = ''¶
Url, path or user (to be used with name), default None for cwd. Does not have .git unless is git+file
- admin(user='j5pu', rm=False)[source]¶
Check if user has admin permissions.
Examples
>>> import nodeps >>> from nodeps import GitUrl >>> from nodeps import NODEPS_PROJECT_NAME >>> >>> assert GitUrl(nodeps.__file__).admin() is True >>> assert GitUrl(nodeps.__file__).admin("foo") is False
- default(rm=False)[source]¶
Default remote branch.
Examples
>>> import nodeps >>> from nodeps import GitUrl >>> >>> assert GitUrl(nodeps.__file__).default() == "main"
- github(rm=False)[source]¶
GitHub repos api.
Examples
>>> from nodeps import GitUrl >>> from nodeps import NODEPS_PROJECT_NAME >>> >>> assert GitUrl().github()["name"] == NODEPS_PROJECT_NAME
- property groups¶
List of groups. GitLab only.
- property host¶
Alias property for domain.
- property is_github¶
GitHub platform.
- property is_bitbucket¶
BitBucket platform.
- property is_friendcode¶
FriendCode platform.
- property is_assembla¶
Assembla platform.
- property is_gitlab¶
GitLab platform.
- property name¶
Alias property for repo.
- property normalized¶
Normalize URL with .git.
- public(rm=False)[source]¶
Check if repo ius public.
Examples
>>> import nodeps >>> from nodeps import GitUrl >>> >>> assert GitUrl(nodeps.__file__).public() is True >>> assert GitUrl(repo="pdf").public() is False
- property resource¶
Alias property for domain.
- property url2git¶
Rewrite url to git.
Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> p = GitUrl(url) >>> p.url2git 'git://github.com/Org/Private-repo.git'
- property url2githttps¶
Rewrite url to git.
Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> p = GitUrl(url) >>> p.url2githttps 'git+https://github.com/Org/Private-repo.git'
- property url2gitssh¶
Rewrite url to git.
Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> p = GitUrl(url) >>> p.url2gitssh 'git+ssh://git@github.com/Org/Private-repo.git'
- property url2https¶
Rewrite url to https.
Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> p = GitUrl(url) >>> p.url2https 'https://github.com/Org/Private-repo.git'
- property url2ssh¶
Rewrite url to ssh.
Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> p = GitUrl(url) >>> p.url2ssh 'git@github.com:Org/Private-repo.git'
- property urls¶
All supported urls for a repo.
Examples
>>> from nodeps import GitUrl >>> url = 'git@github.com:Org/Private-repo.git' >>> >>> GitUrl(url).urls {'git': 'git://github.com/Org/Private-repo.git', 'git+https': 'git+https://github.com/Org/Private-repo.git', 'git+ssh': 'git+ssh://git@github.com/Org/Private-repo.git', 'https': 'https://github.com/Org/Private-repo.git', 'ssh': 'git@github.com:Org/Private-repo.git'}
- property user¶
Alias property for _user or owner. _user == “git for ssh.
- property valid¶
Checks if url is valid.
It is equivalent to
validate().Examples
>>> from nodeps import GitUrl >>> >>> url = 'git@github.com:Org/Private-repo.git' >>> GitUrl(url).valid True >>> GitUrl.validate(url) True
- classmethod validate(data=None, repo=None)[source]¶
Validate url.
Examples
>>> from nodeps import GitUrl >>> >>> u = 'git@bitbucket.org:AaronO/some-repo.git' >>> p = GitUrl(u) >>> p.host, p.owner, p.repo ('bitbucket.org', 'AaronO', 'some-repo') >>> assert p.valid is True >>> assert GitUrl.validate(u) is True
- class nodeps.Gh(data='', repo='')[source]¶
Bases:
GitUrlGit Repo Class.
Examples
>>> import os >>> import pytest >>> import nodeps >>> from nodeps import Gh >>> >>> r = Gh() >>> r.url 'https://github.com/.../nodeps'
- Parameters:
owner – repo owner or Path
repo (str) – repo name or repo path for git+file scheme (default: None)
data (dataclasses.InitVar[str | nodeps.modules.path.Path | nodeps.modules.gh._SupportsWorkingDir | None])
- Raises:
InvalidArgumentError – if GitUrl is not initialized with path
- commit(msg=None, force=False, quiet=True)[source]¶
commit.
- Raises:
CalledProcessError – if fails
RuntimeError – if diverged or dirty
- Parameters:
- Return type:
None
- current()[source]¶
Current branch.
Examples
>>> from nodeps import Gh >>> >>> assert Gh().current() == 'main'
- Return type:
- gh_check_call(line)[source]¶
Runs git command and raises exception if error (stdout is not captured and shown).
Examples
>>> from nodeps import Gh >>> >>> assert Gh().gh_check_call("repo view") == 0
- Parameters:
line (str)
- gh_stdout(line)[source]¶
Runs git command and returns stdout.
Examples
>>> from nodeps import Gh >>> from nodeps import NODEPS_PROJECT_NAME >>> >>> assert NODEPS_PROJECT_NAME in Gh().gh_stdout("repo view")
- Parameters:
line (str)
- git_check_call(line)[source]¶
Runs git command and raises exception if error (stdout is not captured and shown).
Examples
>>> from nodeps import Gh >>> >>> assert Gh().git_check_call("rev-parse --abbrev-ref HEAD") == 0
- Parameters:
line (str)
- git_stdout(line)[source]¶
Runs git command and returns stdout.
Examples
>>> from nodeps import Gh >>> >>> assert Gh().git_stdout("rev-parse --abbrev-ref HEAD") == "main"
- Parameters:
line (str)
- next(part=Bump.PATCH, force=False)[source]¶
Show next version based on fix: feat: or BREAKING CHANGE:.
- pull(force=False, quiet=True)[source]¶
pull.
- Raises:
CalledProcessError – if pull fails
RuntimeError – if diverged or dirty
- Parameters:
- Return type:
None
- push(force=False, quiet=True)[source]¶
push.
- Raises:
CalledProcessError – if push fails
RuntimeError – if diverged
- Parameters:
- Return type:
None
- async nodeps.aioclone(owner=None, repository='nodeps', path=None)[source]¶
Async Clone Repository.
Examples
>>> import asyncio >>> from nodeps import Path >>> from nodeps import aioclone >>> >>> with Path.tempdir() as tmp: ... directory = tmp / "1" / "2" / "3" ... rv = asyncio.run(aioclone("octocat", "Hello-World", path=directory)) ... assert (rv / "README").exists()
- nodeps.clone(owner=None, repository='nodeps', path=None)[source]¶
Clone Repository.
Examples
>>> import os >>> from nodeps import Path >>> from nodeps import clone >>> >>> with Path.tempdir() as tmp: ... directory = tmp / "1" / "2" / "3" >>> if not os.environ.get("CI"): ... rv = clone("octocat", "Hello-World", directory) ... assert (rv / "README").exists()
- class nodeps.PipMetaPathFinder[source]¶
Bases:
MetaPathFinderA importlib.abc.MetaPathFinder to auto-install missing modules using pip.
Examples
>>> import sys >>> from nodeps import PipMetaPathFinder >>> # noinspection PyTypeChecker >>> sys.meta_path.append(PipMetaPathFinder) >>> # noinspection PyUnresolvedReferences >>> import simplejson
- find_spec(path, target=None)[source]¶
Try to find a module spec for the specified module.
- Parameters:
fullname (str)
target (ModuleType | None)
- Return type:
ModuleSpec | None
- nodeps.pipmetapathfinder()[source]¶
Context for
PipMetaPathFinder.Examples
>>> from nodeps import pipmetapathfinder >>> >>> with pipmetapathfinder(): ... import simplejson # type: ignore[attr-defined]
- class nodeps.FileConfig(file=None, config=<factory>)[source]¶
Bases:
objectFileConfig class.
- Parameters:
file (Path | None)
config (dict | configparser.ConfigParser)
- config: dict | configparser.ConfigParser¶
- class nodeps.FrameSimple(back, code, frame, function, globals, lineno, locals, name, package, path, vars)[source]¶
Bases:
objectSimple frame class.
- Parameters:
- back: types.FrameType¶
- code: types.CodeType¶
- frame: types.FrameType¶
- class nodeps.Passwd(data='root')[source]¶
Bases:
objectPasswd class from either uid or user.
Args:¶
- uid: int
User ID
- user: str
Username
Attributes:¶
- gid: int
Group ID
- gecos: str
Full name
- group: str
Group name
- groups: tuple(str)
Groups list
- home: Path
User’s home
- shell: Path
User shell
- uid: int
User ID (default:
os.getuid()current user id)- user: str
Username
- data: dataclasses.InitVar[Union[nodeps.modules.path.Passwd, nodeps.modules.path.Path, AnyStr, os.PathLike[str], os.PathLike[bytes], IO[AnyStr], str, int]] = 'root'¶
- property is_su¶
Returns True if login as root, uid=0 and not SUDO_USER.
- property is_sudo¶
Returns True if SUDO_USER is set.
- property is_user¶
Returns True if user and not SUDO_USER.
- classmethod from_login()[source]¶
Returns instance of
nodeps:Passwdfrom ‘/dev/console’ on macOS and os.getlogin() on Linux.
- Parameters:
data (dataclasses.InitVar[Union[nodeps.modules.path.Passwd, nodeps.modules.path.Path, ~AnyStr, os.PathLike[str], os.PathLike[bytes], IO[~AnyStr], str, int]])
- class nodeps.Path(*args, **kwargs)[source]¶
Bases:
Path,PurePosixPathPath helper class.
Construct a PurePath from one or several strings and or existing PurePath objects. The strings and path objects are combined so as to yield a canonicalized path, which is incorporated into the new PurePath object.
- access(os_mode=2, *, dir_fd=None, effective_ids=True, follow_symlinks=False)[source]¶
Checks if file or directory exists and has access (returns None if file/directory does not exist.
Use the real uid/gid to test for access to a path `Real Effective IDs.`_.
real: user owns the completed.
effective: user invoking.
Examples
>>> import os >>> from nodeps import Path >>> from nodeps import MACOS >>> from nodeps import LOCAL >>> from nodeps import USER >>> >>> assert Path().access() is True >>> if USER == "root": ... assert Path('/usr/bin').access() is True ... else: ... assert Path('/usr/bin').access() is False >>> assert Path('/tmp').access(follow_symlinks=True) is True >>> assert Path('/tmp').access(effective_ids=False, follow_symlinks=True) is True >>> if MACOS and LOCAL: ... assert Path('/etc/bashrc').access(effective_ids=False) is False ... assert Path('/etc/sudoers').access(effective_ids=False, os_mode=os.R_OK) is False
- Parameters:
os_mode – Operating-system mode bitfield. Can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK (default: os.W_OK).
dir_fd – If not None, it should be a file descriptor open to a directory, and path should be relative; path will then be relative to that directory.
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: True).
follow_symlinks – If False, and the last element of the path is a symbolic link, access will examine the symbolic link itself instead of the file the link points to (default: False).
Note
Most operations will use the effective uid/gid (what the operating system looks at to make a decision whether you are allowed to do something), therefore this routine can be used in a suid/sgid environment to test if the invoking user has the specified access to the path.
When a setuid program (-rwsr-xr-x) executes, the completed changes its Effective User ID (EUID) from the default RUID to the owner of this special binary executable file:
euid: owner of executable (os.geteuid()).
uid: user starting the completed (os.getuid()).
- Returns:
True if access.
See Also: Real Effective IDs.
- add(*args, exception=False)[source]¶
Add args to self.
Examples
>>> from nodeps import Path >>> import nodeps >>> >>> p = Path().add('a/a') >>> assert Path() / 'a/a' == p >>> p = Path().add(*['a', 'a']) >>> assert Path() / 'a/a' == p >>> p = Path(nodeps.__file__) >>> p.add('a', exception=True) Traceback (most recent call last): FileNotFoundError...
- Parameters:
*args – parts to be added.
exception – raise exception if self is not dir and parts can not be added (default: False).
- Raises:
FileNotFoundError – if self is not dir and parts can not be added.
- Returns:
Compose path.
- append_text(text, encoding=None, errors=None)[source]¶
Open the file in text mode, append to it, and close the file (creates file if not file).
Examples
>>> from nodeps import Path >>> >>> with Path.tempfile() as tmp: ... _ = tmp.write_text('Hello') ... assert 'Hello World!' in tmp.append_text(' World!')
- Parameters:
text – text to add.
encoding – encoding (default: None).
errors – raise error if there is no file (default: None).
- Returns:
File text with text appended.
- cd()[source]¶
Change dir context manager to self if dir or parent if file and exists.
Examples
>>> from nodeps import Path >>> >>> new = Path('/usr/local') >>> p = Path.cwd() >>> with new.cd() as prev: ... assert new == Path.cwd() ... assert prev == p >>> assert p == Path.cwd()
- Returns:
Old Pwd Path.
- chdir()[source]¶
Change to self if dir or file parent if file and file exists.
Examples
>>> from nodeps import Path >>> >>> new = Path(__file__).chdir() >>> assert new == Path(__file__).parent >>> assert Path.cwd() == new >>> >>> new = Path(__file__).parent >>> assert Path.cwd() == new >>> >>> Path("/tmp/foo").chdir() Traceback (most recent call last): FileNotFoundError: ... No such file or directory: '/tmp/foo'
- Raises:
FileNotFoundError – No such file or directory if path does not exist.
- Returns:
Path with changed directory.
- checksum(algorithm='sha256', block_size=65536)[source]¶
Calculate the checksum of a file.
Examples
>>> from nodeps import Path >>> >>> with Path.tempfile() as tmp: ... _ = tmp.write_text('Hello') ... assert tmp.checksum() == '185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969'
- Parameters:
algorithm – hash algorithm (default: ‘sha256’).
block_size – block size (default: 65536).
- Returns:
Checksum of file.
- chmod(mode=None, effective_ids=True, exception=True, follow_symlinks=False, recursive=False)[source]¶
Change mode of self.
Examples
>>> from nodeps import Path >>> >>> with Path.tempfile() as tmp: ... changed = tmp.chmod(777) ... assert changed.stat().st_mode & 0o777 == 0o777 ... assert changed.stats().mode == "-rwxrwxrwx" ... assert changed.chmod("o-x").stats().mode == '-rwxrwxrw-' >>> >>> Path("/tmp/foo").chmod() Traceback (most recent call last): FileNotFoundError: ... No such file or directory: '/tmp/foo'
- Raises:
FileNotFoundError – No such file or directory if path does not exist and exception is True.
- Parameters:
mode – mode to change to (default: None).
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
follow_symlinks – resolve self if self is symlink (default: True).
exception – raise exception if self does not exist (default: True).
recursive – change owner of self and all subdirectories (default: False).
- Returns:
Path with changed mode.
- chown(passwd=None, effective_ids=True, exception=True, follow_symlinks=False, recursive=False)[source]¶
Change owner of path.
Examples
>>> from nodeps import Path >>> from nodeps import Passwd >>> from nodeps import MACOS >>> >>> with Path.tempfile() as tmp: ... changed = tmp.chown(passwd=Passwd.from_root()) ... st = changed.stat() ... assert st.st_gid == 0 ... assert st.st_uid == 0 ... stats = changed.stats() ... assert stats.gid == 0 ... assert stats.uid == 0 ... assert stats.user == "root" ... if MACOS: ... assert stats.group == "wheel" ... g = "admin" ... else: ... assert stats.group == "root" ... g = "adm" ... changed = tmp.chown(f"{os.getuid()}:{g}") ... stats = changed.stats() ... assert stats.group == g ... assert stats.uid == os.getuid() >>> >>> Path("/tmp/foo").chown() Traceback (most recent call last): FileNotFoundError: ... No such file or directory: '/tmp/foo'
- Raises:
FileNotFoundError – No such file or directory if path does not exist and exception is True.
ValueError – passwd must be string with user:group.
- Parameters:
passwd – user/group passwd to use, or string with user:group (default: None for USER).
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
exception – raise exception if self does not exist (default: True).
follow_symlinks – resolve self if self is symlink (default: True).
recursive – change owner of self and all subdirectories (default: False).
- Returns:
Path with changed owner.
- cmp(other)[source]¶
Determine, whether two files provided to it are the same or not.
By the same means that their contents are the same or not (excluding any metadata). Uses Cryptographic Hashes (using SHA256 - Secure hash algorithm 256) as a hash function.
Examples
>>> from nodeps import Path >>> import nodeps >>> import asyncio >>> >>> assert Path(nodeps.__file__).cmp(nodeps.__file__) is True >>> assert Path(nodeps.__file__).cmp(asyncio.__file__) is False
- Parameters:
other – other file to compare to
- Returns:
True if equal.
- cp(dest, contents=False, effective_ids=True, follow_symlinks=False, preserve=False)[source]¶
Wrapper for shell cp command to copy file recursivily and adding sudo if necessary.
Examples
>>> import sys >>> from nodeps import Path >>> from nodeps import SUDO, USER >>> from nodeps import Passwd >>> >>> with Path.tempfile() as tmp: ... changed = tmp.chown(passwd=Passwd.from_root()) ... copied = Path(__file__).cp(changed) ... st = copied.stat() ... assert st.st_gid == 0 ... assert st.st_uid == 0 ... stats = copied.stats() ... assert "-rw-" in stats.mode ... _ = tmp.chown() ... assert copied.cmp(__file__)
>>> with Path.tempdir() as tmp: ... _ = tmp.chmod("go+rx") ... _ = tmp.chown(passwd=Passwd.from_root()) ... src = Path(__file__).parent ... dirname = src.name ... filename = Path(__file__).name ... ... _ = src.cp(tmp) ... destination = tmp / dirname ... stats = destination.stats() ... assert stats.mode == "drwxr-xr-x" ... file = destination / filename ... st = file.stat() ... assert st.st_gid == 0 ... assert st.st_uid == 0 ... assert file.owner() == "root" ... tmp = tmp.chown(recursive=True) ... assert file.owner() == USER ... assert file.cmp(__file__) ... ... _ = src.cp(tmp, contents=True) ... file = tmp / filename ... assert file.cmp(__file__) >>> >>> Path("/tmp/foo").cp("/tmp/boo") Traceback (most recent call last): FileNotFoundError: ... No such file or directory: '/tmp/foo'
- Parameters:
dest – destination.
contents – copy contents of self to dest, cp src/ dest instead of cp src dest (default: False)`.
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
follow_symlinks – ‘-P’ the ‘cp’ default, no symlinks are followed, all symbolic links are followed when True ‘-L’ (actual files are copyed but if there are existing links will be left them untouched) (default: False) -H cp option is not implemented (default: False).
preserve – preserve file attributes (default: False).
- Raises:
FileNotFoundError – No such file or directory if path does not exist.
- Returns:
Dest.
- exists()[source]¶
Check if file exists or is a broken link (super returns False if it is a broken link, we return True).
Examples
>>> from nodeps import Path >>> >>> Path(__file__).exists() True >>> with Path.tempcd() as tmp: ... source = tmp.touch(tmp / "source") ... destination = source.ln(tmp / "destination") ... assert destination.is_symlink() ... source.unlink() ... assert destination.exists() ... assert not pathlib.Path(destination).exists()
- Returns:
True if file exists or is broken link.
- classmethod expandvars(path=None)[source]¶
Return a Path instance from expanded environment variables in path.
Expand shell variables of form $var and ${var}. Unknown variables are left unchanged.
Examples
>>> from nodeps import Path >>> >>> Path.expandvars('~/repo') Path('~/repo') >>> Path.expandvars('${HOME}/repo') Path('.../repo')
- Returns:
Expanded Path.
- file_in_parents(exception=True, follow_symlinks=False)[source]¶
Find up until file with name is found.
Examples
>>> from nodeps import Path >>> >>> with Path.tempfile() as tmpfile: ... new = tmpfile / "sub" / "file.py" ... assert new.file_in_parents(exception=False) == tmpfile.absolute() >>> >>> with Path.tempdir() as tmpdir: ... new = tmpdir / "sub" / "file.py" ... assert new.file_in_parents() is None
- Parameters:
exception – raise exception if a file is found in parents (default: False).
follow_symlinks – resolve self if self is symlink (default: True).
- Raises:
NotADirectoryError – … No such file or directory: ‘/tmp/foo’
- Returns:
File found in parents (str) or None
- find_up(uppermost=False)[source]¶
Find file or dir up.
Examples
>>> import email.mime.application >>> import email >>> import email.mime >>> from nodeps import Path >>> >>> assert 'email/mime/__init__.py' in Path(email.mime.__file__, "__init__.py").find_up() >>> assert 'email/__init__.py' in Path(email.__file__, "__init__.py").find_up(uppermost=True)
- Parameters:
uppermost – find uppermost (default: False).
- Return type:
FindUp
- has(value)[source]¶
Checks all items in value exist in self.parts (not absolute and not relative).
Only checks parts and not resolved as checked by __contains__ or absolute.
Examples
>>> from nodeps import Path >>> >>> assert Path('/usr/local').has('/usr') is True >>> assert Path('/usr/local').has('usr local') is True >>> assert Path('/usr/local').has('home') is False >>> assert Path('/usr/local').has('') is False
- Parameters:
value – space separated list of items to check, or iterable of items.
- Returns:
bool
- installed()[source]¶
Check if file is installed.
Examples
>>> import pytest >>> from nodeps import Path >>> >>> assert Path(pytest.__file__).installed() is True
- ln(dest, force=True)[source]¶
Wrapper for super symlink_to to return the new path and changing the argument.
If symbolic link already exists and have the same source, it will not be overwritten.
Similar:
dest.symlink_to(src)
src.ln(dest) -> dest
os.symlink(src, dest)
Examples
>>> from nodeps import Path >>> >>> with Path.tempcd() as tmp: ... source = tmp.touch("source") ... _ = source.ln(tmp / "destination") ... destination = source.ln(tmp / "destination") ... assert destination.is_symlink() ... assert destination.resolve() == source.resolve() ... assert destination.readlink().resolve() == source.resolve() ... ... touch = tmp.touch(tmp / "touch") ... _ = tmp.ln(tmp / "destination", force=False) Traceback (most recent call last): FileExistsError:
- Raises:
FileExistsError – if dest already exists or is a symbolic link with different source and force is False.
- Parameters:
dest – link destination (ln -s self dest)
force – force creation of link, if file or link exists and is different (default: True)
- ln_rel(dest)[source]¶
Create a symlink pointing to
targetfromlocation.- Parameters:
dest – The location of the symlink itself.
- mkdir(name='', passwd=None, mode=None, effective_ids=True, follow_symlinks=False)[source]¶
Add directory, make directory, change mode and return new Path.
Examples
>>> import getpass >>> from nodeps import Path >>> from nodeps import Passwd >>> >>> with Path.tempcd() as tmp: ... directory = tmp('1/2/3/4') ... assert directory.is_dir() is True ... assert directory.owner() == getpass.getuser() ... ... _ = directory.chown(passwd=Passwd.from_root()) ... assert directory.owner() == "root" ... five = directory.mkdir("5") ... assert five.text.endswith('/1/2/3/4/5') is True ... assert five.owner() == "root" ... ... six = directory("6") ... assert six.owner() == "root" ... ... seven = directory("7", passwd=Passwd()) ... assert seven.owner() == getpass.getuser() ... ... _ = directory.chown(passwd=Passwd())
- Parameters:
name – name.
passwd – group/user for chown, if None ownership will not be changed (default: None).
mode – mode.
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: True).
follow_symlinks – resolve self if self is symlink (default: True).
- Raises:
NotADirectoryError – Directory can not be made because it’s a file.
- Return type:
- mv(dest)[source]¶
Move.
Examples
>>> from nodeps import Path >>> >>> with Path.tempdir() as tmp: ... name = 'dir' ... pth = tmp(name) ... assert pth.is_dir() ... _ = pth.mv(tmp('dir2')) ... assert not pth.is_dir() ... assert tmp('dir2').is_dir() ... name = 'file' ... pth = tmp(name, "is_file") ... assert pth.is_file() ... _ = pth.mv(tmp('file2')) ... assert not pth.is_file()
- Parameters:
dest – destination.
- Returns:
None.
- open(mode='r', buffering=-1, encoding=None, errors=None, newline=None, token=False)[source]¶
Open the file pointed by this path and return a file object, as the built-in open function does.
- classmethod pickle(data=None, name=None, rm=False)[source]¶
Load or dumps pickle file from ~/.pickle directory.
Examples
>>> import pickle >>> from nodeps import Path >>> >>> assert Path.pickle(name="test") is None >>> >>> obj = {'a': 1} >>> _ = Path.pickle(obj, name="test") >>> assert Path.pickle(name="test") == obj >>> >>> obj2 = {'a': 2} >>> _ = Path.pickle(obj2, name="test", rm=True) >>> assert Path.pickle(name="test") == obj2 >>> >>> assert Path.pickle(name="test", rm=True) is None
- Parameters:
data – data to pickle (default: None to read from file).
name – name.__name__ or name of object which will be used as file stem (default: None to get the name from __name__ in data)
rm – rm existing data.
- Raises:
InvalidArgumentError – when no name can be derived from data.__name__ or not name provided
- Returns:
Pickle object (None if no data exists) if data is None else None.
- privileges(effective_ids=True)[source]¶
Return privileges of file.
- Parameters:
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: True).
- Return type:
Privileges
- realpath(exception=False)[source]¶
Return the canonical path of the specified filename, eliminating any symbolic links encountered in the path.
Examples
>>> from nodeps import Path >>> >>> assert Path('/usr/local').realpath() == Path('/usr/local')
- Parameters:
exception – raise exception if path does not exist (default: False).
- Returns:
Path with real path.
- relative(path)[source]¶
Return relative to path if is relative to path else None.
Examples
>>> from nodeps import Path >>> >>> assert Path('/usr/local').relative('/usr') == Path('local') >>> assert Path('/usr/local').relative('/usr/local') == Path('.') >>> assert Path('/usr/local').relative('/usr/local/bin') is None
- Parameters:
path – path.
- Returns:
Relative path or None.
- rm(*args, effective_ids=True, follow_symlinks=False, missing_ok=True)[source]¶
Delete a folder/file (even if the folder is not empty).
Examples
>>> from nodeps import Path >>> >>> with Path.tempdir() as tmp: ... name = 'dir' ... pth = tmp(name) ... assert pth.is_dir() ... pth.rm() ... assert not pth.is_dir() ... name = 'file' ... pth = tmp(name, "is_file") ... assert pth.is_file() ... pth.rm() ... assert not pth.is_file() ... assert Path('/tmp/a/a/a/a')().is_dir()
- Raises:
FileNotFoundError – … No such file or directory: ‘/tmp/foo’
- Parameters:
*args – parts to add to self.
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
follow_symlinks – True for resolved (default: False).
missing_ok – missing_ok
- rm_empty(preserve=True)[source]¶
Remove empty directories recursive.
Examples
>>> from nodeps import Path >>> >>> with Path.tempdir() as tmp: ... first = tmp("1") ... ... _ = tmp('1/2/3/4') ... first.rm_empty() ... assert first.exists() is True ... assert Path("1").exists() is False ... ... _ = tmp('1/2/3/4') ... first.rm_empty(preserve=False) ... assert first.exists() is False ... ... _ = tmp('1/2/3/4/5/6/7.py', file="is_file") ... first.rm_empty() ... assert first.exists() is True
- Parameters:
preserve – preserve top directory (default: True).
- setid(name=None, uid=True, effective_ids=True, follow_symlinks=False)[source]¶
Sets the set-user-ID-on-execution or set-group-ID-on-execution bits.
Works if interpreter binary is setuid u+s,+x (-rwsr-xr-x), and:
executable script and setuid interpreter on shebang (#!/usr/bin/env setuid_interpreter).
setuid_interpreter -m module (venv would be created as root
Works if interpreter binary is setuid g+s,+x (-rwxr-sr-x), and:
Examples
>>> from nodeps import Path >>> >>> with Path.tempdir() as p: ... a = p.touch('a') ... _ = a.setid() ... assert a.stats().suid is True ... _ = a.setid(uid=False) ... assert a.stats().sgid is True ... ... a.rm() ... ... _ = a.touch() ... b = a.setid('b') ... assert b.stats().suid is True ... assert a.cmp(b) is True ... ... _ = b.setid('b', uid=False) ... assert b.stats().sgid is True ... ... _ = a.write_text('a') ... assert a.cmp(b) is False ... b = a.setid('b') ... assert b.stats().suid is True ... assert a.cmp(b) is True
- Parameters:
name – name to rename if provided.
uid – True to set UID bit, False to set GID bit (default: True).
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
follow_symlinks – True for resolved, False for absolute and None for relative or doesn’t exist (default: True).
- Returns:
Updated Path.
- classmethod setid_executable()[source]¶
Sets the set-user-ID-on-execution bits for sys.executable.
- Returns:
Updated Path.
- classmethod setid_executable_cp(name=None, uid=True)[source]¶
Sets the set-user-ID-on-execution or set-group-ID-on-execution bits for sys.executable.
Examples
>>> import shutil >>> import subprocess >>> from nodeps import Path, MACOS, USER >>> def test(): ... f = Path.setid_executable_cp('setid_python_test') ... assert subprocess.check_output([f, '-c', 'import os;print(os.geteuid())'], text=True) == '0\n' ... if USER != "root": ... assert subprocess.check_output([f, '-c', 'import os;print(os.getuid())'], text=True) != '0\n' ... else: ... assert subprocess.check_output([f, '-c', 'import os;print(os.getuid())'], text=True) == '0\n' ... f.rm() ... assert f.exists() is False >>> test()
- Parameters:
name – name to rename if provided or False to add ‘r’ to original name (default: False).
uid – True to set UID bit, False to set GID bit (default: True).
- Returns:
Updated Path.
- stats(follow_symlinks=False)[source]¶
Return result of the stat() system call on this path, like os.stat() with extra parsing for bits and root.
Examples
>>> from nodeps import Path >>> >>> rv = Path().stats() >>> assert all([rv.root, rv.sgid, rv.sticky, rv.suid]) is False >>> >>> with Path.tempfile() as file: ... _ = file.chmod('u+s,+x') ... assert file.stats().suid is True ... _ = file.chmod('g+s,+x') ... assert file.stats().sgid is True
- Parameters:
follow_symlinks – If False, and the last element of the path is a symbolic link, stat will examine the symbolic link itself instead of the file the link points to (default: False).
- Returns:
gid: file GID group: file group name mode: file mode string formatted as ‘-rwxrwxrwx’ own: user and group string formatted as ‘user:group’ passwd: instance of
nodeps:Passwdfor file owner result: result of os.stat root: is owned by root sgid: group executable and sticky bit (GID bit), members execute as the executable group (i.e.: crontab) sticky: sticky bit (directories), new files created in this directory will be owned by the directory’s owner suid: user executable and sticky bit (UID bit), user execute and as the executable owner (i.e.: sudo) uid: file UID user: file owner name- Return type:
PathStat namedtuple
nodeps.PathStat
- sudo(force=False, to_list=True, os_mode=2, effective_ids=True, follow_symlinks=False)[source]¶
Returns sudo command if path or ancestors exist and is not own by user and sudo command not installed.
Examples
>>> from nodeps import which >>> from nodeps import Path >>> from nodeps import SUDO >>> >>> assert Path('/tmp').sudo(to_list=False, follow_symlinks=True) == '' >>> if SUDO: ... assert "sudo" in Path('/usr/bin').sudo(to_list=False) >>> assert Path('/usr/bin/no_dir/no_file.text').sudo(to_list=False) == SUDO >>> assert Path('no_dir/no_file.text').sudo(to_list=False) == '' >>> assert Path('/tmp').sudo(follow_symlinks=True) == [] >>> if SUDO: ... assert Path('/usr/bin').sudo() == [SUDO] ... else: ... assert Path('/usr/bin').sudo() == []
- Parameters:
force – if sudo installed and user is not root, return always sudo path
to_list – return starred/list for command with no shell (default: True).
os_mode – Operating-system mode bitfield. Can be F_OK to test existence, or the inclusive-OR of R_OK, W_OK, and X_OK (default: os.W_OK).
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: True).
follow_symlinks – If False, and the last element of the path is a symbolic link, access will examine the symbolic link itself instead of the file the link points to (default: False).
- Returns:
sudo or “”, str or list.
- sys()[source]¶
Insert self absolute if exists to sys.path 0 if it is not in sys.path.
Examples
>>> import sys >>> from nodeps import Path >>> >>> cwd = Path.cwd() >>> cwd.sys() >>> assert str(cwd.absolute()) in sys.path
- property text¶
Path as text.
Examples
>>> from nodeps import Path >>> >>> assert Path('/usr/local').text == '/usr/local'
- Returns:
Path string.
- classmethod tempcd(suffix=None, prefix=None, directory=None)[source]¶
Create temporaly directory, change to it and return it.
This has the same behavior as mkdtemp but can be used as a context manager.
Upon exiting the context, the directory and everything contained in it are removed.
Examples
>>> from nodeps import Path >>> >>> work = Path.cwd() >>> with Path.tempcd() as tmp: ... assert tmp.exists() and tmp.is_dir() ... assert Path.cwd() == tmp.resolve() >>> assert work == Path.cwd() >>> assert tmp.exists() is False
- Parameters:
suffix – If ‘suffix’ is not None, the directory name will end with that suffix, otherwise there will be no suffix. For example, …/T/tmpy5tf_0suffix
prefix – If ‘prefix’ is not None, the directory name will begin with that prefix, otherwise a default prefix is used.. For example, …/T/prefixtmpy5tf_0
directory – If ‘directory’ is not None, the directory will be created in that directory (must exist, otherwise a default directory is used. For example, DIRECTORY/tmpy5tf_0
- Returns:
Directory Path.
- classmethod tempdir(suffix=None, prefix=None, directory=None)[source]¶
Create and return tmp directory. This has the same behavior as mkdtemp but can be used as a context manager.
Upon exiting the context, the directory and everything contained in it are removed.
Examples
>>> from nodeps import Path >>> >>> work = Path.cwd() >>> with Path.tempdir() as tmpdir: ... assert tmpdir.exists() and tmpdir.is_dir() ... assert Path.cwd() != tmpdir ... assert work == Path.cwd() >>> assert tmpdir.exists() is False
- Parameters:
suffix – If ‘suffix’ is not None, the directory name will end with that suffix, otherwise there will be no suffix. For example, …/T/tmpy5tf_0suffix
prefix – If ‘prefix’ is not None, the directory name will begin with that prefix, otherwise a default prefix is used.. For example, …/T/prefixtmpy5tf_0
directory – If ‘directory’ is not None, the directory will be created in that directory (must exist, otherwise a default directory is used. For example, DIRECTORY/tmpy5tf_0
- Returns:
Directory Path.
- classmethod tempfile(mode='w', buffering=-1, encoding=None, newline=None, suffix=None, prefix=None, directory=None, delete=True, *, errors=None)[source]¶
Create and return a temporary file.
Examples
>>> from nodeps import Path >>> >>> with Path.tempfile() as tmpfile: ... assert tmpfile.exists() and tmpfile.is_file() >>> assert tmpfile.exists() is False
- Parameters:
mode – the mode argument to io.open (default “w+b”).
buffering – the buffer size argument to io.open (default -1).
encoding – the encoding argument to io.open (default None)
newline – the newline argument to io.open (default None)
delete – whether the file is deleted on close (default True).
suffix – prefix for filename.
prefix – prefix for filename.
directory – directory.
errors – the errors’ argument to io.open (default None)
- Returns:
An object with a file-like interface; the name of the file is accessible as its ‘name’ attribute. The file will be automatically deleted when it is closed unless the ‘delete’ argument is set to False.
- to_parent()[source]¶
Return Parent if is file and exists or self.
Examples
>>> from nodeps import Path >>> >>> assert Path(__file__).to_parent() == Path(__file__).parent
- Returns:
Path of directory if is file or self.
- touch(name='', passwd=None, mode=None, effective_ids=True, follow_symlinks=False)[source]¶
Add file, touch and return post_init Path. Parent paths are created.
Examples
>>> from nodeps import Path >>> from nodeps import Passwd >>> >>> import getpass >>> with Path.tempcd() as tmp: ... file = tmp('1/2/3/4/5/6/root.py', file="is_file", passwd=Passwd.from_root()) ... assert file.is_file() is True ... assert file.parent.owner() == getpass.getuser() ... assert file.owner() == "root" ... ... new = file.parent("user.py", file="is_file") ... assert new.owner() == getpass.getuser() ... ... touch = file.parent.touch("touch.py") ... assert touch.owner() == getpass.getuser() ... ... last = (file.parent / "last.py").touch() ... assert last.owner() == getpass.getuser() ... assert last.is_file() is True ... ... file.rm()
- Parameters:
name – name.
passwd – group/user for chown, if None ownership will not be changed (default: None).
mode – mode.
effective_ids – If True, access will use the effective uid/gid instead of the real uid/gid (default: False).
follow_symlinks – If False, I think is useless (default: False).
- Returns:
Path.
- with_suffix(suffix='')[source]¶
Sets default for suffix to “”, since
pathlib.Pathdoes not have default.Return a new path with the file suffix changed. If the path has no suffix, add given suffix. If the given suffix is an empty string, remove the suffix from the path.
Examples
>>> from nodeps import Path >>> >>> Path("/tmp/test.txt").with_suffix() Path('/tmp/test')
- Parameters:
suffix – suffix (default: ‘’)
- Returns:
Path.
- class nodeps.PathStat(gid, group, mode, own, passwd, result, root, sgid, sticky, suid, uid, user)[source]¶
Bases:
objectHelper class for
nodeps.Path.stats().- Parameters:
gid (int) – file GID
group (str) – file group name
mode (str) – file mode string formatted as ‘-rwxrwxrwx’
own (str) – user and group string formatted as ‘user:group’
passwd (Passwd) – instance of
nodeps:Passwdfor file ownerresult (stat_result) – result of os.stat
root (bool) – is owned by root
sgid (bool) – group executable and sticky bit (GID bit), members execute as the executable group (i.e.: crontab)
sticky (bool) – sticky bit (directories), new files created in this directory will be owned by the directory’s owner
suid (bool) – user executable and sticky bit (UID bit), user execute and as the executable owner (i.e.: sudo)
uid (int) – file UID
user (str) – file user name
- result: stat_result¶
- nodeps.toiter(obj, always=False, split=' ')[source]¶
To iter.
Examples
>>> import pathlib >>> from nodeps import toiter >>> >>> assert toiter('test1') == ['test1'] >>> assert toiter('test1 test2') == ['test1', 'test2'] >>> assert toiter({'a': 1}) == {'a': 1} >>> assert toiter({'a': 1}, always=True) == [{'a': 1}] >>> assert toiter('test1.test2') == ['test1.test2'] >>> assert toiter('test1.test2', split='.') == ['test1', 'test2'] >>> assert toiter(pathlib.Path("/tmp/foo")) == ('/', 'tmp', 'foo')
- Parameters:
obj – obj.
always – return any iterable into a list.
split – split for str.
- Returns:
Iterable.
- class nodeps.Project(data=None, rm=False)[source]¶
Bases:
objectProject Class.
- Parameters:
data (Path | str | ModuleType)
rm (dataclasses.InitVar[bool])
- data: Path | str | ModuleType = None¶
current working directory)
- Type:
File, directory or name (str or path with one word) of project (default
- name: str = None¶
Pypi project name from setup.cfg, pyproject.toml or top name or self.data when is one word
- pyproject_toml: FileConfig¶
- bin(executable=None, version='3.11')[source]¶
Bin directory.
- Args;
executable: command to add to path version: python version
- browser(version='3.11', quiet=True)[source]¶
Build and serve the documentation with live reloading on file changes.
- build(version='3.11', quiet=True, rm=False)[source]¶
Build a project venv, completions, docs and clean.
- completions(uninstall=False)[source]¶
Generate completions to /usr/local/etc/bash_completion.d.
- Parameters:
uninstall (bool)
- extras(as_list=False, rm=False)[source]¶
Optional dependencies from pyproject.toml or distribution.
Examples
>>> import typer >>> from nodeps import Project >>> >>> nodeps = Project.nodeps() >>> nodeps.extras() {'...': ['... >>> nodeps.extras(as_list=True) ['... >>> Project(typer.__name__).extras() {'all':... >>> Project("sampleproject").extras() {'dev':...
- post(uninstall=False)[source]¶
Run post install for package: completions, brew and _post_install.py.
- Parameters:
uninstall (bool)
- Return type:
None
- publish(part=Bump.PATCH, force=False, ruff=True, tox=False, quiet=True, rm=False)[source]¶
Publish runs runs tests, commit, tag, push, twine and clean.
- pypi(rm=False)[source]¶
Pypi information for a package.
Examples
>>> from nodeps import Project >>> from nodeps import NODEPS_PROJECT_NAME >>> >>> assert Project(NODEPS_PROJECT_NAME).pypi()["info"]["name"] == NODEPS_PROJECT_NAME
- classmethod repos(ret=ProjectRepos.NAMES, sync=False, archive=False, rm=False)[source]¶
Repo paths, names or Project instances under home, Archive or parent of nodeps top.
Examples
>>> from nodeps import Project >>> from nodeps import NODEPS_PROJECT_NAME, Path, NODEPS_TOP >>> >>> assert NODEPS_PROJECT_NAME in Project.repos() >>> assert NODEPS_PROJECT_NAME in Project.repos(ProjectRepos.DICT) >>> assert NODEPS_PROJECT_NAME in Project.repos(ProjectRepos.INSTANCES) >>> assert NODEPS_PROJECT_NAME in Project.repos(ProjectRepos.PY) >>> >>> shrc = Path.home() / "shrc/.git" >>> if shrc.is_dir(): ... assert "shrc" not in Project.repos(ProjectRepos.PY) ... assert "shrc" in Project.repos()
- requirement(version='3.11', install=False, upgrade=False, quiet=True, rm=False)[source]¶
Dependencies and optional dependencies from pyproject.toml or distribution.
- requirements(upgrade=False, quiet=True, rm=False)[source]¶
Install dependencies and optional dependencies from pyproject.toml or distribution for python versions.
- test(version='3.11', ruff=True, tox=False, quiet=True)[source]¶
Test project, runs build, ruff, pytest and tox.
- tests(ruff=True, tox=False, quiet=True)[source]¶
Test project, runs build, ruff, pytest and tox for all versions.
- venv(version='3.11', clear=False, upgrade=False, quiet=True, rm=False)[source]¶
Creates venv, runs: write and requirements.
- venvs(upgrade=False, quiet=True, rm=False)[source]¶
Installs venv for all python versions in
PYTHON_VERSIONS.
- nodeps.root(func, *args, **kwargs)[source]¶
Runs function with euid set to 0.
- Parameters:
func (Callable[P, T])
args (P.args)
kwargs (P.kwargs)
- Return type:
T
- class nodeps.ModuleSpec(name, loader, *, origin=None, loader_state=None, is_package=None)[source]¶
Bases:
objectThe specification for a module, used for loading.
A module’s spec is the source for information about the module. For data associated with the module, including source, use the spec’s loader.
name is the absolute name of the module. loader is the loader to use when loading the module. parent is the name of the package the module is in. The parent is derived from the name.
is_package determines if the module is considered a package or not. On modules this is reflected by the __path__ attribute.
origin is the specific location used by the loader from which to load the module, if that information is available. When filename is set, origin will match.
has_location indicates that a spec’s “origin” reflects a location. When this is True, __file__ attribute of the module is set.
cached is the location of the cached bytecode file, if any. It corresponds to the __cached__ attribute.
submodule_search_locations is the sequence of path entries to search when importing submodules. If set, is_package should be True–and False otherwise.
Packages are simply modules that (may) have submodules. If a spec has a non-None value in submodule_search_locations, the import system will consider modules loaded from the spec as packages.
Only finders (see importlib.abc.MetaPathFinder and importlib.abc.PathEntryFinder) should modify ModuleSpec instances.
- property cached¶
- property parent¶
The name of the module’s parent.
- property has_location¶
- nodeps.ThreadLock()¶
allocate_lock() -> lock object (allocate() is an obsolete synonym)
Create a new lock object. See help(type(threading.Lock())) for information about locks.
- nodeps.RunningLoop¶
alias of
_RunningLoop
- nodeps.LockClass¶
alias of
lock
- class nodeps.BuildPy(dist, **kw)[source]¶
Bases:
build_pyBuild py with pth files installed.
Construct the command for dist, updating vars(self) with any keyword parameters.
- Parameters:
dist (Distribution)
- class nodeps.Develop(dist, **kw)[source]¶
Bases:
developPTH Develop Install.
Construct the command for dist, updating vars(self) with any keyword parameters.
- Parameters:
dist (Distribution)