diff options
author | 2011-11-06 18:14:51 +0100 | |
---|---|---|
committer | 2011-11-06 18:14:51 +0100 | |
commit | 6f2d2115fa578e2d5aa611fe03474341d73f73cc (patch) | |
tree | 69cd5dec12ddab57eea69a9f80f7e7d79bde909f /github3/models | |
parent | Cleaning files from origin repo (diff) | |
download | python-github3-6f2d2115fa578e2d5aa611fe03474341d73f73cc.tar.xz python-github3-6f2d2115fa578e2d5aa611fe03474341d73f73cc.zip |
Modelizer class. json<->model parser
Also reorganize code
Diffstat (limited to 'github3/models')
-rw-r--r-- | github3/models/base.py | 145 | ||||
-rw-r--r-- | github3/models/gists.py | 68 | ||||
-rw-r--r-- | github3/models/orgs.py | 27 | ||||
-rw-r--r-- | github3/models/repos.py | 35 | ||||
-rw-r--r-- | github3/models/user.py | 69 |
5 files changed, 119 insertions, 225 deletions
diff --git a/github3/models/base.py b/github3/models/base.py index f98af2d..df0c82b 100644 --- a/github3/models/base.py +++ b/github3/models/base.py @@ -2,149 +2,18 @@ github3.models ~~~~~~~~~~~~~~ -This module provides the Github3 object model. +This package provides the Github3 object model. """ -import json -import inspect - -from github3.helpers import to_python, to_api, key_diff - class BaseResource(object): """A BaseResource object.""" - _strs = [] - _ints = [] - _dates = [] - _bools = [] - _map = {} - _list_map = {} - _writeable = [] - _cache = {} - - def post_map(self): - try: - handler = self.handler() - methods = filter( - lambda x: x[0].startswith('get') and callable(x[1]), - inspect.getmembers(handler, inspect.ismethod)) - for name, callback in methods: - setattr(self, name, callback) - except: - pass - - def __init__(self): - self._bootstrap() + def __init__(self, attrs=None): + if attrs: + for attr, value in attrs.items(): + setattr(self, attr, value) super(BaseResource, self).__init__() - def __dir__(self): - return self.keys() - - def _bootstrap(self): - """Bootstraps the model object based on configured values.""" - - for attr in self.keys(): - setattr(self, attr, None) - - def keys(self): - return self._strs + self._ints + self._dates + self._bools + self._map.keys() - - def dict(self): - d = dict() - for k in self.keys(): - d[k] = self.__dict__.get(k) - - return d - @classmethod - def new_from_dict(cls, d, gh=None): - - return to_python( - obj=cls(), in_dict=d, - str_keys = cls._strs, - int_keys = cls._ints, - date_keys = cls._dates, - bool_keys = cls._bools, - object_map = cls._map, - list_map = cls._list_map, - _gh = gh - ) - - - def update(self): - deploy = key_diff(self._cache, self.dict(), pack=True) - - deploy = to_api(deploy, int_keys=self._ints, date_keys=self._dates, bool_keys=self._bools) - deploy = json.dumps(deploy) - - r = self._gh._patch_resource(self.ri, deploy) - return r - - - -#class Org(BaseResource): -# """Github Organization object model.""" -# -# _strs = [ -# 'login', 'url', 'avatar_url', 'name', 'company', 'blog', 'location', 'email' -# 'html_url', 'type', 'billing_email'] -# _ints = [ -# 'id', 'public_repos', 'public_gists', 'followers', 'following', -# 'total_private_repos', 'owned_private_repos', 'private_gists', 'disk_usage', -# 'collaborators'] -# _dates = ['created_at'] -# _map = {'plan': Plan} -# _writable = ['billing_email', 'blog', 'company', 'email', 'location', 'name'] -# -# @property -# def ri(self): -# return ('orgs', self.login) -# -# def __repr__(self): -# return '<org {0}>'.format(self.login) -# -# def repos(self, limit=None): -# return self._gh._get_resources(('orgs', self.login, 'repos'), Repo, limit=limit) -# -# def members(self, limit=None): -# return self._gh._get_resources(('orgs', self.login, 'members'), User, limit=limit) -# -# def is_member(self, username): -# if isinstance(username, User): -# username = username.login -# -# r = self._gh._http_resource('GET', ('orgs', self.login, 'members', username), check_status=False) -# return (r.status_code == 204) -# -# def publicize_member(self, username): -# if isinstance(username, User): -# username = username.login -# -# r = self._gh._http_resource('PUT', ('orgs', self.login, 'public_members', username), check_status=False, data='') -# return (r.status_code == 204) -# -# def conceal_member(self, username): -# if isinstance(username, User): -# username = username.login -# -# r = self._gh._http_resource('DELETE', ('orgs', self.login, 'public_members', username), check_status=False) -# return (r.status_code == 204) -# -# def remove_member(self, username): -# if isinstance(username, User): -# username = username.login -# -# r = self._gh._http_resource('DELETE', ('orgs', self.login, 'members', username), check_status=False) -# return (r.status_code == 204) -# -# def public_members(self, limit=None): -# return self._gh._get_resources(('orgs', self.login, 'public_members'), User, limit=limit) -# -# def is_public_member(self, username): -# if isinstance(username, User): -# username = username.login -# -# r = self._gh._http_resource('GET', ('orgs', self.login, 'public_members', username), check_status=False) -# return (r.status_code == 204) -# -# + def idl(self): + raise NotImplementedError('Each model need subcass that method') diff --git a/github3/models/gists.py b/github3/models/gists.py index 5ad61c3..d1b416d 100644 --- a/github3/models/gists.py +++ b/github3/models/gists.py @@ -7,43 +7,71 @@ from .base import BaseResource from .user import User class File(BaseResource): - _strs = ['filename', 'raw_url', 'content', 'language', 'type'] - _ints = ['size'] + """ File model """ + + @classmethod + def idl(self): + return { + 'strs': ['filename', 'raw_url', 'content', 'language', 'type'], + 'ints': ['size'], + } def __repr__(self): return '<File gist> %s' % self.filename class GistFork(BaseResource): - _strs = ['url'] - _dates = ['created_at'] - _map = {'user': User} + """ GistFork model """ + + @classmethod + def idl(self): + return { + 'strs': ['url'], + 'dates': ['created_at'], + 'maps': {'user': User} + } def __repr__(self): return '<Gist fork> %s>' % self.user.login class ChangeStatus(BaseResource): - _ints = ['deletions', 'additions', 'total'] + """ ChangeStatus model """ + + @classmethod + def idl(self): + return { + 'ints': ['deletions', 'additions', 'total'], + } def __repr__(self): return '<Gist history> change_status>' class GistHistory(BaseResource): - _strs = ['url', 'version'] - _map = {'user': User, 'change_status': ChangeStatus} - _dates = ['committed_at'] + """ """ + + @classmethod + def idl(self): + return { + 'strs': ['url', 'version'], + 'maps': {'user': User, 'change_status': ChangeStatus}, + 'dates': ['committed_at'], + } + + def __repr__(self): + return '<GistHistory %s/%s>' % (self.user, self.committed_at) class Gist(BaseResource): - _strs = ['url', 'description', 'html_url', 'git_pull_url', 'git_push_url'] - _ints = ['id', 'comments'] - _bools = ['public'] - _dates = ['created_at'] - _map = {'user': User} - _list_map = {'files': File, 'forks': GistFork, 'history': GistHistory} + """ """ - @property - def ri(self): - return ('users', self.user.login, self.id) + @classmethod + def idl(self): + return { + 'strs': ['url', 'description', 'html_url', 'git_pull_url', 'git_push_url'], + 'ints': ['id', 'comments'], + 'bools': ['public'], + 'dates': ['created_at'], + 'maps': {'user': User}, + 'collection_maps': {'files': File, 'forks': GistFork, 'history': GistHistory}, + } def __repr__(self): - return '<gist %s/%s>' % (self.user.login, self.description) - + return '<Gist %s/%s>' % (self.user, self.description) diff --git a/github3/models/orgs.py b/github3/models/orgs.py index 1ce638e..840b51a 100644 --- a/github3/models/orgs.py +++ b/github3/models/orgs.py @@ -9,20 +9,17 @@ from .user import Plan class Org(BaseResource): """Github Organization object model.""" - _strs = [ - 'login', 'url', 'avatar_url', 'name', 'company', 'blog', 'location', 'email' - 'html_url', 'type', 'billing_email'] - _ints = [ - 'id', 'public_repos', 'public_gists', 'followers', 'following', - 'total_private_repos', 'owned_private_repos', 'private_gists', 'disk_usage', - 'collaborators'] - _dates = ['created_at'] - _map = {'plan': Plan} - _writable = ['billing_email', 'blog', 'company', 'email', 'location', 'name'] - - @property - def ri(self): - return ('orgs', self.login) + @classmethod + def idl(self): + return { + 'strs': ['login', 'url', 'avatar_url', 'name', 'company', 'blog', + 'location', 'email', 'html_url', 'type', 'billing_email'], + 'ints': ['id', 'public_repos', 'public_gists', 'followers', + 'following', 'total_private_repos', 'owned_private_repos', + 'private_gists', 'disk_usage', 'collaborators'], + 'dates': ['created_at'], + 'maps': {'plan': plan} + } def __repr__(self): - return '<org {0}>'.format(self.login) + return '<Org %s>' % self.login diff --git a/github3/models/repos.py b/github3/models/repos.py index 8dbe970..ba6ac33 100644 --- a/github3/models/repos.py +++ b/github3/models/repos.py @@ -8,23 +8,24 @@ from .user import User from .orgs import Org class Repo(BaseResource): - _strs = [ - 'url', 'html_url', 'clone_url', 'git_url', 'ssh_url', 'svn_url', - 'name', 'description', 'homepage', 'language', 'master_branch'] - _bools = ['private', 'fork', 'has_issues', 'has_wiki', 'has_downloads'] - _ints = ['forks', 'watchers', 'size', 'open_issues'] - _dates = ['pushed_at', 'created_at'] - _map = { - 'owner': User, - 'organization': Org, - 'parent': 'self', - 'source': 'self', - } + """ Repo model """ - @property - def ri(self): - return ('repos', self.owner.login, self.name) + @classmethod + def idl(self): + return { + 'strs': [ + 'url', 'html_url', 'clone_url', 'git_url', 'ssh_url', 'svn_url', + 'name', 'description', 'homepage', 'language', 'master_branch'], + 'ints': ['forks', 'watchers', 'size', 'open_issues'], + 'dates': ['created_at', 'pushed_at'], + 'bools': ['private', 'fork', 'has_issues', 'has_wiki', 'has_downloads'], + 'maps': { + 'owner': User, + 'organization': Org, + 'parent': 'self', + 'source': 'self', + } + } def __repr__(self): - return '<Repo {0}/{1}>'.format(self.owner.login, self.name) - # owner + return '<Repo %s/%s>' % (self.owner.login, self.name) diff --git a/github3/models/user.py b/github3/models/user.py index 3f0efc4..7ec7999 100644 --- a/github3/models/user.py +++ b/github3/models/user.py @@ -8,59 +8,58 @@ from .base import BaseResource class Plan(BaseResource): """Github Plan object model.""" - _strs = ['name'] - _ints = ['space', 'collaborators', 'private_repos'] + @classmethod + def idl(self): + return { + 'strs': ['name'], + 'ints': ['space', 'collaborators', 'private_repos'], + } def __repr__(self): - return '<Plan {0}>'.format(str(self.name)) + return '<Plan %s>' % self.name class Key(BaseResource): """Github Key object model.""" - _strs = ['url', 'title', 'key'] - _ints = ['id'] + @classmethod + def idl(self): + return { + 'strs': ['url', 'title', 'key'], + 'ints': ['id'], + } def __repr__(self): - return '<Key {0}>'.format(str(self.title)) + return '<Key %s>' % self.title class User(BaseResource): """Github User object model.""" - _strs = [ - 'login','avatar_url', 'url', 'name', 'company', 'blog', 'location', - 'email', 'bio', 'html_url', 'type'] - - _ints = ['id', 'public_repos', 'public_gists', 'followers', 'following'] - _dates = ['created_at',] - _bools = ['hireable', ] - - @property - def ri(self): - return ('users', self.login) + @classmethod + def idl(self): + return { + 'strs': ['login','avatar_url', 'url', 'name', 'company', 'blog', + 'location', 'email', 'bio', 'html_url', 'type'], + 'ints': [ + 'id', 'public_repos', 'public_gists', 'followers', 'following', + 'total_private_repos', 'owned_private_repos', 'private_gists', + 'disk_usage', 'collaborators'], + 'maps': {'plan': Plan}, + 'dates': ['created_at',], + 'bools': ['hireable', ], + } def __repr__(self): - return '<model.User {0}>'.format(self.login) + return '<User %s>' % self.login - def handler(self): - return self._gh.user_handler(self.login, force=True) + #def handler(self): + # return self._gh.user_handler(self.login, force=True) class AuthUser(User): - """Github Current User object model.""" - - _ints = [ - 'id', 'public_repos', 'public_gists', 'followers', 'following', - 'total_private_repos', 'owned_private_repos', 'private_gists', - 'disk_usage', 'collaborators'] - _map = {'plan': Plan} - _writeable = ['name', 'email', 'blog', 'company', 'location', 'hireable', 'bio'] - - def handler(self): - return self._gh.user_handler(self.login, force=True, private=True) + """Github Authenticated User object model.""" - @property - def ri(self): - return ('user',) + #def handler(self): + # return self._gh.user_handler(self.login, force=True, private=True) def __repr__(self): - return '<model.AuthUser {0}>'.format(self.login) + return '<AuthUser %s>' % self.login |