FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add -c command line option. · jangoking/unidecode@b3d1739 · GitHub

Repository navigation

Commit b3d1739

Browse files
committed
Add -c command line option.
1 parent 701e770 commit b3d1739

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎tests/test_utility.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
here = os.path.dirname(__file__)
1111

12+
if PY3:
13+
def _u(x):
14+
return x
15+
else:
16+
def _u(x):
17+
return x.decode('unicode-escape')
18+
1219
def get_cmd():
1320
sys_path = os.path.join(here, "..")
1421

@@ -68,3 +75,7 @@ def test_mixed_piped(self):
6875
if PY3:
6976
out = out.decode(locale.getpreferredencoding())
7077
self.assertEqual(out, self.EXPECTED_MIXED)
78+
79+
def test_commandline(self):
80+
out = run(['-e', 'utf8', '-c', _u('\u4EB0').encode('utf8')])[0]
81+
self.assertEqual(out, 'Jing \n'.encode('ascii'))

‎unidecode/util.py‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,36 @@
1010

1111
PY3 = sys.version_info[0] >= 3
1212

13+
def fatal(msg):
14+
sys.stderr.write(msg + "\n")
15+
sys.exit(1)
16+
1317
def main():
1418
default_encoding = locale.getpreferredencoding()
1519

1620
parser = optparse.OptionParser('%prog [options] [FILE]',
1721
description="Transliterate Unicode text into ASCII. FILE is path to file to transliterate. "
18-
"If omitted, standard input is used.")
22+
"Standard input is used if FILE is omitted and -c is not specified.")
1923
parser.add_option('-e', '--encoding', metavar='ENCODING', default=default_encoding,
2024
help='Specify an encoding (default is %s)' % (default_encoding,))
25+
parser.add_option('-c', metavar='TEXT', dest='text',
26+
help='Transliterate TEXT instead of FILE')
2127

2228
options, args = parser.parse_args()
2329

2430
encoding = options.encoding
2531

2632
if args:
27-
with open(args[0], 'rb') as f:
28-
stream = f.read()
33+
if options.text:
34+
fatal("Can't use both FILE and -c option")
35+
else:
36+
with open(args[0], 'rb') as f:
37+
stream = f.read()
38+
elif options.text:
39+
if PY3:
40+
stream = os.fsencode(options.text)
41+
else:
42+
stream = options.text
2943
else:
3044
if PY3:
3145
stream = sys.stdin.buffer.read()
@@ -35,8 +49,6 @@ def main():
3549
try:
3650
stream = stream.decode(encoding)
3751
except UnicodeDecodeError as e:
38-
msg = 'Unable to decode input: %s, start: %d, end: %d' % (e.reason, e.start, e.end)
39-
print(msg, file=sys.stderr)
40-
sys.exit(1)
52+
fatal('Unable to decode input: %s, start: %d, end: %d' % (e.reason, e.start, e.end))
4153

4254
print(unidecode(stream))

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL