From 40c64601f9df96f247957de647e19679a4e03f0e Mon Sep 17 00:00:00 2001 From: Atsushi Sasaki Date: Wed, 23 Mar 2016 19:19:54 +0900 Subject: [PATCH] reformat according to pep8 --- cloudstack/compute/__init__.py | 35 +++++++--- cloudstack/compute/client.py | 23 +++---- cloudstack/compute/shell.py | 117 ++++++++++++++++++++------------- cloudstack/compute/stack.py | 42 +++++++----- cloudstack/ppj.py | 4 +- cloudstack/utils.py | 42 +++++++----- setup.py | 10 +-- 7 files changed, 164 insertions(+), 109 deletions(-) diff --git a/cloudstack/compute/__init__.py b/cloudstack/compute/__init__.py index 06ceecd..b76f930 100644 --- a/cloudstack/compute/__init__.py +++ b/cloudstack/compute/__init__.py @@ -4,10 +4,13 @@ import urllib import sys + def valid_methods(): return [r['name'] for r in API_REFS] + class Method(object): + def __init__(self, command, method_name): self.command = command self.method_name = method_name @@ -25,30 +28,42 @@ def __call__(self, **kwargs): print '[%s] option is required.' % ', '.join(list(missing)) return - params = dict([(k,v) for (k,v) in kwargs.items() - for opt in options - if opt['option'][2:] == k]) + params = dict([(k, v) for (k, v) in kwargs.items() + for opt in options + if opt['option'][2:] == k]) - json = client.connect(host=self.command.host,api_key=self.command.api_key, - secret_key=self.command.secret_key,debug=self.command.debug - ).get(self.method_name,params) + json = client.connect( + host=self.command.host, + api_key=self.command.api_key, + secret_key=self.command.secret_key, + debug=self.command.debug).get( + self.method_name, + params) if json: retval = dict2obj(json[json.keys()[0]]) - if retval and hasattr(retval,'list'): + if retval and hasattr(retval, 'list'): return retval.list else: return retval + class Compute(object): - def __init__(self,host=None,api_key=None,secret_key=None,api_refs_json=None,debug=False): + + def __init__( + self, + host=None, + api_key=None, + secret_key=None, + api_refs_json=None, + debug=False): self.host = host self.api_key = api_key self.secret_key = secret_key self.api_refs_json = api_refs_json self.debug = debug - def __getattr__(self,method_name): - return Method(self,method_name) + def __getattr__(self, method_name): + return Method(self, method_name) def methods(self): return valid_methods() diff --git a/cloudstack/compute/client.py b/cloudstack/compute/client.py index b1f77b8..e12b85b 100644 --- a/cloudstack/compute/client.py +++ b/cloudstack/compute/client.py @@ -11,12 +11,13 @@ from cloudstack.utils import safe_option from stack import Stack -API_CONF_PATH = os.path.join(os.path.expanduser("~"),".idcfrc") +API_CONF_PATH = os.path.join(os.path.expanduser("~"), ".idcfrc") logging.basicConfig(format="%(asctime)s %(module)s[%(lineno)d] [%(levelname)s]: %(message)s", #filename = "log.txt", level=logging.INFO) -def connect(host=None,api_key=None,secret_key=None,debug=False): + +def connect(host=None, api_key=None, secret_key=None, debug=False): if debug: httplib2.debuglevel = 1 logging.getLogger().setLevel(logging.DEBUG) @@ -32,27 +33,27 @@ def connect(host=None,api_key=None,secret_key=None,debug=False): if not host: host = os.environ.get('IDCF_COMPUTE_HOST') if not host: - host = safe_option(config,"account", "host") + host = safe_option(config, "account", "host") if not api_key: api_key = os.environ.get('IDCF_COMPUTE_API_KEY') if not api_key: - api_key = safe_option(config,"account", "api_key") + api_key = safe_option(config, "account", "api_key") if not secret_key: secret_key = os.environ.get('IDCF_COMPUTE_SECRET_KEY') if not secret_key: - secret_key = safe_option(config,"account","secret_key") + secret_key = safe_option(config, "account", "secret_key") - except ConfigParser.NoSectionError, e: + except ConfigParser.NoSectionError as e: print >> sys.stderr, e.message #f = open(API_CONF_PATH,"w") - #config.add_section("account") + # config.add_section("account") #config.set("account", "host","http://xxx") #config.set("account", "api_key","xxx") - #config.set("account","secret_key","xxx") - #config.write(f) + # config.set("account","secret_key","xxx") + # config.write(f) sys.exit(1) - except Exception, e: + except Exception as e: print >> sys.stderr, e sys.exit(1) - return Stack(http,host,api_key,secret_key) + return Stack(http, host, api_key, secret_key) diff --git a/cloudstack/compute/shell.py b/cloudstack/compute/shell.py index 7f18a03..e087a8f 100644 --- a/cloudstack/compute/shell.py +++ b/cloudstack/compute/shell.py @@ -24,16 +24,18 @@ api_refs_json_path = os.environ.get('IDCF_COMPUTE_API_REFS_JSON') if not api_refs_json_path: api_refs_json_path = config.get("account", "api_refs_json") - api_refs = simplejson.load(open(api_refs_json_path,"r")) + api_refs = simplejson.load(open(api_refs_json_path, "r")) except: api_refs_json = resource_string('cloudstack.compute', 'apirefs.json') api_refs = simplejson.loads(api_refs_json) API_REFS = api_refs + class ShellCommand(object): """コマンドのベースクラス """ + def options(self): return list() @@ -51,24 +53,32 @@ def execute(self, args): csv_fields = d.pop("csv") no_headers = d.pop("noheaders") - for k,v in d.items(): + for k, v in d.items(): if v is None: del(d[k]) - retval = client.connect().get(command.__name__,d) + retval = client.connect().get(command.__name__, d) return retval, fields, xml, csv_fields, no_headers -def arg(*args,**kw): + +def arg(*args, **kw): return (args, kw) + class IdcfShell(object): + def __init__(self): self.arg_parser = argparse.ArgumentParser( - prog = 'cloudstack-api', + prog='cloudstack-api', #usage='%(prog)s [-h]', - ) - self.arg_parser.add_argument('-v','--version', action='version', version="%(prog)s v"+VERSION) + ) + self.arg_parser.add_argument( + '-v', + '--version', + action='version', + version="%(prog)s v" + + VERSION) self.register_commands() def register_commands(self): @@ -89,40 +99,46 @@ def create_commands(self): commands = [] for index, desc in enumerate(API_REFS): key = desc["name"] - command = type(key.encode("utf-8"),(ShellCommand,),{"__doc__":desc["help"].encode("utf-8"), "index":index}) + command = type(key.encode("utf-8"), (ShellCommand,), + {"__doc__": desc["help"].encode("utf-8"), "index": index}) + def options(self): def opt_required(required): if required == "true": return True else: return False - retval = [arg(opt["option"],required=opt_required(opt["required"]), - help=opt["help"].encode("utf-8")) - for opt in API_REFS[self.index]["options"]] - - retval.append(arg("-t","--table",help="displaying tabular format", - nargs="?", const="*")) - retval.append(arg("-x","--xml",help="displaying xml format", + retval = [arg(opt["option"], required=opt_required(opt["required"]), + help=opt["help"].encode("utf-8")) + for opt in API_REFS[self.index]["options"]] + + retval.append(arg("-t", + "--table", + help="displaying tabular format", + nargs="?", + const="*")) + retval.append(arg("-x", "--xml", help="displaying xml format", nargs="?", const="*")) - retval.append(arg("-c","--csv",help="displaying csv format", + retval.append(arg("-c", "--csv", help="displaying csv format", nargs="?", const="*")) - retval.append(arg("--noheaders",help="suppress csv header", + retval.append(arg("--noheaders", help="suppress csv header", action="store_true")) return retval - setattr(command,"options",options) + setattr(command, "options", options) commands.append(command) return commands - def execute(self,raw_args,shell=False): + def execute(self, raw_args, shell=False): args = self.arg_parser.parse_args(raw_args) command = args.command_class() - retval,fields,xml,csv_fields,no_headers = command.execute(args) + retval, fields, xml, csv_fields, no_headers = command.execute(args) if shell: - print_pretty(retval,fields,xml,csv_fields,no_headers) + print_pretty(retval, fields, xml, csv_fields, no_headers) else: return retval -def print_pretty(retval,fields,xml,csv_fields,no_headers): + +def print_pretty(retval, fields, xml, csv_fields, no_headers): if not retval: return elif xml: @@ -131,53 +147,57 @@ def print_pretty(retval,fields,xml,csv_fields,no_headers): res = retval.get(retval.keys()[0]) count = res.get("count") if not count: - return print_dict(res,fields) + return print_dict(res, fields) else: res.pop("count") - return print_list(res,fields) + return print_list(res, fields) elif csv_fields: res = retval.get(retval.keys()[0]) count = res.get("count") if not count: - return print_dict_csv(res,csv_fields,no_headers) + return print_dict_csv(res, csv_fields, no_headers) else: res.pop("count") - return print_list_csv(res,csv_fields,no_headers) + return print_list_csv(res, csv_fields, no_headers) else: return print_json(retval) + def print_xml(xml): root = etree.XML(xml) - print etree.tostring(root,xml_declaration=True, - pretty_print=True,encoding='utf-8') + print etree.tostring(root, xml_declaration=True, + pretty_print=True, encoding='utf-8') + def print_json(json): - print simplejson.dumps(json,sort_keys=True, indent=2) + print simplejson.dumps(json, sort_keys=True, indent=2) def get_csv_writer(): data = StringIO.StringIO() - writer = csv.writer(data,quoting=csv.QUOTE_NONNUMERIC) - return data,writer + writer = csv.writer(data, quoting=csv.QUOTE_NONNUMERIC) + return data, writer + -def print_dict_csv(obj,fields,no_headers): +def print_dict_csv(obj, fields, no_headers): if not obj: print "no data found" else: headers = get_headers(fields) if not headers: headers = obj.keys() - - data,writer = get_csv_writer() + + data, writer = get_csv_writer() if not no_headers: writer.writerow(headers) writer.writerow([obj.get(k) for k in headers]) print data.getvalue() -def print_list_csv(res,fields,no_headers): + +def print_list_csv(res, fields, no_headers): rows_key = res.keys()[0] rows = res.get(rows_key) - for i,obj in enumerate(rows): + for i, obj in enumerate(rows): if i < 1: if fields: headers = get_headers(fields) @@ -186,25 +206,28 @@ def print_list_csv(res,fields,no_headers): else: headers = or_keys(rows) - data,writer = get_csv_writer() + data, writer = get_csv_writer() if not no_headers: writer.writerow(headers) writer.writerow([obj.get(k) for k in headers]) print data.getvalue() + def get_headers(fields): - fields_list = [ f.strip() for f in fields.split(',')] + fields_list = [f.strip() for f in fields.split(',')] if fields_list[0] == '*' and len(fields_list) == 1: - fields_list = [] + fields_list = [] return fields_list + def or_keys(rows): set_keys = set([]) for obj in rows: set_keys |= set(obj.keys()) return sorted(list(set_keys)) -def print_dict(obj,fields): + +def print_dict(obj, fields): if not obj: print "no data found" else: @@ -212,13 +235,14 @@ def print_dict(obj,fields): if not headers: headers = sorted(obj.keys()) pt = PrettyTable(headers) - pt.add_row( [obj.get(k) for k in headers]) + pt.add_row([obj.get(k) for k in headers]) pt.printt(sortby=headers[0]) -def print_list(res,fields): + +def print_list(res, fields): rows_key = res.keys()[0] rows = res.get(rows_key) - for i,obj in enumerate(rows): + for i, obj in enumerate(rows): if i < 1: if fields: headers = get_headers(fields) @@ -228,10 +252,11 @@ def print_list(res,fields): headers = or_keys(rows) pt = PrettyTable(headers) - [pt.set_field_align("%s"%h,"l") for h in headers] - pt.add_row( [obj.get(k) for k in headers]) + [pt.set_field_align("%s" % h, "l") for h in headers] + pt.add_row([obj.get(k) for k in headers]) pt.printt(sortby=headers[0]) + def main(): - IdcfShell().execute(sys.argv[1:],True) + IdcfShell().execute(sys.argv[1:], True) diff --git a/cloudstack/compute/stack.py b/cloudstack/compute/stack.py index 05ca938..c32e58f 100644 --- a/cloudstack/compute/stack.py +++ b/cloudstack/compute/stack.py @@ -3,7 +3,10 @@ import urllib import warnings -warnings.filterwarnings('ignore', category=DeprecationWarning, message=r'the sha module') +warnings.filterwarnings( + 'ignore', + category=DeprecationWarning, + message=r'the sha module') import sha import hmac import base64 @@ -13,7 +16,9 @@ import httplib2 import hashlib + class Stack(object): + def __init__(self, http, host, api_key, secret_key): self.http = http self.host = host @@ -23,41 +28,44 @@ def __init__(self, http, host, api_key, secret_key): def signature(self, command, query): query['command'] = command query['apikey'] = self.api_key - - if not query or not query.has_key('response'): + + if not query or 'response' not in query: query['response'] = 'json' query_str = '&'.join(['='.join( - [k,urllib.quote_plus(query[k]).replace('+', '%20')] - ) for k in query.keys()]) + [k, urllib.quote_plus(query[k]).replace('+', '%20')] + ) for k in query.keys()]) signature_str = '&'.join(['='.join( - [k.lower(), - urllib.quote_plus(query[k]).replace('+','%20').lower() - ] - )for k in sorted(query.iterkeys())]) + [k.lower(), + urllib.quote_plus(query[k]).replace('+', '%20').lower() + ] + )for k in sorted(query.iterkeys())]) signature = urllib.quote_plus( - base64.encodestring(hmac.new(self.secret_key,signature_str,hashlib.sha1).digest()).strip() - ) + base64.encodestring( + hmac.new( + self.secret_key, + signature_str, + hashlib.sha1).digest()).strip()) return (query_str, signature) def url(self, command, query): - quoted_query, signature = self.signature(command,query) + quoted_query, signature = self.signature(command, query) return "%s?%s&signature=%s" % (self.host, quoted_query, signature) def connect(self, method, command, query): try: - url = self.url(command,query) + url = self.url(command, query) response, content = self.http.request(url, method=method) - logging.debug("status : %d"% response.status) + logging.debug("status : %d" % response.status) if query['response'] == 'json': return simplejson.loads(content) else: - parser = etree.XMLParser() + parser = etree.XMLParser() tree = etree.XML(content, parser) return content - except httplib2.ServerNotFoundError,e: + except httplib2.ServerNotFoundError as e: print e except etree.XMLSyntaxError: print content @@ -65,4 +73,4 @@ def connect(self, method, command, query): print content def get(self, command, query=None): - return self.connect('GET',command,query) + return self.connect('GET', command, query) diff --git a/cloudstack/ppj.py b/cloudstack/ppj.py index a30d1f1..12aae24 100755 --- a/cloudstack/ppj.py +++ b/cloudstack/ppj.py @@ -4,6 +4,6 @@ import sys import simplejson -str = sys.stdin.readline().strip() +str = sys.stdin.readline().strip() json = simplejson.loads(str) -print simplejson.dumps(json,sort_keys=True, indent=2) +print simplejson.dumps(json, sort_keys=True, indent=2) diff --git a/cloudstack/utils.py b/cloudstack/utils.py index f1fa09d..816c2c5 100644 --- a/cloudstack/utils.py +++ b/cloudstack/utils.py @@ -4,33 +4,36 @@ import datetime import time + def safe_option(config, section, option): retval = config.get(section, option) if retval: return retval else: - raise Exception, "[%s] に [%s] を設定してください。" % (section,option) + raise Exception("[%s] に [%s] を設定してください。" % (section, option)) + class res(object): + def __repr__(self): reprkeys = sorted(k for k in self.__dict__.keys()) info = [] for k in reprkeys: - val = getattr(self,k) - if isinstance(val,unicode): + val = getattr(self, k) + if isinstance(val, unicode): val = val.encode("utf-8") - elif isinstance(val,int): + elif isinstance(val, int): val = str(val) - info.append("%s=%s" % (k,val)) + info.append("%s=%s" % (k, val)) return "<%s %s>" % (self.__class__.__name__, ", ".join(info)) -def dict2obj(d,use_key=False): +def dict2obj(d, use_key=False): if not d: return None top = res() - if isinstance(d,dict): + if isinstance(d, dict): for k, v in d.iteritems(): if isinstance(v, dict): setattr(top, k, dict2obj(v)) @@ -38,7 +41,7 @@ def dict2obj(d,use_key=False): tmp_list = [] for vv in v: if isinstance(vv, dict): - tmp_list.append(dict2obj(vv,True)) + tmp_list.append(dict2obj(vv, True)) else: tmp_list.append(vv) if use_key: @@ -47,37 +50,40 @@ def dict2obj(d,use_key=False): list_name = 'list' setattr(top, list_name, type(v)(tmp_list)) - #type(v)(dict2obj(vv) if isinstance(vv, dict) else vv for vv in v)) + # type(v)(dict2obj(vv) if isinstance(vv, dict) else vv for vv + # in v)) else: - setattr(top,k,v) + setattr(top, k, v) - elif isinstance(d,list): + elif isinstance(d, list): tmp_list = [] for vv in d: if isinstance(vv, dict): - tmp_list.append(dict2obj(vv,True)) + tmp_list.append(dict2obj(vv, True)) else: tmp_list.append(vv) if use_key: list_name = k else: list_name = 'list' - setattr(top, list_name,type(d)(tmp_list)) - if not hasattr(top,"count"): - setattr(top,"count",len(getattr(top,"list"))) + setattr(top, list_name, type(d)(tmp_list)) + if not hasattr(top, "count"): + setattr(top, "count", len(getattr(top, "list"))) return top + def datetimeFromString(s): c = pdc.Constants() p = pdt.Calendar(c) result, what = p.parse(s) dt = 0 - if what in (1,2,3): - dt = datetime.datetime( *result[:6] ) + if what in (1, 2, 3): + dt = datetime.datetime(*result[:6]) if what == 0: return "" return dt + def time_format(epoch): - return time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(epoch)) + return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(epoch)) diff --git a/setup.py b/setup.py index de552f1..9b63242 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup(name='cloudstack.compute', version=VERSION, - packages=['cloudstack','cloudstack.compute'], + packages=['cloudstack', 'cloudstack.compute'], include_package_data=True, install_requires=['setuptools', 'httplib2', @@ -18,8 +18,8 @@ 'lxml', ], entry_points={ - 'console_scripts': [ - 'cloudstack-api = cloudstack.compute.shell:main' - ] - } + 'console_scripts': [ + 'cloudstack-api = cloudstack.compute.shell:main' + ] + } )