8000 Added automatic convertion of dates to datetime.datetime · ebroder/python-github2@8478db0 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 8478db0

Browse files
Ask SolemAsk Solem
authored andcommitted
Added automatic convertion of dates to datetime.datetime
1 parent 4af0ca0 commit 8478db0

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

github2/core.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
from datetime import datetime
2+
3+
GITHUB_TIMEZONE = "-0700"
4+
GITHUB_DATE_FORMAT = "%Y/%m/%d %H:%M:%S"
5+
6+
7+
def ghdate_to_datetime(github_date):
8+
date_without_tz = github_date.rsplit("-")[0].strip()
9+
return datetime.strptime(date_without_tz, GITHUB_DATE_FORMAT)
10+
11+
12+
def datetime_to_ghdate(datetime_):
13+
date_without_tz = datetime_.strftime(GITHUB_DATE_FORMAT)
14+
return " ".join([date_without_tz, GITHUB_TIMEZONE])
15+
16+
117
class GithubCommand(object):
218

319
def __init__(self, request):
@@ -36,28 +52,45 @@ def __new__(cls, name, bases, attrs):
3652
super_new = super(BaseDataType, cls).__new__
3753

3854
attributes = attrs.pop("attributes", tuple())
55+
date_attributes = set(attrs.pop("date_attributes", tuple()))
3956
attrs.update(dict([(attr_name, None)
4057
for attr_name in attributes]))
4158

59+
def _contribute_method(name, func):
60+
func.func_name = name
61+
attrs[name] = func
62+
4263
def constructor(self, **kwargs):
4364
for attr_name, attr_value in kwargs.items():
4465
if attr_name not in attributes:
4566
raise TypeError("%s.__init__() doesn't support the "
4667
"%s argument." % ( cls_name, attr_name))
4768
setattr(self, attr_name, attr_value)
48-
attrs["__init__"] = constructor
4969

50-
def to_dict(self):
51-
dict_ = {}
52-
for attr_name in self.attributes:
53-
attr_value = getattr(self, attr_name, None)
54-
if attr_value is not None:
55-
dict_[attr_name] = attr_value
56-
return dict_
57-
attrs["to_dict"] = to_dict
70+
# Convert dates to datetime objects.
71+
for date_attr_name in date_attributes:
72+
attr_value = getattr(self, date_attr_name)
73+
if attr_value and not isinstance(attr_value, datetime):
74+
date_as_datetime = ghdate_to_datetime(attr_value)
75+
setattr(self, date_attr_name, date_as_datetime)
76+
_contribute_method("__init__", constructor)
77+
78+
def iterate(self):
79+
not_empty = lambda e: e is not None
80+
objdict = vars(self)
81+
for date_attr_name in date_attributes:
82+
date_value = objdict.get(date_attr_name)
83+
if date_value and isinstance(date_value, datetime):
84+
objdict[date_attr_name] = datetime_to_ghdate(date_value)
85+
86+
return iter(filter(not_empty, objdict.items()))
87+
_contribute_method("__iter__", iterate)
5888

5989
return super_new(cls, name, bases, attrs)
6090

91+
def contribute_method_to_cls(cls, name, func):
92+
func.func_name = name
93+
return func
6194

6295
class BaseData(object):
6396
__metaclass__ = BaseDataType

github2/issues.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Issue(BaseData):
44
attributes = ("position", "number", "votes", "body", "title",
55
"created_at", "updated_at", "user", "state")
6+
date_attributes = ("created_at", "updated_at")
67

78

89
class Issues(GithubCommand):

0 commit comments

Comments
 (0)
0