Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
zackees committed Oct 15, 2023
1 parent 00088b8 commit f059033
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 41 deletions.
38 changes: 25 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/setenvironment/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions src/setenvironment/setenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/setenvironment/setenv_unix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/setenvironment/setenv_win32.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions src/setenvironment/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

CLEAR_ENVIRONMENT = True


@dataclass
class Environment:
vars: dict[str, str]
Expand Down
1 change: 0 additions & 1 deletion tests/test_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
3 changes: 1 addition & 2 deletions tests/test_os_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,14 @@ 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}")
print("PATHS:")
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."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_osenv_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@

class OsEnvironmentalSanity(BaseTest):
"""Tester for the main module."""

def test_OsEnvironSanity(self) -> None:
env = OsEnvironment()
print(env)
self.assertTrue(len(env.paths) > 5)
self.assertTrue(len(env.vars) > 5)



if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions tests/test_path_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 9 additions & 13 deletions tests/test_reload.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 0 additions & 1 deletion tests/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ def test_get_env(self) -> None:
self.assertGreater(total_items, 10)



if __name__ == "__main__":
unittest.main()
1 change: 0 additions & 1 deletion tests/test_win.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down

0 comments on commit f059033

Please sign in to comment.