diff --git a/dist/nvexpo-0.1.0-py3-none-any.whl b/dist/nvexpo-0.1.0-py3-none-any.whl deleted file mode 100644 index 6c19651..0000000 Binary files a/dist/nvexpo-0.1.0-py3-none-any.whl and /dev/null differ diff --git a/dist/nvexpo-0.1.0.tar.gz b/dist/nvexpo-0.1.0.tar.gz deleted file mode 100644 index eb6a95c..0000000 Binary files a/dist/nvexpo-0.1.0.tar.gz and /dev/null differ diff --git a/nvexpo/__init__.py b/nvexpo/__init__.py index 5720ac4..5531d17 100644 --- a/nvexpo/__init__.py +++ b/nvexpo/__init__.py @@ -1,23 +1,27 @@ #!/bin/python +import re import sys from nvexpo import nvexpo +from nvexpo import consts as CONSTS def main() -> int: args = sys.argv + print(args) if len(args) <= 1: - nvexpo.help() - return 0 - if len(args) == 2 and args[1] in ["--load"]: - nvexpo.load() + nvexpo.show_help() return 0 if args[1] in ["--unset"]: nvexpo.unset_env(args[2:]) return 0 + if args[1] in ["--load"]: + nvexpo._load() + return 0 + if args[1] in ["init"]: try: nvexpo.init(args[2]) @@ -26,22 +30,27 @@ def main() -> int: return 0 raw_vars = args[1:] - if not all("=" in x for x in raw_vars): - nvexpo.help() - return 1 - variables = {} for raw_var in raw_vars: + if "=" not in raw_var: + nvexpo.show_help("Use the syntax \"key=value\"") + return 1 try: key, value = raw_var.split("=", 1) - except ValueError: - nvexpo.help() + except ValueError as e: + nvexpo.show_help(f"Unexpected error {e}") return 1 key = key.strip("=") - value = value.strip("=").strip("\"") + value = value.strip("=").strip('"') variables[key] = value - if not key or not value: - nvexpo.help() + if not key: + nvexpo.show_help("Empty name") + return 1 + if not value: + nvexpo.show_help("Empty value") + return 1 + if not re.match(CONSTS.VARNAME_PATTERN, key): + nvexpo.show_help(f"\"{key}\" is not a valid name") return 1 nvexpo.set_env(variables) return 0 diff --git a/nvexpo/__pycache__/__init__.cpython-311.pyc b/nvexpo/__pycache__/__init__.cpython-311.pyc deleted file mode 100644 index 5cf4592..0000000 Binary files a/nvexpo/__pycache__/__init__.cpython-311.pyc and /dev/null differ diff --git a/nvexpo/__pycache__/nvexpo.cpython-311.pyc b/nvexpo/__pycache__/nvexpo.cpython-311.pyc deleted file mode 100644 index c5147bb..0000000 Binary files a/nvexpo/__pycache__/nvexpo.cpython-311.pyc and /dev/null differ diff --git a/nvexpo/consts.py b/nvexpo/consts.py new file mode 100644 index 0000000..a19dc47 --- /dev/null +++ b/nvexpo/consts.py @@ -0,0 +1,3 @@ +import re + +VARNAME_PATTERN = re.compile(r"^(\w|_|)+$") diff --git a/nvexpo/nvexpo.py b/nvexpo/nvexpo.py index c6f6b5c..6d10efe 100644 --- a/nvexpo/nvexpo.py +++ b/nvexpo/nvexpo.py @@ -1,8 +1,9 @@ -import os +from __future__ import annotations + import sys from enum import Enum from pathlib import Path -from typing import IO, Any, Dict, List +from typing import IO, Any ENV_VARS = Path.home() / ".nvexpo.env" @@ -11,6 +12,7 @@ class Mode(Enum): R = "r" W = "w" + class Cmd(Enum): Set = "export" Unset = "unset" @@ -19,7 +21,8 @@ class Cmd(Enum): def _get_file(mode: Mode) -> IO[Any]: return ENV_VARS.open(mode.value) -def _get_saved_vars() -> Dict[str, str]: + +def _get_saved_vars() -> dict[str, str]: output = {} with _get_file(Mode.R) as file: for line in file: @@ -30,31 +33,42 @@ def _get_saved_vars() -> Dict[str, str]: output[key] = value return output -def _save_vars(variables: Dict[str, str]): - new_variables = [f"{key}={value}" for key, value in variables.items()] + +def _save_vars(variables: dict[str, str]) -> None: + new_variables = [] + for key, value in variables.items(): + if "'" in value: + comma = '"' + elif '"' in value: + comma = "'" + else: + comma = "" + + new_variables.append(f"{key}={comma}{value}{comma}") with _get_file(Mode.W) as file: file.write("\n".join(new_variables)) -def _print_bulk(attrs: List[str], cmd: Cmd): +def _print_bulk(attrs: list[str], cmd: Cmd) -> None: for attr in attrs: print(f"{cmd.value} {attr}") -def _load(): + +def _load() -> None: current_vars = _get_saved_vars() attrs = [f"{key}={value}" for key, value in current_vars.items()] _print_bulk(attrs, Cmd.Set) -def set_env(variables: Dict[str, str]): +def set_env(variables: dict[str, str]) -> None: current_vars = _get_saved_vars() current_vars.update(variables) _save_vars(current_vars) _load() -def unset_env(variables: List[str]): +def unset_env(variables: list[str]) -> None: current_vars = _get_saved_vars() for var in variables: current_vars.pop(var, None) @@ -62,7 +76,7 @@ def unset_env(variables: List[str]): _print_bulk(variables, Cmd.Unset) -def help(error: None | str = None): +def show_help(error: None | str = None) -> None: msg = """ nx: Non-Volatile Export @@ -86,22 +100,24 @@ def help(error: None | str = None): hello world """.strip() msg = "\n".join(map(str.strip, msg.splitlines())) + if error is not None: + msg = f"Error: {error}\n\n" + msg print(msg, file=sys.stderr) -def init(shell: str | None = None): +def init(shell: str | None = None) -> None: if shell is None: shell = "bash" if shell == "bash": cmd = """ function nx(){ - eval "$(poetry run nvexpo $@)" + eval "$(nvexpo $@)" } """ print(cmd) _load() -if not os.path.exists(ENV_VARS): +if not ENV_VARS.exists(): _get_file(Mode.W).close() diff --git a/pyproject.toml b/pyproject.toml index 8e114ba..31d4756 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,7 +38,9 @@ ignore = [ "PLR0911", "T201", "PLR2004", - "FA102" + "FA102", + "PLW2901", + "N812" ] fixable = ["ALL"]