-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
73 lines (54 loc) · 1.84 KB
/
utils.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
import copy
import errno
import os
def flatten_commands(config):
config.setdefault('commands', {})
commands = config['commands']
for commandGroup in config.get('commandGroups', {}).values():
prefix = commandGroup.get('prefix', '')
suffix = commandGroup.get('suffix', '')
for key, value in commandGroup['commands'].items():
commands[prefix + key + suffix] = value
def merge_commands(config):
result = copy.deepcopy(config)
flatten_commands(result)
return result
def name_to_desc(name):
labels = name.split('_')
return ' '.join([label.capitalize() for label in labels])
def name_to_nls(name):
nls = ''.join(name.split('_')).upper()
return nls
def desc_to_name(desc):
name = '_'.join(desc.split(' ')).lower()
return name
def create_dir(fileName):
if not os.path.exists(os.path.dirname(fileName)):
try:
os.makedirs(os.path.dirname(fileName))
except OSError as error:
if error.errno != errno.EEXIST:
raise
def get_last_output(command, output, value_sets, searchSuffix):
prefix = command['code'].replace(searchSuffix, '')
keys = value_sets.get(command.get('value_set', ''), {})
output.reverse()
for line in output:
if line.startswith(prefix):
value = line[len(prefix):]
if (command.get('acceptsNumber', False) or
command.get('acceptsFloat', False)):
try:
float(value)
return value
except:
pass
if command.get('acceptsHex', False):
try:
int(value, 16)
return value
except:
pass
elif keys.get(value) is not None:
return value
return None