Skip to content

Commit

Permalink
add support for reading doc8 config from pyproject.toml file (#49)
Browse files Browse the repository at this point in the history
* add support for reading config from pyproject.toml file

* fix lint error

* fix black
  • Loading branch information
swaldhoer authored Nov 27, 2020
1 parent 1fc3c21 commit 431faf9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
46 changes: 35 additions & 11 deletions doc8/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
import os
import sys

try:
import toml

HAVE_TOML = True
except ImportError:
HAVE_TOML = False

from stevedore import extension

Expand All @@ -46,6 +52,8 @@
FILE_PATTERNS = [".rst", ".txt"]
MAX_LINE_LENGTH = 79
CONFIG_FILENAMES = ["doc8.ini", "tox.ini", "pep8.ini", "setup.cfg"]
if HAVE_TOML:
CONFIG_FILENAMES.extend(["pyproject.toml"])


def split_set_type(text, delimiter=","):
Expand All @@ -69,18 +77,11 @@ def parse_ignore_path_errors(entries):
return dict(ignore_path_errors)


def extract_config(args):
def from_ini(fp):
parser = configparser.RawConfigParser()
read_files = []
if args["config"]:
for fn in args["config"]:
with open(fn, "r") as fh:
parser.readfp(fh, filename=fn)
read_files.append(fn)
else:
read_files.extend(parser.read(CONFIG_FILENAMES))
if not read_files:
return {}
with open(fp, "r") as fh:
parser.read_file(fh)

cfg = {}
try:
cfg["max_line_length"] = parser.getint("doc8", "max-line-length")
Expand Down Expand Up @@ -132,6 +133,29 @@ def extract_config(args):
return cfg


def from_toml(fp):
cfg = toml.load(fp).get("tool", {}).get("doc8", {})
return cfg


def extract_config(args):
cfg = {}
for cfg_file in args["config"] or CONFIG_FILENAMES:
if not os.path.isfile(cfg_file):
if args["config"]:
print(
"Configuration file %s does not exist...ignoring" % (args["config"])
)
continue
if cfg_file.endswith((".ini", ".cfg")):
cfg = from_ini(cfg_file)
elif cfg_file.endswith(".toml") and HAVE_TOML:
cfg = from_toml(cfg_file)
if cfg:
break
return cfg


def fetch_checks(cfg):
base = [
checks.CheckValidity(cfg),
Expand Down
1 change: 1 addition & 0 deletions test-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mock # BSD
nose # LGPL
testtools # MIT
toml # MIT

0 comments on commit 431faf9

Please sign in to comment.