Skip to content

Commit

Permalink
Let tox invoke pre-commit hooks (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
laraconda authored May 5, 2024
1 parent cd82c3d commit cfdfd49
Show file tree
Hide file tree
Showing 22 changed files with 124 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ debian/files
debian/rednotebook.postinst.debhelper
debian/rednotebook.prerm.debhelper
debian/rednotebook.substvars
test-translations/
54 changes: 45 additions & 9 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,49 @@
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/psf/black
rev: 22.3.0
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: black
language_version: python3
exclude: rednotebook/external/.*\.py
- id: trailing-whitespace
- id: check-yaml
- id: check-added-large-files

- repo: https://github.com/asottile/pyupgrade
rev: v2.32.0
# Static analysis for Python code
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: pyupgrade
exclude: rednotebook/external/.*\.py
- id: flake8
# E203: whitespace before ':' (not compliant with PEP 8)
# E402: module level import not at top of file
# F821: undefined name "_"
args: ['--extend-ignore=E203,E402,F821', '--max-line-length=110', '--builtins="_"']
exclude: ^rednotebook/external/

# Python code formatter
- repo: https://github.com/psf/black
rev: 23.3.0
hooks:
- id: black
exclude: ^rednotebook/external/

# Imports sorter
- repo: https://github.com/pycqa/isort
rev: 5.11.5
hooks:
- id: isort
exclude: ^rednotebook/journal.py

# Upgrades Python code to a target version
- repo: https://github.com/asottile/pyupgrade
rev: v3.4.0
hooks:
- id: pyupgrade
args: ['--py36-plus', '--exit-zero']
exclude: ^rednotebook/external/

# Finds unused Python code
- repo: https://github.com/jendrikseipp/vulture
rev: v2.7
hooks:
- id: vulture
exclude: ^rednotebook/external/
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ Start RedNotebook:
* Windows: `py rednotebook/journal.py`


## Set up pre-commit hooks

Install [pre-commit](https://pre-commit.com/), then run `pre-commit install`.


## Run tests

Install [tox](https://tox.wiki), then run `tox`.


## Thanks to

* The authors of the libraries listed under 'Requirements'.
Expand Down
3 changes: 2 additions & 1 deletion debian/source_rednotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

from apport.hookutils import attach_file_if_exists


LOGS = (("DebugLog", "rednotebook.log"),)


def add_info(report):
report["CrashDB"] = "rednotebook"
rednotebook_dir = path.join(os.environ["HOME"], ".rednotebook")
for (key, name) in LOGS:
for key, name in LOGS:
log = path.join(rednotebook_dir, name)
attach_file_if_exists(report, log, key)
5 changes: 2 additions & 3 deletions dev/benchmarks/findall.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import timeit


DIR = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.dirname(os.path.dirname(DIR))

Expand All @@ -16,8 +17,6 @@
for text in TEXTS:
timer = timeit.Timer(
"HASHTAG.findall(text)",
setup='from rednotebook.data import HASHTAG; text = "{text}"'.format(
**locals()
),
setup=f'from rednotebook.data import HASHTAG; text = "{text}"',
)
print(text[:10], timer.timeit(ITERATIONS))
8 changes: 6 additions & 2 deletions dev/build_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import argparse
from pathlib import Path
import subprocess
import sys


REPO = Path(__file__).resolve().parents[1]
Expand All @@ -22,8 +23,10 @@ def build_translation_files(po_dir: Path, locale_dir: Path):
dest_dir = dest.parent
if not dest_dir.exists():
dest_dir.mkdir(parents=True, exist_ok=True)
print(f"Compiling {src} to {dest}")
subprocess.check_call(["msgfmt", "--output-file", dest, src])
try:
subprocess.check_call(["msgfmt", "--output-file", dest, src])
except subprocess.CalledProcessError:
sys.exit(f"Compilation failed for {src}")


def main():
Expand All @@ -33,6 +36,7 @@ def main():
print("Building translations")
print(po_dir, "-->", locale_dir)
build_translation_files(po_dir, locale_dir)
print("Finished building translations")


if __name__ == "__main__":
Expand Down
22 changes: 0 additions & 22 deletions dev/check-style.sh

This file was deleted.

2 changes: 2 additions & 0 deletions dev/generate-help.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pathlib import Path
import sys


DIR = Path(__file__).resolve().parent
REPO = DIR.parent
DEFAULT_DATA_DIR = Path.home() / ".rednotebook" / "data"
Expand All @@ -13,6 +14,7 @@
from rednotebook.info import version
from rednotebook.util import markup


print(
markup.convert(
help_text,
Expand Down
17 changes: 17 additions & 0 deletions dev/whitelist.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import gi


gi.require_version("Gtk", "3.0")


Expand All @@ -21,12 +22,14 @@ def __getattr__(self, _):

from rednotebook.journal import Journal


Journal.do_activate
Journal.do_command_line
Journal.do_startup

from gi.repository import Gtk


cell = Gtk.CellRendererText()
cell.props.wrap_mode

Expand All @@ -39,7 +42,21 @@ def __getattr__(self, _):
# CEF Browser
from ctypes import _CFuncPtr


_CFuncPtr.argtypes
_CFuncPtr.restype
Dummy().OnBeforeBrowse
sys.excepthook

Dummy().commandline_help
Dummy().greeting
Dummy().intro
Dummy().help_par
Dummy().preview_par
Dummy().tags_par
Dummy().temp_par
Dummy().save
Dummy().save_par
Dummy().error_par
Dummy().goodbye_par
Dummy().example_entry
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ lines_after_imports = 2 # -1 puts 2 lines before classes and functions, otherwi
multi_line_output = 3
order_by_type = false
skip= "external,journal.py,main_window.py"

[tool.vulture]
min_confidence = 0
paths = ["rednotebook/", "dev/whitelist.py"]
exclude = ["external/"]
6 changes: 3 additions & 3 deletions rednotebook/files/default.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the default config file for RedNotebook.
# All values listed here (except "portable" and "userDir") will be
# copied to the user's personal config file
# All values listed here (except "portable" and "userDir") will be
# copied to the user's personal config file
# (default: ~/.rednotebook/configuration.cfg)
# when the program is launched (and closed) for the first time.
#
Expand All @@ -17,7 +17,7 @@ portable=0
# Change the value of userDir to move the user's personal files
# (configuration.cfg, templates, etc.) to a different location.
# The path can be absolute or relative to the main application directory
# (i.e. the directory containing the exe on Windows).
# (i.e. the directory containing the exe on Windows).
# If there is no value given here, the default will be used:
# - $HOME/.rednotebook/ (if portable=0)
# - rednotebook-executable/user/ (if portable=1)
Expand Down
2 changes: 1 addition & 1 deletion scripts/rednotebook_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
To merge .txt files into a destination RedNotebook journal:
0. Install script requirements: "pip install PyYAML"
1. Click on "Save" within RedNotebook.
1. Click on "Save" within RedNotebook.
2. Do a backup.
3. Quit RedNotebook.
4. Merge in your files (adjust paths):
Expand Down
10 changes: 5 additions & 5 deletions scripts/text2rednotebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
deactivated.
"""


import argparse
import pathlib
import sys
import re
import datetime
import os
import pathlib
import re
import sys


for inst in ["/usr/share/rednotebook", "../rednotebook"]:
if os.path.isdir(inst):
Expand All @@ -34,7 +34,7 @@

# must import rednotebook after munging path in order to find it, as it is
# not (at least in Debian) installed in the default python path
import rednotebook.storage # pylint: disable=wrong-import-position, unused-import
import rednotebook.storage # pylint: disable=wrong-import-position, unused-import # noqa: F401


def list_missing_entries(mindate, maxdate, months, existing_entries):
Expand Down
17 changes: 2 additions & 15 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
from setuptools.command.build_py import build_py as _build_py
from setuptools.command.install import install as _install


REPO = Path(__file__).resolve().parent
sys.path.insert(0, str(REPO))

from rednotebook import info

from dev import build_translations
from rednotebook import info


TMP_LOCALE_DIR = REPO / "build" / "locale"
Expand Down Expand Up @@ -116,19 +116,6 @@ def run(self):
),
("share/metainfo", ["data/rednotebook.appdata.xml"]),
],
"extras_require": {
"dev_style": [
"black==22.3.0",
"flake8==4.0.1",
"flake8-2020==1.6.0",
"flake8-bugbear==21.11.28",
"flake8-comprehensions==3.7.0",
"flake8-executable==2.1.1",
"isort==5.10.1",
"pyupgrade==2.32.0",
"vulture==1.6",
],
},
}

if __name__ == "__main__":
Expand Down
20 changes: 3 additions & 17 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,10 @@ commands =
python -m pytest
python dev/whitelist.py
python win/sampleapp.py
python dev/build_translations.py test-translations
sitepackages = true

[testenv:style]
skipsdist = true
extras = dev_style
deps = pre-commit
commands =
bash dev/check-style.sh
allowlist_externals =
bash
sitepackages = false

[testenv:fix-style]
skipsdist = true
extras = dev_style
commands =
black .
isort rednotebook/ tests/
bash -c 'pyupgrade --py36-plus --exit-zero `find rednotebook tests -name "*.py" -not -path "*external*"`'
allowlist_externals =
bash
sitepackages = false
pre-commit run --all-files --show-diff-on-failure
2 changes: 1 addition & 1 deletion web/fonts/aller_rg-webfont.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions web/src/changelog2html.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys


MAX_RELEASES = 10
CHANGELOG = sys.argv[1]
OUTFILE = "news.txt"
Expand Down
3 changes: 2 additions & 1 deletion web/src/spider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os


spider_dir = os.path.abspath(os.path.dirname(__file__))

src = spider_dir
Expand Down Expand Up @@ -58,7 +59,7 @@
filename = page["filename"]
html = template
with open(filename) as file:
html = html.replace(f"***CONTENT***", file.read())
html = html.replace("***CONTENT***", file.read())
for key in set(page.keys()) | {"scripts"}:
html = html.replace(f"***{key.upper()}***", page.get(key, ""))
with open(os.path.join(dest, filename), "w") as f:
Expand Down
1 change: 1 addition & 0 deletions win/fetch-dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import utils


DIR = os.path.dirname(os.path.abspath(__file__))
DICT_DIR = os.path.join(DIR, "dicts")

Expand Down
Loading

0 comments on commit cfdfd49

Please sign in to comment.