diff --git a/github2/client.py b/github2/client.py index 478673f..6378604 100644 --- a/github2/client.py +++ b/github2/client.py @@ -6,8 +6,10 @@ from github2.commits import Commits class Github(object): - def __init__(self, username, api_token): - self.request = GithubRequest(username=username, api_token=api_token) + def __init__(self, username, api_token, debug=False): + self.debug = debug + self.request = GithubRequest(username=username, api_token=api_token, + debug=self.debug) self.issues = Issues(self.request) self.users = Users(self.request) self.repos = Repositories(self.request) diff --git a/github2/commits.py b/github2/commits.py index 1ccba00..a5a9739 100644 --- a/github2/commits.py +++ b/github2/commits.py @@ -7,8 +7,8 @@ class Commit(BaseData): url = Attribute("Canonical URL for this commit.") author = Attribute("Author metadata (dict with name/email.)") id = Attribute("Commit ID.") - committed_date = DateAttribute("Date committed.") - authored_data = DateAttribute("Date authored.") + committed_date = DateAttribute("Date committed.", format="commit") + authored_data = DateAttribute("Date authored.", format="commit") tree = Attribute("Tree SHA for this commit.") committer = Attribute("Comitter metadata (dict with name/email.)") diff --git a/github2/core.py b/github2/core.py index 36d7eef..fe21f31 100644 --- a/github2/core.py +++ b/github2/core.py @@ -2,6 +2,9 @@ from datetime import datetime GITHUB_TIMEZONE = "-0700" GITHUB_DATE_FORMAT = "%Y/%m/%d %H:%M:%S" +#2009-03-21T18:01:48-07:00 +COMMIT_DATE_FORMAT = "%Y-%m-%dT%H:%M:%S" + def ghdate_to_datetime(github_date): @@ -14,6 +17,16 @@ def datetime_to_ghdate(datetime_): return " ".join([date_without_tz, GITHUB_TIMEZONE]) +def commitdate_to_datetime(commit_date): + date_without_tz = commit_date[:-6] + return datetime.strptime(date_without_tz, COMMIT_DATE_FORMAT) + + +def datetime_to_commitdate(datetime_): + date_without_tz = datetime_.strftime(COMMIT_DATE_FORMAT) + return "".join([date_without_tz, GITHUB_TIMEZONE]) + + class GithubCommand(object): def __init__(self, request): @@ -72,15 +85,30 @@ class Attribute(object): class DateAttribute(Attribute): + format = "github" + converter_for_format = { + "github": { + "to": ghdate_to_datetime, + "from": datetime_to_ghdate, + }, + "commit": { + "to": commitdate_to_datetime, + "from": datetime_to_commitdate, + }, + } + + def __init__(self, *args, **kwargs): + self.format = kwargs.pop("format", self.format) + super(DateAttribute, self).__init__(*args, **kwargs) def to_python(self, value): if value and not isinstance(value, datetime): - return ghdate_to_datetime(value) + return self.converter_for_format[self.format]["to"](value) return value def from_python(self, value): if value and isinstance(value, datetime): - return datetime_to_ghdate(value) + return self.converter_for_format[self.format]["from"](value) return value @@ -105,7 +133,7 @@ class BaseDataType(type): for attr_name, attr_value in kwargs.items(): if attr_name not in self._meta: raise TypeError("%s.__init__() doesn't support the " - "%s argument." % (cls_name, attr_name)) + "%s argument." % (name, attr_name)) attr = self._meta[attr_name] setattr(self, attr_name, attr.to_python(attr_value)) diff --git a/github2/request.py b/github2/request.py index 76a1499..3fef3fd 100644 --- a/github2/request.py +++ b/github2/request.py @@ -23,10 +23,11 @@ class GithubRequest(object): "https": httplib.HTTPSConnection, } - def __init__(self, username, api_token, url_prefix=None): + def __init__(self, username, api_token, url_prefix=None, debug=False): self.username = username self.api_token = api_token self.url_prefix = url_prefix + self.debug = debug if not self.url_prefix: self.url_prefix = self.url_format % { "github_url": self.github_url, @@ -55,15 +56,22 @@ class GithubRequest(object): def raw_request(self, url, extra_post_data): resource = urlparse(url) - post_data = self.encode_authentication_data(extra_post_data) - connector = self.connector_for_scheme[resource.scheme] + post_data = None headers = self.http_headers headers["Accept"] = "text/html" - headers["Content-Length"] = str(len(post_data)) + method = "GET" + if extra_post_data: + post_data = self.encode_authentication_data(extra_post_data) + headers["Content-Length"] = str(len(post_data)) + method = "POST" + connector = self.connector_for_scheme[resource.scheme] connection = connector(resource.hostname, resource.port) - connection.request("POST", resource.path, post_data, headers) + connection.request("GET", resource.path, post_data, headers) response = connection.getresponse() response_text = response.read().encode("utf-8") + if self.debug: + sys.stderr.write("URL:[%s] POST_DATA:%s RESPONSE_TEXT: [%s]\n" % ( + url, post_data, response_text)) json = simplejson.loads(response_text) if json.get("error"): raise self.GithubError(json["error"][0]["error"])