Skip to content

Commit 6ce9f19

Browse files
committed
Python 2.6 compatibility
1 parent b891dfb commit 6ce9f19

File tree

18 files changed

+147
-38
lines changed

18 files changed

+147
-38
lines changed

pygithub3/core/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def __parse_kwargs(func):
5959
""" Decorator to put extra args into requests.params """
6060

6161
def wrapper(self, verb, request, **kwargs):
62-
diffs = kwargs.viewkeys() - VALID_REQUEST_ARGS
62+
diffs = set(kwargs.keys()) - VALID_REQUEST_ARGS
6363
new_params = kwargs.get('params', {})
6464
for key in diffs: # Put each key in new_params and delete it
6565
new_params[key] = kwargs[key]

pygithub3/core/utils.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
""" Utils to support python 2.6 compatibility """
4+
5+
from collections import MutableMapping
6+
7+
8+
def _import_module(module_uri):
9+
return __import__(module_uri, {}, {}, [''])
10+
11+
12+
def import_module(module_uri):
13+
""" Import module by string 'from.path.module'
14+
15+
To support python 2.6
16+
"""
17+
try:
18+
from importlib import import_module
19+
callback = import_module
20+
except ImportError:
21+
callback = _import_module
22+
23+
return callback(module_uri)
24+
25+
26+
class _OrderedDict(dict, MutableMapping):
27+
"""
28+
Src: http://code.activestate.com/recipes/576669/
29+
Author: Raymond Hettinger (Promoter of PEP which introduces OrderDict into
30+
colletions module in python >2.7)
31+
"""
32+
33+
# Methods with direct access to underlying attributes
34+
35+
def __init__(self, *args, **kwds):
36+
if len(args) > 1:
37+
raise TypeError('expected at 1 argument, got %d', len(args))
38+
if not hasattr(self, '_keys'):
39+
self._keys = []
40+
self.update(*args, **kwds)
41+
42+
def clear(self):
43+
del self._keys[:]
44+
dict.clear(self)
45+
46+
def __setitem__(self, key, value):
47+
if key not in self:
48+
self._keys.append(key)
49+
dict.__setitem__(self, key, value)
50+
51+
def __delitem__(self, key):
52+
dict.__delitem__(self, key)
53+
self._keys.remove(key)
54+
55+
def __iter__(self):
56+
return iter(self._keys)
57+
58+
def __reversed__(self):
59+
return reversed(self._keys)
60+
61+
def popitem(self):
62+
if not self:
63+
raise KeyError
64+
key = self._keys.pop()
65+
value = dict.pop(self, key)
66+
return key, value
67+
68+
def __reduce__(self):
69+
items = [[k, self[k]] for k in self]
70+
inst_dict = vars(self).copy()
71+
inst_dict.pop('_keys', None)
72+
return (self.__class__, (items,), inst_dict)
73+
74+
# Methods with indirect access via the above methods
75+
76+
setdefault = MutableMapping.setdefault
77+
update = MutableMapping.update
78+
pop = MutableMapping.pop
79+
keys = MutableMapping.keys
80+
values = MutableMapping.values
81+
items = MutableMapping.items
82+
83+
def __repr__(self):
84+
pairs = ', '.join(map('%r: %r'.__mod__, self.items()))
85+
return '%s({%s})' % (self.__class__.__name__, pairs)
86+
87+
def copy(self):
88+
return self.__class__(self)
89+
90+
@classmethod
91+
def fromkeys(cls, iterable, value=None):
92+
d = cls()
93+
for key in iterable:
94+
d[key] = value
95+
return d
96+
97+
try:
98+
from collections import OrderedDict
99+
except ImportError:
100+
OrderedDict = _OrderedDict

pygithub3/requests/base.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- encoding: utf-8 -*-
33

44
import re
5-
from importlib import import_module
65
try:
76
import simplejson as json
87
except ImportError:
@@ -11,6 +10,7 @@
1110
from pygithub3.exceptions import (DoesNotExists, UriInvalid, ValidationError,
1211
InvalidBodySchema)
1312
from pygithub3.resources.base import Raw
13+
from pygithub3.core.utils import import_module
1414

1515
ABS_IMPORT_PREFIX = 'pygithub3.requests'
1616

@@ -31,8 +31,8 @@ def parse(self):
3131
if not hasattr(self.content, 'items'):
3232
raise ValidationError("'%s' needs a content dictionary"
3333
% self.__class__.__name__)
34-
parsed = {key: self.content[key] for key in self.schema
35-
if key in self.content}
34+
parsed = dict([(key, self.content[key]) for key in self.schema
35+
if key in self.content])
3636
for attr_required in self.required:
3737
if attr_required not in parsed:
3838
raise ValidationError("'%s' attribute is required" %
@@ -96,12 +96,13 @@ def get_body(self):
9696

9797

9898
class Factory(object):
99-
""" """
99+
""" Request builder """
100100

101101
import_pattern = re.compile(r'^(\w+\.)+\w+$')
102102

103103
def validate(func):
104-
""" """
104+
""" Decorator to check if request_uri
105+
has valid format: 'from.path.module.class' """
105106

106107
def wrapper(self, request_uri, **kwargs):
107108
if not Factory.import_pattern.match(request_uri):
@@ -110,22 +111,21 @@ def wrapper(self, request_uri, **kwargs):
110111
return wrapper
111112

112113
def dispatch(func):
113-
""" """
114-
115114
def wrapper(self, request_uri, **kwargs):
116115
module_chunk, s, request_chunk = request_uri.rpartition('.')
116+
request_chunk = request_chunk.capitalize()
117117
try:
118118
# TODO: CamelCase and under_score support, now only Class Name
119119
module = import_module('%s.%s'
120120
% (ABS_IMPORT_PREFIX, module_chunk))
121-
request = getattr(module, request_chunk.capitalize())
121+
request = getattr(module, request_chunk)
122122
except ImportError:
123123
raise DoesNotExists("'%s' module does not exists"
124124
% module_chunk)
125125
except AttributeError:
126126
raise DoesNotExists(
127127
"'%s' request doesn't exists into '%s' module"
128-
% (request_chunk.capitalize(), module_chunk))
128+
% (request_chunk, module_chunk))
129129
return func(self, request, **kwargs)
130130
return wrapper
131131

pygithub3/requests/repos/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- encoding: utf-8 -*-
22

3-
from ..base import Request, ValidationError
3+
from pygithub3.requests.base import Request, ValidationError
44
from pygithub3.resources.users import User
55
from pygithub3.resources.repos import Repo, Team, Tag, Branch
66

pygithub3/requests/users/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- encoding: utf-8 -*-
22

3-
from ..base import Request, ValidationError
3+
from pygithub3.requests.base import Request, ValidationError
44
from pygithub3.resources.users import User
55

66

pygithub3/resources/base.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,17 +73,17 @@ def parse_collection_map(resource, raw_resources):
7373
for raw_resource in raw_resources]
7474

7575
new_resource = raw_resource.copy()
76-
new_resource.update(
77-
{attr: parse_date(raw_resource[attr])
78-
for attr in self._dates if attr in raw_resource})
79-
new_resource.update(
80-
{attr: parse_map(resource, raw_resource[attr])
76+
new_resource.update(dict([
77+
(attr, parse_date(raw_resource[attr]))
78+
for attr in self._dates if attr in raw_resource]))
79+
new_resource.update(dict([
80+
(attr, parse_map(resource, raw_resource[attr]))
8181
for attr, resource in self._maps.items()
82-
if attr in raw_resource})
83-
new_resource.update(
84-
{attr: parse_collection_map(resource, raw_resource[attr])
82+
if attr in raw_resource]))
83+
new_resource.update(dict([
84+
(attr, parse_collection_map(resource, raw_resource[attr]))
8585
for attr, resource in self._collection_maps.items()
86-
if attr in raw_resource})
86+
if attr in raw_resource]))
8787

8888
return self(new_resource)
8989

pygithub3/resources/repos.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from collections import OrderedDict
4+
from pygithub3.core.utils import OrderedDict
55

66
from .base import Resource
77
from .users import User

pygithub3/services/repos/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from ..base import Service, MimeTypeMixin
4+
from pygithub3.services.base import Service, MimeTypeMixin
55
from .collaborators import Collaborators
66
from .commits import Commits
77
from .downloads import Downloads

pygithub3/services/users/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from ..base import Service
4+
from pygithub3.services.base import Service
55
from .keys import Keys
66
from .emails import Emails
77
from .followers import Followers

pygithub3/tests/core/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/env python
22
# -*- encoding: utf-8 -*-
33

4-
from unittest import TestCase
5-
64
import requests
5+
76
from mock import patch
87

8+
from pygithub3.tests.utils.core import TestCase
99
from pygithub3.core.client import Client
1010
from pygithub3.exceptions import NotFound, BadRequest, UnprocessableEntity
1111
from pygithub3.tests.utils.base import mock_response

0 commit comments

Comments
 (0)