comparison hgext/win32mbcs.py @ 43076:2372284d9457

formatting: blacken the codebase This is using my patch to black (https://github.com/psf/black/pull/826) so we don't un-wrap collection literals. Done with: hg files 'set:**.py - mercurial/thirdparty/** - "contrib/python-zstandard/**"' | xargs black -S # skip-blame mass-reformatting only # no-check-commit reformats foo_bar functions Differential Revision: https://phab.mercurial-scm.org/D6971
author Augie Fackler <augie@google.com>
date Sun, 06 Oct 2019 09:45:02 -0400
parents 79dd61a4554f
children 687b865b95ad
comparison
equal deleted inserted replaced
43075:57875cf423c9 43076:2372284d9457
66 configtable = {} 66 configtable = {}
67 configitem = registrar.configitem(configtable) 67 configitem = registrar.configitem(configtable)
68 68
69 # Encoding.encoding may be updated by --encoding option. 69 # Encoding.encoding may be updated by --encoding option.
70 # Use a lambda do delay the resolution. 70 # Use a lambda do delay the resolution.
71 configitem('win32mbcs', 'encoding', 71 configitem(
72 default=lambda: encoding.encoding, 72 'win32mbcs', 'encoding', default=lambda: encoding.encoding,
73 ) 73 )
74 74
75 _encoding = None # see extsetup 75 _encoding = None # see extsetup
76
76 77
77 def decode(arg): 78 def decode(arg):
78 if isinstance(arg, str): 79 if isinstance(arg, str):
79 uarg = arg.decode(_encoding) 80 uarg = arg.decode(_encoding)
80 if arg == uarg.encode(_encoding): 81 if arg == uarg.encode(_encoding):
87 elif isinstance(arg, dict): 88 elif isinstance(arg, dict):
88 for k, v in arg.items(): 89 for k, v in arg.items():
89 arg[k] = decode(v) 90 arg[k] = decode(v)
90 return arg 91 return arg
91 92
93
92 def encode(arg): 94 def encode(arg):
93 if isinstance(arg, pycompat.unicode): 95 if isinstance(arg, pycompat.unicode):
94 return arg.encode(_encoding) 96 return arg.encode(_encoding)
95 elif isinstance(arg, tuple): 97 elif isinstance(arg, tuple):
96 return tuple(map(encode, arg)) 98 return tuple(map(encode, arg))
99 elif isinstance(arg, dict): 101 elif isinstance(arg, dict):
100 for k, v in arg.items(): 102 for k, v in arg.items():
101 arg[k] = encode(v) 103 arg[k] = encode(v)
102 return arg 104 return arg
103 105
106
104 def appendsep(s): 107 def appendsep(s):
105 # ensure the path ends with os.sep, appending it if necessary. 108 # ensure the path ends with os.sep, appending it if necessary.
106 try: 109 try:
107 us = decode(s) 110 us = decode(s)
108 except UnicodeError: 111 except UnicodeError:
121 try: 124 try:
122 # convert string arguments, call func, then convert back the 125 # convert string arguments, call func, then convert back the
123 # return value. 126 # return value.
124 return enc(func(*dec(args), **dec(kwds))) 127 return enc(func(*dec(args), **dec(kwds)))
125 except UnicodeError: 128 except UnicodeError:
126 raise error.Abort(_("[win32mbcs] filename conversion failed with" 129 raise error.Abort(
127 " %s encoding\n") % (_encoding)) 130 _("[win32mbcs] filename conversion failed with" " %s encoding\n")
131 % _encoding
132 )
133
128 134
129 def wrapper(func, args, kwds): 135 def wrapper(func, args, kwds):
130 return basewrapper(func, pycompat.unicode, encode, decode, args, kwds) 136 return basewrapper(func, pycompat.unicode, encode, decode, args, kwds)
131 137
132 138
133 def reversewrapper(func, args, kwds): 139 def reversewrapper(func, args, kwds):
134 return basewrapper(func, str, decode, encode, args, kwds) 140 return basewrapper(func, str, decode, encode, args, kwds)
141
135 142
136 def wrapperforlistdir(func, args, kwds): 143 def wrapperforlistdir(func, args, kwds):
137 # Ensure 'path' argument ends with os.sep to avoids 144 # Ensure 'path' argument ends with os.sep to avoids
138 # misinterpreting last 0x5c of MBCS 2nd byte as path separator. 145 # misinterpreting last 0x5c of MBCS 2nd byte as path separator.
139 if args: 146 if args:
141 args[0] = appendsep(args[0]) 148 args[0] = appendsep(args[0])
142 if 'path' in kwds: 149 if 'path' in kwds:
143 kwds['path'] = appendsep(kwds['path']) 150 kwds['path'] = appendsep(kwds['path'])
144 return func(*args, **kwds) 151 return func(*args, **kwds)
145 152
153
146 def wrapname(name, wrapper): 154 def wrapname(name, wrapper):
147 module, name = name.rsplit('.', 1) 155 module, name = name.rsplit('.', 1)
148 module = sys.modules[module] 156 module = sys.modules[module]
149 func = getattr(module, name) 157 func = getattr(module, name)
158
150 def f(*args, **kwds): 159 def f(*args, **kwds):
151 return wrapper(func, args, kwds) 160 return wrapper(func, args, kwds)
161
152 f.__name__ = func.__name__ 162 f.__name__ = func.__name__
153 setattr(module, name, f) 163 setattr(module, name, f)
164
154 165
155 # List of functions to be wrapped. 166 # List of functions to be wrapped.
156 # NOTE: os.path.dirname() and os.path.basename() are safe because 167 # NOTE: os.path.dirname() and os.path.basename() are safe because
157 # they use result of os.path.split() 168 # they use result of os.path.split()
158 funcs = '''os.path.join os.path.split os.path.splitext 169 funcs = '''os.path.join os.path.split os.path.splitext
175 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs 186 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
176 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis 187 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
177 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 188 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
178 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 ''' 189 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 '''
179 190
191
180 def extsetup(ui): 192 def extsetup(ui):
181 # TODO: decide use of config section for this extension 193 # TODO: decide use of config section for this extension
182 if ((not os.path.supports_unicode_filenames) and 194 if (not os.path.supports_unicode_filenames) and (
183 (pycompat.sysplatform != 'cygwin')): 195 pycompat.sysplatform != 'cygwin'
196 ):
184 ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) 197 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
185 return 198 return
186 # determine encoding for filename 199 # determine encoding for filename
187 global _encoding 200 global _encoding
188 _encoding = ui.config('win32mbcs', 'encoding') 201 _encoding = ui.config('win32mbcs', 'encoding')
200 wrapname(f, reversewrapper) 213 wrapname(f, reversewrapper)
201 # Check sys.args manually instead of using ui.debug() because 214 # Check sys.args manually instead of using ui.debug() because
202 # command line options is not yet applied when 215 # command line options is not yet applied when
203 # extensions.loadall() is called. 216 # extensions.loadall() is called.
204 if '--debug' in sys.argv: 217 if '--debug' in sys.argv:
205 ui.write(("[win32mbcs] activated with encoding: %s\n") 218 ui.write("[win32mbcs] activated with encoding: %s\n" % _encoding)
206 % _encoding)