##// END OF EJS Templates
win32mbcs: fix typos and reST syntax
Martin Geisler -
r10067:fd672980 default
parent child Browse files
Show More
@@ -1,156 +1,154 b''
1 1 # win32mbcs.py -- MBCS filename support for Mercurial
2 2 #
3 3 # Copyright (c) 2008 Shun-ichi Goto <shunichi.goto@gmail.com>
4 4 #
5 5 # Version: 0.3
6 6 # Author: Shun-ichi Goto <shunichi.goto@gmail.com>
7 7 #
8 8 # This software may be used and distributed according to the terms of the
9 9 # GNU General Public License version 2, incorporated herein by reference.
10 10 #
11 11
12 12 '''allow the use of MBCS paths with problematic encodings
13 13
14 14 Some MBCS encodings are not good for some path operations (i.e.
15 15 splitting path, case conversion, etc.) with its encoded bytes. We call
16 16 such a encoding (i.e. shift_jis and big5) as "problematic encoding".
17 17 This extension can be used to fix the issue with those encodings by
18 18 wrapping some functions to convert to Unicode string before path
19 19 operation.
20 20
21 21 This extension is useful for:
22 22
23 23 - Japanese Windows users using shift_jis encoding.
24 24 - Chinese Windows users using big5 encoding.
25 25 - All users who use a repository with one of problematic encodings on
26 26 case-insensitive file system.
27 27
28 28 This extension is not needed for:
29 29
30 30 - Any user who use only ASCII chars in path.
31 31 - Any user who do not use any of problematic encodings.
32 32
33 33 Note that there are some limitations on using this extension:
34 34
35 35 - You should use single encoding in one repository.
36 36
37 37
38 By default, win32mbcs uses encoding.encoding decided by mercurial.
39 You can specify the encoding by config option.
38 By default, win32mbcs uses encoding.encoding decided by Mercurial.
39 You can specify the encoding by config option::
40 40
41 ex.)
42 41 [win32mbcs]
43 42 encoding = sjis
44 43
45 It is usefull for the users who want to commit with utf-8 log message.
46
44 It is useful for the users who want to commit with UTF-8 log message.
47 45 '''
48 46
49 47 import os, sys
50 48 from mercurial.i18n import _
51 49 from mercurial import util, encoding
52 50
53 51 _encoding = None # see reposetup()
54 52
55 53 def decode(arg):
56 54 if isinstance(arg, str):
57 55 uarg = arg.decode(_encoding)
58 56 if arg == uarg.encode(_encoding):
59 57 return uarg
60 58 raise UnicodeError("Not local encoding")
61 59 elif isinstance(arg, tuple):
62 60 return tuple(map(decode, arg))
63 61 elif isinstance(arg, list):
64 62 return map(decode, arg)
65 63 elif isinstance(arg, dict):
66 64 for k, v in arg.items():
67 65 arg[k] = decode(v)
68 66 return arg
69 67
70 68 def encode(arg):
71 69 if isinstance(arg, unicode):
72 70 return arg.encode(_encoding)
73 71 elif isinstance(arg, tuple):
74 72 return tuple(map(encode, arg))
75 73 elif isinstance(arg, list):
76 74 return map(encode, arg)
77 75 elif isinstance(arg, dict):
78 76 for k, v in arg.items():
79 77 arg[k] = encode(v)
80 78 return arg
81 79
82 80 def appendsep(s):
83 81 # ensure the path ends with os.sep, appending it if necessary.
84 82 try:
85 83 us = decode(s)
86 84 except UnicodeError:
87 85 us = s
88 86 if us and us[-1] not in ':/\\':
89 87 s += os.sep
90 88 return s
91 89
92 90 def wrapper(func, args, kwds):
93 91 # check argument is unicode, then call original
94 92 for arg in args:
95 93 if isinstance(arg, unicode):
96 94 return func(*args, **kwds)
97 95
98 96 try:
99 97 # convert arguments to unicode, call func, then convert back
100 98 return encode(func(*decode(args), **decode(kwds)))
101 99 except UnicodeError:
102 100 raise util.Abort(_("[win32mbcs] filename conversion failed with"
103 101 " %s encoding\n") % (_encoding))
104 102
105 103 def wrapperforlistdir(func, args, kwds):
106 104 # Ensure 'path' argument ends with os.sep to avoids
107 105 # misinterpreting last 0x5c of MBCS 2nd byte as path separator.
108 106 if args:
109 107 args = list(args)
110 108 args[0] = appendsep(args[0])
111 109 if 'path' in kwds:
112 110 kwds['path'] = appendsep(kwds['path'])
113 111 return func(*args, **kwds)
114 112
115 113 def wrapname(name, wrapper):
116 114 module, name = name.rsplit('.', 1)
117 115 module = sys.modules[module]
118 116 func = getattr(module, name)
119 117 def f(*args, **kwds):
120 118 return wrapper(func, args, kwds)
121 119 try:
122 120 f.__name__ = func.__name__ # fail with python23
123 121 except Exception:
124 122 pass
125 123 setattr(module, name, f)
126 124
127 125 # List of functions to be wrapped.
128 126 # NOTE: os.path.dirname() and os.path.basename() are safe because
129 127 # they use result of os.path.split()
130 128 funcs = '''os.path.join os.path.split os.path.splitext
131 129 os.path.splitunc os.path.normpath os.path.normcase os.makedirs
132 130 mercurial.util.endswithsep mercurial.util.splitpath mercurial.util.checkcase
133 131 mercurial.util.fspath mercurial.util.pconvert mercurial.util.normpath'''
134 132
135 133 # codec and alias names of sjis and big5 to be faked.
136 134 problematic_encodings = '''big5 big5-tw csbig5 big5hkscs big5-hkscs
137 135 hkscs cp932 932 ms932 mskanji ms-kanji shift_jis csshiftjis shiftjis
138 136 sjis s_jis shift_jis_2004 shiftjis2004 sjis_2004 sjis2004
139 137 shift_jisx0213 shiftjisx0213 sjisx0213 s_jisx0213 950 cp950 ms950 '''
140 138
141 139 def reposetup(ui, repo):
142 140 # TODO: decide use of config section for this extension
143 141 if not os.path.supports_unicode_filenames:
144 142 ui.warn(_("[win32mbcs] cannot activate on this platform.\n"))
145 143 return
146 144 # determine encoding for filename
147 145 global _encoding
148 146 _encoding = ui.config('win32mbcs', 'encoding', encoding.encoding)
149 147 # fake is only for relevant environment.
150 148 if _encoding.lower() in problematic_encodings.split():
151 149 for f in funcs.split():
152 150 wrapname(f, wrapper)
153 151 wrapname("mercurial.osutil.listdir", wrapperforlistdir)
154 152 ui.debug("[win32mbcs] activated with encoding: %s\n"
155 153 % _encoding)
156 154
General Comments 0
You need to be logged in to leave comments. Login now