From bb430d939ab867486b19a84ced7ffe576dde8ea6 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 31 Mar 2011 04:31:19 -0400 Subject: [PATCH] Added GistComment support --- gistapi/core.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/gistapi/core.py b/gistapi/core.py index df297ec..b9d5708 100644 --- a/gistapi/core.py +++ b/gistapi/core.py @@ -32,7 +32,7 @@ import cStringIO import os.path import urllib2 -from dateutil.parser import parse as dtime +from datetime import datetime try: import simplejson as json @@ -67,13 +67,17 @@ class Gist(object): self.epic_embed_url = url + '.pibb' self.json_url = url + '.json' self.post_url = GIST_BASE % 'gists/%s' % self.id + self.comments = [] + + def __repr__(self): + return '' % self.id def __getattribute__(self, name): """Get attributes, but only if needed.""" # Only make external API calls if needed if name in ('owner', 'description', 'created_at', 'public', - 'files', 'filenames', 'repo'): + 'files', 'filenames', 'repo', 'comments'): if not hasattr(self, '_meta'): self._meta = self._get_meta() @@ -103,7 +107,14 @@ class Gist(object): setattr(self, key, value) elif key == 'created_at': # Attach datetime - setattr(self, key, dtime(value)) + datetime.strptime(value[:-6], '%Y/%m/%d %H:%M:%S') + + elif key == 'comments': + _comments = [] + for comment in value: + c = GistComment().from_api(comment) + _comments.append(c) + setattr(self, 'comments', _comments) else: # Attach properties to object setattr(self, key, unicode(value)) @@ -214,3 +225,40 @@ class Gists(object): # Return a list of Gist objects return [Gist(json=g) for g in json.load(urllib2.urlopen(_url))['gists']] + + +class GistComment(object): + """Gist comments.""" + + def __init__(self): + self.body = None + self.created_at = None + self.gravatar_id = None + self.id = None + self.updated_at = None + self.user = None + + def __repr__(self): + return '' % self.id + + @staticmethod + def from_api(jsondict): + """Returns new instance of GistComment containing given api dict.""" + comment = GistComment() + + comment.body = jsondict.get('body', None) + comment.created_at = datetime.strptime(jsondict.get('created_at')[:-6], '%Y/%m/%d %H:%M:%S') + comment.gravatar_id = jsondict.get('gravatar_id', None) + comment.id = jsondict.get('id', None) + comment.updated_at = datetime.strptime(jsondict.get('updated_at')[:-6], '%Y/%m/%d %H:%M:%S') + comment.user = jsondict.get('user', None) + + return comment + + + + + + + + \ No newline at end of file