nutools/lib/pyulib/src/uapps/uencdetect.py

47 lines
1.2 KiB
Python
Raw Normal View History

2013-08-27 15:14:44 +04:00
#!/usr/bin/env python
# -*- coding: utf-8 -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
u"""%(name)s - Détecter l'encoding d'un fichier ou d'une chaine
USAGE
%(name)s "string"
%(name)s -f inputfile"""
import sys
from ulib.all import *
def run_uencdetect():
def print_help():
uprint(__doc__ % {'name': scriptname})
ins = None
infile = None
inf = None
close_inf = False
options, longoptions = build_options([
(None, 'help', u"Afficher l'aide"),
('f:', 'input-file=', u"Fichier dont il faut détecter l'encoding"),
])
options, args = get_args(None, options, longoptions)
for option, value in options:
if option in ('--help', ):
print_help()
sys.exit(0)
elif option in ('-f', '--input-file'):
if value == '-': inf = sys.stdin
else: infile = value
if infile is not None:
inf = open(infile, 'rb')
close_inf = True
elif args: ins = ' '.join(args)
else: inf = sys.stdin
try: print guess_encoding(ins, inf)
finally:
if close_inf: inf.close()
if __name__ == '__main__':
run_uencdetect()