make iro working with python v2.6 devel
authorSandro Knauß <knauss@netzguerilla.net>
Fri, 06 Jul 2012 12:22:15 +0200
branchdevel
changeset 282 50cc13814bfb
parent 281 76572d14bd27
child 283 809cccc518ec
make iro working with python v2.6
iro/config.py
iro/inspect_getcallargs.py
iro/model/user.py
iro/offer/provider.py
iro/tests/config.py
iro/tests/email_validate.py
iro/tests/telnumber.py
iro/validate.py
--- a/iro/config.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/config.py	Fri Jul 06 12:22:15 2012 +0200
@@ -3,7 +3,10 @@
 from ConfigParser import ConfigParser
 import signal
 from functools import partial
-from collections import OrderedDict
+try:
+    from collections import OrderedDict
+except ImportError:
+    from ordereddict import OrderedDict
 
 from validate import vInteger
 from error import NeededOption 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/iro/inspect_getcallargs.py	Fri Jul 06 12:22:15 2012 +0200
@@ -0,0 +1,95 @@
+from inspect import getargspec, ismethod
+import sys
+
+#code just copied form python 2.7.3 
+
+def getcallargs(func, *positional, **named):
+    """Get the mapping of arguments to values.
+
+    A dict is returned, with keys the function argument names (including the
+    names of the * and ** arguments, if any), and values the respective bound
+    values from 'positional' and 'named'."""
+    args, varargs, varkw, defaults = getargspec(func)
+    f_name = func.__name__
+    arg2value = {}
+
+    # The following closures are basically because of tuple parameter unpacking.
+    assigned_tuple_params = []
+    def assign(arg, value):
+        if isinstance(arg, str):
+            arg2value[arg] = value
+        else:
+            assigned_tuple_params.append(arg)
+            value = iter(value)
+            for i, subarg in enumerate(arg):
+                try:
+                    subvalue = next(value)
+                except StopIteration:
+                    raise ValueError('need more than %d %s to unpack' %
+                                     (i, 'values' if i > 1 else 'value'))
+                assign(subarg,subvalue)
+            try:
+                next(value)
+            except StopIteration:
+                pass
+            else:
+                raise ValueError('too many values to unpack')
+    def is_assigned(arg):
+        if isinstance(arg,str):
+            return arg in arg2value
+        return arg in assigned_tuple_params
+    if ismethod(func) and func.im_self is not None:
+        # implicit 'self' (or 'cls' for classmethods) argument
+        positional = (func.im_self,) + positional
+    num_pos = len(positional)
+    num_total = num_pos + len(named)
+    num_args = len(args)
+    num_defaults = len(defaults) if defaults else 0
+    for arg, value in zip(args, positional):
+        assign(arg, value)
+    if varargs:
+        if num_pos > num_args:
+            assign(varargs, positional[-(num_pos-num_args):])
+        else:
+            assign(varargs, ())
+    elif 0 < num_args < num_pos:
+        raise TypeError('%s() takes %s %d %s (%d given)' % (
+            f_name, 'at most' if defaults else 'exactly', num_args,
+            'arguments' if num_args > 1 else 'argument', num_total))
+    elif num_args == 0 and num_total:
+        if varkw:
+            if num_pos:
+                # XXX: We should use num_pos, but Python also uses num_total:
+                raise TypeError('%s() takes exactly 0 arguments '
+                                '(%d given)' % (f_name, num_total))
+        else:
+            raise TypeError('%s() takes no arguments (%d given)' %
+                            (f_name, num_total))
+    for arg in args:
+        if isinstance(arg, str) and arg in named:
+            if is_assigned(arg):
+                raise TypeError("%s() got multiple values for keyword "
+                                "argument '%s'" % (f_name, arg))
+            else:
+                assign(arg, named.pop(arg))
+    if defaults:    # fill in any missing values with the defaults
+        for arg, value in zip(args[-num_defaults:], defaults):
+            if not is_assigned(arg):
+                assign(arg, value)
+    if varkw:
+        assign(varkw, named)
+    elif named:
+        unexpected = next(iter(named))
+        if isinstance(unexpected, unicode):
+            unexpected = unexpected.encode(sys.getdefaultencoding(), 'replace')
+        raise TypeError("%s() got an unexpected keyword argument '%s'" %
+                        (f_name, unexpected))
+    unassigned = num_args - len([arg for arg in args if is_assigned(arg)])
+    if unassigned:
+        num_required = num_args - num_defaults
+        raise TypeError('%s() takes %s %d %s (%d given)' % (
+            f_name, 'at least' if defaults else 'exactly', num_required,
+            'arguments' if num_required > 1 else 'argument', num_total))
+    return arg2value
+
+
--- a/iro/model/user.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/model/user.py	Fri Jul 06 12:22:15 2012 +0200
@@ -1,5 +1,9 @@
 from twisted.internet import defer
-from inspect import getcallargs
+try:
+    from inspect import getcallargs
+except ImportError:
+    from ..inspect_getcallargs import getcallargs
+
 from decorator import decorator
 
 from .schema import User
--- a/iro/offer/provider.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/offer/provider.py	Fri Jul 06 12:22:15 2012 +0200
@@ -1,5 +1,8 @@
 from functools import partial
-from collections import OrderedDict
+try:
+    from collections import OrderedDict
+except ImportError:
+    from ordereddict import OrderedDict
 
 from ..error import NoRoute, NoTyp, ValidateException, NoProvider
 from ..config import Option, Config
--- a/iro/tests/config.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/tests/config.py	Fri Jul 06 12:22:15 2012 +0200
@@ -3,10 +3,10 @@
 import signal
 import io
 import ConfigParser
-from collections import OrderedDict
 from functools import partial
 
 from iro import config, error, validate 
+from iro.config import OrderedDict
 from iro.offer.provider import Provider, providers
 
 class TestModuleConfig(unittest.TestCase):
--- a/iro/tests/email_validate.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/tests/email_validate.py	Fri Jul 06 12:22:15 2012 +0200
@@ -1,4 +1,8 @@
-import unittest
+try:
+    import unittest
+    unittest.case
+except AttributeError:
+    import unittest2 as unittest
 
 from iro.validate import vEmail
 from iro.error import InvalidMail, ValidateException
--- a/iro/tests/telnumber.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/tests/telnumber.py	Fri Jul 06 12:22:15 2012 +0200
@@ -1,4 +1,8 @@
-import unittest
+try:
+    import unittest
+    unittest.case
+except AttributeError:
+    import unittest2 as unittest
 
 from iro.validate import vTel
 from iro.error import InvalidTel
--- a/iro/validate.py	Wed Apr 25 17:15:15 2012 +0200
+++ b/iro/validate.py	Fri Jul 06 12:22:15 2012 +0200
@@ -2,7 +2,10 @@
 
 import re
 from decorator import decorator
-from inspect import getcallargs
+try:
+    from inspect import getcallargs
+except ImportError:
+    from .inspect_getcallargs import getcallargs
 import types
 
 from .error import ValidateException, InvalidTel, InvalidMail