X-FTP
[x93.git/.git] / xftp.git / ext / pyftpdlib / _compat.py
diff --git a/xftp.git/ext/pyftpdlib/_compat.py b/xftp.git/ext/pyftpdlib/_compat.py
new file mode 100644 (file)
index 0000000..8ec454d
--- /dev/null
@@ -0,0 +1,105 @@
+#!/usr/bin/env python\r
+\r
+"""\r
+Compatibility module similar to six which helps maintaining\r
+a single code base working with python from 2.6 to 3.x.\r
+"""\r
+\r
+import os\r
+import errno\r
+import sys\r
+\r
+PY3 = sys.version_info[0] == 3\r
+\r
+if PY3:\r
+    def u(s):\r
+        return s\r
+\r
+    def b(s):\r
+        return s.encode("latin-1")\r
+\r
+    getcwdu = os.getcwd\r
+    unicode = str\r
+    xrange = range\r
+    long = int\r
+else:\r
+    def u(s):\r
+        return unicode(s)\r
+\r
+    def b(s):\r
+        return s\r
+\r
+    getcwdu = os.getcwdu\r
+    unicode = unicode\r
+    xrange = xrange\r
+    long = long\r
+\r
+\r
+# removed in 3.0, reintroduced in 3.2\r
+try:\r
+    callable = callable\r
+except Exception:\r
+    def callable(obj):\r
+        for klass in type(obj).__mro__:\r
+            if "__call__" in klass.__dict__:\r
+                return True\r
+        return False\r
+\r
+\r
+# --- exceptions\r
+\r
+\r
+if PY3:\r
+    FileNotFoundError = FileNotFoundError  # NOQA\r
+    FileExistsError = FileExistsError  # NOQA\r
+else:\r
+    # https://github.com/PythonCharmers/python-future/blob/exceptions/\r
+    #     src/future/types/exceptions/pep3151.py\r
+    import platform\r
+\r
+    _SENTINEL = object()\r
+\r
+    def _instance_checking_exception(base_exception=Exception):\r
+        def wrapped(instance_checker):\r
+            class TemporaryClass(base_exception):\r
+\r
+                def __init__(self, *args, **kwargs):\r
+                    if len(args) == 1 and isinstance(args[0], TemporaryClass):\r
+                        unwrap_me = args[0]\r
+                        for attr in dir(unwrap_me):\r
+                            if not attr.startswith('__'):\r
+                                setattr(self, attr, getattr(unwrap_me, attr))\r
+                    else:\r
+                        super(TemporaryClass, self).__init__(*args, **kwargs)\r
+\r
+                class __metaclass__(type):\r
+                    def __instancecheck__(cls, inst):\r
+                        return instance_checker(inst)\r
+\r
+                    def __subclasscheck__(cls, classinfo):\r
+                        value = sys.exc_info()[1]\r
+                        return isinstance(value, cls)\r
+\r
+            TemporaryClass.__name__ = instance_checker.__name__\r
+            TemporaryClass.__doc__ = instance_checker.__doc__\r
+            return TemporaryClass\r
+\r
+        return wrapped\r
+\r
+    @_instance_checking_exception(EnvironmentError)\r
+    def FileNotFoundError(inst):\r
+        return getattr(inst, 'errno', _SENTINEL) == errno.ENOENT\r
+\r
+    @_instance_checking_exception(EnvironmentError)\r
+    def FileExistsError(inst):\r
+        return getattr(inst, 'errno', _SENTINEL) == errno.EEXIST\r
+\r
+    if platform.python_implementation() != "CPython":\r
+        try:\r
+            raise OSError(errno.EEXIST, "perm")\r
+        except FileExistsError:\r
+            pass\r
+        except OSError:\r
+            raise RuntimeError(\r
+                "broken or incompatible Python implementation, see: "\r
+                "https://github.com/giampaolo/psutil/issues/1659")\r