1 # -*- coding: utf-8 -*- |
1 # -*- coding: utf-8 -*- |
2 |
2 |
3 # Copyright (c) 2013 - 2013 Tobias Rzepka <tobias.rzepka@gmail.com> |
3 # Copyright (c) 2013 - 2015 Tobias Rzepka <tobias.rzepka@gmail.com> |
4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing the open behavior of Python3 for use with Eric6. |
7 Module implementing some workarounds to let eric6 run under Python 2. |
|
8 """ |
|
9 |
|
10 |
|
11 import __builtin__ |
|
12 import codecs |
|
13 import imp |
|
14 import locale |
|
15 import os |
|
16 import sys |
|
17 |
|
18 # convert all command line arguments to unicode |
|
19 sys.argv = [arg.decode(locale.getpreferredencoding()) for arg in sys.argv] |
|
20 |
|
21 """ |
|
22 Improvement for the os.path.join function because the original join doesn't |
|
23 use the correct encoding. |
|
24 """ |
|
25 # Save original function for use in joinAsUnicode |
|
26 __join = os.path.join |
|
27 # Flag to disable unicode conversion of join function |
|
28 os.path.join_unicode = True |
|
29 |
|
30 |
|
31 def joinAsUnicode(*args): |
|
32 """ |
|
33 Convert none unicode parameter of the os.path.join into unicode. |
|
34 |
|
35 @param args paths which should be joined (str, unicode) |
|
36 @return unicode str of the path (unicode) |
|
37 """ |
|
38 if os.path.join_unicode: |
|
39 convArgs = [] |
|
40 for arg in args: |
|
41 if isinstance(arg, str): |
|
42 arg = arg.decode(locale.getpreferredencoding(), 'replace') |
|
43 convArgs.append(arg) |
|
44 return __join(*convArgs) |
|
45 else: |
|
46 return __join(*args) |
|
47 |
|
48 # Replace os.path.join with unicode aware version |
|
49 os.path.join = joinAsUnicode |
|
50 |
|
51 """ |
|
52 Improvement for the imp.load_source and imp.find_module functions because the |
|
53 originals doesn't use the correct encoding. |
|
54 """ |
|
55 # Save original function for use in load_sourceAsStr and find_moduleAsStr |
|
56 __load_source = imp.load_source |
|
57 __find_module = imp.find_module |
|
58 |
|
59 |
|
60 def load_sourceAsStr(*args): |
|
61 """ |
|
62 Convert none str parameter of the imp.load_source into str. |
|
63 |
|
64 @param args (str, unicode) |
|
65 @return list of args converted to str (list) |
|
66 """ |
|
67 convArgs = [] |
|
68 for arg in args: |
|
69 if isinstance(arg, unicode): |
|
70 arg = arg.encode(sys.getfilesystemencoding(), 'strict') |
|
71 convArgs.append(arg) |
|
72 return __load_source(*convArgs) |
|
73 |
|
74 |
|
75 def find_moduleAsStr(*args): |
|
76 """ |
|
77 Convert none str parameter of the imp.find_module into str. |
|
78 |
|
79 @param args (str, unicode) |
|
80 @return list of args converted to str (list) |
|
81 """ |
|
82 convArgs = [] |
|
83 for arg in args: |
|
84 if isinstance(arg, unicode): |
|
85 arg = arg.encode(sys.getfilesystemencoding(), 'strict') |
|
86 convArgs.append(arg) |
|
87 return __find_module(*convArgs) |
|
88 |
|
89 # Replace imp.load_source and imp.find_module with unicode aware version |
|
90 imp.load_source = load_sourceAsStr |
|
91 imp.find_module = find_moduleAsStr |
|
92 |
|
93 """ |
|
94 Improvement for the sys.path list because some other functions doesn't expect |
|
95 unicode in the sys.path list. |
|
96 """ |
|
97 |
|
98 |
|
99 class PlainStrList(list): |
|
100 """ |
|
101 Keep track that all added paths to sys.path are str. |
|
102 """ |
|
103 def __init__(self, *args): |
|
104 """ |
|
105 Constructor |
|
106 |
|
107 @param args list of paths to start with (list) |
|
108 """ |
|
109 super(PlainStrList, self).__init__() |
|
110 self.extend(list(args)) |
|
111 |
|
112 def __convert(self, element): |
|
113 """ |
|
114 Private method to convert unicode to file system encoding. |
|
115 |
|
116 @param element to convert from unicode to file system encoding (any) |
|
117 @return converted element |
|
118 """ |
|
119 if isinstance(element, unicode): |
|
120 # Throw exception if it can't be converted, otherwise exception |
|
121 # could occur somewhere else |
|
122 element = element.encode(sys.getfilesystemencoding(), 'strict') |
|
123 return element |
|
124 |
|
125 def __setitem__(self, idx, value): |
|
126 """ |
|
127 Special method to overwrite a specific list item. |
|
128 |
|
129 @param idx index of the item (int) |
|
130 @param value the new value (any) |
|
131 """ |
|
132 super(PlainStrList, self).__setitem__(idx, self.__convert(value)) |
|
133 |
|
134 def insert(self, idx, value): |
|
135 """ |
|
136 Public method to insert a specific list item. |
|
137 |
|
138 @param idx index of the item (int) |
|
139 @param value the new value (any) |
|
140 """ |
|
141 super(PlainStrList, self).insert(idx, self.__convert(value)) |
|
142 |
|
143 |
|
144 # insert a conversion function from unicode to str at sys.path access |
|
145 sys.path = PlainStrList(*sys.path) |
|
146 |
|
147 """ |
|
148 The open function and File class simulates the open behaviour of Python3. |
8 |
149 |
9 The Eric6 used features are emulated only. The not emulated features |
150 The Eric6 used features are emulated only. The not emulated features |
10 should throw a NotImplementedError exception. |
151 should throw a NotImplementedError exception. |
11 """ |
152 """ |
12 |
|
13 import __builtin__ # __IGNORE_EXCEPTION__ |
|
14 import codecs |
|
15 |
153 |
16 |
154 |
17 def open(file, mode='r', buffering=-1, encoding=None, |
155 def open(file, mode='r', buffering=-1, encoding=None, |
18 errors=None, newline=None, closefd=True): |
156 errors=None, newline=None, closefd=True): |
19 """ |
157 """ |