-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #30 from craigds/bin-script
Add `cchardetect` CLI script
- Loading branch information
Showing
4 changed files
with
33 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ dist | |
build | ||
eggs | ||
parts | ||
bin | ||
var | ||
sdist | ||
develop-eggs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
- Implement cli tool (like chardet cli) | ||
- Improve docs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('rb')) | ||
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters