Skip to content

Commit

Permalink
Merge branch 'main' into feature/dependabot-rollup-2021-04-12
Browse files Browse the repository at this point in the history
  • Loading branch information
David Glick authored Apr 13, 2021
2 parents 387c7b8 + 9237c96 commit 46f6f12
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@ repos:
hooks:
- id: check-merge-conflict
- id: flake8
- repo: local
hooks:
- id: merge-docs
name: Merge examples into docs
entry: tools/docs_samples.py
language: python
files: ".*.md|examples/.*.yml"
2 changes: 0 additions & 2 deletions examples/company.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
- option: A # used for a pytest
default: AA
- object: Company
fields:
Name:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def test_arguments(self):
outfile = Path(t) / "foo.txt"
continuation = Path(t) / "out.yml"
generate_data(
yaml_file="examples/company.yml",
user_options={"A": "B"},
target_number=(20, "Employee"),
yaml_file="tests/BDI_generator.yml",
user_options={"num_accounts": "15"},
target_number=(20, "Account"),
debug_internals=True,
output_format="json",
output_file=outfile,
Expand Down
35 changes: 30 additions & 5 deletions tools/docs_samples.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#!/usr/bin/env python3
from pathlib import Path
from glob import glob
from difflib import context_diff

# This tool merges example files into the docs and thus ensures
# that the tested version of the example is also the one in the
# doc.

being_replaced = []
being_replaced_filename = None
replacement = None


class TOP:
"""Top level of the Markdown doc"""
Expand All @@ -27,26 +34,38 @@ def next_state(line, location):


class REPLACING:
"""Code example is open being replaced because it corresponds to a file on disk"""
"""Code example is being replaced because it corresponds to a file on disk"""

def next_state(line, location):
if line.startswith(TRIPLE_QUOTE):
check_triple_quote_alone(line, location)
if replacement != being_replaced:
for diff in context_diff(
replacement,
being_replaced,
fromfile=being_replaced_filename,
tofile="Docs",
):
print(diff, end="")
being_replaced.clear()
return TOP, [line]
else:
being_replaced.append(line)
return REPLACING, [] # IGNORE lines to be replaced


class START_EXAMPLE:
"""First line of a code example"""

def next_state(line, location):
global replacement, being_replaced_filename
if line.startswith("#"):
filename = line.split("#")[1].strip()
assert Path(filename).exists(), location
with Path(filename).open() as f:
example_lines = f.readlines()
print("Replacing", filename)
replacement = example_lines
being_replaced_filename = filename
return REPLACING, [line] + example_lines
else:
return OPEN, [line]
Expand Down Expand Up @@ -78,12 +97,18 @@ def replace_samples(input_file) -> str:
return "".join(output)


def main(filename):
def replace_examples(filename):
with open(filename) as f:
output = replace_samples(f)
with open(filename, "w") as f:
f.write(output)


filename = "docs/index.md"
main(filename)
def main():
filenames = glob("docs/*.md")
for filename in filenames:
replace_examples(filename)


if __name__ == "__main__":
main()

0 comments on commit 46f6f12

Please sign in to comment.