/
envs.py
101 lines (75 loc) · 3.09 KB
/
envs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""Environment variable hooks."""
import logging
import os
import platform
from tackle import BaseHook, Field
logger = logging.getLogger(__name__)
class GetEnvHook(BaseHook):
"""Hook for getting environment variables."""
hook_name = 'get_env'
environment_variable: str = Field(
None,
description="Dict for setting and string for getting environment variables",
)
fallback: str = Field(None, description="A fallback for getting.")
args: list = ['environment_variable', 'fallback']
def exec(self):
"""Set env vars."""
if platform.system() == 'Windows':
from tackle.exceptions import ContributionNeededException
raise ContributionNeededException()
# robcxyz - Not working on windows support but would love PRs
else:
return os.getenv(self.environment_variable, self.fallback)
class EnvironmentVariableHook(BaseHook):
"""Hook for setting environment variables."""
hook_name = 'set_env'
environment_variable: str = Field(
..., description="The name of the environment variable to set."
)
value: str = Field(None, description="The value to set it.")
args: list = ['environment_variable', 'value']
def exec(self):
"""Get or set env vars."""
if platform.system() == 'Windows':
from tackle.exceptions import ContributionNeededException
raise ContributionNeededException()
# robcxyz - Not working on windows support but would love PRs
else:
# TODO: Implement parsing for strings to set -> export THING=stuff
os.environ[self.environment_variable] = self.value
return self.value
class ExportHook(BaseHook):
"""Hook for setting environment variables that returns None."""
hook_name = 'export'
environment_variable: str = Field(
..., description="The name of the environment variable to set."
)
value: str = Field(None, description="The value to set it.")
args: list = ['environment_variable', 'value']
# TODO: Implement parsing for strings to set -> <: export THING=stuff
def exec(self):
"""Set env vars."""
if platform.system() == 'Windows':
from tackle.exceptions import ContributionNeededException
raise ContributionNeededException()
# robcxyz - Not working on windows support but would love PRs
else:
os.environ[self.environment_variable] = self.value
return
class UnsetHook(BaseHook):
"""Hook for unsetting environment variables."""
hook_name = 'unset'
environment_variable: str = Field(
..., description="The name of the environment variable to set."
)
args: list = ['environment_variable']
def exec(self):
"""Set env vars."""
if platform.system() == 'Windows':
from tackle.exceptions import ContributionNeededException
raise ContributionNeededException()
# robcxyz - Not working on windows support but would love PRs
else:
os.unsetenv(self.environment_variable)
return