Feat: import old code
This commit is contained in:
parent
42806568af
commit
0c3ee1c020
4
clean_rst/__main__.py
Normal file
4
clean_rst/__main__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from clean_rst.main import main
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
raise SystemExit(main())
|
140
clean_rst/main.py
Normal file
140
clean_rst/main.py
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
|
||||||
|
import restructuredtext_lint
|
||||||
|
from git import Repo
|
||||||
|
|
||||||
|
formatter = logging.Formatter("%(name)s :: %(levelname)s :: %(message)s")
|
||||||
|
steam_handler = logging.StreamHandler()
|
||||||
|
# steam_handler = logging.FileHandler('logging.conf')
|
||||||
|
steam_handler.setLevel(logging.DEBUG)
|
||||||
|
steam_handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
# création de l'objet logger qui va nous servir à écrire dans les logs
|
||||||
|
logger = logging.getLogger("precommit")
|
||||||
|
# on met le niveau du logger à DEBUG, comme ça il écrit tout
|
||||||
|
logger.setLevel(logging.WARNING)
|
||||||
|
logger.addHandler(steam_handler)
|
||||||
|
|
||||||
|
# Files selection
|
||||||
|
|
||||||
|
|
||||||
|
def get_commited_files(repo):
|
||||||
|
hdiff = repo.head.commit.diff()
|
||||||
|
diff = {"A": [], "M": []}
|
||||||
|
for f in hdiff.iter_change_type("A"):
|
||||||
|
diff["A"].append(f.b_path)
|
||||||
|
for f in hdiff.iter_change_type("M"):
|
||||||
|
diff["M"].append(f.b_path)
|
||||||
|
|
||||||
|
return diff
|
||||||
|
|
||||||
|
|
||||||
|
def select_by_extension(files, ext="rst"):
|
||||||
|
return [i for i in files if i.split(".")[-1] == ext and "skeleton" not in i]
|
||||||
|
|
||||||
|
|
||||||
|
# Rst linter
|
||||||
|
|
||||||
|
|
||||||
|
def rst_lint(filename):
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
errors = restructuredtext_lint.lint(f.read())
|
||||||
|
for e in errors:
|
||||||
|
logger.warning(f"{filename} \n{e.full_message}\n")
|
||||||
|
return errors
|
||||||
|
|
||||||
|
|
||||||
|
# Rst parameters normalize
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_file(filename, normalizers={}):
|
||||||
|
logger.debug(f"Normalizing {filename}")
|
||||||
|
logger.debug(f"With {normalizers}")
|
||||||
|
new_file = ""
|
||||||
|
modified_lines = []
|
||||||
|
with open(filename, "r") as f:
|
||||||
|
for l in f.readlines():
|
||||||
|
new_line = run_normalizers(l, normalizers)
|
||||||
|
if new_line != l:
|
||||||
|
modified_lines.append(f"{l}")
|
||||||
|
logger.warning(f"{filename}\n\t{l}\t{new_line}")
|
||||||
|
new_file += new_line
|
||||||
|
|
||||||
|
with open(filename, "w") as f:
|
||||||
|
f.write(new_file)
|
||||||
|
logger.debug(f"{filename} written")
|
||||||
|
|
||||||
|
return modified_lines
|
||||||
|
|
||||||
|
|
||||||
|
def run_normalizers(line, normalizers):
|
||||||
|
for c in normalizers:
|
||||||
|
obs = re.search(c, line)
|
||||||
|
if obs:
|
||||||
|
logger.debug(f"Find for {c}")
|
||||||
|
return normalizers[c](line)
|
||||||
|
return line
|
||||||
|
|
||||||
|
|
||||||
|
# Rst function tools
|
||||||
|
|
||||||
|
|
||||||
|
def update_date(line):
|
||||||
|
date = time.strftime("%Y-%m-%d")
|
||||||
|
logger.debug(f"Update Date to: {date}")
|
||||||
|
return f":date: {date}\n"
|
||||||
|
|
||||||
|
|
||||||
|
def update_modified(line):
|
||||||
|
modified = time.strftime("%Y-%m-%d")
|
||||||
|
logger.debug(f"Update modified to: {modified}")
|
||||||
|
return f":modified: {modified}\n"
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_tags(line):
|
||||||
|
logger.debug(f"Normaizing tags")
|
||||||
|
tags = line.split(":")[-1]
|
||||||
|
tags = [i.strip().capitalize() for i in tags.split(",")]
|
||||||
|
tags_str = ", ".join(tags)
|
||||||
|
logger.debug(f"Update tags to: {tags_str}")
|
||||||
|
return f":tags: {tags_str}\n"
|
||||||
|
|
||||||
|
|
||||||
|
NORMALIZERS_MODIFIED = {
|
||||||
|
":modified:.*": update_modified,
|
||||||
|
":tags:.*": normalize_tags,
|
||||||
|
}
|
||||||
|
|
||||||
|
NORMALIZERS_NEW = {
|
||||||
|
":date:.*": update_date,
|
||||||
|
":modified:.*": update_modified,
|
||||||
|
":tags:.*": normalize_tags,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str] = None) -> int:
|
||||||
|
r = Repo()
|
||||||
|
diff = get_commited_files(r)
|
||||||
|
|
||||||
|
errors = []
|
||||||
|
modified = []
|
||||||
|
|
||||||
|
# New files
|
||||||
|
for f in select_by_extension(diff["A"], "rst"):
|
||||||
|
errors += rst_lint(f)
|
||||||
|
modified += normalize_file(f, NORMALIZERS_NEW)
|
||||||
|
r.index.add([f])
|
||||||
|
# Modified files
|
||||||
|
for f in select_by_extension(diff["M"], "rst"):
|
||||||
|
errors += rst_lint(f)
|
||||||
|
modified += normalize_file(f, NORMALIZERS_MODIFIED)
|
||||||
|
r.index.add([f])
|
||||||
|
|
||||||
|
return int(len(errors) > 0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
70
poetry.lock
generated
70
poetry.lock
generated
@ -44,6 +44,14 @@ category = "dev"
|
|||||||
optional = false
|
optional = false
|
||||||
python-versions = "*"
|
python-versions = "*"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "docutils"
|
||||||
|
version = "0.19"
|
||||||
|
description = "Docutils -- Python Documentation Utilities"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "filelock"
|
name = "filelock"
|
||||||
version = "3.8.0"
|
version = "3.8.0"
|
||||||
@ -56,6 +64,28 @@ python-versions = ">=3.7"
|
|||||||
docs = ["furo (>=2022.6.21)", "sphinx (>=5.1.1)", "sphinx-autodoc-typehints (>=1.19.1)"]
|
docs = ["furo (>=2022.6.21)", "sphinx (>=5.1.1)", "sphinx-autodoc-typehints (>=1.19.1)"]
|
||||||
testing = ["covdefaults (>=2.2)", "coverage (>=6.4.2)", "pytest (>=7.1.2)", "pytest-cov (>=3)", "pytest-timeout (>=2.1)"]
|
testing = ["covdefaults (>=2.2)", "coverage (>=6.4.2)", "pytest (>=7.1.2)", "pytest-cov (>=3)", "pytest-timeout (>=2.1)"]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gitdb"
|
||||||
|
version = "4.0.9"
|
||||||
|
description = "Git Object Database"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.6"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
smmap = ">=3.0.1,<6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "gitpython"
|
||||||
|
version = "3.1.27"
|
||||||
|
description = "GitPython is a python library used to interact with Git repositories"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.7"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
gitdb = ">=4.0.1,<5"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "identify"
|
name = "identify"
|
||||||
version = "2.5.3"
|
version = "2.5.3"
|
||||||
@ -182,6 +212,25 @@ category = "dev"
|
|||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.6"
|
python-versions = ">=3.6"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "restructuredtext-lint"
|
||||||
|
version = "1.4.0"
|
||||||
|
description = "reStructuredText linter"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = "*"
|
||||||
|
|
||||||
|
[package.dependencies]
|
||||||
|
docutils = ">=0.11,<1.0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "smmap"
|
||||||
|
version = "5.0.0"
|
||||||
|
description = "A pure Python implementation of a sliding window memory map manager"
|
||||||
|
category = "main"
|
||||||
|
optional = false
|
||||||
|
python-versions = ">=3.6"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.10.2"
|
version = "0.10.2"
|
||||||
@ -218,7 +267,7 @@ python-versions = "*"
|
|||||||
[metadata]
|
[metadata]
|
||||||
lock-version = "1.1"
|
lock-version = "1.1"
|
||||||
python-versions = "^3.10"
|
python-versions = "^3.10"
|
||||||
content-hash = "1e4f6b994d740ae7f8d013222899e67fdb51d817014e239c70f4266618c82919"
|
content-hash = "f42f1e61719183591304472b3ed90dfbd50fb6ff8ecbd502c7c23c0cd85ce935"
|
||||||
|
|
||||||
[metadata.files]
|
[metadata.files]
|
||||||
atomicwrites = [
|
atomicwrites = [
|
||||||
@ -240,10 +289,22 @@ distlib = [
|
|||||||
{file = "distlib-0.3.5-py2.py3-none-any.whl", hash = "sha256:b710088c59f06338ca514800ad795a132da19fda270e3ce4affc74abf955a26c"},
|
{file = "distlib-0.3.5-py2.py3-none-any.whl", hash = "sha256:b710088c59f06338ca514800ad795a132da19fda270e3ce4affc74abf955a26c"},
|
||||||
{file = "distlib-0.3.5.tar.gz", hash = "sha256:a7f75737c70be3b25e2bee06288cec4e4c221de18455b2dd037fe2a795cab2fe"},
|
{file = "distlib-0.3.5.tar.gz", hash = "sha256:a7f75737c70be3b25e2bee06288cec4e4c221de18455b2dd037fe2a795cab2fe"},
|
||||||
]
|
]
|
||||||
|
docutils = [
|
||||||
|
{file = "docutils-0.19-py3-none-any.whl", hash = "sha256:5e1de4d849fee02c63b040a4a3fd567f4ab104defd8a5511fbbc24a8a017efbc"},
|
||||||
|
{file = "docutils-0.19.tar.gz", hash = "sha256:33995a6753c30b7f577febfc2c50411fec6aac7f7ffeb7c4cfe5991072dcf9e6"},
|
||||||
|
]
|
||||||
filelock = [
|
filelock = [
|
||||||
{file = "filelock-3.8.0-py3-none-any.whl", hash = "sha256:617eb4e5eedc82fc5f47b6d61e4d11cb837c56cb4544e39081099fa17ad109d4"},
|
{file = "filelock-3.8.0-py3-none-any.whl", hash = "sha256:617eb4e5eedc82fc5f47b6d61e4d11cb837c56cb4544e39081099fa17ad109d4"},
|
||||||
{file = "filelock-3.8.0.tar.gz", hash = "sha256:55447caa666f2198c5b6b13a26d2084d26fa5b115c00d065664b2124680c4edc"},
|
{file = "filelock-3.8.0.tar.gz", hash = "sha256:55447caa666f2198c5b6b13a26d2084d26fa5b115c00d065664b2124680c4edc"},
|
||||||
]
|
]
|
||||||
|
gitdb = [
|
||||||
|
{file = "gitdb-4.0.9-py3-none-any.whl", hash = "sha256:8033ad4e853066ba6ca92050b9df2f89301b8fc8bf7e9324d412a63f8bf1a8fd"},
|
||||||
|
{file = "gitdb-4.0.9.tar.gz", hash = "sha256:bac2fd45c0a1c9cf619e63a90d62bdc63892ef92387424b855792a6cabe789aa"},
|
||||||
|
]
|
||||||
|
gitpython = [
|
||||||
|
{file = "GitPython-3.1.27-py3-none-any.whl", hash = "sha256:5b68b000463593e05ff2b261acff0ff0972df8ab1b70d3cdbd41b546c8b8fc3d"},
|
||||||
|
{file = "GitPython-3.1.27.tar.gz", hash = "sha256:1c885ce809e8ba2d88a29befeb385fcea06338d3640712b59ca623c220bb5704"},
|
||||||
|
]
|
||||||
identify = [
|
identify = [
|
||||||
{file = "identify-2.5.3-py2.py3-none-any.whl", hash = "sha256:25851c8c1370effb22aaa3c987b30449e9ff0cece408f810ae6ce408fdd20893"},
|
{file = "identify-2.5.3-py2.py3-none-any.whl", hash = "sha256:25851c8c1370effb22aaa3c987b30449e9ff0cece408f810ae6ce408fdd20893"},
|
||||||
{file = "identify-2.5.3.tar.gz", hash = "sha256:887e7b91a1be152b0d46bbf072130235a8117392b9f1828446079a816a05ef44"},
|
{file = "identify-2.5.3.tar.gz", hash = "sha256:887e7b91a1be152b0d46bbf072130235a8117392b9f1828446079a816a05ef44"},
|
||||||
@ -319,6 +380,13 @@ pyyaml = [
|
|||||||
{file = "PyYAML-6.0-cp39-cp39-win_amd64.whl", hash = "sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c"},
|
{file = "PyYAML-6.0-cp39-cp39-win_amd64.whl", hash = "sha256:b3d267842bf12586ba6c734f89d1f5b871df0273157918b0ccefa29deb05c21c"},
|
||||||
{file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"},
|
{file = "PyYAML-6.0.tar.gz", hash = "sha256:68fb519c14306fec9720a2a5b45bc9f0c8d1b9c72adf45c37baedfcd949c35a2"},
|
||||||
]
|
]
|
||||||
|
restructuredtext-lint = [
|
||||||
|
{file = "restructuredtext_lint-1.4.0.tar.gz", hash = "sha256:1b235c0c922341ab6c530390892eb9e92f90b9b75046063e047cacfb0f050c45"},
|
||||||
|
]
|
||||||
|
smmap = [
|
||||||
|
{file = "smmap-5.0.0-py3-none-any.whl", hash = "sha256:2aba19d6a040e78d8b09de5c57e96207b09ed71d8e55ce0959eeee6c8e190d94"},
|
||||||
|
{file = "smmap-5.0.0.tar.gz", hash = "sha256:c840e62059cd3be204b0c9c9f74be2c09d5648eddd4580d9314c3ecde0b30936"},
|
||||||
|
]
|
||||||
toml = [
|
toml = [
|
||||||
{file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"},
|
{file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"},
|
||||||
{file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"},
|
{file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"},
|
||||||
|
@ -4,8 +4,13 @@ version = "0.1.0"
|
|||||||
description = "pre-commit to clean rst index files"
|
description = "pre-commit to clean rst index files"
|
||||||
authors = ["Bertrand Benjamin <benjamin.bertrand@opytex.org>"]
|
authors = ["Bertrand Benjamin <benjamin.bertrand@opytex.org>"]
|
||||||
|
|
||||||
|
[tool.poetry.scripts]
|
||||||
|
clean-rst = "clean_rst.main:main"
|
||||||
|
|
||||||
[tool.poetry.dependencies]
|
[tool.poetry.dependencies]
|
||||||
python = "^3.10"
|
python = "^3.10"
|
||||||
|
restructuredtext-lint = "^1.4.0"
|
||||||
|
GitPython = "^3.1.27"
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
pytest = "^5.2"
|
pytest = "^5.2"
|
||||||
|
Loading…
Reference in New Issue
Block a user