| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,13 @@ | |||
| 9 | 9 | ||
| 10 | 10 | here = os.path.dirname(__file__) | |
| 11 | 11 | ||
| 12 | + if PY3: | ||
| 13 | + def _u(x): | ||
| 14 | + return x | ||
| 15 | + else: | ||
| 16 | + def _u(x): | ||
| 17 | + return x.decode('unicode-escape') | ||
| 18 | + | ||
| 12 | 19 | def get_cmd(): | |
| 13 | 20 | sys_path = os.path.join(here, "..") | |
| 14 | 21 | ||
@@ -68,3 +75,7 @@ def test_mixed_piped(self): | |||
| 68 | 75 | if PY3: | |
| 69 | 76 | out = out.decode(locale.getpreferredencoding()) | |
| 70 | 77 | 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')) | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,22 +10,36 @@ | |||
| 10 | 10 | ||
| 11 | 11 | PY3 = sys.version_info[0] >= 3 | |
| 12 | 12 | ||
| 13 | + def fatal(msg): | ||
| 14 | + sys.stderr.write(msg + "\n") | ||
| 15 | + sys.exit(1) | ||
| 16 | + | ||
| 13 | 17 | def main(): | |
| 14 | 18 | default_encoding = locale.getpreferredencoding() | |
| 15 | 19 | ||
| 16 | 20 | parser = optparse.OptionParser('%prog [options] [FILE]', | |
| 17 | 21 | 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.") | ||
| 19 | 23 | parser.add_option('-e', '--encoding', metavar='ENCODING', default=default_encoding, | |
| 20 | 24 | 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') | ||
| 21 | 27 | ||
| 22 | 28 | options, args = parser.parse_args() | |
| 23 | 29 | ||
| 24 | 30 | encoding = options.encoding | |
| 25 | 31 | ||
| 26 | 32 | 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 | ||
| 29 | 43 | else: | |
| 30 | 44 | if PY3: | |
| 31 | 45 | stream = sys.stdin.buffer.read() | |
@@ -35,8 +49,6 @@ def main(): | |||
| 35 | 49 | try: | |
| 36 | 50 | stream = stream.decode(encoding) | |
| 37 | 51 | 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)) | ||
| 41 | 53 | ||
| 42 | 54 | print(unidecode(stream)) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments