-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
58 lines (52 loc) · 2.04 KB
/
__init__.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
import os, sys
from datetime import timedelta
import logging
import yaml
DEBUG = os.environ.get('DEBUG', False)
DIR = os.path.dirname(os.path.abspath(__file__))
CONF_PATH = os.environ.get('SUPR_CONF', 'supr.yaml')
DB_PATH = os.environ.get('SUPR_DB', '.supr.db')
LOG_PATH = os.environ.get('SUPR_LOG', '.supr.log')
CONF_DEFAULTS = {
# `stop` idle hosts after a period of activity. default: disabled
'IDLE_TIMEOUT': None,
# rsync exlcude patterns used when installing with `local:`
'LOCAL_EXCLUDE': ('.env', '.git', '.github'),
# packages needed to support supr features
'APT_ESSENTIAL': ('rsync', 's3fs', 'git-core', 'python3', 'python3-pip', 'python3-venv'),
'PIP_ESSENTIAL': ('wheel', 'setuptools'),
}
ANSI = dict([
(v, "\033[%sm" % i) for v, i in \
list(zip(['RST', 'BLD', 'DIM', 'ITL', 'UDL', 'SBL', 'FBL', 'REV'], range(8))) + \
list(zip(['BLK', 'RED', 'GRN', 'YEL', 'BLU', 'PNK', 'CYN', 'WHT'], range(30, 38)))
])
class conf(dict):
@classmethod
def load(cls, filename):
return yaml.load(open(filename), Loader=yaml.Loader)
def __str__(self):
return yaml.dump(self, default_flow_style=False, Dumper=yaml.SafeDumper)
def __getattr__(self, key):
return self[key]
def __getitem__(self, key):
assert key in self, f'"{key}" not found'
return super().__getitem__(key)
yaml.Loader.add_constructor('tag:yaml.org,2002:map', lambda l, n: conf(l.construct_mapping(n)))
yaml.SafeDumper.add_representer(conf, lambda d, x: d.represent_dict(x))
CONF = {**CONF_DEFAULTS, **conf.load(CONF_PATH)}
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG if DEBUG else logging.INFO)
log.addHandler(logging.FileHandler(LOG_PATH))
from supr.state import localstate
state = localstate(DB_PATH, CONF['IDLE_TIMEOUT'])
if 'local' in CONF:
from supr.backend import local
backend = local.backend()
instance = local.instance
elif 'aws' in CONF:
from supr.backend import aws
backend = aws.backend()
instance = aws.instance
else:
assert False, "no backend configured"