Package yakumo :: Module utils
[hide private]
[frames] | no frames]

Source Code for Module yakumo.utils

  1  # Copyright 2014-2017 by Akira Yoshiyama <akirayoshiyama@gmail.com>. 
  2  # All Rights Reserved. 
  3  # 
  4  #    Licensed under the Apache License, Version 2.0 (the "License"); you may 
  5  #    not use this file except in compliance with the License. You may obtain 
  6  #    a copy of the License at 
  7  # 
  8  #         http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  #    Unless required by applicable law or agreed to in writing, software 
 11  #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
 12  #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
 13  #    License for the specific language governing permissions and limitations 
 14  #    under the License. 
 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   
41 -def get_client():
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
55 -def join_path(*args):
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
71 -def str2bool(value):
72 if value == u'true': 73 return True 74 if value == u'false': 75 return False
76 77
78 -def bool2str(value):
79 if value is True: 80 return u'true' 81 if value is False: 82 return u'false'
83 84
85 -def gen_chunk(file):
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
94 -class Completer(rlcompleter.Completer):
95 96 PATTERN = re.compile(r"(\w+(\.\w+)*)\.(\w*)") 97
98 - def attr_matches(self, text):
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 # get the content of the object, except __builtins__ 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