2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
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.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import sys
|
2023-06-23 04:03:23 +00:00
|
|
|
from typing import Iterable, List, Optional
|
2018-09-17 00:27:00 +00:00
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
from .. import __version__
|
|
|
|
from ..universaldetector import UniversalDetector
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
2023-06-23 04:03:23 +00:00
|
|
|
def description_of(
|
|
|
|
lines: Iterable[bytes],
|
|
|
|
name: str = "stdin",
|
|
|
|
minimal: bool = False,
|
|
|
|
should_rename_legacy: bool = False,
|
|
|
|
) -> Optional[str]:
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
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
|
2023-06-23 04:03:23 +00:00
|
|
|
:param should_rename_legacy: Should we rename legacy encodings to
|
|
|
|
their more modern equivalents?
|
|
|
|
:type should_rename_legacy: ``bool``
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
2023-06-23 04:03:23 +00:00
|
|
|
u = UniversalDetector(should_rename_legacy=should_rename_legacy)
|
2018-09-17 00:27:00 +00:00
|
|
|
for line in lines:
|
|
|
|
line = bytearray(line)
|
|
|
|
u.feed(line)
|
|
|
|
# shortcut out of the loop to save reading further - particularly useful if we read a BOM.
|
|
|
|
if u.done:
|
|
|
|
break
|
|
|
|
u.close()
|
|
|
|
result = u.result
|
2023-06-23 04:03:23 +00:00
|
|
|
if minimal:
|
|
|
|
return result["encoding"]
|
2022-11-07 18:06:49 +00:00
|
|
|
if result["encoding"]:
|
|
|
|
return f'{name}: {result["encoding"]} with confidence {result["confidence"]}'
|
|
|
|
return f"{name}: no result"
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
2023-06-23 04:03:23 +00:00
|
|
|
def main(argv: Optional[List[str]] = None) -> None:
|
2018-09-17 00:27:00 +00:00
|
|
|
"""
|
|
|
|
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(
|
2023-06-23 04:03:23 +00:00
|
|
|
description=(
|
|
|
|
"Takes one or more file paths and reports their detected encodings"
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"input",
|
2023-06-23 04:03:23 +00:00
|
|
|
help="File whose encoding we would like to determine. (default: stdin)",
|
2022-11-07 18:06:49 +00:00
|
|
|
type=argparse.FileType("rb"),
|
|
|
|
nargs="*",
|
|
|
|
default=[sys.stdin.buffer],
|
|
|
|
)
|
2023-06-23 04:03:23 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--minimal",
|
|
|
|
help="Print only the encoding to standard output",
|
|
|
|
action="store_true",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-l",
|
|
|
|
"--legacy",
|
|
|
|
help="Rename legacy encodings to more modern ones.",
|
|
|
|
action="store_true",
|
|
|
|
)
|
2022-11-07 18:06:49 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--version", action="version", version=f"%(prog)s {__version__}"
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
args = parser.parse_args(argv)
|
|
|
|
|
|
|
|
for f in args.input:
|
|
|
|
if f.isatty():
|
2022-11-07 18:06:49 +00:00
|
|
|
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,
|
|
|
|
)
|
2023-06-23 04:03:23 +00:00
|
|
|
print(
|
|
|
|
description_of(
|
|
|
|
f, f.name, minimal=args.minimal, should_rename_legacy=args.legacy
|
|
|
|
)
|
|
|
|
)
|
2018-09-17 00:27:00 +00:00
|
|
|
|
|
|
|
|
2022-11-07 18:06:49 +00:00
|
|
|
if __name__ == "__main__":
|
2018-09-17 00:27:00 +00:00
|
|
|
main()
|