1 ######################## BEGIN LICENSE BLOCK ########################
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of the GNU Lesser General Public
4 # License as published by the Free Software Foundation; either
5 # version 2.1 of the License, or (at your option) any later version.
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
16 ######################### END LICENSE BLOCK #########################
19 from sys import version_info
23 if ((version_info < (3, 0) and isinstance(aBuf, unicode)) or
24 (version_info >= (3, 0) and not isinstance(aBuf, bytes))):
25 raise ValueError('Expected a bytes object, not a unicode object')
27 from . import universaldetector
28 u = universaldetector.UniversalDetector()
34 def _description_of(path):
35 """Return a string describing the probable encoding of a file."""
36 from charade.universaldetector import UniversalDetector
38 u = UniversalDetector()
39 for line in open(path, 'rb'):
43 if result['encoding']:
44 return '%s: %s with confidence %s' % (path,
48 return '%s: no result' % path
53 Script which takes one or more file paths and reports on their detected
58 % chardetect.py somefile someotherfile
59 somefile: windows-1252 with confidence 0.5
60 someotherfile: ascii with confidence 1.0
65 print(_description_of(path))