-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.py
More file actions
122 lines (89 loc) · 3.4 KB
/
utils.py
File metadata and controls
122 lines (89 loc) · 3.4 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import os
import pycountry
import re
from billingstack import exceptions
from billingstack.openstack.common import cfg
from billingstack.openstack.common import log
LOG = log.getLogger(__name__)
def find_config(config_path):
"""
Find a configuration file using the given hint.
Code nabbed from cinder.
:param config_path: Full or relative path to the config.
:returns: List of config paths
"""
possible_locations = [
config_path,
os.path.join(cfg.CONF.state_path, "etc", "billingstack", config_path),
os.path.join(cfg.CONF.state_path, "etc", config_path),
os.path.join(cfg.CONF.state_path, config_path),
"/etc/billingstack/%s" % config_path,
]
found_locations = []
for path in possible_locations:
LOG.debug('Searching for configuration at path: %s' % path)
if os.path.exists(path):
LOG.debug('Found configuration at path: %s' % path)
found_locations.append(os.path.abspath(path))
return found_locations
def read_config(prog, argv):
config_files = find_config('%s.conf' % prog)
cfg.CONF(argv[1:], project='billingstack', prog=prog,
default_config_files=config_files)
def capital_to_underscore(string):
return "_".join(l.lower() for l in re.findall('[A-Z][^A-Z]*',
string))
def underscore_to_capital(string):
return ''.join(x.capitalize() or '_' for x in string.split('_'))
def get_country(country_obj, **kw):
try:
obj = country_obj.get(**kw)
except KeyError:
raise exceptions.InvalidObject(errors=kw)
return dict([(k, v) for k, v in obj.__dict__.items() \
if not k.startswith('_')])
def get_currency(letter):
obj = get_country(pycountry.currencies, letter=letter.upper())
obj['letter'] = obj['letter'].lower()
del obj['numeric']
return obj
def get_language(letter):
obj = get_country(pycountry.languages, terminology=letter)
return {'letter': obj['terminology'].lower(), 'name': obj['name']}
def get_item_properties(item, fields, mixed_case_fields=[], formatters={}):
"""Return a tuple containing the item properties.
:param item: a single item resource (e.g. Server, Tenant, etc)
:param fields: tuple of strings with the desired field names
:param mixed_case_fields: tuple of field names to preserve case
:param formatters: dictionary mapping field names to callables
to format the values
"""
row = []
for field in fields:
if field in formatters:
row.append(formatters[field](item))
else:
if field in mixed_case_fields:
field_name = field.replace(' ', '_')
else:
field_name = field.lower().replace(' ', '_')
if not hasattr(item, field_name) and \
(isinstance(item, dict) and field_name in item):
data = item[field_name]
else:
data = getattr(item, field_name, '')
if data is None:
data = ''
row.append(data)
return tuple(row)
def get_columns(data):
"""
Some row's might have variable count of columns, ensure that we have the
same.
:param data: Results in [{}, {]}]
"""
columns = set()
def _seen(col):
columns.add(str(col))
map(lambda item: map(_seen, item.keys()), data)
return list(columns)