diff --git a/README.md b/README.md index db7858b..d2cb878 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ Works with Windows, MacOS and Linux. ## Python API ```python -from setenvironment import set_env_var, add_env_path, unset_env_var, remove_env_path, set_config_file +from setenvironment import ( + set_env_var, add_env_path, unset_env_var, remove_env_path, set_config_file, reload_environment, ... +) # by default, ~/.bashrc is used set_env_var("FOO", "BAR") get_env_var("FOO") # returns BAR @@ -26,18 +28,28 @@ set_env_var("FOO", "BAR") add_env_path("MYPATH") unset_env_var("FOO") remove_env_path("MYPATH") -# These template functions keeps your os.environ["PATH"] -# cleaner by grouping a bunch of paths into an expandable -# variable. This is particularly usefull to enable an -# uninstall feature. -# MYPATHKEY will show up in os.environ["PATHS"] -# as $MYPATHKEY (linux) or %MYPATHKEY% (win). -# The environmental variable MYPATHKEY will -# point to the path value. -add_template_path("MYPATHKEY", "/path/to/dir") -remove_template_path("MYPATHKEY", "/path/to/dir", remove_if_empty=True) -# Or elese you can just remove ALL of the paths at once. -remove_template_group("MYPATHKEY") +# Loads settings into the current environment. This reads the +# registry on windows or the ~/.bashrc file on unix. +reload_environment() +# Path groups are usefull for uninstall programs. Each add +# copies the path both into the PATH and also to the key. +# When you want to remove paths you can query the key and selectively +# remove paths that are in the set. +add_to_path_group("MYPATHKEY", "/path/to/dir") +remove_to_path_group("MYPATHKEY", "/path/to/dir") +# Or else you can just remove ALL of the paths at once. +remove_path_group("MYPATHKEY") +``` + +## Command line interface + +```bash +# Setting environmental variables +setenvironment set foo bar +setenvironment remove foo +# Adding and removing paths. +setenvironment addpath /my/path +setenvironment removepath /my/path ``` ## Github diff --git a/src/setenvironment/main.py b/src/setenvironment/main.py index 435ff9d..462f1b9 100644 --- a/src/setenvironment/main.py +++ b/src/setenvironment/main.py @@ -61,7 +61,7 @@ def parse_args() -> argparse.Namespace: "get", help="Get the value of an environment variable" ) parser_get.add_argument("key", help="Name of the environment variable") - parser_unset = subparsers.add_parser("unset", help="Unset an environment variable") + parser_unset = subparsers.add_parser("remove", help="Unset an environment variable") parser_unset.add_argument("key", help="Name of the environment variable") parser_addpath = subparsers.add_parser( "addpath", help="Add a path to the PATH environment variable" @@ -85,7 +85,7 @@ def main() -> int: do_set(args.key, args.value) elif args.mode == "get": print(do_get(args.key)) - elif args.mode == "unset": + elif args.mode == "remove": do_unset(args.key) elif args.mode == "addpath": do_addpath(args.path) diff --git a/src/setenvironment/setenv.py b/src/setenvironment/setenv.py index 955cf24..9fca553 100644 --- a/src/setenvironment/setenv.py +++ b/src/setenvironment/setenv.py @@ -163,14 +163,14 @@ def add_to_path_group(group_name: str, new_path: str) -> None: unix_add_path_group(group_name, new_path) -def remove_to_path_group(group_name: str, path_to_remove: str) -> None: +def remove_from_path_group(group_name: str, path_to_remove: str) -> None: """Removes a path from the PATH environment variable.""" if _IS_WINDOWS: - from .setenv_win32 import remove_to_path_group as win32_remove_path_group + from .setenv_win32 import remove_from_path_group as win32_remove_path_group win32_remove_path_group(group_name, path_to_remove) else: - from .setenv_unix import remove_to_path_group as unix_remove_path_group + from .setenv_unix import remove_from_path_group as unix_remove_path_group unix_remove_path_group(group_name, path_to_remove) diff --git a/src/setenvironment/setenv_unix.py b/src/setenvironment/setenv_unix.py index 2b92b02..2535835 100644 --- a/src/setenvironment/setenv_unix.py +++ b/src/setenvironment/setenv_unix.py @@ -158,7 +158,7 @@ def get_env() -> Environment: return combine_environments(parent=shell_env, child=bash_env) -def remove_to_path_group(group_name: str, path_to_remove: str) -> None: +def remove_from_path_group(group_name: str, path_to_remove: str) -> None: assert group_name != "PATH" env: BashEnvironment = bash_make_environment() os_env: OsEnvironment = OsEnvironment() diff --git a/src/setenvironment/setenv_win32.py b/src/setenvironment/setenv_win32.py index 44c6ca3..7e2f1c9 100644 --- a/src/setenvironment/setenv_win32.py +++ b/src/setenvironment/setenv_win32.py @@ -261,7 +261,7 @@ def add_path_group(group_name: str, new_path: str) -> None: win32_registry_save(env.user) -def remove_to_path_group(group_name: str, path_to_remove: str) -> None: +def remove_from_path_group(group_name: str, path_to_remove: str) -> None: assert group_name != "PATH" env: RegistryEnvironment = query_registry_environment() os_env: OsEnvironment = os_env_make_environment() diff --git a/src/setenvironment/types.py b/src/setenvironment/types.py index 7836d9a..2532fcc 100644 --- a/src/setenvironment/types.py +++ b/src/setenvironment/types.py @@ -3,6 +3,7 @@ CLEAR_ENVIRONMENT = True + @dataclass class Environment: vars: dict[str, str] diff --git a/tests/test_environment.py b/tests/test_environment.py index 20c8621..f678c82 100644 --- a/tests/test_environment.py +++ b/tests/test_environment.py @@ -19,7 +19,6 @@ from setenvironment.testing.basetest import BaseTest from setenvironment.types import Environment, OsEnvironment - MY_PATH = os.path.join("setenvironment", "test", "path2") MY_VAR = ("SET_ENVIRONMENT_TEST_ENV_VAR", "foo") diff --git a/tests/test_os_env.py b/tests/test_os_env.py index 9c4eef1..c96b034 100644 --- a/tests/test_os_env.py +++ b/tests/test_os_env.py @@ -24,7 +24,7 @@ def test_os_make_environment(self) -> None: if path not in paths: print("path not in paths: " + path) data.pop("PATH", None) - items= sorted(data.items()) + items = sorted(data.items()) print("VARS:") for key, value in items: print(f" {key}={value}") @@ -32,7 +32,6 @@ def test_os_make_environment(self) -> None: for path in os_env.paths: print(f" {path}") self.fail("Force test_os_make_environment crash to read output\n" + str(os_env)) - def test_os_env_save_works(self) -> None: """Test that making and saving an os environment works.""" diff --git a/tests/test_osenv_sanity.py b/tests/test_osenv_sanity.py index 225cac0..8299842 100644 --- a/tests/test_osenv_sanity.py +++ b/tests/test_osenv_sanity.py @@ -14,6 +14,7 @@ class OsEnvironmentalSanity(BaseTest): """Tester for the main module.""" + def test_OsEnvironSanity(self) -> None: env = OsEnvironment() print(env) @@ -21,6 +22,5 @@ def test_OsEnvironSanity(self) -> None: self.assertTrue(len(env.vars) > 5) - if __name__ == "__main__": unittest.main() diff --git a/tests/test_path_group.py b/tests/test_path_group.py index 706bdec..a118cc6 100644 --- a/tests/test_path_group.py +++ b/tests/test_path_group.py @@ -13,8 +13,8 @@ add_to_path_group, get_env, reload_environment, + remove_from_path_group, remove_path_group, - remove_to_path_group, ) from setenvironment.testing.basetest import BaseTest from setenvironment.types import Environment, OsEnvironment @@ -79,7 +79,7 @@ def test_add_group_path(self) -> None: print(exc) raise exc finally: - remove_to_path_group(KEY, VAL) + remove_from_path_group(KEY, VAL) # now test the system environment to ensure they are gone env: OsEnvironment = OsEnvironment() self.assertNotIn(VAL, env.paths) diff --git a/tests/test_reload.py b/tests/test_reload.py index 441e9a5..228ebd0 100644 --- a/tests/test_reload.py +++ b/tests/test_reload.py @@ -6,42 +6,38 @@ from setenvironment import ( add_env_path, get_env, + get_env_var, + get_paths, reload_environment, remove_env_path, set_env_var, unset_env_var, - get_env_var, - get_paths, ) from setenvironment.testing.basetest import BaseTest from setenvironment.types import Environment, OsEnvironment -class EnvironmentTester(BaseTest): +class EnvironmentTester(BaseTest): def test_variable(self) -> None: """Tests that we can add an environmental variable and then reload it.""" # Sanity check. - #paths = os.environ["PATH"].split(os.pathsep) - #self.assertNotIn(MY_PATH, paths) - #add_env_path(MY_PATH) + # paths = os.environ["PATH"].split(os.pathsep) + # self.assertNotIn(MY_PATH, paths) + # add_env_path(MY_PATH) set_env_var("FOO", "BAR") reload_environment() self.assertEqual(get_env_var("FOO"), "BAR") - - def test_path(self) -> None: """Tests that we can add an environmental variable and then reload it.""" # Sanity check. - #paths = os.environ["PATH"].split(os.pathsep) - #self.assertNotIn(MY_PATH, paths) - #add_env_path(MY_PATH) + # paths = os.environ["PATH"].split(os.pathsep) + # self.assertNotIn(MY_PATH, paths) + # add_env_path(MY_PATH) add_env_path("MY_PATH") reload_environment() self.assertIn("MY_PATH", get_paths()) - - if __name__ == "__main__": unittest.main() diff --git a/tests/test_sanity.py b/tests/test_sanity.py index dc00cc4..f59b209 100644 --- a/tests/test_sanity.py +++ b/tests/test_sanity.py @@ -31,6 +31,5 @@ def test_get_env(self) -> None: self.assertGreater(total_items, 10) - if __name__ == "__main__": unittest.main() diff --git a/tests/test_win.py b/tests/test_win.py index d48ebea..f9b75b6 100644 --- a/tests/test_win.py +++ b/tests/test_win.py @@ -52,7 +52,6 @@ def test_get_env_path_system_registry(self) -> None: self.assertIn("C:\\Windows", path_str) print("done") - @unittest.skipIf(sys.platform != "win32", "Windows only test") def test_temp_dir_is_resolved(self) -> None: """Test setting an environment variable."""