diff options
author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-08-01 14:10:29 +0900 |
---|---|---|
committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-08-01 14:29:51 +0900 |
commit | 71e632764963e538f06fff9e830e95cf5d0fc1dc (patch) | |
tree | e0c1423865ed7b1b118f4b5efcfb4dbcceb68543 /pygerrit | |
parent | 37b8b76c49b3f0a5f8a26434f9653b0d3327c7a2 (diff) | |
download | pygerrit-71e632764963e538f06fff9e830e95cf5d0fc1dc.tar.gz |
Add helper methods for initialisation from json data
Added helper methods to initialise model member variables. Now
the models' constructors will not throw exception if a value is
missing, but instead just initialise the variable to None.
Change-Id: I7473ec9bb471f95ea0142067af4750c7e26a0339
Diffstat (limited to 'pygerrit')
-rw-r--r-- | pygerrit/__init__.py | 14 | ||||
-rw-r--r-- | pygerrit/events.py | 15 | ||||
-rw-r--r-- | pygerrit/models.py | 88 |
3 files changed, 58 insertions, 59 deletions
diff --git a/pygerrit/__init__.py b/pygerrit/__init__.py index cb77bdc..100380b 100644 --- a/pygerrit/__init__.py +++ b/pygerrit/__init__.py @@ -1,7 +1,13 @@ """ Module to interface with Gerrit. """ -from pygerrit.error import * -from pygerrit.events import * -from pygerrit.models import * -from pygerrit.stream import * +def from_json(json_data, key): + """ Helper method to extract values from JSON data. + + Return the value of `key` from `json_data`, or None if `json_data` + does not contain `key`. + + """ + if key in json_data: + return json_data[key] + return None diff --git a/pygerrit/events.py b/pygerrit/events.py index 90459f4..c07979a 100644 --- a/pygerrit/events.py +++ b/pygerrit/events.py @@ -81,10 +81,7 @@ class ChangeAbandonedEvent(GerritEvent): super(ChangeAbandonedEvent, self).__init__() try: self.change = Change(json_data["change"]) - if "patchSet" in json_data: - self.patchset = Patchset(json_data["patchSet"]) - else: - self.patchset = None + self.patchset = Patchset.from_json(json_data) self.abandoner = Account(json_data["abandoner"]) self.reason = json_data["reason"] except KeyError, e: @@ -99,10 +96,7 @@ class ChangeRestoredEvent(GerritEvent): super(ChangeRestoredEvent, self).__init__() try: self.change = Change(json_data["change"]) - if "patchSet" in json_data: - self.patchset = Patchset(json_data["patchSet"]) - else: - self.patchset = None + self.patchset = Patchset.from_json(json_data) self.restorer = Account(json_data["restorer"]) self.reason = json_data["reason"] except KeyError, e: @@ -117,9 +111,6 @@ class RefUpdatedEvent(GerritEvent): super(RefUpdatedEvent, self).__init__() try: self.ref_update = RefUpdate(json_data["refUpdate"]) - if "submitter" in json_data: - self.submitter = Account(json_data["submitter"]) - else: - self.submitter = None + self.submitter = Account.from_json(json_data, "submitter") except KeyError, e: raise GerritError("RefUpdatedEvent: %s" % e) diff --git a/pygerrit/models.py b/pygerrit/models.py index c8c70d5..813b53f 100644 --- a/pygerrit/models.py +++ b/pygerrit/models.py @@ -1,6 +1,6 @@ """ Models for Gerrit JSON data. """ -from pygerrit.error import GerritError +from pygerrit import from_json class Account(object): @@ -8,14 +8,20 @@ class Account(object): """ Gerrit user account (name and email address). """ def __init__(self, json_data): - try: - self.name = json_data["name"] - if "email" in json_data: - self.email = json_data["email"] - else: - self.email = "" - except KeyError, e: - raise GerritError("GerritAccount: %s" % e) + self.name = from_json(json_data, "name") + self.email = from_json(json_data, "email") + + @staticmethod + def from_json(json_data, key): + """ Create an Account instance. + + Return an instance of Account initialised with values from `key` + in `json_data`, or None if `json_data` does not contain `key`. + + """ + if key in json_data: + return Account(json_data[key]) + return None class Change(object): @@ -23,16 +29,13 @@ class Change(object): """ Gerrit change. """ def __init__(self, json_data): - try: - self.project = json_data["project"] - self.branch = json_data["branch"] - self.change_id = json_data["id"] - self.number = json_data["number"] - self.subject = json_data["subject"] - self.url = json_data["url"] - self.owner = Account(json_data["owner"]) - except KeyError, e: - raise GerritError("GerritChange: %s" % e) + self.project = from_json(json_data, "project") + self.branch = from_json(json_data, "branch") + self.change_id = from_json(json_data, "id") + self.number = from_json(json_data, "number") + self.subject = from_json(json_data, "subject") + self.url = from_json(json_data, "url") + self.owner = Account.from_json(json_data, "owner") class Patchset(object): @@ -40,13 +43,22 @@ class Patchset(object): """ Gerrit patch set. """ def __init__(self, json_data): - try: - self.number = json_data["number"] - self.revision = json_data["revision"] - self.ref = json_data["ref"] - self.uploader = Account(json_data["uploader"]) - except KeyError, e: - raise GerritError("GerritPatchset: %s" % e) + self.number = from_json(json_data, "number") + self.revision = from_json(json_data, "revision") + self.ref = from_json(json_data, "ref") + self.uploader = Account.from_json(json_data, "uploader") + + @staticmethod + def from_json(json_data): + r""" Create a Patchset instance. + + Return an instance of Patchset initialised with values from "patchSet" + in `json_data`, or None if `json_data` does not contain "patchSet". + + """ + if "patchSet" in json_data: + return Patchset(json_data["patchSet"]) + return None class Approval(object): @@ -54,16 +66,9 @@ class Approval(object): """ Gerrit approval (verified, code review, etc). """ def __init__(self, json_data): - if "type" not in json_data: - raise GerritError("GerritApproval: Missing type") - if "value" not in json_data: - raise GerritError("GerritApproval: Missing value") - self.category = json_data["type"] - self.value = json_data["value"] - if "description" in json_data: - self.description = json_data["description"] - else: - self.description = None + self.category = from_json(json_data, "type") + self.value = from_json(json_data, "value") + self.description = from_json(json_data, "description") class RefUpdate(object): @@ -71,10 +76,7 @@ class RefUpdate(object): """ Gerrit ref update. """ def __init__(self, json_data): - try: - self.oldrev = json_data["oldRev"] - self.newrev = json_data["newRev"] - self.refname = json_data["refName"] - self.project = json_data["project"] - except KeyError, e: - raise GerritError("GerritRefUpdate: %s" % e) + self.oldrev = from_json(json_data, "oldRev") + self.newrev = from_json(json_data, "newRev") + self.refname = from_json(json_data, "refName") + self.project = from_json(json_data, "project") |