diff options
author | Timothy Crosley <timothy.crosley@gmail.com> | 2013-11-13 21:59:05 -0500 |
---|---|---|
committer | Timothy Crosley <timothy.crosley@gmail.com> | 2013-11-13 21:59:05 -0500 |
commit | f7780c91f8e50719ed8b3be641942f2db8b564e9 (patch) | |
tree | 0eeb0b72bcc8c18f700cde60de13c2c69f856480 /pies | |
parent | 4badd66c595e8d90e8ffa80ce44ab4de228ab393 (diff) | |
download | pies-f7780c91f8e50719ed8b3be641942f2db8b564e9.tar.gz |
First steps to pies 2.0
Diffstat (limited to 'pies')
-rw-r--r-- | pies/StringIO.py | 11 | ||||
-rw-r--r-- | pies/__init__.py | 33 | ||||
-rw-r--r-- | pies/collections.py | 9 | ||||
-rw-r--r-- | pies/dbm/__init__.py | 10 | ||||
-rw-r--r-- | pies/dbm/dumb.py | 8 | ||||
-rw-r--r-- | pies/dbm/gnu.py | 8 | ||||
-rw-r--r-- | pies/dbm/ndbm.py | 8 | ||||
-rw-r--r-- | pies/functools.py | 8 | ||||
-rw-r--r-- | pies/http/__init__.py | 8 | ||||
-rw-r--r-- | pies/http/client.py | 8 | ||||
-rw-r--r-- | pies/http/cookiejar | 6 | ||||
-rw-r--r-- | pies/http/cookies.py | 8 | ||||
-rw-r--r-- | pies/http/server.py | 10 | ||||
-rw-r--r-- | pies/imp.py | 8 | ||||
-rw-r--r-- | pies/itertools.py | 8 | ||||
-rw-r--r-- | pies/overrides.py | 199 | ||||
-rw-r--r-- | pies/pickle.py | 11 | ||||
-rw-r--r-- | pies/sys.py | 6 | ||||
-rw-r--r-- | pies/urllib/__init__.py | 8 | ||||
-rw-r--r-- | pies/urllib/error.py | 9 | ||||
-rw-r--r-- | pies/urllib/parse.py | 8 | ||||
-rw-r--r-- | pies/urllib/request.py | 16 | ||||
-rw-r--r-- | pies/urllib/robotparser.py | 8 | ||||
-rw-r--r-- | pies/version_info.py | 7 |
24 files changed, 423 insertions, 0 deletions
diff --git a/pies/StringIO.py b/pies/StringIO.py new file mode 100644 index 0000000..fd5ce5e --- /dev/null +++ b/pies/StringIO.py @@ -0,0 +1,11 @@ +from __future__ import absolute_import + +from .version_info import PY3 + +if PY3: + from StringIO import * +else: + try: + from cStringIO import * + except ImportError: + from StringIO import * diff --git a/pies/__init__.py b/pies/__init__.py new file mode 100644 index 0000000..d083f27 --- /dev/null +++ b/pies/__init__.py @@ -0,0 +1,33 @@ +""" + pies/__init__.py + + Adds necessary hooks to allow Python code to run on multiple major versions of Python at once + (currently 2.6 - 3.x) + + Usage: + Anywhere you want to gain support for multiple versions of Python simply add the following two lines + from __future__ import absolute_import, division, print_function, unicode_literals + from pies.overrides import * + + And for changed stdlibs: + from pies import [libname] + + Copyright (C) 2013 Timothy Edmund Crosley + + 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. +""" + + +from __future__ import absolute_import diff --git a/pies/collections.py b/pies/collections.py new file mode 100644 index 0000000..f5fd457 --- /dev/null +++ b/pies/collections.py @@ -0,0 +1,9 @@ +from __future__ import absolute_import + +from collections import * + +from .version_info import PY2 + +if PY2: + from UserString import * + from UserList import * diff --git a/pies/dbm/__init__.py b/pies/dbm/__init__.py new file mode 100644 index 0000000..3461248 --- /dev/null +++ b/pies/dbm/__init__.py @@ -0,0 +1,10 @@ +from __future__ import absolute_import + +from dbm import * + +from ..version_info import PY2 + +if PY2: + from . import dumb, gnu, ndbm + from whichdb import * + from anydbm import * diff --git a/pies/dbm/dumb.py b/pies/dbm/dumb.py new file mode 100644 index 0000000..94dc12c --- /dev/null +++ b/pies/dbm/dumb.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from dbm.dumb import * +else: + from dumb import * diff --git a/pies/dbm/gnu.py b/pies/dbm/gnu.py new file mode 100644 index 0000000..5f9c37f --- /dev/null +++ b/pies/dbm/gnu.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from dbm.gnu import * +else: + from gdbm import * diff --git a/pies/dbm/ndbm.py b/pies/dbm/ndbm.py new file mode 100644 index 0000000..fef8c9e --- /dev/null +++ b/pies/dbm/ndbm.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from dbm.ndbm import * +else: + from dbm import * diff --git a/pies/functools.py b/pies/functools.py new file mode 100644 index 0000000..29749cf --- /dev/null +++ b/pies/functools.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from functools import * + +from .version_info import PY2 + +if PY2: + reduce = reduce diff --git a/pies/http/__init__.py b/pies/http/__init__.py new file mode 100644 index 0000000..60b2c7a --- /dev/null +++ b/pies/http/__init__.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from http import * + +from ..version_info import PY2 + +if PY2: + from . import client, cookiejar, cookies, server diff --git a/pies/http/client.py b/pies/http/client.py new file mode 100644 index 0000000..426d978 --- /dev/null +++ b/pies/http/client.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from http.client import * +else: + from httplib import * diff --git a/pies/http/cookiejar b/pies/http/cookiejar new file mode 100644 index 0000000..949f59d --- /dev/null +++ b/pies/http/cookiejar @@ -0,0 +1,6 @@ +from ..version_info import PY3 + +if PY3: + from http.cookiejar import * +else: + from cookielib import *
\ No newline at end of file diff --git a/pies/http/cookies.py b/pies/http/cookies.py new file mode 100644 index 0000000..8108b66 --- /dev/null +++ b/pies/http/cookies.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from http.cookies import * +else: + from Cookie import * diff --git a/pies/http/server.py b/pies/http/server.py new file mode 100644 index 0000000..8702c94 --- /dev/null +++ b/pies/http/server.py @@ -0,0 +1,10 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from http.server import * +else: + from BaseHTTPServer import * + from CGIHTTPServer import * + from SimpleHTTPServer import * diff --git a/pies/imp.py b/pies/imp.py new file mode 100644 index 0000000..8a85e73 --- /dev/null +++ b/pies/imp.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from imp import * + +from .version_info import PY2 + +if PY2: + reload = reload diff --git a/pies/itertools.py b/pies/itertools.py new file mode 100644 index 0000000..40636f1 --- /dev/null +++ b/pies/itertools.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from itertools import * + +from .version_info import PY2 + +if PY2: + filterfalse = ifilterfalse diff --git a/pies/overrides.py b/pies/overrides.py new file mode 100644 index 0000000..8572ebe --- /dev/null +++ b/pies/overrides.py @@ -0,0 +1,199 @@ +""" + pies/overrides.py + + Overrides Python syntax to conform to the Python3 version as much as possible using a '*' import + + Copyright (C) 2013 Timothy Edmund Crosley + + 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. +""" +from __future__ import absolute_import + +import functools +from numbers import Integral + +from .version_info import PY2, PY3, VERSION + +__version__ = "1.0.3" + +native_dict = dict +native_round = round +native_filter = filter +native_map = map +native_zip = zip +native_range = range +native_str = str +native_chr = chr +native_input = input +native_next = next + +common = ['native_dict', 'native_round', 'native_filter', 'native_map', 'native_range', 'native_str', 'native_chr', + 'native_input', 'PY2', 'PY3', 'u', 'itemsview', 'valuesview', 'keysview', 'execute', 'integer_types', + 'native_next'] + +if PY3: + import urllib + import builtins + from urllib import parse + + from collections import OrderedDict + + integer_types = (int, ) + + def u(string): + return string + + def itemsview(collection): + return collection.items() + + def valuesview(collection): + return collection.values() + + def keysview(collection): + return collection.keys() + + urllib.quote = parse.quote + urllib.quote_plus = parse.quote_plus + urllib.unquote = parse.unquote + urllib.unquote_plus = parse.unquote_plus + urllib.urlencode = parse.urlencode + execute = getattr('builtins', 'exec') + if VERSION < '3.2': + def callable(entity): + return hasattr(entity, '__call__') + + __all__ = common + ['OrderedDict', 'urllib', 'callable'] +else: + from itertools import ifilter as filter + from itertools import imap as map + from itertools import izip as zip + from decimal import Decimal, ROUND_HALF_EVEN + + + try: + from collections import OrderedDict + except ImportError: + from ordereddict import OrderedDict + + import codecs + str = unicode + chr = unichr + input = raw_input + range = xrange + integer_types = (int, long) + + for removed in ('apply', 'cmp', 'coerce', 'execfile', 'raw_input', 'unpacks'): + def _not_allow(*args, **kwargs): + raise NameError("name '{0}' is not defined".format(removed)) + + _not_allow.__name__ = removed + globals()[removed] = _not_allow + + def u(string): + return codecs.unicode_escape_decode(string[0]) + + def execute(_code_, _globs_=None, _locs_=None): + """Execute code in a namespace.""" + if _globs_ is None: + frame = sys._getframe(1) + _globs_ = frame.f_globals + if _locs_ is None: + _locs_ = frame.f_locals + del frame + elif _locs_ is None: + _locs_ = _globs_ + exec("""exec _code_ in _globs_, _locs_""") + + class _dict_view_base(object): + __slots__ = ('_dictionary', ) + + def __init__(self, dictionary): + self._dictionary = dictionary + + def __repr__(self): + return "{0}({1})".format(self.__class__.__name__, str(list(self.__iter__()))) + + def __unicode__(self): + return str(self.__repr__()) + + def __str__(self): + return str(self.__unicode__()) + + class dict_keys(_dict_view_base): + __slots__ = () + + def __iter__(self): + return self._dictionary.iterkeys() + + class dict_values(_dict_view_base): + __slots__ = () + + def __iter__(self): + return self._dictionary.itervalues() + + class dict_items(_dict_view_base): + __slots__ = () + + def __iter__(self): + return self._dictionary.iteritems() + + def itemsview(collection): + return dict_items(collection) + + def valuesview(collection): + return dict_values(collection) + + def keysview(collection): + return dict_keys(collection) + + class dict(native_dict): + def has_key(self, *args, **kwargs): + return AttributeError("'dict' object has no attribute 'has_key'") + + def items(self): + return dict_items(self, 'iteritems') + + def keys(self): + return dict_keys(self, 'iterkeys') + + def values(self): + return dict_values(self, 'itervalues') + + def round(number, ndigits=None): + return_int = False + if ndigits is None: + return_int = True + ndigits = 0 + if hasattr(number, '__round__'): + return number.__round__(ndigits) + + if ndigits < 0: + raise NotImplementedError('negative ndigits not supported yet') + exponent = Decimal('10') ** (-ndigits) + d = Decimal.from_float(number).quantize(exponent, + rounding=ROUND_HALF_EVEN) + if return_int: + return int(d) + else: + return float(d) + + def next(iterator): + try: + iterator.__next__() + except Exception: + native_next(iterator) + + __all__ = common + ['round', 'dict', 'apply', 'cmp', 'coerce', 'execfile', 'raw_input', 'unpacks', 'str', 'chr', + 'input', 'range'] diff --git a/pies/pickle.py b/pies/pickle.py new file mode 100644 index 0000000..d606ee7 --- /dev/null +++ b/pies/pickle.py @@ -0,0 +1,11 @@ +from __future__ import absolute_import + +from .version_info import PY3 + +if PY3: + from pickle import * +else: + try: + from cPickle import * + except ImportError: + from pickle import * diff --git a/pies/sys.py b/pies/sys.py new file mode 100644 index 0000000..64087b0 --- /dev/null +++ b/pies/sys.py @@ -0,0 +1,6 @@ +from __future__ import absolute_import + +from sys import * + +if version_info[0] == 2: + intern = intern diff --git a/pies/urllib/__init__.py b/pies/urllib/__init__.py new file mode 100644 index 0000000..d6a12ff --- /dev/null +++ b/pies/urllib/__init__.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from urllib import * + +from ..version_info import PY2 + +if PY2: + from . import error, parse, request, robotparser diff --git a/pies/urllib/error.py b/pies/urllib/error.py new file mode 100644 index 0000000..73c85f1 --- /dev/null +++ b/pies/urllib/error.py @@ -0,0 +1,9 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from urllib.error import * +else: + from urllib import ContentTooShortError + from urllib2 import HTTPError, URLError diff --git a/pies/urllib/parse.py b/pies/urllib/parse.py new file mode 100644 index 0000000..9cbbff3 --- /dev/null +++ b/pies/urllib/parse.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from urllib.parse import * +else: + from urllib import quote, unquote, quote_plus, unquote_plus, urlencode diff --git a/pies/urllib/request.py b/pies/urllib/request.py new file mode 100644 index 0000000..a1470f1 --- /dev/null +++ b/pies/urllib/request.py @@ -0,0 +1,16 @@ + + +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from urllib.request import * +else: + from urllib import FancyURLopener, getproxies, pathname2url, url2pathname, urlcleanup, URLopener, urlretrieve + from urllib2 import (AbstractBasicAuthHandler, AbstractDigestAuthHandler, BaseHandler, build_opener, + CacheFTPHandler, FileHandler, FTPHandler, HTTPBasicAuthHandler, HTTPCookieProcessor, + HTTPDefaultErrorHandler, HTTPDigestAuthHandler, HTTPHandler, HTTPPasswordMgr, + HTTPPasswordMgrWithDefaultRealm, HTTPRedirectHandler, HTTPSHandler, install_opener, + OpenerDirector, ProxyBasicAuthHandler, ProxyDigestAuthHandler, ProxyHandler, Request, + UnknownHandler, urlopen) diff --git a/pies/urllib/robotparser.py b/pies/urllib/robotparser.py new file mode 100644 index 0000000..e45b609 --- /dev/null +++ b/pies/urllib/robotparser.py @@ -0,0 +1,8 @@ +from __future__ import absolute_import + +from ..version_info import PY3 + +if PY3: + from urllib.parse import * +else: + from robotparser import * diff --git a/pies/version_info.py b/pies/version_info.py new file mode 100644 index 0000000..d3547bb --- /dev/null +++ b/pies/version_info.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import + +import sys + +PY2 = sys.version_info[0] == 2 +PY3 = sys.version_info[0] == 3 +VERSION = sys.version_info |