Compare commits

...

3 Commits

8 changed files with 399 additions and 3 deletions

160
.gitignore vendored Normal file
View File

@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

19
.pre-commit-config.yaml Normal file
View File

@ -0,0 +1,19 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-toml
- id: check-yaml
- id: end-of-file-fixer
- id: mixed-line-ending
- repo: https://github.com/psf/black
rev: 22.3.0
hooks:
- id: black
- repo: https://github.com/PyCQA/isort
rev: 5.10.1
hooks:
- id: isort
args: ["--profile", "black"]

View File

@ -1 +1 @@
__version__ = '0.1.0'
__version__ = "0.1.0"

4
clean_rst/__main__.py Normal file
View File

@ -0,0 +1,4 @@
from clean_rst.main import main
if __name__ == "__main__":
raise SystemExit(main())

140
clean_rst/main.py Normal file
View 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
View File

@ -44,6 +44,14 @@ category = "dev"
optional = false
python-versions = "*"
[[package]]
name = "docutils"
version = "0.19"
description = "Docutils -- Python Documentation Utilities"
category = "main"
optional = false
python-versions = ">=3.7"
[[package]]
name = "filelock"
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)"]
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]]
name = "identify"
version = "2.5.3"
@ -182,6 +212,25 @@ category = "dev"
optional = false
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]]
name = "toml"
version = "0.10.2"
@ -218,7 +267,7 @@ python-versions = "*"
[metadata]
lock-version = "1.1"
python-versions = "^3.10"
content-hash = "1e4f6b994d740ae7f8d013222899e67fdb51d817014e239c70f4266618c82919"
content-hash = "f42f1e61719183591304472b3ed90dfbd50fb6ff8ecbd502c7c23c0cd85ce935"
[metadata.files]
atomicwrites = [
@ -240,10 +289,22 @@ distlib = [
{file = "distlib-0.3.5-py2.py3-none-any.whl", hash = "sha256:b710088c59f06338ca514800ad795a132da19fda270e3ce4affc74abf955a26c"},
{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 = [
{file = "filelock-3.8.0-py3-none-any.whl", hash = "sha256:617eb4e5eedc82fc5f47b6d61e4d11cb837c56cb4544e39081099fa17ad109d4"},
{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 = [
{file = "identify-2.5.3-py2.py3-none-any.whl", hash = "sha256:25851c8c1370effb22aaa3c987b30449e9ff0cece408f810ae6ce408fdd20893"},
{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.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 = [
{file = "toml-0.10.2-py2.py3-none-any.whl", hash = "sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b"},
{file = "toml-0.10.2.tar.gz", hash = "sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f"},

View File

@ -4,8 +4,13 @@ version = "0.1.0"
description = "pre-commit to clean rst index files"
authors = ["Bertrand Benjamin <benjamin.bertrand@opytex.org>"]
[tool.poetry.scripts]
clean-rst = "clean_rst.main:main"
[tool.poetry.dependencies]
python = "^3.10"
restructuredtext-lint = "^1.4.0"
GitPython = "^3.1.27"
[tool.poetry.dev-dependencies]
pytest = "^5.2"

View File

@ -2,4 +2,4 @@ from clean_rst import __version__
def test_version():
assert __version__ == '0.1.0'
assert __version__ == "0.1.0"