##// END OF EJS Templates
convert: add some utility code for working with shlex on Python 3...
Augie Fackler -
r36575:d60430dc default
parent child Browse files
Show More
@@ -11,6 +11,7 b' import datetime'
11 import errno
11 import errno
12 import os
12 import os
13 import re
13 import re
14 import shlex
14 import subprocess
15 import subprocess
15
16
16 from mercurial.i18n import _
17 from mercurial.i18n import _
@@ -25,6 +26,58 b' from mercurial import ('
25 pickle = util.pickle
26 pickle = util.pickle
26 propertycache = util.propertycache
27 propertycache = util.propertycache
27
28
29 def _encodeornone(d):
30 if d is None:
31 return
32 return d.encode('latin1')
33
34 class _shlexpy3proxy(object):
35
36 def __init__(self, l):
37 self._l = l
38
39 def __iter__(self):
40 return (_encodeornone(v) for v in self._l)
41
42 def get_token(self):
43 return _encodeornone(self._l.get_token())
44
45 @property
46 def infile(self):
47 return self._l.infile or '<unknown>'
48
49 @property
50 def lineno(self):
51 return self._l.lineno
52
53 def shlexer(data=None, filepath=None, wordchars=None, whitespace=None):
54 if data is None:
55 if pycompat.ispy3:
56 data = open(filepath, 'r', encoding=r'latin1')
57 else:
58 data = open(filepath, 'r')
59 else:
60 if filepath is not None:
61 raise error.ProgrammingError(
62 'shlexer only accepts data or filepath, not both')
63 if pycompat.ispy3:
64 data = data.decode('latin1')
65 l = shlex.shlex(data, infile=filepath, posix=True)
66 if whitespace is not None:
67 l.whitespace_split = True
68 if pycompat.ispy3:
69 l.whitespace += whitespace.decode('latin1')
70 else:
71 l.whitespace += whitespace
72 if wordchars is not None:
73 if pycompat.ispy3:
74 l.wordchars += wordchars.decode('latin1')
75 else:
76 l.wordchars += wordchars
77 if pycompat.ispy3:
78 return _shlexpy3proxy(l)
79 return l
80
28 def encodeargs(args):
81 def encodeargs(args):
29 def encodearg(s):
82 def encodearg(s):
30 lines = base64.encodestring(s)
83 lines = base64.encodestring(s)
General Comments 0
You need to be logged in to leave comments. Login now