Skip to content

Commit

Permalink
Make toml support implicit
Browse files Browse the repository at this point in the history
  • Loading branch information
ssbarnea committed Jul 27, 2022
1 parent fb12b0a commit ce06e46
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 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 or when using a Python version greater or equal than 3.11
* ``$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.10'
Pygments

[options.entry_points]
Expand Down
30 changes: 16 additions & 14 deletions src/doc8/main.py
Expand Up @@ -31,20 +31,17 @@
import argparse
import collections
import configparser
import importlib
import logging
import os
import sys


HAVE_TOML = False
for module_name in ("tomllib", "tomli"):
try:
toml_module = importlib.import_module(module_name)
HAVE_TOML = True
break
except ModuleNotFoundError:
pass
try:
# py3.10+
from tomllib import load as toml_load # type: ignore
except ImportError:
# py3.9 or older
from tomli import load as toml_load

from stevedore import extension

Expand All @@ -55,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 @@ -139,7 +141,7 @@ def from_ini(fp):

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

cfg = {}
for key, value in parsed.items():
Expand All @@ -161,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 ce06e46

Please sign in to comment.