-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.py
More file actions
77 lines (62 loc) · 2.49 KB
/
config.py
File metadata and controls
77 lines (62 loc) · 2.49 KB
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
import yaml
import re
import click
import os.path
import subprocess
from os.path import basename, dirname
from collections import defaultdict
from functools import lru_cache
import config
class Config():
def __init__(self, path):
self.path = path
def _read_data():
with open(self.path, 'r') as stream:
return yaml.load(stream) or {}
if os.path.isfile(path):
self.data = _read_data()
else:
self.data = defaultdict(lambda: 'File not found')
@lru_cache()
def cache(*a, **kw): return Config(*a, **kw)
def name(self):
prefix = (self.additional_namespace() + ' ' if self.additional_namespace() else '')
last_name = basename(dirname(self.path))
return "{}{}".format(prefix, last_name)
def names(self):
return self.name().split()
def additional_namespace(self):
top_path = dirname(dirname(self.path))
regex_expr = re.escape(config.TOOL_DIR) + r'(\/.*?$)'
result = re.search(regex_expr, top_path)
namespace_path = result.group(1) if result else ''
return namespace_path.translate(namespace_path.maketrans('/', ' ')).strip()
def command(self):
default = 'MISSING: Command for {}'.format(self.name())
self.data.setdefault('command', default)
if not self.data['command']: self.data['command'] = default
return self.data['command']
def help(self):
default = 'MISSING: Help for {}'.format(self.name())
self.data.setdefault('help', default)
if not self.data['help']: self.data['help'] = default
return self.data['help']
def args(self):
if self.arguments: return [str(a).lower() for a in self.arguments]
else: return []
# TODO: Deprecated, avoid usage
def interactive_only(self):
return self.interactive()
def __getattr__(self, attr):
return self.data.setdefault(attr)
# TODO: Use __getattr__ here
def interactive(self):
return 'interactive' in self.data and self.data['interactive'] == True
def command_exists(self):
return 'command' in self.data and self.data['command']
def working_files(self):
ls_cmd = 'ls {}'.format(dirname(self.path))
stdout, _err = subprocess.Popen(ls_cmd, stdout=subprocess.PIPE, shell=True)\
.communicate()
working_files = stdout.decode("utf-8").rstrip('\n').split('\n')
return list(map(lambda f: "./{}".format(f), working_files))