Package: lib-detect-encoding
lib_detect_encodingVersion v1.0.1 as of 2023-10-14 seeChangelogdetects the encoding of textfilesdetects the system preferred encodingreturns the language (if possible) for a given encodingautomated tests, Github Actions, Documentation, Badges, etc. are managed withPizzaCutter(cookiecutter on steroids)Python version required: 3.8.0 or newertested on recent linux with python 3.8, 3.9, 3.10, 3.11, 3.12-dev, pypy-3.9, pypy-3.10 - architectures: amd64100% code coverage, flake8 style checking ,mypy static type checking ,tested underLinux, macOS, Windows, automatic daily builds and monitoringTry it OnlineUsageUsage from CommandlineInstallation and UpgradeRequirementsAcknowledgementsContributeReport IssuesPull RequestCode of ConductLicenseChangelogTry it OnlineYou might try it right away in Jupyter Notebook by using the “launch binder” badge, or clickhereUsagedefget_system_preferred_encoding()->str:""" returns the system preferred encoding in lowercase. Works on posix, windows and WINE
    On windows, the python default function "locale.getpreferredencoding" sometimes reports falsely cp1252 instead of cp850,
    therefore we check also with windows command "chcp" for the correct preferred codepage
    Note that the python codec name will be returned, such as : utf_8, utf_8_sig etc.
    see: https://docs.python.org/3/library/codecs.html#standard-encodings
    """defget_file_encoding(raw_bytes:bytes)->str:""" returns the encoding for the raw_bytes passed.
    if the confidence of the detection is below 95 percent, the system default encoding will be returned
    Note that the python codec name will be returned, such as : utf_8, utf_8_sig etc.
    see: https://docs.python.org/3/library/codecs.html#standard-encodings

    >>> # Setup
    >>> import pathlib
    >>> path_testfile_utf8 = pathlib.Path(__file__).parent.parent / "tests/testfile_utf8.txt"
    >>> raw_utf8_bytes = path_testfile_utf8.read_bytes()

    >>> # Test get encoding from bytes
    >>> assert get_file_encoding(raw_utf8_bytes) == 'utf_8'

    >>> # test get encoding with low confidence (returning system default encoding)
    >>> assert get_file_encoding(b'') is not None
    >>> assert get_file_encoding(b'x') is not None
    >>> assert len(get_file_encoding(b'x')) > 0

    """defget_language_by_codec_name(codec_name:str)->str:""" get the language by python codec name

    >>> # Test OK
    >>> assert  get_language_by_codec_name('utf-8') == "all languages"
    >>> assert  get_language_by_codec_name('utf-8') == "all languages"

    >>> # Test unknown encoding
    >>> get_language_by_codec_name('unknown')
    Traceback (most recent call last):
        ...
    KeyError: 'codec "unknown" not found'

    >>> # Test if language is present for all codepage_aliases
    >>> for codec_alias in codec_aliases: \
            codec_language = get_language_by_codec_name(codec_alias)
    """Usage from CommandlineUsage: lib_detect_encoding [OPTIONS] COMMAND [ARGS]...

  detects encodings of raw files, or the system default encoding

Options:
  --version                     Show the version and exit.
  --traceback / --no-traceback  return traceback information on cli
  -h, --help                    Show this message and exit.

Commands:
  get_file_encoding              get encoding from a (text)file
  get_language                   get the language from a codec name
  get_system_preferred_encoding  get the system preferred encoding
  info                           get program informationsInstallation and UpgradeBefore You start, its highly recommended to update pip and setup tools:python -m pip --upgrade pip
python -m pip --upgrade setuptoolsto install the latest release from PyPi via pip (recommended):python -m pip install --upgrade lib_detect_encodingto install the latest release from PyPi via pip, including test dependencies:python -m pip install --upgrade lib_detect_encoding[test]to install the latest version from github via pip:python -m pip install --upgrade git+https://github.com/bitranox/lib_detect_encoding.gitinclude it into Your requirements.txt:# Insert following line in Your requirements.txt:
# for the latest Release on pypi:
lib_detect_encoding

# for the latest development version :
lib_detect_encoding @ git+https://github.com/bitranox/lib_detect_encoding.git

# to install and upgrade all modules mentioned in requirements.txt:
python -m pip install --upgrade -r /<path>/requirements.txtto install the latest development version, including test dependencies from source code:# cd ~
$ git clone https://github.com/bitranox/lib_detect_encoding.git
$ cd lib_detect_encoding
python -m pip install -e .[test]via makefile:
makefiles are a very convenient way to install. Here we can do much more,
like installing virtual environments, clean caches and so on.# from Your shell's homedirectory:$gitclonehttps://github.com/bitranox/lib_detect_encoding.git$cdlib_detect_encoding# to run the tests:$maketest# to install the package$makeinstall# to clean the package$makeclean# uninstall the package$makeuninstallRequirementsfollowing modules will be automatically installed :## Project Requirementsclickcli_exit_toolschardetlib_log_utilslib_platformAcknowledgementsspecial thanks to “uncle bob” Robert C. Martin, especially for his books on “clean code” and “clean architecture”ContributeI would love for you to fork and send me pull request for this project.
-please ContributeLicenseThis software is licensed under theMIT license—Changelogv1.0.12023-10-14:update documentationv1.0.02023-10-14:create mypy cache dir ‘.mypy_cache’require minimum python 3.8remove python 3.7 testsintroduce PEP517 packaging standardintroduce pyproject.toml build-systemremove mypy.iniremove pytest.iniremove setup.cfgremove setup.pyremove .bettercodehub.ymlremove .travis.ymlupdate black configclean ./tests/test_cli.pyadd codeql badgemove 3rd_party_stubs outside the src directory to./.3rd_party_stubsadd pypy 3.10 testsadd python 3.12-dev tests0.0.12019-07-22: Initial public release