diff options
author | 2012-03-01 23:23:15 +0100 | |
---|---|---|
committer | 2012-03-01 23:23:15 +0100 | |
commit | 6ce9f197f4681875a2595a5a8d86bd047f993a99 (patch) | |
tree | 0bd35db35b9fe09eccd7262fcc60b4ada5f07e44 /pygithub3 | |
parent | Merge branch 'docs' (diff) | |
download | python-github3-6ce9f197f4681875a2595a5a8d86bd047f993a99.tar.xz python-github3-6ce9f197f4681875a2595a5a8d86bd047f993a99.zip |
Python 2.6 compatibility
Diffstat (limited to 'pygithub3')
-rw-r--r-- | pygithub3/core/client.py | 2 | ||||
-rw-r--r-- | pygithub3/core/utils.py | 100 | ||||
-rw-r--r-- | pygithub3/requests/base.py | 18 | ||||
-rw-r--r-- | pygithub3/requests/repos/__init__.py | 2 | ||||
-rw-r--r-- | pygithub3/requests/users/__init__.py | 2 | ||||
-rw-r--r-- | pygithub3/resources/base.py | 18 | ||||
-rw-r--r-- | pygithub3/resources/repos.py | 2 | ||||
-rw-r--r-- | pygithub3/services/repos/__init__.py | 2 | ||||
-rw-r--r-- | pygithub3/services/users/__init__.py | 2 | ||||
-rw-r--r-- | pygithub3/tests/core/test_client.py | 4 | ||||
-rw-r--r-- | pygithub3/tests/core/test_result.py | 3 | ||||
-rw-r--r-- | pygithub3/tests/requests/test_core.py | 2 | ||||
-rw-r--r-- | pygithub3/tests/resources/test_core.py | 2 | ||||
-rw-r--r-- | pygithub3/tests/services/test_core.py | 8 | ||||
-rw-r--r-- | pygithub3/tests/services/test_repos.py | 3 | ||||
-rw-r--r-- | pygithub3/tests/services/test_users.py | 3 | ||||
-rw-r--r-- | pygithub3/tests/utils/core.py | 5 | ||||
-rw-r--r-- | pygithub3/tests/utils/resources.py | 7 |
18 files changed, 147 insertions, 38 deletions
diff --git a/pygithub3/core/client.py b/pygithub3/core/client.py index ce6cf7c..a8e33a1 100644 --- a/pygithub3/core/client.py +++ b/pygithub3/core/client.py @@ -59,7 +59,7 @@ class Client(object): """ Decorator to put extra args into requests.params """ def wrapper(self, verb, request, **kwargs): - diffs = kwargs.viewkeys() - VALID_REQUEST_ARGS + diffs = set(kwargs.keys()) - VALID_REQUEST_ARGS new_params = kwargs.get('params', {}) for key in diffs: # Put each key in new_params and delete it new_params[key] = kwargs[key] diff --git a/pygithub3/core/utils.py b/pygithub3/core/utils.py new file mode 100644 index 0000000..c6dbf0a --- /dev/null +++ b/pygithub3/core/utils.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- +""" Utils to support python 2.6 compatibility """ + +from collections import MutableMapping + + +def _import_module(module_uri): + return __import__(module_uri, {}, {}, ['']) + + +def import_module(module_uri): + """ Import module by string 'from.path.module' + + To support python 2.6 + """ + try: + from importlib import import_module + callback = import_module + except ImportError: + callback = _import_module + + return callback(module_uri) + + +class _OrderedDict(dict, MutableMapping): + """ + Src: http://code.activestate.com/recipes/576669/ + Author: Raymond Hettinger (Promoter of PEP which introduces OrderDict into + colletions module in python >2.7) + """ + + # Methods with direct access to underlying attributes + + def __init__(self, *args, **kwds): + if len(args) > 1: + raise TypeError('expected at 1 argument, got %d', len(args)) + if not hasattr(self, '_keys'): + self._keys = [] + self.update(*args, **kwds) + + def clear(self): + del self._keys[:] + dict.clear(self) + + def __setitem__(self, key, value): + if key not in self: + self._keys.append(key) + dict.__setitem__(self, key, value) + + def __delitem__(self, key): + dict.__delitem__(self, key) + self._keys.remove(key) + + def __iter__(self): + return iter(self._keys) + + def __reversed__(self): + return reversed(self._keys) + + def popitem(self): + if not self: + raise KeyError + key = self._keys.pop() + value = dict.pop(self, key) + return key, value + + def __reduce__(self): + items = [[k, self[k]] for k in self] + inst_dict = vars(self).copy() + inst_dict.pop('_keys', None) + return (self.__class__, (items,), inst_dict) + + # Methods with indirect access via the above methods + + setdefault = MutableMapping.setdefault + update = MutableMapping.update + pop = MutableMapping.pop + keys = MutableMapping.keys + values = MutableMapping.values + items = MutableMapping.items + + def __repr__(self): + pairs = ', '.join(map('%r: %r'.__mod__, self.items())) + return '%s({%s})' % (self.__class__.__name__, pairs) + + def copy(self): + return self.__class__(self) + + @classmethod + def fromkeys(cls, iterable, value=None): + d = cls() + for key in iterable: + d[key] = value + return d + +try: + from collections import OrderedDict +except ImportError: + OrderedDict = _OrderedDict diff --git a/pygithub3/requests/base.py b/pygithub3/requests/base.py index 4b1aa9b..65bc558 100644 --- a/pygithub3/requests/base.py +++ b/pygithub3/requests/base.py @@ -2,7 +2,6 @@ # -*- encoding: utf-8 -*- import re -from importlib import import_module try: import simplejson as json except ImportError: @@ -11,6 +10,7 @@ except ImportError: from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError, InvalidBodySchema) from pygithub3.resources.base import Raw +from pygithub3.core.utils import import_module ABS_IMPORT_PREFIX = 'pygithub3.requests' @@ -31,8 +31,8 @@ class Body(object): if not hasattr(self.content, 'items'): raise ValidationError("'%s' needs a content dictionary" % self.__class__.__name__) - parsed = {key: self.content[key] for key in self.schema - if key in self.content} + parsed = dict([(key, self.content[key]) for key in self.schema + if key in self.content]) for attr_required in self.required: if attr_required not in parsed: raise ValidationError("'%s' attribute is required" % @@ -96,12 +96,13 @@ class Request(object): class Factory(object): - """ """ + """ Request builder """ import_pattern = re.compile(r'^(\w+\.)+\w+$') def validate(func): - """ """ + """ Decorator to check if request_uri + has valid format: 'from.path.module.class' """ def wrapper(self, request_uri, **kwargs): if not Factory.import_pattern.match(request_uri): @@ -110,22 +111,21 @@ class Factory(object): return wrapper def dispatch(func): - """ """ - def wrapper(self, request_uri, **kwargs): module_chunk, s, request_chunk = request_uri.rpartition('.') + request_chunk = request_chunk.capitalize() try: # TODO: CamelCase and under_score support, now only Class Name module = import_module('%s.%s' % (ABS_IMPORT_PREFIX, module_chunk)) - request = getattr(module, request_chunk.capitalize()) + request = getattr(module, request_chunk) except ImportError: raise DoesNotExists("'%s' module does not exists" % module_chunk) except AttributeError: raise DoesNotExists( "'%s' request doesn't exists into '%s' module" - % (request_chunk.capitalize(), module_chunk)) + % (request_chunk, module_chunk)) return func(self, request, **kwargs) return wrapper diff --git a/pygithub3/requests/repos/__init__.py b/pygithub3/requests/repos/__init__.py index 5c7785a..cd920fe 100644 --- a/pygithub3/requests/repos/__init__.py +++ b/pygithub3/requests/repos/__init__.py @@ -1,6 +1,6 @@ # -*- encoding: utf-8 -*- -from ..base import Request, ValidationError +from pygithub3.requests.base import Request, ValidationError from pygithub3.resources.users import User from pygithub3.resources.repos import Repo, Team, Tag, Branch diff --git a/pygithub3/requests/users/__init__.py b/pygithub3/requests/users/__init__.py index d8df00d..3335bc8 100644 --- a/pygithub3/requests/users/__init__.py +++ b/pygithub3/requests/users/__init__.py @@ -1,6 +1,6 @@ # -*- encoding: utf-8 -*- -from ..base import Request, ValidationError +from pygithub3.requests.base import Request, ValidationError from pygithub3.resources.users import User diff --git a/pygithub3/resources/base.py b/pygithub3/resources/base.py index 255f4c1..4ca2aa3 100644 --- a/pygithub3/resources/base.py +++ b/pygithub3/resources/base.py @@ -73,17 +73,17 @@ class Resource(object): for raw_resource in raw_resources] new_resource = raw_resource.copy() - new_resource.update( - {attr: parse_date(raw_resource[attr]) - for attr in self._dates if attr in raw_resource}) - new_resource.update( - {attr: parse_map(resource, raw_resource[attr]) + new_resource.update(dict([ + (attr, parse_date(raw_resource[attr])) + for attr in self._dates if attr in raw_resource])) + new_resource.update(dict([ + (attr, parse_map(resource, raw_resource[attr])) for attr, resource in self._maps.items() - if attr in raw_resource}) - new_resource.update( - {attr: parse_collection_map(resource, raw_resource[attr]) + if attr in raw_resource])) + new_resource.update(dict([ + (attr, parse_collection_map(resource, raw_resource[attr])) for attr, resource in self._collection_maps.items() - if attr in raw_resource}) + if attr in raw_resource])) return self(new_resource) diff --git a/pygithub3/resources/repos.py b/pygithub3/resources/repos.py index 89e5682..99a6d4b 100644 --- a/pygithub3/resources/repos.py +++ b/pygithub3/resources/repos.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from collections import OrderedDict +from pygithub3.core.utils import OrderedDict from .base import Resource from .users import User diff --git a/pygithub3/services/repos/__init__.py b/pygithub3/services/repos/__init__.py index 484dd49..6695265 100644 --- a/pygithub3/services/repos/__init__.py +++ b/pygithub3/services/repos/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from ..base import Service, MimeTypeMixin +from pygithub3.services.base import Service, MimeTypeMixin from .collaborators import Collaborators from .commits import Commits from .downloads import Downloads diff --git a/pygithub3/services/users/__init__.py b/pygithub3/services/users/__init__.py index 29a8e1c..fa93ca2 100644 --- a/pygithub3/services/users/__init__.py +++ b/pygithub3/services/users/__init__.py @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from ..base import Service +from pygithub3.services.base import Service from .keys import Keys from .emails import Emails from .followers import Followers diff --git a/pygithub3/tests/core/test_client.py b/pygithub3/tests/core/test_client.py index 4a68687..f65fc80 100644 --- a/pygithub3/tests/core/test_client.py +++ b/pygithub3/tests/core/test_client.py @@ -1,11 +1,11 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase - import requests + from mock import patch +from pygithub3.tests.utils.core import TestCase from pygithub3.core.client import Client from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity from pygithub3.tests.utils.base import mock_response diff --git a/pygithub3/tests/core/test_result.py b/pygithub3/tests/core/test_result.py index 3c26630..a42bd44 100644 --- a/pygithub3/tests/core/test_result.py +++ b/pygithub3/tests/core/test_result.py @@ -1,10 +1,9 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase - from mock import Mock +from pygithub3.tests.utils.core import TestCase from pygithub3.core.client import Client from pygithub3.core.result import Result, Page from pygithub3.tests.utils.core import (mock_paginate_github_in_GET, request, diff --git a/pygithub3/tests/requests/test_core.py b/pygithub3/tests/requests/test_core.py index 3f2d615..cd162b3 100644 --- a/pygithub3/tests/requests/test_core.py +++ b/pygithub3/tests/requests/test_core.py @@ -1,9 +1,9 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase from mock import Mock +from pygithub3.tests.utils.core import TestCase from pygithub3.requests.base import Factory, Body, json, Request from pygithub3.exceptions import (UriInvalid, DoesNotExists, ValidationError, InvalidBodySchema) diff --git a/pygithub3/tests/resources/test_core.py b/pygithub3/tests/resources/test_core.py index 9c05e2b..d8b8541 100644 --- a/pygithub3/tests/resources/test_core.py +++ b/pygithub3/tests/resources/test_core.py @@ -1,9 +1,9 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase from datetime import datetime +from pygithub3.tests.utils.core import TestCase from pygithub3.resources.base import Raw from pygithub3.tests.utils.resources import Nested, Simple, HasSimple diff --git a/pygithub3/tests/services/test_core.py b/pygithub3/tests/services/test_core.py index 612b5ea..8ee1c6e 100644 --- a/pygithub3/tests/services/test_core.py +++ b/pygithub3/tests/services/test_core.py @@ -1,11 +1,11 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase -from mock import patch - import requests +from mock import patch + +from pygithub3.tests.utils.core import TestCase from pygithub3.services.base import Service, MimeTypeMixin from pygithub3.core.result import Result from pygithub3.tests.utils.base import DummyRequest, mock_response @@ -68,7 +68,7 @@ class TestMimeType(TestCase): def test_WITH_mimetype(self, request_method): request_method.return_value = mock_response() - self.ms.set_html_mimetype() + self.ms.set_html() self.ms.dummy_request() request_method.assert_called_with('get', _('dummyrequest'), headers={'Accept': 'application/vnd.github.%s.html+json' % diff --git a/pygithub3/tests/services/test_repos.py b/pygithub3/tests/services/test_repos.py index 865c5df..1993340 100644 --- a/pygithub3/tests/services/test_repos.py +++ b/pygithub3/tests/services/test_repos.py @@ -1,11 +1,10 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase - import requests from mock import patch, Mock +from pygithub3.tests.utils.core import TestCase from pygithub3.services.repos import (Repos, Collaborators, Commits, Downloads, Forks, Keys, Watchers) from pygithub3.resources.base import json diff --git a/pygithub3/tests/services/test_users.py b/pygithub3/tests/services/test_users.py index 049197b..f777d6d 100644 --- a/pygithub3/tests/services/test_users.py +++ b/pygithub3/tests/services/test_users.py @@ -1,11 +1,10 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- -from unittest import TestCase - import requests from mock import patch, Mock +from pygithub3.tests.utils.core import TestCase from pygithub3.core.client import Client from pygithub3.services.users import User, Emails, Followers, Keys from pygithub3.exceptions import ValidationError diff --git a/pygithub3/tests/utils/core.py b/pygithub3/tests/utils/core.py index 8d18732..32170e4 100644 --- a/pygithub3/tests/utils/core.py +++ b/pygithub3/tests/utils/core.py @@ -1,6 +1,11 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- +try: + from unittest2 import TestCase # Python 2.6 +except ImportError: + from unittest import TestCase # Python >2.7 + from .base import Mock, DummyRequest request = DummyRequest() diff --git a/pygithub3/tests/utils/resources.py b/pygithub3/tests/utils/resources.py index d9e4975..be9245b 100644 --- a/pygithub3/tests/utils/resources.py +++ b/pygithub3/tests/utils/resources.py @@ -1,7 +1,14 @@ #!/usr/bin/env python # -*- encoding: utf-8 -*- +from mock import Mock + from .base import Resource +from pygithub3.resources.base import json +from pygithub3.tests.utils.base import mock_json + +json.dumps = Mock(side_effect=mock_json) +json.loads = Mock(side_effect=mock_json) class Simple(Resource): |