aboutsummaryrefslogtreecommitdiff
path: root/pandora/py2compat.py
diff options
context:
space:
mode:
Diffstat (limited to 'pandora/py2compat.py')
-rw-r--r--pandora/py2compat.py89
1 files changed, 0 insertions, 89 deletions
diff --git a/pandora/py2compat.py b/pandora/py2compat.py
deleted file mode 100644
index dfd9abd..0000000
--- a/pandora/py2compat.py
+++ /dev/null
@@ -1,89 +0,0 @@
1"""
2Python 2 Compatibility Layer
3
4This module exists to work around compatibility issues between Python 2 and
5Python 3. The main code-base will use Python 3 idioms and this module will
6patch Python 2 code to support those changes. When Python 2 support is
7dropped this module can be removed and imports can be updated.
8"""
9
10
11def with_metaclass(meta, *bases):
12 return meta("NewBase", bases, {})
13
14
15try:
16 from configparser import ConfigParser
17except ImportError:
18 from ConfigParser import SafeConfigParser
19
20 class ConfigParser(SafeConfigParser):
21
22 def read_file(self, fp):
23 return self.readfp(fp)
24
25
26# Used in pydora
27def input(prompt):
28 try:
29 return raw_input(prompt)
30 except NameError:
31 import builtins
32 return builtins.input(prompt)
33
34
35# Only used in tests
36try:
37 from unittest.mock import Mock, MagicMock, call, patch # noqa: F401
38except ImportError:
39 try:
40 from mock import Mock, MagicMock, call, patch # noqa: F401
41 except ImportError:
42 pass
43
44
45try:
46 from shutil import which
47except ImportError:
48 import os
49 import sys
50
51 # Copypasta from Python 3.6, exists in 3.3+
52 def which(cmd, mode=os.F_OK | os.X_OK, path=None):
53 def _access_check(fn, mode):
54 return (os.path.exists(fn) and os.access(fn, mode)
55 and not os.path.isdir(fn))
56
57 if os.path.dirname(cmd):
58 if _access_check(cmd, mode):
59 return cmd
60 return None
61
62 if path is None:
63 path = os.environ.get("PATH", os.defpath)
64 if not path:
65 return None
66 path = path.split(os.pathsep)
67
68 if sys.platform == "win32":
69 if os.curdir not in path:
70 path.insert(0, os.curdir)
71
72 pathext = os.environ.get("PATHEXT", "").split(os.pathsep)
73 if any(cmd.lower().endswith(ext.lower()) for ext in pathext):
74 files = [cmd]
75 else:
76 files = [cmd + ext for ext in pathext]
77 else:
78 files = [cmd]
79
80 seen = set()
81 for dir in path:
82 normdir = os.path.normcase(dir)
83 if normdir not in seen:
84 seen.add(normdir)
85 for thefile in files:
86 name = os.path.join(dir, thefile)
87 if _access_check(name, mode):
88 return name
89 return None