44 import os |
44 import os |
45 from mercurial.i18n import _ |
45 from mercurial.i18n import _ |
46 from mercurial import util |
46 from mercurial import util |
47 |
47 |
48 def decode(arg): |
48 def decode(arg): |
49 if isinstance(arg, str): |
49 if isinstance(arg, str): |
50 uarg = arg.decode(util._encoding) |
50 uarg = arg.decode(util._encoding) |
51 if arg == uarg.encode(util._encoding): |
51 if arg == uarg.encode(util._encoding): |
52 return uarg |
52 return uarg |
53 raise UnicodeError("Not local encoding") |
53 raise UnicodeError("Not local encoding") |
54 elif isinstance(arg, tuple): |
54 elif isinstance(arg, tuple): |
55 return tuple(map(decode, arg)) |
55 return tuple(map(decode, arg)) |
56 elif isinstance(arg, list): |
56 elif isinstance(arg, list): |
57 return map(decode, arg) |
57 return map(decode, arg) |
58 return arg |
58 return arg |
59 |
59 |
60 def encode(arg): |
60 def encode(arg): |
61 if isinstance(arg, unicode): |
61 if isinstance(arg, unicode): |
62 return arg.encode(util._encoding) |
62 return arg.encode(util._encoding) |
63 elif isinstance(arg, tuple): |
63 elif isinstance(arg, tuple): |
64 return tuple(map(encode, arg)) |
64 return tuple(map(encode, arg)) |
65 elif isinstance(arg, list): |
65 elif isinstance(arg, list): |
66 return map(encode, arg) |
66 return map(encode, arg) |
67 return arg |
67 return arg |
68 |
68 |
69 def wrapper(func, args): |
69 def wrapper(func, args): |
70 # check argument is unicode, then call original |
70 # check argument is unicode, then call original |
71 for arg in args: |
71 for arg in args: |
72 if isinstance(arg, unicode): |
72 if isinstance(arg, unicode): |
73 return func(*args) |
73 return func(*args) |
74 |
74 |
75 try: |
75 try: |
76 # convert arguments to unicode, call func, then convert back |
76 # convert arguments to unicode, call func, then convert back |
77 return encode(func(*decode(args))) |
77 return encode(func(*decode(args))) |
78 except UnicodeError: |
78 except UnicodeError: |
79 # If not encoded with util._encoding, report it then |
79 # If not encoded with util._encoding, report it then |
80 # continue with calling original function. |
80 # continue with calling original function. |
81 raise util.Abort(_("[win32mbcs] filename conversion fail with" |
81 raise util.Abort(_("[win32mbcs] filename conversion fail with" |
82 " %s encoding\n") % (util._encoding)) |
82 " %s encoding\n") % (util._encoding)) |
83 |
83 |
84 def wrapname(name): |
84 def wrapname(name): |
85 idx = name.rfind('.') |
85 idx = name.rfind('.') |
86 module = name[:idx] |
86 module = name[:idx] |
87 name = name[idx+1:] |
87 name = name[idx+1:] |
88 module = eval(module) |
88 module = eval(module) |
89 func = getattr(module, name) |
89 func = getattr(module, name) |
90 def f(*args): |
90 def f(*args): |
91 return wrapper(func, args) |
91 return wrapper(func, args) |
92 try: |
92 try: |
93 f.__name__ = func.__name__ # fail with python23 |
93 f.__name__ = func.__name__ # fail with python23 |
94 except Exception: |
94 except Exception: |
95 pass |
95 pass |
96 setattr(module, name, f) |
96 setattr(module, name, f) |
97 |
97 |
98 # List of functions to be wrapped. |
98 # List of functions to be wrapped. |
99 # NOTE: os.path.dirname() and os.path.basename() are safe because |
99 # NOTE: os.path.dirname() and os.path.basename() are safe because |
100 # they use result of os.path.split() |
100 # they use result of os.path.split() |
101 funcs = '''os.path.join os.path.split os.path.splitext |
101 funcs = '''os.path.join os.path.split os.path.splitext |
107 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
107 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis |
108 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
108 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004 |
109 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213''' |
109 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213''' |
110 |
110 |
111 def reposetup(ui, repo): |
111 def reposetup(ui, repo): |
112 # TODO: decide use of config section for this extension |
112 # TODO: decide use of config section for this extension |
113 if not os.path.supports_unicode_filenames: |
113 if not os.path.supports_unicode_filenames: |
114 ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) |
114 ui.warn(_("[win32mbcs] cannot activate on this platform.\n")) |
115 return |
115 return |
116 |
116 |
117 # fake is only for relevant environment. |
117 # fake is only for relevant environment. |
118 if util._encoding.lower() in problematic_encodings.split(): |
118 if util._encoding.lower() in problematic_encodings.split(): |
119 for f in funcs.split(): |
119 for f in funcs.split(): |
120 wrapname(f) |
120 wrapname(f) |
121 ui.debug(_("[win32mbcs] activated with encoding: %s\n") % util._encoding) |
121 ui.debug(_("[win32mbcs] activated with encoding: %s\n") % util._encoding) |
122 |
122 |