Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ setenv -l

## How to use as Python package
### Get variable
Get value of variable _my_var_
Get value of variable _my_var_
Note Throws error KeyError when Enviroment variable is not found
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Note Throws error KeyError when Enviroment variable is not found
> Note: Throws error `KeyError` when Environment variable is not found
>

```python
from py_setenv import setenv
setenv("my_var")
Expand Down
33 changes: 16 additions & 17 deletions py_setenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,11 @@

@click.command()
@click.argument("name", required=False)
@click.option("-v", "--value", required=False, default=None,
help="Variable value")
@click.option("-u", "--user", is_flag=True, required=False,
help="Specifies if configure user environment")
@click.option("-a", "--append", is_flag=True, required=False,
help="Appends to/Creates environment variable")
@click.option("-d", "--delete", is_flag=True, required=False,
help="Deletes environment variable")
@click.option("-l", "--list-all", is_flag=True, required=False,
help="Lists all environment variables")
@click.option("-v", "--value", required=False, default=None, help="Variable value")
@click.option("-u", "--user", is_flag=True, required=False, help="Specifies if configure user environment")
@click.option("-a", "--append", is_flag=True, required=False, help="Appends to/Creates environment variable")
@click.option("-d", "--delete", is_flag=True, required=False, help="Deletes environment variable")
@click.option("-l", "--list-all", is_flag=True, required=False, help="Lists all environment variables")
def click_command(name, value, user, append, delete, list_all):
"""
Utility to set/get/modify/delete windows environment variables via registry
Expand All @@ -34,6 +29,7 @@ def click_command(name, value, user, append, delete, list_all):
"""
setenv(name, value, user, append, delete, list_all)


def setenv(name="", value=None, user=False, append=False, delete=False, list_all=False, suppress_echo=False):
if list_all:
result = list_all_variables(user)
Expand All @@ -52,7 +48,7 @@ def setenv(name="", value=None, user=False, append=False, delete=False, list_all

if append:
if value is not None:
result = append_variable(name, value, user)
result = append_variable(name, value, user, suppress_echo)
else:
if not suppress_echo:
click.echo("No value is provided in append mode", err=True)
Expand All @@ -63,7 +59,7 @@ def setenv(name="", value=None, user=False, append=False, delete=False, list_all
elif delete:
result = delete_variable(name, user)
else:
result = get_variable(name, user)
result = get_variable(name, user, suppress_echo)

if not suppress_echo:
click.echo(result)
Expand All @@ -83,11 +79,11 @@ def set_variable(name, value, user):
return False


def append_variable(name, value, user):
def append_variable(name, value, user, suppress_echo):
"""
Creates/appends environment variable
"""
new_val = get_variable(name=name, user=user)
new_val = get_variable(name, user, suppress_echo)
if new_val:
new_val += ";" + value
else:
Expand All @@ -97,7 +93,7 @@ def append_variable(name, value, user):
return result


def get_variable(name, user):
def get_variable(name, user, suppress_echo):
"""
Gets the value of environment variable
"""
Expand All @@ -107,8 +103,10 @@ def get_variable(name, user):
value, regtype = winreg.QueryValueEx(key, name)
return value
except WindowsError:
click.echo("Environment Variable '{}' does not exist".format(name))
return ""
if not suppress_echo:
click.echo("Environment Variable '{}' does not exist".format(name))
raise KeyError
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some concerns how this will work in CLI implementation. It will be a long unreadable traceback.

I think we need to raise a KeyError for Python module and just return empty and error message in CLI

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also unittests fail with current implementation. Need to fix it as well



def delete_variable(name, user):
"""
Expand All @@ -122,6 +120,7 @@ def delete_variable(name, user):
except WindowsError:
return False


def list_all_variables(user):
hkey = user_hkey if user else system_hkey
all_vars = {}
Expand Down
4 changes: 3 additions & 1 deletion tests/test_setenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def test_02_system_environment(self):
def run_checks(self, user):
# check that variable does not exist before start
user_list = ["--user"] if user else []

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

result = self.runner.invoke(click_command, ["unittest"] + user_list)
self.assertEqual("Environment Variable 'unittest' does not exist", result.output.strip())

Expand All @@ -42,7 +43,8 @@ def run_checks(self, user):
# test that new text was appended
result = self.runner.invoke(click_command, ["unittest", "-v", "appended_str", "-a"] + user_list)
self.assertTrue(bool(result.output.strip()))


#
Copy link
Owner

@beliaev-maksim beliaev-maksim Jan 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#

result = self.runner.invoke(click_command, ["unittest"] + user_list)
self.assertEqual("test_str;appended_str", result.output.strip())

Expand Down