From 5d5e0f7f49ddc1908339c0537fd4490f1ce2a1ed Mon Sep 17 00:00:00 2001 From: Gauvain Pocentek Date: Thu, 21 Aug 2014 16:18:14 +0200 Subject: [PATCH] flake8 fixes --- gitlab | 75 ++++++++++++++++++++++++++++++++++++------------------- gitlab.py | 11 +++----- 2 files changed, 53 insertions(+), 33 deletions(-) diff --git a/gitlab b/gitlab index 4c8fb198c..5389a0877 100755 --- a/gitlab +++ b/gitlab @@ -32,26 +32,28 @@ import gitlab camel_re = re.compile('(.)([A-Z])') extra_actions = { - gitlab.ProjectBranch: { - 'protect': {'requiredAttrs': ['id', 'project-id']}, - 'unprotect': {'requiredAttrs': ['id', 'project-id']} - }, - gitlab.Project: { - 'search': {'requiredAttrs': ['query']}, - 'owned': {'requiredAttrs': []}, - 'all': {'requiredAttrs': []} - }, + gitlab.ProjectBranch: {'protect': {'requiredAttrs': ['id', 'project-id']}, + 'unprotect': {'requiredAttrs': ['id', 'project-id']} + }, + gitlab.Project: {'search': {'requiredAttrs': ['query']}, + 'owned': {'requiredAttrs': []}, + 'all': {'requiredAttrs': []} + }, } + def die(msg): sys.stderr.write(msg + "\n") sys.exit(1) + def whatToCls(what): return "".join([s.capitalize() for s in what.split("-")]) + def clsToWhat(cls): - return camel_re.sub(r'\1-\2', cls.__name__).lower() + return camel_re.sub(r'\1-\2', cls.__name__).lower() + def actionHelpList(cls): l = [] @@ -66,27 +68,33 @@ def actionHelpList(cls): detail = '' if action == 'list': - detail = " ".join(["--%s=ARG" % x.replace('_', '-') for x in cls.requiredListAttrs]) + detail = " ".join(["--%s=ARG" % x.replace('_', '-') + for x in cls.requiredListAttrs]) if detail: detail += " " detail += "--page=ARG --per-page=ARG" elif action in ['get', 'delete']: if cls not in [gitlab.CurrentUser]: detail = "--id=ARG " - detail += " ".join(["--%s=ARG" % x.replace('_', '-') for x in cls.requiredGetAttrs]) + detail += " ".join(["--%s=ARG" % x.replace('_', '-') + for x in cls.requiredGetAttrs]) elif action == 'create': - detail = " ".join(["--%s=ARG" % x.replace('_', '-') for x in cls.requiredCreateAttrs]) + detail = " ".join(["--%s=ARG" % x.replace('_', '-') + for x in cls.requiredCreateAttrs]) if detail: detail += " " - detail += " ".join(["[--%s=ARG]" % x.replace('_', '-') for x in cls.optionalCreateAttrs]) + detail += " ".join(["[--%s=ARG]" % x.replace('_', '-') + for x in cls.optionalCreateAttrs]) elif action == 'update': - detail = " ".join(["[--%s=ARG]" % x.replace('_', '-') for x in cls.requiredCreateAttrs]) + detail = " ".join(["[--%s=ARG]" % x.replace('_', '-') + for x in cls.requiredCreateAttrs]) if detail: detail += " " - detail += " ".join(["[--%s=ARG]" % x.replace('_', '-') for x in cls.optionalCreateAttrs]) + detail += " ".join(["[--%s=ARG]" % x.replace('_', '-') + for x in cls.optionalCreateAttrs]) l.append("%s %s" % (action, detail)) - if extra_actions.has_key(cls): + if cls in extra_actions: for action in sorted(extra_actions[cls]): d = extra_actions[cls][action] detail = " ".join(["--%s=ARG" % arg for arg in d['requiredAttrs']]) @@ -94,11 +102,14 @@ def actionHelpList(cls): return (l) + def usage(): - print("usage: gitlab [--help|-h] [--fancy|--verbose|-v] [--gitlab=GITLAB] WHAT ACTION [options]") + print("usage: gitlab [--help|-h] [--fancy|--verbose|-v] [--gitlab=GITLAB] " + "WHAT ACTION [options]") print("") print("--gitlab=GITLAB") - print(" Specifies which python-gitlab.cfg configuration section should be used.") + print(" Specifies which python-gitlab.cfg configuration section should " + "be used.") print(" If not defined, the default selection will be used.") print("") print("--fancy, --verbose, -v") @@ -108,7 +119,8 @@ def usage(): print(" Displays this message.") print("") print("Available `options` depend on which WHAT/ACTION couple is used.") - print("If `ACTION` is \"help\", available actions and options will be listed for `ACTION`.") + print("If `ACTION` is \"help\", available actions and options will be " + "listed for `ACTION`.") print("") print("Available `WHAT` values are:") @@ -129,15 +141,18 @@ def usage(): for cls in classes: print(" %s" % clsToWhat(cls)) + def do_auth(): try: - gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token, ssl_verify=ssl_verify) + gl = gitlab.Gitlab(gitlab_url, private_token=gitlab_token, + ssl_verify=ssl_verify) gl.auth() except: die("Could not connect to GitLab (%s)" % gitlab_url) return gl + def get_id(): try: id = d.pop('id') @@ -146,6 +161,7 @@ def get_id(): return id + def do_create(cls, d): if not cls.canCreate: die("%s objects can't be created" % what) @@ -158,6 +174,7 @@ def do_create(cls, d): return o + def do_list(cls, d): if not cls.canList: die("%s objects can't be listed" % what) @@ -169,6 +186,7 @@ def do_list(cls, d): return l + def do_get(cls, d): if not cls.canGet: die("%s objects can't be retrieved" % what) @@ -184,6 +202,7 @@ def do_get(cls, d): return o + def do_delete(cls, d): if not cls.canDelete: die("%s objects can't be deleted" % what) @@ -194,6 +213,7 @@ def do_delete(cls, d): except Exception as e: die("Impossible to destroy object (%s)" % str(e)) + def do_update(cls, d): if not cls.canUpdate: die("%s objects can't be updated" % what) @@ -208,22 +228,25 @@ def do_update(cls, d): return o + def do_project_search(d): try: return gl.search_projects(d['query']) - except: + except Exception as e: die("Impossible to search projects (%s)" % str(e)) + def do_project_all(): try: return gl.all_projects() except Exception as e: die("Impossible to list all projects (%s)" % str(e)) + def do_project_owned(): try: return gl.owned_projects() - except: + except Exception as e: die("Impossible to list owned projects (%s)" % str(e)) @@ -294,7 +317,8 @@ try: gitlab_url = config.get(gitlab_id, 'url') gitlab_token = config.get(gitlab_id, 'private_token') except: - die("Impossible to get gitlab informations from configuration (%s)" % gitlab_id) + die("Impossible to get gitlab informations from configuration (%s)" % + gitlab_id) try: ssl_verify = config.getboolean('global', 'ssl_verify') @@ -383,6 +407,7 @@ elif action == "all": o.display(verbose) else: - die("Unknown action: %s. Use \"gitlab %s help\" to get details." % (action, what)) + die("Unknown action: %s. Use \"gitlab %s help\" to get details." % + (action, what)) sys.exit(0) diff --git a/gitlab.py b/gitlab.py index d7e844960..b2501dbad 100644 --- a/gitlab.py +++ b/gitlab.py @@ -468,7 +468,7 @@ def _sanitize(value): def _sanitize_dict(src): - return {k:_sanitize(v) for k, v in src.items()} + return {k: _sanitize(v) for k, v in src.items()} class GitlabObject(object): @@ -761,8 +761,8 @@ class ProjectCommit(GitlabObject): shortPrintAttr = 'title' def diff(self): - url = '/projects/%(project_id)s/repository/commits/%(commit_id)s/diff' % \ - {'project_id': self.project_id, 'commit_id': self.id} + url = ('/projects/%(project_id)s/repository/commits/%(commit_id)s/diff' + % {'project_id': self.project_id, 'commit_id': self.id}) r = self.gitlab.rawGet(url) if r.status_code == 200: return r.json() @@ -978,11 +978,6 @@ def Event(self, id=None, **kwargs): project_id=self.id, **kwargs) - def File(self, id=None, **kwargs): - return self._getListOrObject(ProjectFile, id, - project_id=self.id, - **kwargs) - def Hook(self, id=None, **kwargs): return self._getListOrObject(ProjectHook, id, project_id=self.id,