FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-unidecode/unidecode/util.py at master · sysfce2/python-unidecode · GitHub
sysfce2
python-unidecode
Repository navigation
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
python-unidecode
/
unidecode
/
util.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
52 lines (43 loc) · 1.71 KB
Breadcrumbs
python-unidecode
/
unidecode
/
util.py
Copy path
File metadata and controls
52 lines (43 loc) · 1.71 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# vim:ts=4 sw=4 expandtab softtabstop=4
import
argparse
import
io
import
locale
import
os
import
sys
from
unidecode
import
unidecode
def
fatal
(
msg
):
sys
.
stderr
.
write
(
msg
+
"
\n
"
)
sys
.
exit
(
1
)
def
main
():
default_encoding
=
locale
.
getpreferredencoding
()
parser
=
argparse
.
ArgumentParser
(
description
=
"Transliterate Unicode text into ASCII. FILE is path to file to transliterate. "
"Standard input is used if FILE is omitted and -c is not specified."
)
parser
.
add_argument
(
'-e'
,
'--encoding'
,
metavar
=
'ENCODING'
,
default
=
default_encoding
,
help
=
'Specify input encoding (default from your locale is %s)'
%
(
default_encoding
,))
parser
.
add_argument
(
'-c'
,
metavar
=
'TEXT'
,
dest
=
'text'
,
help
=
'Transliterate TEXT instead of FILE'
)
parser
.
add_argument
(
'path'
,
nargs
=
'?'
,
metavar
=
'FILE'
)
args
=
parser
.
parse_args
()
encoding
=
args
.
encoding
if
args
.
path
:
if
args
.
text
:
fatal
(
"Can't use both FILE and -c option"
)
else
:
stream
=
open
(
args
.
path
,
'rb'
)
elif
args
.
text
:
text
=
os
.
fsencode
(
args
.
text
)
# add a newline to the string if it comes from the
# command line so that the result is printed nicely
# on the console.
stream
=
io
.
BytesIO
(
text
+
b'
\n
'
)
else
:
stream
=
sys
.
stdin
.
buffer
for
line_nr
,
line
in
enumerate
(
stream
):
try
:
line
=
line
.
decode
(
encoding
)
except
UnicodeDecodeError
as
e
:
s
=
' '
.
join
(
'%02x'
%
e
.
object
[
n
]
for
n
in
range
(
e
.
start
,
e
.
end
))
fatal
(
'Unable to decode input line %s, bytes %d-%d (%s): %s'
%
(
line_nr
,
e
.
start
,
e
.
end
,
s
,
e
.
reason
))
sys
.
stdout
.
write
(
unidecode
(
line
))
stream
.
close
()
Back
|
FazBrowse Home
|
New Git URL