1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """
17 Miscellaneous functions/decorators
18 """
19
20 import argparse
21 import os
22 import rlcompleter
23 import re
24 import sys
25
26 import os_client_config
27 import yakumo
28
29
30 ENVIRONMENT_VARIABLES = {
31 'os_cloud': 'OS_CLOUD',
32 'os_cert': 'OS_CERT',
33 'os_cacert': 'OS_CACERT',
34 'os_region_name': 'OS_REGION_NAME',
35 'os_interface': 'OS_INTERFACE',
36 'os_key': 'OS_KEY',
37 'os_auth_type': 'OS_AUTH_TYPE',
38 }
39
40
42 kwargs = {dest: os.environ.get(env)
43 for dest, env in ENVIRONMENT_VARIABLES.items()}
44 parser = argparse.ArgumentParser()
45 cloud_config = os_client_config.OpenStackConfig()
46 cloud_config.register_argparse_arguments(parser, sys.argv)
47 for opt in parser._actions:
48 if opt.dest in ENVIRONMENT_VARIABLES:
49 opt.metavar = ENVIRONMENT_VARIABLES[opt.dest]
50 parser.set_defaults(timeout=None, insecure=False, **kwargs)
51 options = parser.parse_args()
52 return yakumo.Client(**options.__dict__)
53
54
56 return '/'.join([str(x).strip('/') for x in args if x is not None])
57
58
59 -def get_json_body(base, **params):
60 data = {}
61 if not params:
62 return {base: None}
63 for key, value in params.items():
64 if value is not None:
65 data[key] = value
66 if not data:
67 data = None
68 return {base: data}
69
70
72 if value == u'true':
73 return True
74 if value == u'false':
75 return False
76
77
79 if value is True:
80 return u'true'
81 if value is False:
82 return u'false'
83
84
86 with open(file, 'rb') as f:
87 while True:
88 chunk = f.read(4096)
89 if not chunk:
90 break
91 yield chunk
92
93
95
96 PATTERN = re.compile(r"(\w+(\.\w+)*)\.(\w*)")
97
99 """
100 Derived from rlcompleter.Completer.attr_matches()
101 """
102 m = self.PATTERN.match(text)
103 if not m:
104 return []
105 expr, attr = m.group(1, 3)
106 try:
107 thisobject = eval(expr, self.namespace)
108 except Exception:
109 return []
110
111
112 words = dir(thisobject)
113 if "__builtins__" in words:
114 words.remove("__builtins__")
115
116 if hasattr(thisobject, '__class__'):
117 words.append('__class__')
118 words.extend(rlcompleter.get_class_members(thisobject.__class__))
119 matches = []
120 n = len(attr)
121 for word in words:
122 if attr == '' and word[0] == '_':
123 continue
124 if word[:n] == attr and hasattr(thisobject, word):
125 val = getattr(thisobject, word)
126 word = self._callable_postfix(val, "%s.%s" % (expr, word))
127 matches.append(word)
128 return matches
129