Add cchardetect CLI script, with similar output to chardet's chardetect

This commit is contained in:
Craig de Stigter 2017-05-10 11:15:42 +12:00
parent c991f86f6f
commit b5a00f7abe
4 changed files with 33 additions and 2 deletions

1
.gitignore vendored
View file

@ -10,7 +10,6 @@ dist
build
eggs
parts
bin
var
sdist
develop-eggs

View file

@ -1,2 +1 @@
- Implement cli tool (like chardet cli)
- Improve docs

32
bin/cchardetect Executable file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env python
from __future__ import print_function, unicode_literals
import argparse
import cchardet
def read_chunks(f, chunk_size):
chunk = f.read(chunk_size)
while chunk:
yield chunk
chunk = f.read(chunk_size)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='+', help="Files to detect encoding of", type=argparse.FileType('r'))
parser.add_argument('--chunk-size', type=int, default=(256 * 1024))
args = parser.parse_args()
for f in args.files:
detector = cchardet.UniversalDetector()
for chunk in read_chunks(f, args.chunk_size):
detector.feed(chunk)
detector.close()
print('{file.name}: {result[encoding]} with confidence {result[confidence]}'.format(
file=f,
result=detector.result
))
if __name__ == '__main__':
main()

View file

@ -137,6 +137,7 @@ setup(
cmdclass={'build_ext': build_ext},
package_dir={'': 'src'},
packages=['cchardet', ],
scripts=['bin/cchardetect'],
ext_modules=[
cchardet_module
],