Skip to content

Commit

Permalink
flake8 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Gauvain Pocentek committed Aug 21, 2014
1 parent d4803f9 commit 5d5e0f7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 33 deletions.
75 changes: 50 additions & 25 deletions gitlab
Expand Up @@ -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 = []
Expand All @@ -66,39 +68,48 @@ 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']])
l.append("%s %s" % (action, detail))

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")
Expand All @@ -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:")

Expand All @@ -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')
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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))


Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
11 changes: 3 additions & 8 deletions gitlab.py
Expand Up @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 5d5e0f7

Please sign in to comment.