Skip to content

Commit

Permalink
Make toml support implicit (#93)
Browse files Browse the repository at this point in the history
* Add support for tomllib

* Make toml support implicit

Co-authored-by: Sorin Sbarnea <ssbarnea@redhat.com>
  • Loading branch information
JakobDev and ssbarnea committed Jul 27, 2022
1 parent 6dcee38 commit a3638de
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.rst
Expand Up @@ -102,7 +102,7 @@ instead.
* ``$CWD/tox.ini``
* ``$CWD/pep8.ini``
* ``$CWD/setup.cfg``
* ``$CWD/pyproject.toml`` in section ``[tool.doc8]`` if ``tomli`` is installed
* ``$CWD/pyproject.toml``

An example section that can be placed into one of these files::

Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Expand Up @@ -63,6 +63,7 @@ install_requires =
docutils
restructuredtext-lint>=0.7
stevedore
tomli; python_version < '3.11'
Pygments

[options.entry_points]
Expand Down
24 changes: 15 additions & 9 deletions src/doc8/main.py
Expand Up @@ -35,12 +35,13 @@
import os
import sys

try:
import tomli

HAVE_TOML = True
try:
# py3.11+
from tomllib import load as toml_load # type: ignore
except ImportError:
HAVE_TOML = False
# py3.10 or older
from tomli import load as toml_load

from stevedore import extension

Expand All @@ -51,9 +52,14 @@

FILE_PATTERNS = [".rst", ".txt"]
MAX_LINE_LENGTH = 79
CONFIG_FILENAMES = ["doc8.ini", ".config/doc8.ini", "tox.ini", "pep8.ini", "setup.cfg"]
if HAVE_TOML:
CONFIG_FILENAMES.extend(["pyproject.toml"])
CONFIG_FILENAMES = [
"doc8.ini",
".config/doc8.ini",
"tox.ini",
"pep8.ini",
"setup.cfg",
"pyproject.toml",
]


def split_set_type(text, delimiter=","):
Expand Down Expand Up @@ -135,7 +141,7 @@ def from_ini(fp):

def from_toml(fp):
with open(fp, "rb") as f:
parsed = tomli.load(f).get("tool", {}).get("doc8", {})
parsed = toml_load(f).get("tool", {}).get("doc8", {})

cfg = {}
for key, value in parsed.items():
Expand All @@ -157,7 +163,7 @@ def extract_config(args):
continue
if cfg_file.endswith((".ini", ".cfg")):
cfg = from_ini(cfg_file)
elif cfg_file.endswith(".toml") and HAVE_TOML:
elif cfg_file.endswith(".toml"):
cfg = from_toml(cfg_file)
if cfg:
break
Expand Down

0 comments on commit a3638de

Please sign in to comment.