diff --git a/.gitignore b/.gitignore index b802fae..a6cb653 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -toy.py \ No newline at end of file +.workon +toy.py diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..1f3ab8c --- /dev/null +++ b/AUTHORS @@ -0,0 +1,13 @@ +python-github3 is written and maintained by Kenneth Reitz and +various contributors: + +Development Lead +```````````````` + +- Kenneth Reitz + + +Patches and Suggestions +``````````````````````` + +- Mahdi Yusuf \ No newline at end of file diff --git a/HACKING b/HACKING new file mode 100644 index 0000000..018f9b7 --- /dev/null +++ b/HACKING @@ -0,0 +1,14 @@ +Where possible, please follow PEP8 with regard to coding style. Sometimes the line +length restriction is too hard to follow, so don't bend over backwards there. + +Triple-quotes should always be """, single quotes are ' unless using " +would result in less escaping within the string. + +All modules, functions, and methods should be well documented reStructuredText for +Sphinx AutoDoc. + +All functionality should be available in pure Python. Optional C (via Cython) +implementations may be written for performance reasons, but should never +replace the Python implementation. + +Lastly, don't take yourself too seriously :) \ No newline at end of file diff --git a/LICENSE b/LICENSE index 8cb0518..8a9ee98 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,13 @@ -Copyright (c) 2011 Kenneth Reitz +Copyright (c) 2011 Kenneth Reitz. -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. \ No newline at end of file diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..4c7a2bc --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include README.rst LICENSE AUTHORS \ No newline at end of file diff --git a/Makefile b/Makefile deleted file mode 100644 index 74a993d..0000000 --- a/Makefile +++ /dev/null @@ -1,3 +0,0 @@ -init: - pip install -r requirements.txt - diff --git a/NOTICE b/NOTICE new file mode 100644 index 0000000..0a0d3fe --- /dev/null +++ b/NOTICE @@ -0,0 +1,65 @@ +python-github3 includes some vendorized python libraries: ordereddict, omijson, and simplejson. + + +OrderedDict License +=================== + +Copyright (c) 2009 Raymond Hettinger + +Permission is hereby granted, free of charge, to any person +obtaining a copy of this software and associated documentation files +(the "Software"), to deal in the Software without restriction, +including without limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of the Software, +and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + + The above copyright notice and this permission notice shall be + included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES + OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT + HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, + WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + OTHER DEALINGS IN THE SOFTWARE. + + + +OmniJSON License +================== + +Copyright (c) 2011 Kenneth Reitz + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +SimpleJSON License +================== + + +Copyright (c) 2006 Bob Ippolito + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.rst b/README.rst index 51ad11a..4e5df72 100644 --- a/README.rst +++ b/README.rst @@ -1,36 +1,78 @@ -Github3: Python API Wrapper -=========================== +Github3: Python wrapper for the (new) GitHub API v3 +=================================================== + +Github has a new API. This is the best Python wrapper for it. + +**This a work in progress.** Should be relased soon. -This is an awesome Python wrapper for the Github API. Usage ----- -Detailed docs forthcoming. +:: + + import github3 + + gh = github3.basic_auth('username', 'password') + + gh.get_repo('kennethreitz', 'python-github3') + + + Installation ------------ -To install github3, simply:: +To install Github3, simply: :: $ pip install github3 -Or, if you absolutely must:: +Or, if you absolutely must: :: $ easy_install github3 -But, you `really shouldn't do that `_. +But, you really shouldn't do that. + License ------- -Copyright (c) 2011 Kenneth Reitz +ISC License. :: -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + Copyright (c) 2011, Kenneth Reitz -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + Permission to use, copy, modify, and/or distribute this software for any + purpose with or without fee is hereby granted, provided that the above + copyright notice and this permission notice appear in all copies. + + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + + +Contribute +---------- + +If you'd like to contribute, simply fork `the repository`_, commit your changes +to the **develop** branch (or branch off of it), and send a pull request. Make +sure you add yourself to AUTHORS_. + + + +Roadmap +------- -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +- Get it Started +- HTTP BASIC +- Get it working +- Sphinx Documetnation +- Examples +- Unittests +- OAuth Last (how?) \ No newline at end of file diff --git a/github3/__init__.py b/github3/__init__.py index 02ad01e..c2fdbad 100644 --- a/github3/__init__.py +++ b/github3/__init__.py @@ -1,39 +1,3 @@ # -*- coding: utf-8 -*- -# I8 ,dPYb, ,dPYb, -# I8 IP'`Yb IP'`Yb -# gg 88888888 I8 8I I8 8I -# "" I8 I8 8' I8 8' -# ,gggg,gg gg I8 I8 dPgg, gg gg I8 dP -# dP" "Y8I 88 I8 I8dP" "8I I8 8I I8dP 88gg -# i8' ,8I 88 ,I8, I8P I8 I8, ,8I I8P 8I -# ,d8, ,d8I _,88,_ ,d88b, ,d8 I8,,d8b, ,d8b,,d8b, ,8I -# P"Y8888P"8888P""Y8 8P""Y8 88P `Y88P'"Y88P"`Y88P'"Y88P"' -# ,d8I' -# ,dP'8I -# ,8" 8I -# I8 8I -# `8, ,8I -# `Y8P" - - -""" -github3 -~~~~~~~ - -:copyright: (c) 2011 by Kenneth Reitz. -:license: MIT, see LICENSE for more details. -""" - -# Meta. - -__title__ = 'github3' -__author__ = 'Kenneth Reitz' -__copyright__ = 'Copyright 2011 Kenneth Reitz' -__license__ = 'MIT' - -__version__ = '0.0.1' - -# Module namespace. - -from .core import login \ No newline at end of file +from core import * \ No newline at end of file diff --git a/github3/api.py b/github3/api.py index b210135..d6d7ba0 100644 --- a/github3/api.py +++ b/github3/api.py @@ -2,62 +2,36 @@ """ github3.api -~~~~~~~~~~ +~~~~~~~~~~~ -This module provides the basic API interface for Github. +This module provies the core GitHub3 API interface. """ -import json +from .packages import omnijson as json +from .packages.link_header import parse_link_value -import requests -from .helpers import is_collection -from .structures import KeyedListResource from .models import * +from .helpers import is_collection, to_python, to_api, get_scope +from .config import settings -GITHUB_URL = 'https://api.github.com' - - -class GithubCore(object): - """The core Github class.""" - def __init__(self): - super(GithubCore, self).__init__() - - #: The User's API Key. - self._api_key = None - self._api_key_verified = None - self._s = requests.session() - self._github_url = GITHUB_URL - # We only want JSON back. - self._s.headers.update({'Accept': 'application/json'}) - - def __repr__(self): - return '' % (id(self)) +import requests - def login(self, username, password): - """Logs user into Github with given credentials.""" +from decorator import decorator - # Attach auth to session. - self._s.auth = (username.password) - return True +class GithubCore(object): - @property - def is_authenticated(self): - if self._api_key_verified is None: - return self._verify_api_key() - else: - return self._api_key_verified + _rate_limit = None + _rate_limit_remaining = None - def _url_for(self, *args): - args = map(str, args) - return '/'.join([self._github_url] + list(args)) @staticmethod def _resource_serialize(o): """Returns JSON serialization of given object.""" return json.dumps(o) + @staticmethod def _resource_deserialize(s): """Returns dict deserialization of a given JSON string.""" @@ -67,64 +41,123 @@ def _resource_deserialize(s): except ValueError: raise ResponseError('The API Response was not valid.') - def _http_resource(self, method, resource, params=None, data=None): - """Makes an HTTP request.""" - if not is_collection(resource): - resource = [resource] + @staticmethod + def _generate_url(endpoint): + """Generates proper endpoint URL.""" + + if is_collection(endpoint): + resource = map(str, endpoint) + resource = '/'.join(endpoint) + else: + resource = endpoint + + return (settings.base_url + resource) + + + def _requests_pre_hook(*args, **kwargs): + """Pre-processing for HTTP requests arguments.""" + + return args, kwargs + + + def _requests_post_hook(self, r): + """Post-processing for HTTP response objects.""" + + self._ratelimit = int(r.headers.get('x-ratelimit-limit', -1)) + self._ratelimit_remaining = int(r.headers.get('x-ratelimit-remaining', -1)) + + return r - url = self._url_for(*resource) - r = self._s.request(method, url, params=params, data=data) + + def _http_resource(self, verb, endpoint, params=None, authed=True): + + url = self._generate_url(endpoint) + + if authed: + args, kwargs = self._requests_pre_hook(verb, url, params=params) + else: + args = (verb, url) + kwargs = {'params': params} + + r = requests.request(*args, **kwargs) + r = self._requests_post_hook(r) + + # print self._ratelimit_remaining r.raise_for_status() return r - def _get_resource(self, resource, obj, params=None, **kwargs): - """Returns a mapped object from an HTTP resource.""" - r = self._http_resource('GET', resource, params=params) + + def _get_resource(self, resource, obj, authed=True, **kwargs): + + r = self._http_resource('GET', resource, params=kwargs, authed=authed) item = self._resource_deserialize(r.content) - return obj.new_from_dict(item, h=self, **kwargs) + return obj.new_from_dict(item, gh=self) + - def _get_resources(self, resource, obj, params=None, map=None, **kwargs): - """Returns a list of mapped objects from an HTTP resource.""" - r = self._http_resource('GET', resource, params=params) + def _get_resources(self, resource, obj, authed=True, **kwargs): + + r = self._http_resource('GET', resource, params=kwargs, authed=authed) d_items = self._resource_deserialize(r.content) - items = [obj.new_from_dict(item, h=self, **kwargs) for item in d_items] + items = [] + + for item in d_items: + items.append(obj.new_from_dict(item, gh=self)) - if map is None: - map = KeyedListResource + return items - list_resource = map(items=items) - list_resource._h = self - list_resource._obj = obj - return list_resource + def _to_map(self, obj, iterable): + """Maps given dict iterable to a given Resource object.""" + + a = list() + + for it in iterable: + a.append(obj.new_from_dict(it, rdd=self)) + + return a + + def _get_url(self, resource): + + if is_collection(resource): + resource = map(str, resource) + resource = '/'.join(resource) + + return resource + class Github(GithubCore): - """The main Github class.""" + """docstring for Github""" def __init__(self): super(Github, self).__init__() + self.is_authenticated = False + + + def get_user(self, username): + """Get a single user.""" + return self._get_resource(('users', username), User) + + + def get_me(self): + """Get the authenticated user.""" + return self._get_resource(('user'), CurrentUser) - def __repr__(self): - return '' % (id(self)) + # def get_repos(self, username): + # """Get repos.""" + # return self._get_resource(('user', 'username', 'repos'), Repo) - # @property - # def addons(self): - # return self._get_resources(('addons'), Addon) + def get_repo(self, username, reponame): + """Get the authenticated user.""" + return self._get_resource(('repos', username, reponame), Repo) - # @property - # def apps(self): - # return self._get_resources(('apps'), App) - # @property - # def keys(self): - # return self._get_resources(('user', 'keys'), Key, map=SSHKeyListResource) +class ResponseError(Exception): + """The API Response was unexpected.""" -class ResponseError(ValueError): - """The API Response was unexpected.""" \ No newline at end of file diff --git a/github3/config.py b/github3/config.py new file mode 100644 index 0000000..9fbf305 --- /dev/null +++ b/github3/config.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- + +""" +github3.config +~~~~~~~~~~~~~~ + +This module provides the Github3 settings feature set. + +""" + +class Settings(object): + _singleton = {} + + # attributes with defaults + __attrs__ = [] + + def __init__(self, **kwargs): + super(Settings, self).__init__() + + self.__dict__ = self._singleton + + + def __call__(self, *args, **kwargs): + # new instance of class to call + r = self.__class__() + + # cache previous settings for __exit__ + r.__cache = self.__dict__.copy() + map(self.__cache.setdefault, self.__attrs__) + + # set new settings + self.__dict__.update(*args, **kwargs) + + return r + + + def __enter__(self): + pass + + + def __exit__(self, *args): + + # restore cached copy + self.__dict__.update(self.__cache.copy()) + del self.__cache + + + def __getattribute__(self, key): + if key in object.__getattribute__(self, '__attrs__'): + try: + return object.__getattribute__(self, key) + except AttributeError: + return None + return object.__getattribute__(self, key) + +settings = Settings() +settings.verbose = False +settings.base_url = 'https://api.github.com/' \ No newline at end of file diff --git a/github3/core.py b/github3/core.py index 071dea2..725d09d 100644 --- a/github3/core.py +++ b/github3/core.py @@ -2,19 +2,35 @@ """ github3.core -~~~~~~~~~~~~~ +~~~~~~~~~~~~ -This module provides the base entrypoint for github3. +This module provides the entrance point for the GitHub3 module. """ -from .api import Github +__version__ = '0.0.0' +__license__ = 'MIT' +__author__ = 'Kenneth Reitz' -def login(username, password): - """Returns an authenticated Github instance, via API Key.""" +from .api import Github, settings - gh = Github() - # Login. - gh.login(username, password) +def no_auth(): + """Returns an un-authenticated Github object.""" + + gh = Github() return gh + + +def basic_auth(username, password): + """Returns an authenticated Github object, via HTTP Basic.""" + + def enable_auth(*args, **kwargs): + kwargs['auth'] = (username, password) + return args, kwargs + + gh = Github() + gh.is_authenticated = True + gh._requests_pre_hook = enable_auth + + return gh \ No newline at end of file diff --git a/github3/helpers.py b/github3/helpers.py index d742acb..91b4be5 100644 --- a/github3/helpers.py +++ b/github3/helpers.py @@ -2,15 +2,17 @@ """ github3.helpers -~~~~~~~~~~~~~~~ +~~~~~~~~~~~~~~~~ -This module contians the helpers. +This module provides various helper functions to the rest of the package. """ +import inspect from datetime import datetime from dateutil.parser import parse as parse_datetime + def is_collection(obj): """Tests if an object is a collection.""" @@ -23,17 +25,14 @@ def is_collection(obj): return val - -# from kennethreitz/python-github3 +# from arc90/python-readability-api def to_python(obj, in_dict, str_keys=None, date_keys=None, int_keys=None, object_map=None, - bool_keys=None, - dict_keys=None, - **kwargs): + bool_keys=None, **kwargs): """Extends a given object for API Consumption. :param obj: Object to extend. @@ -43,53 +42,41 @@ def to_python(obj, :param object_map: Dict of {key, obj} map, for nested object results. """ - d = dict() - if str_keys: for in_key in str_keys: - d[in_key] = in_dict.get(in_key) + obj.__dict__[in_key] = in_dict.get(in_key) if date_keys: for in_key in date_keys: in_date = in_dict.get(in_key) try: - out_date = parse_datetime(in_date) - except TypeError, e: - raise e + out_date = datetime.strptime(in_date, '%Y-%m-%dT%H:%M:%SZ') + except TypeError: out_date = None - d[in_key] = out_date + obj.__dict__[in_key] = out_date if int_keys: for in_key in int_keys: if (in_dict is not None) and (in_dict.get(in_key) is not None): - d[in_key] = int(in_dict.get(in_key)) + obj.__dict__[in_key] = int(in_dict.get(in_key)) if bool_keys: for in_key in bool_keys: if in_dict.get(in_key) is not None: - d[in_key] = bool(in_dict.get(in_key)) - - if dict_keys: - for in_key in dict_keys: - if in_dict.get(in_key) is not None: - d[in_key] = dict(in_dict.get(in_key)) + obj.__dict__[in_key] = bool(in_dict.get(in_key)) if object_map: for (k, v) in object_map.items(): if in_dict.get(k): - d[k] = v.new_from_dict(in_dict.get(k)) + obj.__dict__[k] = v.new_from_dict(in_dict.get(k)) - obj.__dict__.update(d) obj.__dict__.update(kwargs) - # Save the dictionary, for write comparisons. - obj._cache = d - obj.__cache = in_dict - return obj +# from arc90/python-readability-api def to_api(in_dict, int_keys=None, date_keys=None, bool_keys=None): """Extends a given object for API Production.""" @@ -123,3 +110,28 @@ def to_api(in_dict, int_keys=None, date_keys=None, bool_keys=None): del in_dict[k] return in_dict + + + +# from kennethreitz/showme +def get_scope(f, args=None): + """Get scope of given function for Exception scopes.""" + + if args is None: + args=list() + + scope = inspect.getmodule(f).__name__ + # guess that function is a method of it's class + try: + if f.func_name in dir(args[0].__class__): + scope += '.' + args[0].__class__.__name__ + scope += '.' + f.__name__ + else: + scope += '.' + f.__name__ + except IndexError: + scope += '.' + f.__name__ + + # scrub readability.models namespace + scope = scope.replace('readability.api.', '') + + return scope \ No newline at end of file diff --git a/github3/models.py b/github3/models.py index 4380b62..fe9e6aa 100644 --- a/github3/models.py +++ b/github3/models.py @@ -1,263 +1,124 @@ -# -*- coding: utf-8 -*- - """ github3.models ~~~~~~~~~~~~~~ -This module contains the models that comprise the Github API. +This module provides the Github3 object model. """ -import json -from urllib import quote - -import requests -from .helpers import to_python -from .structures import * +from .helpers import to_python, to_api class BaseResource(object): + """A BaseResource object.""" _strs = [] _ints = [] _dates = [] _bools = [] - _dicts = [] _map = {} - _pks = [] + _writeable = [] + _modified = [] + def __init__(self): self._bootstrap() - self._h = None super(BaseResource, self).__init__() - def __repr__(self): - return "".format(self._id) - - 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 __dir__(self): + d = self.__dict__.copy() - @property - def _id(self): try: - return getattr(self, self._pks[0]) - except IndexError: - return None + del d['_gh'] + except KeyError: + pass - @property - def _ids(self): - """The list of primary keys to validate against.""" - for pk in self._pks: - yield getattr(self, pk) + return d.keys() - for pk in self._pks: - - try: - yield str(getattr(self, pk)) - except ValueError: - pass + def _bootstrap(self): + """Bootstraps the model object based on configured values.""" - def dict(self): - d = dict() - for k in self.keys(): - d[k] = self.__dict__.get(k) + for attr in (self._strs + self._ints + self._dates + self._bools + self._map.keys()): + setattr(self, attr, None) - return d @classmethod - def new_from_dict(cls, d, h=None, **kwargs): - - d = to_python( - obj=cls(), - in_dict=d, - str_keys=cls._strs, - int_keys=cls._ints, - date_keys=cls._dates, - bool_keys=cls._bools, - dict_keys= cls._dicts, - object_map=cls._map, - _h = h + 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, + _gh = gh ) - d.__dict__.update(kwargs) - - return d + def update(self): + pass + def setattr(self, k, v): + # TODO: when writable key changed, + pass -class App(BaseResource): - """Heroku App.""" +class Plan(BaseResource): + """Github Plan object model.""" - _strs = ['name', 'create_status', 'stack', 'repo_migrate_status'] - _ints = ['id', 'slug_size', 'repo_size', 'dynos', 'workers'] - _dates = ['created_at',] - _pks = ['name', 'id'] - - def __init__(self): - super(App, self).__init__() + _strs = ['name'] + _ints = ['space', 'collaborators', 'private_repos'] def __repr__(self): - return "".format(self.name) - - def new(self, name=None, stack='cedar'): - """Creates a new app.""" - - payload = {} - - if name: - payload['app[name]'] = name - - if stack: - payload['app[stack]'] = stack + return ''.format(str(self.name)) - r = self._h._http_resource( - method='POST', - resource=('apps',), - data=payload - ) - - name = json.loads(r.content).get('name') - return self._h.apps.get(name) - @property - def addons(self): - return self._h._get_resources( - resource=('apps', self.name, 'addons'), - obj=Addon, app=self - ) - @property - def collaborators(self): - """The collaborators for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'collaborators'), - obj=Collaborator, app=self - ) +class User(BaseResource): + """Github User object model.""" - @property - def domains(self): - """The domains for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'domains'), - obj=Domain, app=self - ) - - @property - def releases(self): - """The releases for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'releases'), - obj=Release, app=self - ) + _strs = [ + 'login','gravatar_url', 'url', 'name', 'company', 'blog', 'location', + 'email', 'bio', 'html_url'] - @property - def processes(self): - """The proccesses for this app.""" - return self._h._get_resources( - resource=('apps', self.name, 'ps'), - obj=Process, app=self, map=ProcessListResource - ) - - @property - def config(self): - """The envs for this app.""" - - return self._h._get_resource( - resource=('apps', self.name, 'config_vars'), - obj=ConfigVars, app=self - ) - - @property - def info(self): - """Returns current info for this app.""" - - return self._h._get_resource( - resource=('apps', self.name), - obj=App, - ) - - def rollback(self, release): - """Rolls back the release to the given version.""" - r = self._h._http_resource( - method='POST', - resource=('apps', self.name, 'releases'), - data={'rollback': release} - ) - return self.releases[-1] - - - def rename(self, name): - """Renames app to given name.""" - - r = self._h._http_resource( - method='PUT', - resource=('apps', self.name), - data={'app[name]': name} - ) - return r.ok - - def transfer(self, user): - """Transfers app to given username's account.""" - - r = self._h._http_resource( - method='PUT', - resource=('apps', self.name), - data={'app[transfer_owner]': user} - ) - return r.ok - - def maintenance(self, on=True): - """Toggles maintenance mode.""" - - r = self._h._http_resource( - method='POST', - resource=('apps', self.name, 'server', 'maintenance'), - data={'maintenance_mode': int(on)} - ) - return r.ok - - def destroy(self): - """Destoys the app. Do be careful.""" - - r = self._h._http_resource( - method='DELETE', - resource=('apps', self.name) - ) - return r.ok + _ints = ['id', 'public_repos', 'public_gists', 'followers', 'following'] + _dates = ['created_at',] + _bools = ['hireable', ] + # _map = {} + # _writeable = [] - def logs(self, num=None, source=None, tail=False): - """Returns the requested log.""" + def __repr__(self): + return ''.format(self.login) - # Bootstrap payload package. - payload = {'logplex': 'true'} + def repos(self): + # return self._gh.get_repos(username=self.login) + repos = self._gh._get_resources(('users', self.login, 'repos'), Repo) + return repos - if num: - payload['num'] = num - if source: - payload['source'] = source +class CurrentUser(User): + """Github Current User object model.""" - if tail: - payload['tail'] = 1 + _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'] - # Grab the URL of the logplex endpoint. - r = self._h._http_resource( - method='GET', - resource=('apps', self.name, 'logs'), - data=payload - ) + def __repr__(self): + return ''.format(self.login) - # Grab the actual logs. - r = requests.get(r.content) - if not tail: - return r.content - else: - # Return line iterator for tail! - return r.iter_lines() +class Repo(BaseResource): + _strs = [ + 'url', 'html_url', 'clone_url', 'git_url', 'ssh_url', 'svn_url', + 'name', 'description', 'homepage', 'language', 'master_branch'] + _bools = ['private', 'fork'] + _ints = ['forks', 'watchers', 'size',] + _dates = ['pushed_at', 'created_at'] + _map = {'owner': User} + def __repr__(self): + return ''.format(self.owner.login, self.name) + # owner diff --git a/github3/packages/__init__.py b/github3/packages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/github3/packages/link_header.py b/github3/packages/link_header.py new file mode 100644 index 0000000..3959604 --- /dev/null +++ b/github3/packages/link_header.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python + +""" +HTTP Link Header Parsing + +Simple routines to parse and manipulate Link headers. +""" + +__license__ = """ +Copyright (c) 2009 Mark Nottingham + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +""" + +import re + +TOKEN = r'(?:[^\(\)<>@,;:\\"/\[\]\?={} \t]+?)' +QUOTED_STRING = r'(?:"(?:\\"|[^"])*")' +PARAMETER = r'(?:%(TOKEN)s(?:=(?:%(TOKEN)s|%(QUOTED_STRING)s))?)' % locals() +LINK = r'<[^>]*>\s*(?:;\s*%(PARAMETER)s?\s*)*' % locals() +COMMA = r'(?:\s*(?:,\s*)+)' +LINK_SPLIT = r'%s(?=%s|\s*$)' % (LINK, COMMA) + +def _unquotestring(instr): + if instr[0] == instr[-1] == '"': + instr = instr[1:-1] + instr = re.sub(r'\\(.)', r'\1', instr) + return instr +def _splitstring(instr, item, split): + if not instr: + return [] + return [ h.strip() for h in re.findall(r'%s(?=%s|\s*$)' % (item, split), instr)] + +link_splitter = re.compile(LINK_SPLIT) + +def parse_link_value(instr): + """ + Given a link-value (i.e., after separating the header-value on commas), + return a dictionary whose keys are link URLs and values are dictionaries + of the parameters for their associated links. + + Note that internationalised parameters (e.g., title*) are + NOT percent-decoded. + + Also, only the last instance of a given parameter will be included. + + For example, + + >>> parse_link_value('; rel="self"; title*=utf-8\'de\'letztes%20Kapitel') + {'/foo': {'title*': "utf-8'de'letztes%20Kapitel", 'rel': 'self'}} + + """ + out = {} + if not instr: + return out + for link in [h.strip() for h in link_splitter.findall(instr)]: + url, params = link.split(">", 1) + url = url[1:] + param_dict = {} + for param in _splitstring(params, PARAMETER, "\s*;\s*"): + try: + a, v = param.split("=", 1) + param_dict[a.lower()] = _unquotestring(v) + except ValueError: + param_dict[param.lower()] = None + out[url] = param_dict + return out + + +if __name__ == '__main__': + import sys + if len(sys.argv) > 1: + print parse_link_value(sys.argv[1]) \ No newline at end of file diff --git a/github3/packages/omnijson/__init__.py b/github3/packages/omnijson/__init__.py new file mode 100644 index 0000000..c10c328 --- /dev/null +++ b/github3/packages/omnijson/__init__.py @@ -0,0 +1,13 @@ +# -*- coding: utf-8 -*- + +from __future__ import absolute_import + +from .core import loads, dumps, JSONError + + +__all__ = ('loads', 'dumps', 'JSONError') + + +__version__ = '0.1.2' +__author__ = 'Kenneth Reitz' +__license__ = 'MIT' diff --git a/github3/packages/omnijson/core.py b/github3/packages/omnijson/core.py new file mode 100644 index 0000000..8b49537 --- /dev/null +++ b/github3/packages/omnijson/core.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- + +""" +omijson.core +~~~~~~~~~~~~ + +This module provides the core omnijson functionality. + +""" + +import sys + +engine = None +_engine = None + + +options = [ + ['ujson', 'loads', 'dumps', (ValueError,)], + ['yajl', 'loads', 'dumps', (TypeError, ValueError)], + ['jsonlib2', 'read', 'write', (ValueError,)], + ['jsonlib', 'read', 'write', (ValueError,)], + ['simplejson', 'loads', 'dumps', (TypeError, ValueError)], + ['json', 'loads', 'dumps', (TypeError, ValueError)], + ['simplejson_from_packages', 'loads', 'dumps', (ValueError,)], +] + + +def _import(engine): + try: + if '_from_' in engine: + engine, package = engine.split('_from_') + m = __import__(package, globals(), locals(), [engine], -1) + return getattr(m, engine) + + return __import__(engine) + + except ImportError: + return False + + +def loads(s, **kwargs): + """Loads JSON object.""" + + try: + return _engine[0](s) + + except: + # crazy 2/3 exception hack + # http://www.voidspace.org.uk/python/weblog/arch_d7_2010_03_20.shtml + + ExceptionClass, why = sys.exc_info()[:2] + + if any([(issubclass(ExceptionClass, e)) for e in _engine[2]]): + raise JSONError(why) + else: + raise why + + +def dumps(o, **kwargs): + """Dumps JSON object.""" + + try: + return _engine[1](o) + + except: + ExceptionClass, why = sys.exc_info()[:2] + + if any([(issubclass(ExceptionClass, e)) for e in _engine[2]]): + raise JSONError(why) + else: + raise why + + +class JSONError(ValueError): + """JSON Failed.""" + + +# ------ +# Magic! +# ------ + + +for e in options: + + __engine = _import(e[0]) + + if __engine: + engine, _engine = e[0], e[1:4] + + for i in (0, 1): + _engine[i] = getattr(__engine, _engine[i]) + + break diff --git a/github3/packages/omnijson/packages/__init__.py b/github3/packages/omnijson/packages/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/github3/packages/omnijson/packages/simplejson/__init__.py b/github3/packages/omnijson/packages/simplejson/__init__.py new file mode 100644 index 0000000..210b957 --- /dev/null +++ b/github3/packages/omnijson/packages/simplejson/__init__.py @@ -0,0 +1,438 @@ +r"""JSON (JavaScript Object Notation) is a subset of +JavaScript syntax (ECMA-262 3rd edition) used as a lightweight data +interchange format. + +:mod:`simplejson` exposes an API familiar to users of the standard library +:mod:`marshal` and :mod:`pickle` modules. It is the externally maintained +version of the :mod:`json` library contained in Python 2.6, but maintains +compatibility with Python 2.4 and Python 2.5 and (currently) has +significant performance advantages, even without using the optional C +extension for speedups. + +Encoding basic Python object hierarchies:: + + >>> import simplejson as json + >>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) + '["foo", {"bar": ["baz", null, 1.0, 2]}]' + >>> print json.dumps("\"foo\bar") + "\"foo\bar" + >>> print json.dumps(u'\u1234') + "\u1234" + >>> print json.dumps('\\') + "\\" + >>> print json.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) + {"a": 0, "b": 0, "c": 0} + >>> from StringIO import StringIO + >>> io = StringIO() + >>> json.dump(['streaming API'], io) + >>> io.getvalue() + '["streaming API"]' + +Compact encoding:: + + >>> import simplejson as json + >>> json.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) + '[1,2,3,{"4":5,"6":7}]' + +Pretty printing:: + + >>> import simplejson as json + >>> s = json.dumps({'4': 5, '6': 7}, sort_keys=True, indent=' ') + >>> print '\n'.join([l.rstrip() for l in s.splitlines()]) + { + "4": 5, + "6": 7 + } + +Decoding JSON:: + + >>> import simplejson as json + >>> obj = [u'foo', {u'bar': [u'baz', None, 1.0, 2]}] + >>> json.loads('["foo", {"bar":["baz", null, 1.0, 2]}]') == obj + True + >>> json.loads('"\\"foo\\bar"') == u'"foo\x08ar' + True + >>> from StringIO import StringIO + >>> io = StringIO('["streaming API"]') + >>> json.load(io)[0] == 'streaming API' + True + +Specializing JSON object decoding:: + + >>> import simplejson as json + >>> def as_complex(dct): + ... if '__complex__' in dct: + ... return complex(dct['real'], dct['imag']) + ... return dct + ... + >>> json.loads('{"__complex__": true, "real": 1, "imag": 2}', + ... object_hook=as_complex) + (1+2j) + >>> from decimal import Decimal + >>> json.loads('1.1', parse_float=Decimal) == Decimal('1.1') + True + +Specializing JSON object encoding:: + + >>> import simplejson as json + >>> def encode_complex(obj): + ... if isinstance(obj, complex): + ... return [obj.real, obj.imag] + ... raise TypeError(repr(o) + " is not JSON serializable") + ... + >>> json.dumps(2 + 1j, default=encode_complex) + '[2.0, 1.0]' + >>> json.JSONEncoder(default=encode_complex).encode(2 + 1j) + '[2.0, 1.0]' + >>> ''.join(json.JSONEncoder(default=encode_complex).iterencode(2 + 1j)) + '[2.0, 1.0]' + + +Using simplejson.tool from the shell to validate and pretty-print:: + + $ echo '{"json":"obj"}' | python -m simplejson.tool + { + "json": "obj" + } + $ echo '{ 1.2:3.4}' | python -m simplejson.tool + Expecting property name: line 1 column 2 (char 2) +""" +__version__ = '2.1.6' +__all__ = [ + 'dump', 'dumps', 'load', 'loads', + 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', + 'OrderedDict', +] + +__author__ = 'Bob Ippolito ' + +from decimal import Decimal + +from decoder import JSONDecoder, JSONDecodeError +from encoder import JSONEncoder +def _import_OrderedDict(): + import collections + try: + return collections.OrderedDict + except AttributeError: + import ordered_dict + return ordered_dict.OrderedDict +OrderedDict = _import_OrderedDict() + +def _import_c_make_encoder(): + try: + from simplejson._speedups import make_encoder + return make_encoder + except ImportError: + return None + +_default_encoder = JSONEncoder( + skipkeys=False, + ensure_ascii=True, + check_circular=True, + allow_nan=True, + indent=None, + separators=None, + encoding='utf-8', + default=None, + use_decimal=False, +) + +def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, indent=None, separators=None, + encoding='utf-8', default=None, use_decimal=False, **kw): + """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a + ``.write()``-supporting file-like object). + + If ``skipkeys`` is true then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is false, then the some chunks written to ``fp`` + may be ``unicode`` instances, subject to normal Python ``str`` to + ``unicode`` coercion rules. Unless ``fp.write()`` explicitly + understands ``unicode`` (as in ``codecs.getwriter()``) this is likely + to cause an error. + + If ``check_circular`` is false, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is false, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) + in strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + If *indent* is a string, then JSON array elements and object members + will be pretty-printed with a newline followed by that string repeated + for each level of nesting. ``None`` (the default) selects the most compact + representation without any newlines. For backwards compatibility with + versions of simplejson earlier than 2.1.0, an integer is also accepted + and is converted to a string with that many spaces. + + If ``separators`` is an ``(item_separator, dict_separator)`` tuple + then it will be used instead of the default ``(', ', ': ')`` separators. + ``(',', ':')`` is the most compact JSON representation. + + ``encoding`` is the character encoding for str instances, default is UTF-8. + + ``default(obj)`` is a function that should return a serializable version + of obj or raise TypeError. The default simply raises TypeError. + + If *use_decimal* is true (default: ``False``) then decimal.Decimal + will be natively serialized to JSON with full precision. + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + + """ + # cached encoder + if (not skipkeys and ensure_ascii and + check_circular and allow_nan and + cls is None and indent is None and separators is None and + encoding == 'utf-8' and default is None and not use_decimal + and not kw): + iterable = _default_encoder.iterencode(obj) + else: + if cls is None: + cls = JSONEncoder + iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, + separators=separators, encoding=encoding, + default=default, use_decimal=use_decimal, **kw).iterencode(obj) + # could accelerate with writelines in some versions of Python, at + # a debuggability cost + for chunk in iterable: + fp.write(chunk) + + +def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, + allow_nan=True, cls=None, indent=None, separators=None, + encoding='utf-8', default=None, use_decimal=False, **kw): + """Serialize ``obj`` to a JSON formatted ``str``. + + If ``skipkeys`` is false then ``dict`` keys that are not basic types + (``str``, ``unicode``, ``int``, ``long``, ``float``, ``bool``, ``None``) + will be skipped instead of raising a ``TypeError``. + + If ``ensure_ascii`` is false, then the return value will be a + ``unicode`` instance subject to normal Python ``str`` to ``unicode`` + coercion rules instead of being escaped to an ASCII ``str``. + + If ``check_circular`` is false, then the circular reference check + for container types will be skipped and a circular reference will + result in an ``OverflowError`` (or worse). + + If ``allow_nan`` is false, then it will be a ``ValueError`` to + serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in + strict compliance of the JSON specification, instead of using the + JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). + + If ``indent`` is a string, then JSON array elements and object members + will be pretty-printed with a newline followed by that string repeated + for each level of nesting. ``None`` (the default) selects the most compact + representation without any newlines. For backwards compatibility with + versions of simplejson earlier than 2.1.0, an integer is also accepted + and is converted to a string with that many spaces. + + If ``separators`` is an ``(item_separator, dict_separator)`` tuple + then it will be used instead of the default ``(', ', ': ')`` separators. + ``(',', ':')`` is the most compact JSON representation. + + ``encoding`` is the character encoding for str instances, default is UTF-8. + + ``default(obj)`` is a function that should return a serializable version + of obj or raise TypeError. The default simply raises TypeError. + + If *use_decimal* is true (default: ``False``) then decimal.Decimal + will be natively serialized to JSON with full precision. + + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the + ``.default()`` method to serialize additional types), specify it with + the ``cls`` kwarg. + + """ + # cached encoder + if (not skipkeys and ensure_ascii and + check_circular and allow_nan and + cls is None and indent is None and separators is None and + encoding == 'utf-8' and default is None and not use_decimal + and not kw): + return _default_encoder.encode(obj) + if cls is None: + cls = JSONEncoder + return cls( + skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, + separators=separators, encoding=encoding, default=default, + use_decimal=use_decimal, **kw).encode(obj) + + +_default_decoder = JSONDecoder(encoding=None, object_hook=None, + object_pairs_hook=None) + + +def load(fp, encoding=None, cls=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None, object_pairs_hook=None, + use_decimal=False, **kw): + """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing + a JSON document) to a Python object. + + *encoding* determines the encoding used to interpret any + :class:`str` objects decoded by this instance (``'utf-8'`` by + default). It has no effect when decoding :class:`unicode` objects. + + Note that currently only encodings that are a superset of ASCII work, + strings of other encodings should be passed in as :class:`unicode`. + + *object_hook*, if specified, will be called with the result of every + JSON object decoded and its return value will be used in place of the + given :class:`dict`. This can be used to provide custom + deserializations (e.g. to support JSON-RPC class hinting). + + *object_pairs_hook* is an optional function that will be called with + the result of any object literal decode with an ordered list of pairs. + The return value of *object_pairs_hook* will be used instead of the + :class:`dict`. This feature can be used to implement custom decoders + that rely on the order that the key and value pairs are decoded (for + example, :func:`collections.OrderedDict` will remember the order of + insertion). If *object_hook* is also defined, the *object_pairs_hook* + takes priority. + + *parse_float*, if specified, will be called with the string of every + JSON float to be decoded. By default, this is equivalent to + ``float(num_str)``. This can be used to use another datatype or parser + for JSON floats (e.g. :class:`decimal.Decimal`). + + *parse_int*, if specified, will be called with the string of every + JSON int to be decoded. By default, this is equivalent to + ``int(num_str)``. This can be used to use another datatype or parser + for JSON integers (e.g. :class:`float`). + + *parse_constant*, if specified, will be called with one of the + following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This + can be used to raise an exception if invalid JSON numbers are + encountered. + + If *use_decimal* is true (default: ``False``) then it implies + parse_float=decimal.Decimal for parity with ``dump``. + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + + """ + return loads(fp.read(), + encoding=encoding, cls=cls, object_hook=object_hook, + parse_float=parse_float, parse_int=parse_int, + parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, + use_decimal=use_decimal, **kw) + + +def loads(s, encoding=None, cls=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None, object_pairs_hook=None, + use_decimal=False, **kw): + """Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON + document) to a Python object. + + *encoding* determines the encoding used to interpret any + :class:`str` objects decoded by this instance (``'utf-8'`` by + default). It has no effect when decoding :class:`unicode` objects. + + Note that currently only encodings that are a superset of ASCII work, + strings of other encodings should be passed in as :class:`unicode`. + + *object_hook*, if specified, will be called with the result of every + JSON object decoded and its return value will be used in place of the + given :class:`dict`. This can be used to provide custom + deserializations (e.g. to support JSON-RPC class hinting). + + *object_pairs_hook* is an optional function that will be called with + the result of any object literal decode with an ordered list of pairs. + The return value of *object_pairs_hook* will be used instead of the + :class:`dict`. This feature can be used to implement custom decoders + that rely on the order that the key and value pairs are decoded (for + example, :func:`collections.OrderedDict` will remember the order of + insertion). If *object_hook* is also defined, the *object_pairs_hook* + takes priority. + + *parse_float*, if specified, will be called with the string of every + JSON float to be decoded. By default, this is equivalent to + ``float(num_str)``. This can be used to use another datatype or parser + for JSON floats (e.g. :class:`decimal.Decimal`). + + *parse_int*, if specified, will be called with the string of every + JSON int to be decoded. By default, this is equivalent to + ``int(num_str)``. This can be used to use another datatype or parser + for JSON integers (e.g. :class:`float`). + + *parse_constant*, if specified, will be called with one of the + following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This + can be used to raise an exception if invalid JSON numbers are + encountered. + + If *use_decimal* is true (default: ``False``) then it implies + parse_float=decimal.Decimal for parity with ``dump``. + + To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` + kwarg. + + """ + if (cls is None and encoding is None and object_hook is None and + parse_int is None and parse_float is None and + parse_constant is None and object_pairs_hook is None + and not use_decimal and not kw): + return _default_decoder.decode(s) + if cls is None: + cls = JSONDecoder + if object_hook is not None: + kw['object_hook'] = object_hook + if object_pairs_hook is not None: + kw['object_pairs_hook'] = object_pairs_hook + if parse_float is not None: + kw['parse_float'] = parse_float + if parse_int is not None: + kw['parse_int'] = parse_int + if parse_constant is not None: + kw['parse_constant'] = parse_constant + if use_decimal: + if parse_float is not None: + raise TypeError("use_decimal=True implies parse_float=Decimal") + kw['parse_float'] = Decimal + return cls(encoding=encoding, **kw).decode(s) + + +def _toggle_speedups(enabled): + import simplejson.decoder as dec + import simplejson.encoder as enc + import simplejson.scanner as scan + c_make_encoder = _import_c_make_encoder() + if enabled: + dec.scanstring = dec.c_scanstring or dec.py_scanstring + enc.c_make_encoder = c_make_encoder + enc.encode_basestring_ascii = (enc.c_encode_basestring_ascii or + enc.py_encode_basestring_ascii) + scan.make_scanner = scan.c_make_scanner or scan.py_make_scanner + else: + dec.scanstring = dec.py_scanstring + enc.c_make_encoder = None + enc.encode_basestring_ascii = enc.py_encode_basestring_ascii + scan.make_scanner = scan.py_make_scanner + dec.make_scanner = scan.make_scanner + global _default_decoder + _default_decoder = JSONDecoder( + encoding=None, + object_hook=None, + object_pairs_hook=None, + ) + global _default_encoder + _default_encoder = JSONEncoder( + skipkeys=False, + ensure_ascii=True, + check_circular=True, + allow_nan=True, + indent=None, + separators=None, + encoding='utf-8', + default=None, + ) diff --git a/github3/packages/omnijson/packages/simplejson/decoder.py b/github3/packages/omnijson/packages/simplejson/decoder.py new file mode 100644 index 0000000..3e36e56 --- /dev/null +++ b/github3/packages/omnijson/packages/simplejson/decoder.py @@ -0,0 +1,421 @@ +"""Implementation of JSONDecoder +""" +import re +import sys +import struct + +from .scanner import make_scanner +def _import_c_scanstring(): + try: + from simplejson._speedups import scanstring + return scanstring + except ImportError: + return None +c_scanstring = _import_c_scanstring() + +__all__ = ['JSONDecoder'] + +FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL + +def _floatconstants(): + _BYTES = '7FF80000000000007FF0000000000000'.decode('hex') + # The struct module in Python 2.4 would get frexp() out of range here + # when an endian is specified in the format string. Fixed in Python 2.5+ + if sys.byteorder != 'big': + _BYTES = _BYTES[:8][::-1] + _BYTES[8:][::-1] + nan, inf = struct.unpack('dd', _BYTES) + return nan, inf, -inf + +NaN, PosInf, NegInf = _floatconstants() + + +class JSONDecodeError(ValueError): + """Subclass of ValueError with the following additional properties: + + msg: The unformatted error message + doc: The JSON document being parsed + pos: The start index of doc where parsing failed + end: The end index of doc where parsing failed (may be None) + lineno: The line corresponding to pos + colno: The column corresponding to pos + endlineno: The line corresponding to end (may be None) + endcolno: The column corresponding to end (may be None) + + """ + def __init__(self, msg, doc, pos, end=None): + ValueError.__init__(self, errmsg(msg, doc, pos, end=end)) + self.msg = msg + self.doc = doc + self.pos = pos + self.end = end + self.lineno, self.colno = linecol(doc, pos) + if end is not None: + self.endlineno, self.endcolno = linecol(doc, end) + else: + self.endlineno, self.endcolno = None, None + + +def linecol(doc, pos): + lineno = doc.count('\n', 0, pos) + 1 + if lineno == 1: + colno = pos + else: + colno = pos - doc.rindex('\n', 0, pos) + return lineno, colno + + +def errmsg(msg, doc, pos, end=None): + # Note that this function is called from _speedups + lineno, colno = linecol(doc, pos) + if end is None: + #fmt = '{0}: line {1} column {2} (char {3})' + #return fmt.format(msg, lineno, colno, pos) + fmt = '%s: line %d column %d (char %d)' + return fmt % (msg, lineno, colno, pos) + endlineno, endcolno = linecol(doc, end) + #fmt = '{0}: line {1} column {2} - line {3} column {4} (char {5} - {6})' + #return fmt.format(msg, lineno, colno, endlineno, endcolno, pos, end) + fmt = '%s: line %d column %d - line %d column %d (char %d - %d)' + return fmt % (msg, lineno, colno, endlineno, endcolno, pos, end) + + +_CONSTANTS = { + '-Infinity': NegInf, + 'Infinity': PosInf, + 'NaN': NaN, +} + +STRINGCHUNK = re.compile(r'(.*?)(["\\\x00-\x1f])', FLAGS) +BACKSLASH = { + '"': u'"', '\\': u'\\', '/': u'/', + 'b': u'\b', 'f': u'\f', 'n': u'\n', 'r': u'\r', 't': u'\t', +} + +DEFAULT_ENCODING = "utf-8" + +def py_scanstring(s, end, encoding=None, strict=True, + _b=BACKSLASH, _m=STRINGCHUNK.match): + """Scan the string s for a JSON string. End is the index of the + character in s after the quote that started the JSON string. + Unescapes all valid JSON string escape sequences and raises ValueError + on attempt to decode an invalid string. If strict is False then literal + control characters are allowed in the string. + + Returns a tuple of the decoded string and the index of the character in s + after the end quote.""" + if encoding is None: + encoding = DEFAULT_ENCODING + chunks = [] + _append = chunks.append + begin = end - 1 + while 1: + chunk = _m(s, end) + if chunk is None: + raise JSONDecodeError( + "Unterminated string starting at", s, begin) + end = chunk.end() + content, terminator = chunk.groups() + # Content is contains zero or more unescaped string characters + if content: + if not isinstance(content, unicode): + content = unicode(content, encoding) + _append(content) + # Terminator is the end of string, a literal control character, + # or a backslash denoting that an escape sequence follows + if terminator == '"': + break + elif terminator != '\\': + if strict: + msg = "Invalid control character %r at" % (terminator,) + #msg = "Invalid control character {0!r} at".format(terminator) + raise JSONDecodeError(msg, s, end) + else: + _append(terminator) + continue + try: + esc = s[end] + except IndexError: + raise JSONDecodeError( + "Unterminated string starting at", s, begin) + # If not a unicode escape sequence, must be in the lookup table + if esc != 'u': + try: + char = _b[esc] + except KeyError: + msg = "Invalid \\escape: " + repr(esc) + raise JSONDecodeError(msg, s, end) + end += 1 + else: + # Unicode escape sequence + esc = s[end + 1:end + 5] + next_end = end + 5 + if len(esc) != 4: + msg = "Invalid \\uXXXX escape" + raise JSONDecodeError(msg, s, end) + uni = int(esc, 16) + # Check for surrogate pair on UCS-4 systems + if 0xd800 <= uni <= 0xdbff and sys.maxunicode > 65535: + msg = "Invalid \\uXXXX\\uXXXX surrogate pair" + if not s[end + 5:end + 7] == '\\u': + raise JSONDecodeError(msg, s, end) + esc2 = s[end + 7:end + 11] + if len(esc2) != 4: + raise JSONDecodeError(msg, s, end) + uni2 = int(esc2, 16) + uni = 0x10000 + (((uni - 0xd800) << 10) | (uni2 - 0xdc00)) + next_end += 6 + char = unichr(uni) + end = next_end + # Append the unescaped character + _append(char) + return u''.join(chunks), end + + +# Use speedup if available +scanstring = c_scanstring or py_scanstring + +WHITESPACE = re.compile(r'[ \t\n\r]*', FLAGS) +WHITESPACE_STR = ' \t\n\r' + +def JSONObject((s, end), encoding, strict, scan_once, object_hook, + object_pairs_hook, memo=None, + _w=WHITESPACE.match, _ws=WHITESPACE_STR): + # Backwards compatibility + if memo is None: + memo = {} + memo_get = memo.setdefault + pairs = [] + # Use a slice to prevent IndexError from being raised, the following + # check will raise a more specific ValueError if the string is empty + nextchar = s[end:end + 1] + # Normally we expect nextchar == '"' + if nextchar != '"': + if nextchar in _ws: + end = _w(s, end).end() + nextchar = s[end:end + 1] + # Trivial empty object + if nextchar == '}': + if object_pairs_hook is not None: + result = object_pairs_hook(pairs) + return result, end + 1 + pairs = {} + if object_hook is not None: + pairs = object_hook(pairs) + return pairs, end + 1 + elif nextchar != '"': + raise JSONDecodeError("Expecting property name", s, end) + end += 1 + while True: + key, end = scanstring(s, end, encoding, strict) + key = memo_get(key, key) + + # To skip some function call overhead we optimize the fast paths where + # the JSON key separator is ": " or just ":". + if s[end:end + 1] != ':': + end = _w(s, end).end() + if s[end:end + 1] != ':': + raise JSONDecodeError("Expecting : delimiter", s, end) + + end += 1 + + try: + if s[end] in _ws: + end += 1 + if s[end] in _ws: + end = _w(s, end + 1).end() + except IndexError: + pass + + try: + value, end = scan_once(s, end) + except StopIteration: + raise JSONDecodeError("Expecting object", s, end) + pairs.append((key, value)) + + try: + nextchar = s[end] + if nextchar in _ws: + end = _w(s, end + 1).end() + nextchar = s[end] + except IndexError: + nextchar = '' + end += 1 + + if nextchar == '}': + break + elif nextchar != ',': + raise JSONDecodeError("Expecting , delimiter", s, end - 1) + + try: + nextchar = s[end] + if nextchar in _ws: + end += 1 + nextchar = s[end] + if nextchar in _ws: + end = _w(s, end + 1).end() + nextchar = s[end] + except IndexError: + nextchar = '' + + end += 1 + if nextchar != '"': + raise JSONDecodeError("Expecting property name", s, end - 1) + + if object_pairs_hook is not None: + result = object_pairs_hook(pairs) + return result, end + pairs = dict(pairs) + if object_hook is not None: + pairs = object_hook(pairs) + return pairs, end + +def JSONArray((s, end), scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR): + values = [] + nextchar = s[end:end + 1] + if nextchar in _ws: + end = _w(s, end + 1).end() + nextchar = s[end:end + 1] + # Look-ahead for trivial empty array + if nextchar == ']': + return values, end + 1 + _append = values.append + while True: + try: + value, end = scan_once(s, end) + except StopIteration: + raise JSONDecodeError("Expecting object", s, end) + _append(value) + nextchar = s[end:end + 1] + if nextchar in _ws: + end = _w(s, end + 1).end() + nextchar = s[end:end + 1] + end += 1 + if nextchar == ']': + break + elif nextchar != ',': + raise JSONDecodeError("Expecting , delimiter", s, end) + + try: + if s[end] in _ws: + end += 1 + if s[end] in _ws: + end = _w(s, end + 1).end() + except IndexError: + pass + + return values, end + +class JSONDecoder(object): + """Simple JSON decoder + + Performs the following translations in decoding by default: + + +---------------+-------------------+ + | JSON | Python | + +===============+===================+ + | object | dict | + +---------------+-------------------+ + | array | list | + +---------------+-------------------+ + | string | unicode | + +---------------+-------------------+ + | number (int) | int, long | + +---------------+-------------------+ + | number (real) | float | + +---------------+-------------------+ + | true | True | + +---------------+-------------------+ + | false | False | + +---------------+-------------------+ + | null | None | + +---------------+-------------------+ + + It also understands ``NaN``, ``Infinity``, and ``-Infinity`` as + their corresponding ``float`` values, which is outside the JSON spec. + + """ + + def __init__(self, encoding=None, object_hook=None, parse_float=None, + parse_int=None, parse_constant=None, strict=True, + object_pairs_hook=None): + """ + *encoding* determines the encoding used to interpret any + :class:`str` objects decoded by this instance (``'utf-8'`` by + default). It has no effect when decoding :class:`unicode` objects. + + Note that currently only encodings that are a superset of ASCII work, + strings of other encodings should be passed in as :class:`unicode`. + + *object_hook*, if specified, will be called with the result of every + JSON object decoded and its return value will be used in place of the + given :class:`dict`. This can be used to provide custom + deserializations (e.g. to support JSON-RPC class hinting). + + *object_pairs_hook* is an optional function that will be called with + the result of any object literal decode with an ordered list of pairs. + The return value of *object_pairs_hook* will be used instead of the + :class:`dict`. This feature can be used to implement custom decoders + that rely on the order that the key and value pairs are decoded (for + example, :func:`collections.OrderedDict` will remember the order of + insertion). If *object_hook* is also defined, the *object_pairs_hook* + takes priority. + + *parse_float*, if specified, will be called with the string of every + JSON float to be decoded. By default, this is equivalent to + ``float(num_str)``. This can be used to use another datatype or parser + for JSON floats (e.g. :class:`decimal.Decimal`). + + *parse_int*, if specified, will be called with the string of every + JSON int to be decoded. By default, this is equivalent to + ``int(num_str)``. This can be used to use another datatype or parser + for JSON integers (e.g. :class:`float`). + + *parse_constant*, if specified, will be called with one of the + following strings: ``'-Infinity'``, ``'Infinity'``, ``'NaN'``. This + can be used to raise an exception if invalid JSON numbers are + encountered. + + *strict* controls the parser's behavior when it encounters an + invalid control character in a string. The default setting of + ``True`` means that unescaped control characters are parse errors, if + ``False`` then control characters will be allowed in strings. + + """ + self.encoding = encoding + self.object_hook = object_hook + self.object_pairs_hook = object_pairs_hook + self.parse_float = parse_float or float + self.parse_int = parse_int or int + self.parse_constant = parse_constant or _CONSTANTS.__getitem__ + self.strict = strict + self.parse_object = JSONObject + self.parse_array = JSONArray + self.parse_string = scanstring + self.memo = {} + self.scan_once = make_scanner(self) + + def decode(self, s, _w=WHITESPACE.match): + """Return the Python representation of ``s`` (a ``str`` or ``unicode`` + instance containing a JSON document) + + """ + obj, end = self.raw_decode(s, idx=_w(s, 0).end()) + end = _w(s, end).end() + if end != len(s): + raise JSONDecodeError("Extra data", s, end, len(s)) + return obj + + def raw_decode(self, s, idx=0): + """Decode a JSON document from ``s`` (a ``str`` or ``unicode`` + beginning with a JSON document) and return a 2-tuple of the Python + representation and the index in ``s`` where the document ended. + + This can be used to decode a JSON document from a string that may + have extraneous data at the end. + + """ + try: + obj, end = self.scan_once(s, idx) + except StopIteration: + raise JSONDecodeError("No JSON object could be decoded", s, idx) + return obj, end diff --git a/github3/packages/omnijson/packages/simplejson/encoder.py b/github3/packages/omnijson/packages/simplejson/encoder.py new file mode 100644 index 0000000..f1269f3 --- /dev/null +++ b/github3/packages/omnijson/packages/simplejson/encoder.py @@ -0,0 +1,503 @@ +"""Implementation of JSONEncoder +""" +import re +from decimal import Decimal + +def _import_speedups(): + try: + from simplejson import _speedups + return _speedups.encode_basestring_ascii, _speedups.make_encoder + except ImportError: + return None, None +c_encode_basestring_ascii, c_make_encoder = _import_speedups() + +from .decoder import PosInf + +ESCAPE = re.compile(r'[\x00-\x1f\\"\b\f\n\r\t]') +ESCAPE_ASCII = re.compile(r'([\\"]|[^\ -~])') +HAS_UTF8 = re.compile(r'[\x80-\xff]') +ESCAPE_DCT = { + '\\': '\\\\', + '"': '\\"', + '\b': '\\b', + '\f': '\\f', + '\n': '\\n', + '\r': '\\r', + '\t': '\\t', +} +for i in range(0x20): + #ESCAPE_DCT.setdefault(chr(i), '\\u{0:04x}'.format(i)) + ESCAPE_DCT.setdefault(chr(i), '\\u%04x' % (i,)) + +FLOAT_REPR = repr + +def encode_basestring(s): + """Return a JSON representation of a Python string + + """ + if isinstance(s, str) and HAS_UTF8.search(s) is not None: + s = s.decode('utf-8') + def replace(match): + return ESCAPE_DCT[match.group(0)] + return u'"' + ESCAPE.sub(replace, s) + u'"' + + +def py_encode_basestring_ascii(s): + """Return an ASCII-only JSON representation of a Python string + + """ + if isinstance(s, str) and HAS_UTF8.search(s) is not None: + s = s.decode('utf-8') + def replace(match): + s = match.group(0) + try: + return ESCAPE_DCT[s] + except KeyError: + n = ord(s) + if n < 0x10000: + #return '\\u{0:04x}'.format(n) + return '\\u%04x' % (n,) + else: + # surrogate pair + n -= 0x10000 + s1 = 0xd800 | ((n >> 10) & 0x3ff) + s2 = 0xdc00 | (n & 0x3ff) + #return '\\u{0:04x}\\u{1:04x}'.format(s1, s2) + return '\\u%04x\\u%04x' % (s1, s2) + return '"' + str(ESCAPE_ASCII.sub(replace, s)) + '"' + + +encode_basestring_ascii = ( + c_encode_basestring_ascii or py_encode_basestring_ascii) + +class JSONEncoder(object): + """Extensible JSON encoder for Python data structures. + + Supports the following objects and types by default: + + +-------------------+---------------+ + | Python | JSON | + +===================+===============+ + | dict | object | + +-------------------+---------------+ + | list, tuple | array | + +-------------------+---------------+ + | str, unicode | string | + +-------------------+---------------+ + | int, long, float | number | + +-------------------+---------------+ + | True | true | + +-------------------+---------------+ + | False | false | + +-------------------+---------------+ + | None | null | + +-------------------+---------------+ + + To extend this to recognize other objects, subclass and implement a + ``.default()`` method with another method that returns a serializable + object for ``o`` if possible, otherwise it should call the superclass + implementation (to raise ``TypeError``). + + """ + item_separator = ', ' + key_separator = ': ' + def __init__(self, skipkeys=False, ensure_ascii=True, + check_circular=True, allow_nan=True, sort_keys=False, + indent=None, separators=None, encoding='utf-8', default=None, + use_decimal=False): + """Constructor for JSONEncoder, with sensible defaults. + + If skipkeys is false, then it is a TypeError to attempt + encoding of keys that are not str, int, long, float or None. If + skipkeys is True, such items are simply skipped. + + If ensure_ascii is true, the output is guaranteed to be str + objects with all incoming unicode characters escaped. If + ensure_ascii is false, the output will be unicode object. + + If check_circular is true, then lists, dicts, and custom encoded + objects will be checked for circular references during encoding to + prevent an infinite recursion (which would cause an OverflowError). + Otherwise, no such check takes place. + + If allow_nan is true, then NaN, Infinity, and -Infinity will be + encoded as such. This behavior is not JSON specification compliant, + but is consistent with most JavaScript based encoders and decoders. + Otherwise, it will be a ValueError to encode such floats. + + If sort_keys is true, then the output of dictionaries will be + sorted by key; this is useful for regression tests to ensure + that JSON serializations can be compared on a day-to-day basis. + + If indent is a string, then JSON array elements and object members + will be pretty-printed with a newline followed by that string repeated + for each level of nesting. ``None`` (the default) selects the most compact + representation without any newlines. For backwards compatibility with + versions of simplejson earlier than 2.1.0, an integer is also accepted + and is converted to a string with that many spaces. + + If specified, separators should be a (item_separator, key_separator) + tuple. The default is (', ', ': '). To get the most compact JSON + representation you should specify (',', ':') to eliminate whitespace. + + If specified, default is a function that gets called for objects + that can't otherwise be serialized. It should return a JSON encodable + version of the object or raise a ``TypeError``. + + If encoding is not None, then all input strings will be + transformed into unicode using that encoding prior to JSON-encoding. + The default is UTF-8. + + If use_decimal is true (not the default), ``decimal.Decimal`` will + be supported directly by the encoder. For the inverse, decode JSON + with ``parse_float=decimal.Decimal``. + + """ + + self.skipkeys = skipkeys + self.ensure_ascii = ensure_ascii + self.check_circular = check_circular + self.allow_nan = allow_nan + self.sort_keys = sort_keys + self.use_decimal = use_decimal + if isinstance(indent, (int, long)): + indent = ' ' * indent + self.indent = indent + if separators is not None: + self.item_separator, self.key_separator = separators + elif indent is not None: + self.item_separator = ',' + if default is not None: + self.default = default + self.encoding = encoding + + def default(self, o): + """Implement this method in a subclass such that it returns + a serializable object for ``o``, or calls the base implementation + (to raise a ``TypeError``). + + For example, to support arbitrary iterators, you could + implement default like this:: + + def default(self, o): + try: + iterable = iter(o) + except TypeError: + pass + else: + return list(iterable) + return JSONEncoder.default(self, o) + + """ + raise TypeError(repr(o) + " is not JSON serializable") + + def encode(self, o): + """Return a JSON string representation of a Python data structure. + + >>> from simplejson import JSONEncoder + >>> JSONEncoder().encode({"foo": ["bar", "baz"]}) + '{"foo": ["bar", "baz"]}' + + """ + # This is for extremely simple cases and benchmarks. + if isinstance(o, basestring): + if isinstance(o, str): + _encoding = self.encoding + if (_encoding is not None + and not (_encoding == 'utf-8')): + o = o.decode(_encoding) + if self.ensure_ascii: + return encode_basestring_ascii(o) + else: + return encode_basestring(o) + # This doesn't pass the iterator directly to ''.join() because the + # exceptions aren't as detailed. The list call should be roughly + # equivalent to the PySequence_Fast that ''.join() would do. + chunks = self.iterencode(o, _one_shot=True) + if not isinstance(chunks, (list, tuple)): + chunks = list(chunks) + if self.ensure_ascii: + return ''.join(chunks) + else: + return u''.join(chunks) + + def iterencode(self, o, _one_shot=False): + """Encode the given object and yield each string + representation as available. + + For example:: + + for chunk in JSONEncoder().iterencode(bigobject): + mysocket.write(chunk) + + """ + if self.check_circular: + markers = {} + else: + markers = None + if self.ensure_ascii: + _encoder = encode_basestring_ascii + else: + _encoder = encode_basestring + if self.encoding != 'utf-8': + def _encoder(o, _orig_encoder=_encoder, _encoding=self.encoding): + if isinstance(o, str): + o = o.decode(_encoding) + return _orig_encoder(o) + + def floatstr(o, allow_nan=self.allow_nan, + _repr=FLOAT_REPR, _inf=PosInf, _neginf=-PosInf): + # Check for specials. Note that this type of test is processor + # and/or platform-specific, so do tests which don't depend on + # the internals. + + if o != o: + text = 'NaN' + elif o == _inf: + text = 'Infinity' + elif o == _neginf: + text = '-Infinity' + else: + return _repr(o) + + if not allow_nan: + raise ValueError( + "Out of range float values are not JSON compliant: " + + repr(o)) + + return text + + + key_memo = {} + if (_one_shot and c_make_encoder is not None + and self.indent is None): + _iterencode = c_make_encoder( + markers, self.default, _encoder, self.indent, + self.key_separator, self.item_separator, self.sort_keys, + self.skipkeys, self.allow_nan, key_memo, self.use_decimal) + else: + _iterencode = _make_iterencode( + markers, self.default, _encoder, self.indent, floatstr, + self.key_separator, self.item_separator, self.sort_keys, + self.skipkeys, _one_shot, self.use_decimal) + try: + return _iterencode(o, 0) + finally: + key_memo.clear() + + +class JSONEncoderForHTML(JSONEncoder): + """An encoder that produces JSON safe to embed in HTML. + + To embed JSON content in, say, a script tag on a web page, the + characters &, < and > should be escaped. They cannot be escaped + with the usual entities (e.g. &) because they are not expanded + within