-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael Chase
committed
Nov 10, 2014
1 parent
8018295
commit 85bf726
Showing
44 changed files
with
7,578 additions
and
14 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
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
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
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
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
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 @@ | ||
######################## BEGIN LICENSE BLOCK ######################## | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
# 02110-1301 USA | ||
######################### END LICENSE BLOCK ######################### | ||
|
||
__version__ = "2.3.0" | ||
from sys import version_info | ||
|
||
|
||
def detect(aBuf): | ||
if ((version_info < (3, 0) and isinstance(aBuf, unicode)) or | ||
(version_info >= (3, 0) and not isinstance(aBuf, bytes))): | ||
raise ValueError('Expected a bytes object, not a unicode object') | ||
|
||
from . import universaldetector | ||
u = universaldetector.UniversalDetector() | ||
u.reset() | ||
u.feed(aBuf) | ||
u.close() | ||
return u.result |
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
######################## BEGIN LICENSE BLOCK ######################## | ||
# The Original Code is Mozilla Communicator client code. | ||
# | ||
# The Initial Developer of the Original Code is | ||
# Netscape Communications Corporation. | ||
# Portions created by the Initial Developer are Copyright (C) 1998 | ||
# the Initial Developer. All Rights Reserved. | ||
# | ||
# Contributor(s): | ||
# Mark Pilgrim - port to Python | ||
# | ||
# This library is free software; you can redistribute it and/or | ||
# modify it under the terms of the GNU Lesser General Public | ||
# License as published by the Free Software Foundation; either | ||
# version 2.1 of the License, or (at your option) any later version. | ||
# | ||
# This library is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
# Lesser General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Lesser General Public | ||
# License along with this library; if not, write to the Free Software | ||
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
# 02110-1301 USA | ||
######################### END LICENSE BLOCK ######################### | ||
|
||
from .mbcharsetprober import MultiByteCharSetProber | ||
from .codingstatemachine import CodingStateMachine | ||
from .chardistribution import Big5DistributionAnalysis | ||
from .mbcssm import Big5SMModel | ||
|
||
|
||
class Big5Prober(MultiByteCharSetProber): | ||
def __init__(self): | ||
MultiByteCharSetProber.__init__(self) | ||
self._mCodingSM = CodingStateMachine(Big5SMModel) | ||
self._mDistributionAnalyzer = Big5DistributionAnalysis() | ||
self.reset() | ||
|
||
def get_charset_name(self): | ||
return "Big5" |
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,80 @@ | ||
#!/usr/bin/env python | ||
""" | ||
Script which takes one or more file paths and reports on their detected | ||
encodings | ||
Example:: | ||
% chardetect somefile someotherfile | ||
somefile: windows-1252 with confidence 0.5 | ||
someotherfile: ascii with confidence 1.0 | ||
If no paths are provided, it takes its input from stdin. | ||
""" | ||
|
||
from __future__ import absolute_import, print_function, unicode_literals | ||
|
||
import argparse | ||
import sys | ||
from io import open | ||
|
||
from chardet import __version__ | ||
from chardet.universaldetector import UniversalDetector | ||
|
||
|
||
def description_of(lines, name='stdin'): | ||
""" | ||
Return a string describing the probable encoding of a file or | ||
list of strings. | ||
:param lines: The lines to get the encoding of. | ||
:type lines: Iterable of bytes | ||
:param name: Name of file or collection of lines | ||
:type name: str | ||
""" | ||
u = UniversalDetector() | ||
for line in lines: | ||
u.feed(line) | ||
u.close() | ||
result = u.result | ||
if result['encoding']: | ||
return '{0}: {1} with confidence {2}'.format(name, result['encoding'], | ||
result['confidence']) | ||
else: | ||
return '{0}: no result'.format(name) | ||
|
||
|
||
def main(argv=None): | ||
''' | ||
Handles command line arguments and gets things started. | ||
:param argv: List of arguments, as if specified on the command-line. | ||
If None, ``sys.argv[1:]`` is used instead. | ||
:type argv: list of str | ||
''' | ||
# Get command line arguments | ||
parser = argparse.ArgumentParser( | ||
description="Takes one or more file paths and reports their detected \ | ||
encodings", | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
conflict_handler='resolve') | ||
parser.add_argument('input', | ||
help='File whose encoding we would like to determine.', | ||
type=argparse.FileType('rb'), nargs='*', | ||
default=[sys.stdin]) | ||
parser.add_argument('--version', action='version', | ||
version='%(prog)s {0}'.format(__version__)) | ||
args = parser.parse_args(argv) | ||
|
||
for f in args.input: | ||
if f.isatty(): | ||
print("You are running chardetect interactively. Press " + | ||
"CTRL-D twice at the start of a blank line to signal the " + | ||
"end of your input. If you want help, run chardetect " + | ||
"--help\n", file=sys.stderr) | ||
print(description_of(f, f.name)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
Oops, something went wrong.