forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
192 lines (141 loc) · 4.9 KB
/
utils.py
File metadata and controls
192 lines (141 loc) · 4.9 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
SoftLayer.utils
~~~~~~~~~~~~~~~
Utility function/classes.
:license: MIT, see LICENSE for more details.
"""
import datetime
import re
import six
# pylint: disable=pointless-except, no-member, invalid-name
UUID_RE = re.compile(r'^[0-9a-f\-]{36}$', re.I)
KNOWN_OPERATIONS = ['<=', '>=', '<', '>', '~', '!~', '*=', '^=', '$=', '_=']
configparser = six.moves.configparser
string_types = six.string_types
StringIO = six.StringIO
xmlrpc_client = six.moves.xmlrpc_client
def lookup(dic, key, *keys):
"""A generic dictionary access helper.
This helps simplify code that uses heavily nested dictionaries. It will
return None if any of the keys in *keys do not exist.
::
>>> lookup({'this': {'is': 'nested'}}, 'this', 'is')
nested
>>> lookup({}, 'this', 'is')
None
"""
if keys:
return lookup(dic.get(key, {}), *keys)
return dic.get(key)
class NestedDict(dict):
"""This helps with accessing a heavily nested dictionary.
Dictionary where accessing keys that don't exist will return another
NestedDict object.
"""
def __getitem__(self, key):
if key in self:
return self.get(key)
return self.setdefault(key, NestedDict())
def to_dict(self):
"""Converts a NestedDict instance into a real dictionary.
This is needed for places where strict type checking is done.
"""
new_dict = {}
for key, val in self.items():
if isinstance(val, NestedDict):
new_dict[key] = val.to_dict()
else:
new_dict[key] = val
return new_dict
def query_filter(query):
"""Translate a query-style string to a 'filter'.
Query can be the following formats:
Case Insensitive
'value' OR '*= value' Contains
'value*' OR '^= value' Begins with value
'*value' OR '$= value' Ends with value
'*value*' OR '_= value' Contains value
Case Sensitive
'~ value' Contains
'!~ value' Does not contain
'> value' Greater than value
'< value' Less than value
'>= value' Greater than or equal to value
'<= value' Less than or equal to value
:param string query: query string
"""
try:
return {'operation': int(query)}
except ValueError:
pass
if isinstance(query, string_types):
query = query.strip()
for operation in KNOWN_OPERATIONS:
if query.startswith(operation):
query = "%s %s" % (operation, query[len(operation):].strip())
return {'operation': query}
if query.startswith('*') and query.endswith('*'):
query = "*= %s" % query.strip('*')
elif query.startswith('*'):
query = "$= %s" % query.strip('*')
elif query.endswith('*'):
query = "^= %s" % query.strip('*')
else:
query = "_= %s" % query
return {'operation': query}
def query_filter_date(start, end):
"""Query filters given start and end date.
:param start:YY-MM-DD
:param end: YY-MM-DD
"""
sdate = datetime.datetime.strptime(start, "%Y-%m-%d")
edate = datetime.datetime.strptime(end, "%Y-%m-%d")
startdate = "%s/%s/%s" % (sdate.month, sdate.day, sdate.year)
enddate = "%s/%s/%s" % (edate.month, edate.day, edate.year)
return {
'operation': 'betweenDate',
'options': [
{'name': 'startDate', 'value': [startdate+' 0:0:0']},
{'name': 'endDate', 'value': [enddate+' 0:0:0']}
]
}
class IdentifierMixin(object):
"""Mixin used to resolve ids from other names of objects.
This mixin provides an interface to provide multiple methods for
converting an 'indentifier' to an id
"""
resolvers = []
def resolve_ids(self, identifier):
"""Takes a string and tries to resolve to a list of matching ids.
What exactly 'identifier' can be depends on the resolvers
:param string identifier: identifying string
:returns list:
"""
return resolve_ids(identifier, self.resolvers)
def resolve_ids(identifier, resolvers):
"""Resolves IDs given a list of functions.
:param string identifier: identifier string
:param list resolvers: a list of functions
:returns list:
"""
# Before doing anything, let's see if this is an integer
try:
return [int(identifier)]
except ValueError:
pass # It was worth a shot
# This looks like a globalIdentifier (UUID)
if len(identifier) == 36 and UUID_RE.match(identifier):
return [identifier]
for resolver in resolvers:
ids = resolver(identifier)
if ids:
return ids
return []
class UTC(datetime.tzinfo):
"""UTC timezone."""
def utcoffset(self, _):
return datetime.timedelta(0)
def tzname(self, _):
return "UTC"
def dst(self, _):
return datetime.timedelta(0)