Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit 9b2b5b9

Browse files
committed
Abstract github datetime serialize
With Json coder/decoder hooks
1 parent b3e2a70 commit 9b2b5b9

File tree

5 files changed

+46
-25
lines changed

5 files changed

+46
-25
lines changed

pygithub3/core/compat.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@
22
# -*- encoding: utf-8 -*-
33
""" Utils to support python 2.6 compatibility """
44

5-
try:
6-
import simplejson as json
7-
except ImportError:
8-
import json
9-
105
from collections import MutableMapping
116

127

pygithub3/core/errors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from pygithub3.core.compat import json
4+
from pygithub3.core import json
55
from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity
66

77

pygithub3/core/json/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Emulate json module with encode/decoders to support github datetime format
4+
"""
5+
6+
from datetime import datetime
7+
try:
8+
import simplejson as json
9+
except ImportError:
10+
import json
11+
12+
GITHUB_DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
13+
14+
15+
class GHJSONEncoder(json.JSONEncoder):
16+
def default(self, o):
17+
try:
18+
return datetime.strftime(o, GITHUB_DATE_FORMAT)
19+
except:
20+
return super(GHJSONEncoder, self).default(o)
21+
22+
23+
def gh_decoder_hook(dict_):
24+
for k, v in dict_.iteritems():
25+
try:
26+
date = datetime.strptime(v, GITHUB_DATE_FORMAT)
27+
dict_[k] = date
28+
except:
29+
continue
30+
return dict_
31+
32+
33+
def dumps(obj, cls=GHJSONEncoder, **kwargs):
34+
return json.dumps(obj, cls=cls, **kwargs)
35+
36+
37+
def loads(s, object_hook=gh_decoder_hook, **kwargs):
38+
return json.loads(s, object_hook=object_hook, **kwargs)
39+
40+
dump = json.dump
41+
load = json.load

pygithub3/requests/base.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import re
55

6-
from pygithub3.core.compat import import_module, json
6+
from pygithub3.core import json
7+
from pygithub3.core.compat import import_module
78
from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid,
89
ValidationError, InvalidBodySchema)
910
from pygithub3.resources.base import Raw

pygithub3/resources/base.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
1-
#!/usr/bin/env python
2-
# -*- encoding: utf-8 -*-
1+
# -*- coding: utf-8 -*-
32

4-
from datetime import datetime
5-
6-
from pygithub3.core.compat import json
7-
8-
GITHUB_DATE_FORMAT = '%Y-%m-%dT%H:%M:%SZ'
3+
from pygithub3.core import json
94

105

116
class Resource(object):
@@ -15,7 +10,6 @@ class Resource(object):
1510
_collection_maps = {}
1611

1712
def __init__(self, attrs):
18-
""" """
1913
self._attrs = attrs
2014
self.__set_attrs()
2115

@@ -47,13 +41,6 @@ def wrapper(resource, raw_resource):
4741
return func(resource, raw_resource)
4842
return wrapper
4943

50-
def parse_date(string_date):
51-
try:
52-
date = datetime.strptime(string_date, GITHUB_DATE_FORMAT)
53-
except TypeError:
54-
date = None
55-
return date
56-
5744
@self_resource
5845
def parse_map(resource, raw_resource):
5946
if hasattr(raw_resource, 'items'):
@@ -73,9 +60,6 @@ def parse_collection_map(resource, raw_resources):
7360
for raw_resource in raw_resources]
7461

7562
new_resource = raw_resource.copy()
76-
new_resource.update(dict([
77-
(attr, parse_date(raw_resource[attr]))
78-
for attr in self._dates if attr in raw_resource]))
7963
new_resource.update(dict([
8064
(attr, parse_map(resource, raw_resource[attr]))
8165
for attr, resource in self._maps.items()

0 commit comments

Comments
 (0)