-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure README is synched with code (#43)
* Ensure README is synched with code * Use main function * Add docstring to readme.py * Compensate for regex substitution of \\ to \ Replacement strings used in regex substitution process backlash escapes including R"\\". So we need to expand these to R"\\\\" before [1]. [1] https://docs.python.org/3/library/re.html#re.sub * Make script independent of working directory and pin paths relative to the scripts position. Should be a bit more robust. * Update README.md Co-authored-by: Lars Grüter <lagru+github@mailbox.org> * Refactor before expanding to other sections * Expand to other sections * Regenerate README * Run linter * Fix pre-commit hook for readme.py * Fix comments --------- Co-authored-by: Lars Grüter <lagru+github@mailbox.org> Co-authored-by: Lars Grüter <lagru@mailbox.org>
- Loading branch information
1 parent
711a0c7
commit ac56307
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
"""Update README.md, used by pre-commit.""" | ||
|
||
import re | ||
from pathlib import Path | ||
|
||
PROJECT_ROOT = Path(__file__).parent.parent | ||
|
||
|
||
def get_section_info(file): | ||
p = Path(file) | ||
name = p.parts[-1] | ||
ext = p.suffix[1:] | ||
begin = rf"<!--- begin {name} --->\n" | ||
end = rf"<!--- end {name} --->\n" | ||
|
||
with open(PROJECT_ROOT / file) as fh: | ||
section = fh.read() | ||
|
||
if ext == "toml": | ||
section = begin + rf"\n````{ext}\n" + section + r"````\n\n" + end | ||
else: | ||
section = begin + rf"\n```{ext}\n" + section + r"```\n\n" + end | ||
|
||
return begin, end, section | ||
|
||
|
||
def main(): | ||
with open(PROJECT_ROOT / "README.md") as fh: | ||
readme = fh.read() | ||
|
||
# default_config.toml | ||
begin, end, section = get_section_info("src/changelist/default_config.toml") | ||
rx = re.compile(begin + ".*?" + end, re.DOTALL) | ||
# Regex substitution replaces r"\\" with to r"\", compensate | ||
section = section.replace(r"\\", r"\\\\") | ||
readme = rx.sub(section, readme) | ||
|
||
# label-check.yaml | ||
begin, end, section = get_section_info(".github/workflows/label-check.yaml") | ||
rx = re.compile(begin + ".*?" + end, re.DOTALL) | ||
readme = rx.sub(section, readme) | ||
|
||
# milestone-merged-prs.yaml | ||
begin, end, section = get_section_info( | ||
".github/workflows/milestone-merged-prs.yaml" | ||
) | ||
rx = re.compile(begin + ".*?" + end, re.DOTALL) | ||
readme = rx.sub(section, readme) | ||
|
||
with open(PROJECT_ROOT / "README.md", "w") as fh: | ||
fh.write(readme) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters