From ca8099afb22c12c365648b3de78c2f9124a3c71e Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Thu, 30 Mar 2023 09:40:37 +0200 Subject: [PATCH] Move scripts to a .scripts/ directory. --- .gitignore | 1 + .scripts/line-length.py | 38 +++++++++++++++++++++++++++++++++++ merge.py => .scripts/merge.py | 0 Makefile | 13 +----------- 4 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 .scripts/line-length.py rename merge.py => .scripts/merge.py (100%) diff --git a/.gitignore b/.gitignore index 6d4d663d..78c4fbf3 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ venv/ .potodo/ locales/ .venv/ +.envrc diff --git a/.scripts/line-length.py b/.scripts/line-length.py new file mode 100644 index 00000000..b4ac21f4 --- /dev/null +++ b/.scripts/line-length.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python +"""Measure line length in given files, run as: + + python line-length.py *.po + +It does not count zero-width caracters from the Mn Unicode category +(Nonspacing Mark). + +It returns 0 on success, 1 on failure. +""" + +from unicodedata import category +import fileinput +import sys + +SOFT_LIMIT = 80 # used for splitables lines (with spaces in them) +HARD_LIMIT = 88 # used for non-splitables lines (without spaces in them) + + +def clean(line): + return "".join(char for char in line if category(char) != "Mn").rstrip("\n") + + +return_code = 0 + +for line in fileinput.input(encoding="utf-8"): + line = clean(line) + limit = SOFT_LIMIT if line.count(" ") > 1 else HARD_LIMIT + if len(line) > limit: + print( + f"{fileinput.filename()}:{fileinput.filelineno()} line too long " + f"({len(line)} > {limit} characters)", + file=sys.stderr, + ) + return_code = 1 + + +sys.exit(return_code) diff --git a/merge.py b/.scripts/merge.py similarity index 100% rename from merge.py rename to .scripts/merge.py diff --git a/Makefile b/Makefile index 5689df8c..6042da54 100644 --- a/Makefile +++ b/Makefile @@ -150,18 +150,7 @@ spell: ensure_test_prerequisites $(DESTS) .PHONY: line-length line-length: @echo "Searching for long lines..." -# soft limit is used for splitables lines (with spaces in them) -# hard limit is used for non-splitables lines (without spaces in them) - @python -c 'soft_limit=80; \ - hard_limit=88; \ - from unicodedata import category; \ - import fileinput as fi; \ - clean = lambda line: "".join(char for char in line if category(char) != "Mn").strip(); \ - [print(f"""{fi.filename()}:{fi.filelineno()} line too long ({len(line)} > {soft_limit if line.count(" ") > 1 else hard_limit} characters)""") \ - for _line in fi.input(encoding="utf-8") \ - for line in [clean(_line)] \ - if line.count(" ") > 1 and len(line) > soft_limit or line.count(" ") <= 1 and len(line) > hard_limit]' \ - *.po */*.po + @python .scripts/line-length.py *.po */*.po .PHONY: sphinx-lint sphinx-lint: ensure_test_prerequisites