diff options
author | 2012-02-04 19:07:23 +0100 | |
---|---|---|
committer | 2012-02-04 19:07:23 +0100 | |
commit | 8a723b0815841ea9e34c148b06c0fa6529b90e33 (patch) | |
tree | 6161c4aef077d2ac9e2430d935f53e37117a0b9b /pygithub3/resources | |
parent | Full support to Result dynamic and lazy iterator (diff) | |
download | python-github3-8a723b0815841ea9e34c148b06c0fa6529b90e33.tar.xz python-github3-8a723b0815841ea9e34c148b06c0fa6529b90e33.zip |
Renaming (What I was thinking?)
resources => ghrequests
models => resources
+ Also it's near to complete user service
Diffstat (limited to 'pygithub3/resources')
-rw-r--r-- | pygithub3/resources/__init__.py | 1 | ||||
-rw-r--r-- | pygithub3/resources/base.py | 78 | ||||
-rw-r--r-- | pygithub3/resources/users.py | 22 |
3 files changed, 101 insertions, 0 deletions
diff --git a/pygithub3/resources/__init__.py b/pygithub3/resources/__init__.py new file mode 100644 index 0000000..dae354a --- /dev/null +++ b/pygithub3/resources/__init__.py @@ -0,0 +1 @@ +# -*- encoding: utf-8 -*- diff --git a/pygithub3/resources/base.py b/pygithub3/resources/base.py new file mode 100644 index 0000000..5a9418a --- /dev/null +++ b/pygithub3/resources/base.py @@ -0,0 +1,78 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +try: + import simplejson as json +except ImportError: + import json + + +class Resource(object): + + _dates = () + _maps = {} + _collection_maps = {} + + def __init__(self, attrs): + """ """ + self._attrs = attrs + self.__set_attrs() + + def __set_attrs(self): + for attr in self._attrs: + setattr(self, attr, self._attrs[attr]) + + @classmethod + def loads(self, json_content): + resource_chunk = json.loads(json_content) + if not hasattr(resource_chunk, 'items'): + return [self.__load(raw_resource) + for raw_resource in resource_chunk] + else: + return self.__load(resource_chunk) + + @classmethod + def __load(self, raw_resource): + def parse_date(string_date): + from datetime import datetime + try: + date = datetime.strptime(string_date, '%Y-%m-%dT%H:%M:%SZ') + except TypeError: + date = None + return date + + def parse_map(resource, raw_resource): + if hasattr(raw_resource, 'items'): + return resource.__load(raw_resource) + + def parse_collection_map(resource, raw_resources): + # Dict of resources (Ex: Gist file) + if hasattr(raw_resources, 'items'): + dict_map = {} + for key, raw_resource in raw_resources.items(): + dict_map[key] = resource.__load(raw_resource) + return dict_map + # list of resources + elif hasattr(raw_resources, '__iter__'): + return [resource.__load(raw_resource) + for raw_resource in raw_resources] + raw_resource.update( + {attr: parse_date(raw_resource[attr]) + for attr in self._dates if attr in raw_resource}) + raw_resource.update( + {attr: parse_map(resource , raw_resource[attr]) + for attr, resource in self._maps.items() + if attr in raw_resource}) + raw_resource.update( + {attr: parse_collection_map(resource, raw_resource[attr]) + for attr, resource in self._collection_maps.items() + if attr in raw_resource}) + + return self(raw_resource) + + +class Raw(Resource): + + @classmethod + def loads(self, json_content): + return json.loads(json_content) diff --git a/pygithub3/resources/users.py b/pygithub3/resources/users.py new file mode 100644 index 0000000..7e40025 --- /dev/null +++ b/pygithub3/resources/users.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +from .base import Resource + +__all__ = ('Plan', 'User') + + +class Plan(Resource): + + def __str__(self): + return '<Plan (%s)>' % getattr(self, 'name', '') + + +class User(Resource): + """ """ + + _maps = {'plan': Plan} + _dates = ('created_at', ) + + def __str__(self): + return '<User (%s)>' % getattr(self, 'login', '') |