5 changed files with 92 additions and 4 deletions
@ -1,4 +1,5 @@ |
|||
.envrc |
|||
__pycache__ |
|||
venv |
|||
.mypy_cache |
|||
deletefb.log |
|||
|
@ -1,4 +1,5 @@ |
|||
settings = { |
|||
"ARCHIVE" : True, |
|||
"MAX_POSTS" : 5000 |
|||
"MAX_POSTS" : 5000, |
|||
"RETRY_THRESHOLD": 5 |
|||
} |
|||
|
@ -0,0 +1,50 @@ |
|||
#!/usr/bin/env python3 |
|||
|
|||
import sys |
|||
import os |
|||
import re |
|||
import typing as ty |
|||
|
|||
from types import SimpleNamespace |
|||
|
|||
|
|||
class EnvVar: |
|||
![]()
Review
Could you make it into an attrs class, similar to the other ones in https://github.com/weskerfoot/DeleteFB/blob/master/deletefb/types.py ? |
|||
def __init__(self, |
|||
env_key: str, |
|||
dest: ty.Optional[str] = None, |
|||
type: ty.Type = str, |
|||
default: ty.Optional[str] = None, |
|||
help: ty.Optional[str] = None): |
|||
|
|||
assert re.match('^[a-zA-Z_]+[a-zA-Z0-9_]*$', env_key), "Bad env key {0}".format(env_key) |
|||
![]()
Review
Can we change it to something like this, for clarity?
|
|||
self.key = env_key |
|||
self.dest = dest or env_key.lower() |
|||
self.type = type |
|||
self.help = help |
|||
self.default = default |
|||
|
|||
|
|||
class EnvParser: |
|||
def __init__(self): |
|||
self.env_vars = set() |
|||
|
|||
def add_argument(self, *args, **kwargs): |
|||
self.env_vars.add(EnvVar(*args, **kwargs)) |
|||
|
|||
def print_help(self): |
|||
print("environment variables:") |
|||
for v in self.env_vars: |
|||
print(" {0}=<{1}>\t{2}".format(v.key, v.type.__name__, v.help or '')) |
|||
|
|||
def parse_args(self): |
|||
args = {} |
|||
for v in self.env_vars: |
|||
try: |
|||
args[v.dest] = v.type(os.environ.get(v.key, v.default)) |
|||
except ValueError: |
|||
print("{0}: error: environment variable {1} should be of type {2}".format( |
|||
sys.argv[0].split('/')[-1], |
|||
v.key, |
|||
v.type.__name__)) |
|||
sys.exit(1) |
|||
return SimpleNamespace(**args) |
Loading…
Reference in new issue
Let's change it to
"Print out environment variable usage"