##// END OF EJS Templates
win32mbcs: capitalize Unicode
Martin Geisler -
r8665:e4ad46f9 default
parent child Browse files
Show More
@@ -1,126 +1,126 b''
1 # win32mbcs.py -- MBCS filename support for Mercurial
1 # win32mbcs.py -- MBCS filename support for Mercurial
2 #
2 #
3 # Copyright (c) 2008 Shun-ichi Goto <shunichi.goto@gmail.com>
3 # Copyright (c) 2008 Shun-ichi Goto <shunichi.goto@gmail.com>
4 #
4 #
5 # Version: 0.2
5 # Version: 0.2
6 # Author: Shun-ichi Goto <shunichi.goto@gmail.com>
6 # Author: Shun-ichi Goto <shunichi.goto@gmail.com>
7 #
7 #
8 # This software may be used and distributed according to the terms of the
8 # This software may be used and distributed according to the terms of the
9 # GNU General Public License version 2, incorporated herein by reference.
9 # GNU General Public License version 2, incorporated herein by reference.
10 #
10 #
11
11
12 """allow to use MBCS path with problematic encoding.
12 """allow to use MBCS path with problematic encoding.
13
13
14 Some MBCS encodings are not good for some path operations (i.e.
14 Some MBCS encodings are not good for some path operations (i.e.
15 splitting path, case conversion, etc.) with its encoded bytes. We call
15 splitting path, case conversion, etc.) with its encoded bytes. We call
16 such a encoding (i.e. shift_jis and big5) as "problematic encoding".
16 such a encoding (i.e. shift_jis and big5) as "problematic encoding".
17 This extension can be used to fix the issue with those encodings by
17 This extension can be used to fix the issue with those encodings by
18 wrapping some functions to convert to unicode string before path
18 wrapping some functions to convert to Unicode string before path
19 operation.
19 operation.
20
20
21 This extension is usefull for:
21 This extension is usefull for:
22 * Japanese Windows users using shift_jis encoding.
22 * Japanese Windows users using shift_jis encoding.
23 * Chinese Windows users using big5 encoding.
23 * Chinese Windows users using big5 encoding.
24 * All users who use a repository with one of problematic encodings on
24 * All users who use a repository with one of problematic encodings on
25 case-insensitive file system.
25 case-insensitive file system.
26
26
27 This extension is not needed for:
27 This extension is not needed for:
28 * Any user who use only ascii chars in path.
28 * Any user who use only ascii chars in path.
29 * Any user who do not use any of problematic encodings.
29 * Any user who do not use any of problematic encodings.
30
30
31 Note that there are some limitations on using this extension:
31 Note that there are some limitations on using this extension:
32 * You should use single encoding in one repository.
32 * You should use single encoding in one repository.
33 * You should set same encoding for the repository by locale or
33 * You should set same encoding for the repository by locale or
34 HGENCODING.
34 HGENCODING.
35
35
36 To use this extension, enable the extension in .hg/hgrc or ~/.hgrc:
36 To use this extension, enable the extension in .hg/hgrc or ~/.hgrc:
37
37
38 [extensions]
38 [extensions]
39 hgext.win32mbcs =
39 hgext.win32mbcs =
40
40
41 Path encoding conversion are done between unicode and
41 Path encoding conversion are done between Unicode and
42 encoding.encoding which is decided by mercurial from current locale
42 encoding.encoding which is decided by mercurial from current locale
43 setting or HGENCODING.
43 setting or HGENCODING.
44
44
45 """
45 """
46
46
47 import os
47 import os
48 from mercurial.i18n import _
48 from mercurial.i18n import _
49 from mercurial import util, encoding
49 from mercurial import util, encoding
50
50
51 def decode(arg):
51 def decode(arg):
52 if isinstance(arg, str):
52 if isinstance(arg, str):
53 uarg = arg.decode(encoding.encoding)
53 uarg = arg.decode(encoding.encoding)
54 if arg == uarg.encode(encoding.encoding):
54 if arg == uarg.encode(encoding.encoding):
55 return uarg
55 return uarg
56 raise UnicodeError("Not local encoding")
56 raise UnicodeError("Not local encoding")
57 elif isinstance(arg, tuple):
57 elif isinstance(arg, tuple):
58 return tuple(map(decode, arg))
58 return tuple(map(decode, arg))
59 elif isinstance(arg, list):
59 elif isinstance(arg, list):
60 return map(decode, arg)
60 return map(decode, arg)
61 return arg
61 return arg
62
62
63 def encode(arg):
63 def encode(arg):
64 if isinstance(arg, unicode):
64 if isinstance(arg, unicode):
65 return arg.encode(encoding.encoding)
65 return arg.encode(encoding.encoding)
66 elif isinstance(arg, tuple):
66 elif isinstance(arg, tuple):
67 return tuple(map(encode, arg))
67 return tuple(map(encode, arg))
68 elif isinstance(arg, list):
68 elif isinstance(arg, list):
69 return map(encode, arg)
69 return map(encode, arg)
70 return arg
70 return arg
71
71
72 def wrapper(func, args):
72 def wrapper(func, args):
73 # check argument is unicode, then call original
73 # check argument is unicode, then call original
74 for arg in args:
74 for arg in args:
75 if isinstance(arg, unicode):
75 if isinstance(arg, unicode):
76 return func(*args)
76 return func(*args)
77
77
78 try:
78 try:
79 # convert arguments to unicode, call func, then convert back
79 # convert arguments to unicode, call func, then convert back
80 return encode(func(*decode(args)))
80 return encode(func(*decode(args)))
81 except UnicodeError:
81 except UnicodeError:
82 # If not encoded with encoding.encoding, report it then
82 # If not encoded with encoding.encoding, report it then
83 # continue with calling original function.
83 # continue with calling original function.
84 raise util.Abort(_("[win32mbcs] filename conversion fail with"
84 raise util.Abort(_("[win32mbcs] filename conversion fail with"
85 " %s encoding\n") % (encoding.encoding))
85 " %s encoding\n") % (encoding.encoding))
86
86
87 def wrapname(name):
87 def wrapname(name):
88 idx = name.rfind('.')
88 idx = name.rfind('.')
89 module = name[:idx]
89 module = name[:idx]
90 name = name[idx+1:]
90 name = name[idx+1:]
91 module = globals()[module]
91 module = globals()[module]
92 func = getattr(module, name)
92 func = getattr(module, name)
93 def f(*args):
93 def f(*args):
94 return wrapper(func, args)
94 return wrapper(func, args)
95 try:
95 try:
96 f.__name__ = func.__name__ # fail with python23
96 f.__name__ = func.__name__ # fail with python23
97 except Exception:
97 except Exception:
98 pass
98 pass
99 setattr(module, name, f)
99 setattr(module, name, f)
100
100
101 # List of functions to be wrapped.
101 # List of functions to be wrapped.
102 # NOTE: os.path.dirname() and os.path.basename() are safe because
102 # NOTE: os.path.dirname() and os.path.basename() are safe because
103 # they use result of os.path.split()
103 # they use result of os.path.split()
104 funcs = '''os.path.join os.path.split os.path.splitext
104 funcs = '''os.path.join os.path.split os.path.splitext
105 os.path.splitunc os.path.normpath os.path.normcase os.makedirs
105 os.path.splitunc os.path.normpath os.path.normcase os.makedirs
106 util.endswithsep util.splitpath util.checkcase util.fspath'''
106 util.endswithsep util.splitpath util.checkcase util.fspath'''
107
107
108 # codec and alias names of sjis and big5 to be faked.
108 # codec and alias names of sjis and big5 to be faked.
109 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
109 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
110 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
110 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
111 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
111 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
112 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213'''
112 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213'''
113
113
114 def reposetup(ui, repo):
114 def reposetup(ui, repo):
115 # TODO: decide use of config section for this extension
115 # TODO: decide use of config section for this extension
116 if not os.path.supports_unicode_filenames:
116 if not os.path.supports_unicode_filenames:
117 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
117 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
118 return
118 return
119
119
120 # fake is only for relevant environment.
120 # fake is only for relevant environment.
121 if encoding.encoding.lower() in problematic_encodings.split():
121 if encoding.encoding.lower() in problematic_encodings.split():
122 for f in funcs.split():
122 for f in funcs.split():
123 wrapname(f)
123 wrapname(f)
124 ui.debug(_("[win32mbcs] activated with encoding: %s\n")
124 ui.debug(_("[win32mbcs] activated with encoding: %s\n")
125 % encoding.encoding)
125 % encoding.encoding)
126
126
General Comments 0
You need to be logged in to leave comments. Login now