GeezerVison is showing github.com — Abstract github datetime serialize · audriusm/python-github3@9b2b5b9
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pygithub3/core/compat.py -5Lines changed: 0 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | # -*- encoding: utf-8 -*- | 3 | 3 | """ Utils to support python 2.6 compatibility """ | 4 | 4 |
| 5 | | -try: | 6 | | - import simplejson as json | 7 | | -except ImportError: | 8 | | - import json | 9 | | - | 10 | 5 | from collections import MutableMapping | 11 | 6 |
| 12 | 7 |
| |
Collapse file Copy file name to clipboardExpand all lines: pygithub3/core/errors.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | 2 | # -*- encoding: utf-8 -*- | 3 | 3 |
| 4 | | -from pygithub3.core.compat import json | | 4 | +from pygithub3.core import json | 5 | 5 | from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity | 6 | 6 |
| 7 | 7 |
| |
Collapse file Copy file name to clipboard +41Lines changed: 41 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 |
Collapse file Copy file name to clipboardExpand all lines: pygithub3/requests/base.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 |
| 4 | 4 | import re | 5 | 5 |
| 6 | | -from pygithub3.core.compat import import_module, json | | 6 | +from pygithub3.core import json | | 7 | +from pygithub3.core.compat import import_module | 7 | 8 | from pygithub3.exceptions import (RequestDoesNotExist, UriInvalid, | 8 | 9 | ValidationError, InvalidBodySchema) | 9 | 10 | from pygithub3.resources.base import Raw | |
Collapse file Copy file name to clipboardExpand all lines: pygithub3/resources/base.py +2-18Lines changed: 2 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -#!/usr/bin/env python | 2 | | -# -*- encoding: utf-8 -*- | | 1 | +# -*- coding: utf-8 -*- | 3 | 2 |
| 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 | 9 | 4 |
| 10 | 5 |
| 11 | 6 | class Resource(object): | @@ -15,7 +10,6 @@ class Resource(object): | 15 | 10 | _collection_maps = {} | 16 | 11 |
| 17 | 12 | def __init__(self, attrs): | 18 | | - """ """ | 19 | 13 | self._attrs = attrs | 20 | 14 | self.__set_attrs() | 21 | 15 |
| @@ -47,13 +41,6 @@ def wrapper(resource, raw_resource): | 47 | 41 | return func(resource, raw_resource) | 48 | 42 | return wrapper | 49 | 43 |
| 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 | | - | 57 | 44 | @self_resource | 58 | 45 | def parse_map(resource, raw_resource): | 59 | 46 | if hasattr(raw_resource, 'items'): | @@ -73,9 +60,6 @@ def parse_collection_map(resource, raw_resources): | 73 | 60 | for raw_resource in raw_resources] | 74 | 61 |
| 75 | 62 | 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])) | 79 | 63 | new_resource.update(dict([ | 80 | 64 | (attr, parse_map(resource, raw_resource[attr])) | 81 | 65 | for attr, resource in self._maps.items() | |
|