Skip to content

Commit f92dbb2

Browse files
authored
Use pre-commit (#396)
* Add a basic pre-commit file * Run `pre-commit run -a` * Add pyupgrade as a pre-commit hook * Run `pre-commit run -a` * Add black as a pre-commit hook * Run `pre-commit run -a` * Add isort as a pre-commit hook * Run `pre-commit run -a` * Add flake8 as a pre-commit hook * Manually fix all flake8 errors * Add actionlint as a pre-commit hook * Manually fix errors reported by actionlint * Add Kurt McKee to the list of contributors
1 parent ed97464 commit f92dbb2

File tree

16 files changed

+976
-643
lines changed

16 files changed

+976
-643
lines changed

.flake8

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[flake8]
2+
max-line-length = 80
3+
extend-select = B950
4+
extend-ignore = E203,E501,E701

.github/workflows/ci.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ defaults:
1010
concurrency:
1111
group: ci-tests-${{ github.ref }}-1
1212
cancel-in-progress: true
13-
13+
1414
jobs:
1515
test:
1616
strategy:
@@ -40,7 +40,7 @@ jobs:
4040
id: cached-poetry-dependencies
4141
with:
4242
path: .venv
43-
key: venv-${{ runner.os }}-${{ runner.arch }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
43+
key: venv-${{ runner.os }}-${{ runner.arch }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
4444
- name: Install dependencies
4545
run: poetry install
4646
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
@@ -95,7 +95,7 @@ jobs:
9595
# - pandoc does not publish binaries for Linux 32bit
9696
CIBW_ARCHS_LINUX: "auto64 aarch64"
9797
CIBW_ARCHS_MACOS: "x86_64 arm64"
98-
CIBW_ARCHS_WINDOWS: "AMD64"
98+
CIBW_ARCHS_WINDOWS: "AMD64"
9999
- name: Upload artifacts
100100
uses: actions/upload-artifact@v4
101101
with:

.pre-commit-config.yaml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
ci:
2+
autoupdate_schedule: "quarterly"
3+
4+
default_language_version:
5+
python: "python3.13"
6+
7+
repos:
8+
- repo: "meta"
9+
hooks:
10+
- id: "check-hooks-apply"
11+
- id: "check-useless-excludes"
12+
13+
- repo: "https://github.com/pre-commit/pre-commit-hooks"
14+
rev: "v5.0.0"
15+
hooks:
16+
- id: "check-added-large-files"
17+
- id: "check-merge-conflict"
18+
- id: "check-yaml"
19+
- id: "end-of-file-fixer"
20+
- id: "mixed-line-ending"
21+
args:
22+
- "--fix=lf"
23+
- id: "trailing-whitespace"
24+
25+
- repo: "https://github.com/asottile/pyupgrade"
26+
rev: "v3.19.1"
27+
hooks:
28+
- id: "pyupgrade"
29+
name: "Enforce Python 3.7+ idioms"
30+
args:
31+
- "--py37-plus"
32+
33+
- repo: "https://github.com/psf/black-pre-commit-mirror"
34+
rev: "25.1.0"
35+
hooks:
36+
- id: "black"
37+
38+
- repo: "https://github.com/pycqa/isort"
39+
rev: "6.0.0"
40+
hooks:
41+
- id: "isort"
42+
43+
- repo: "https://github.com/pycqa/flake8"
44+
rev: "7.1.1"
45+
hooks:
46+
- id: "flake8"
47+
additional_dependencies:
48+
- "flake8-bugbear==24.12.12"
49+
50+
- repo: "https://github.com/rhysd/actionlint"
51+
rev: "v1.7.7"
52+
hooks:
53+
- id: "actionlint"

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ RUN apt update && apt upgrade -y && apt install pandoc -y
1212
# Update pip
1313
RUN pip install --upgrade pip
1414

15-
# Copy the files to container
15+
# Copy the files to container
1616
COPY . pypandoc
1717
WORKDIR pypandoc
1818

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ it won't work. This gotcha has to do with the way
237237
## Logging Messages
238238

239239
Pypandoc logs messages using the [Python logging library](https://docs.python.org/3/library/logging.html).
240-
By default, it will send messages to the console, including any messages
240+
By default, it will send messages to the console, including any messages
241241
generated by Pandoc. If desired, this behaviour can be changed by adding
242-
[handlers](https://docs.python.org/3/library/logging.html#handler-objects) to
243-
the pypandoc logger **before calling any functions**. For example, to mute all
242+
[handlers](https://docs.python.org/3/library/logging.html#handler-objects) to
243+
the pypandoc logger **before calling any functions**. For example, to mute all
244244
logging add a [null handler](https://docs.python.org/3/library/logging.handlers.html#nullhandler):
245245

246246
```python
@@ -313,6 +313,7 @@ Note that for citeproc tests to pass you'll need to have [pandoc-citeproc](https
313313
* [Juho Vepsäläinen](https://github.com/bebraw/) - Creator and former maintainer of pypandoc
314314
* [Connor](https://github.com/DisSupEng/) - Updated Dockerfile to Python 3.9 image and added docker compose file
315315
* [Colin Bull](https://github.com/colinbull) - Added ability to control whether files are sorted before being passed to pandoc process.
316+
* [Kurt McKee](https://github.com/kurtmckee) - Project infrastructure improvements
316317

317318
## License
318319

examples/services.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1-
# -*- coding: utf-8 -*-
21
"""
32
Example Services for using pypandoc
43
"""
4+
55
from tempfile import NamedTemporaryFile
66

77
import pypandoc
88

99

10-
class BasePandocService(object):
10+
class BasePandocService:
1111
"""
1212
Base class for converting provided HTML to a doc or docx
1313
"""
14+
1415
file_object = None
1516

1617
def __init__(self):
@@ -27,23 +28,22 @@ class PandocPDFService(BasePandocService):
2728
"""
2829
Generate html to pdf format
2930
"""
31+
3032
def generate(self, html, **kwargs):
3133
"""
3234
generate the pdf but needs to be set as tex so pandoc handles it
3335
correctly see docs: http://johnmacfarlane.net/pandoc/ #search pdf
3436
"""
35-
from_format = kwargs.get('from_format', 'html')
36-
to_format = kwargs.get('to_format', 'tex')
37+
from_format = kwargs.get("from_format", "html")
38+
to_format = kwargs.get("to_format", "tex")
3739
# create temp file
38-
self.file_object = NamedTemporaryFile(suffix='.pdf')
40+
self.file_object = NamedTemporaryFile(suffix=".pdf")
3941

40-
extra_args = (
41-
'--smart',
42-
'--standalone',
43-
'-o', self.file_object.name
44-
)
42+
extra_args = ("--smart", "--standalone", "-o", self.file_object.name)
4543
# generate it using pandoc
46-
self.service.convert_text(html, to_format, format=from_format, extra_args=extra_args)
44+
self.service.convert_text(
45+
html, to_format, format=from_format, extra_args=extra_args
46+
)
4747
# return the file which is now populated with the docx forms
4848
return self.file_object
4949

@@ -52,18 +52,17 @@ class PandocDocxService(BasePandocService):
5252
"""
5353
Generate html to docx format
5454
"""
55+
5556
def generate(self, html, **kwargs):
56-
from_format = kwargs.get('from_format', 'html')
57-
to_format = kwargs.get('to_format', 'docx')
57+
from_format = kwargs.get("from_format", "html")
58+
to_format = kwargs.get("to_format", "docx")
5859
# create temp file
59-
self.file_object = NamedTemporaryFile(suffix='.docx')
60+
self.file_object = NamedTemporaryFile(suffix=".docx")
6061

61-
extra_args = (
62-
'--smart',
63-
'--standalone',
64-
'-o', self.file_object.name
65-
)
62+
extra_args = ("--smart", "--standalone", "-o", self.file_object.name)
6663
# generate it using pandoc
67-
self.service.convert_text(html, to_format, format=from_format, extra_args=extra_args)
64+
self.service.convert_text(
65+
html, to_format, format=from_format, extra_args=extra_args
66+
)
6867
# return the file which is now populated with the docx forms
6968
return self.file_object

0 commit comments

Comments
 (0)