Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pre-commit #396

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[flake8]
max-line-length = 80
extend-select = B950
extend-ignore = E203,E501,E701
6 changes: 3 additions & 3 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defaults:
concurrency:
group: ci-tests-${{ github.ref }}-1
cancel-in-progress: true

jobs:
test:
strategy:
Expand Down Expand Up @@ -40,7 +40,7 @@ jobs:
id: cached-poetry-dependencies
with:
path: .venv
key: venv-${{ runner.os }}-${{ runner.arch }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }}
key: venv-${{ runner.os }}-${{ runner.arch }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
- name: Install dependencies
run: poetry install
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
Expand Down Expand Up @@ -95,7 +95,7 @@ jobs:
# - pandoc does not publish binaries for Linux 32bit
CIBW_ARCHS_LINUX: "auto64 aarch64"
CIBW_ARCHS_MACOS: "x86_64 arm64"
CIBW_ARCHS_WINDOWS: "AMD64"
CIBW_ARCHS_WINDOWS: "AMD64"
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
Expand Down
53 changes: 53 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
ci:
autoupdate_schedule: "quarterly"

default_language_version:
python: "python3.13"

repos:
- repo: "meta"
hooks:
- id: "check-hooks-apply"
- id: "check-useless-excludes"

- repo: "https://github.com/pre-commit/pre-commit-hooks"
rev: "v5.0.0"
hooks:
- id: "check-added-large-files"
- id: "check-merge-conflict"
- id: "check-yaml"
- id: "end-of-file-fixer"
- id: "mixed-line-ending"
args:
- "--fix=lf"
- id: "trailing-whitespace"

- repo: "https://github.com/asottile/pyupgrade"
rev: "v3.19.1"
hooks:
- id: "pyupgrade"
name: "Enforce Python 3.7+ idioms"
args:
- "--py37-plus"

- repo: "https://github.com/psf/black-pre-commit-mirror"
rev: "25.1.0"
hooks:
- id: "black"

- repo: "https://github.com/pycqa/isort"
rev: "6.0.0"
hooks:
- id: "isort"

- repo: "https://github.com/pycqa/flake8"
rev: "7.1.1"
hooks:
- id: "flake8"
additional_dependencies:
- "flake8-bugbear==24.12.12"

- repo: "https://github.com/rhysd/actionlint"
rev: "v1.7.7"
hooks:
- id: "actionlint"
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ RUN apt update && apt upgrade -y && apt install pandoc -y
# Update pip
RUN pip install --upgrade pip

# Copy the files to container
# Copy the files to container
COPY . pypandoc
WORKDIR pypandoc

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ it won't work. This gotcha has to do with the way
## Logging Messages

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

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

## License

Expand Down
39 changes: 19 additions & 20 deletions examples/services.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# -*- coding: utf-8 -*-
"""
Example Services for using pypandoc
"""

from tempfile import NamedTemporaryFile

import pypandoc


class BasePandocService(object):
class BasePandocService:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was made by pyupgrade -- in Python 3, all classes inherit from object, so when upgrading to Python 3.7+ syntax and idioms, pyupgrade knows that it isn't necessary to inherit from object.

"""
Base class for converting provided HTML to a doc or docx
"""

file_object = None

def __init__(self):
Expand All @@ -27,23 +28,22 @@ class PandocPDFService(BasePandocService):
"""
Generate html to pdf format
"""

def generate(self, html, **kwargs):
"""
generate the pdf but needs to be set as tex so pandoc handles it
correctly see docs: http://johnmacfarlane.net/pandoc/ #search pdf
"""
from_format = kwargs.get('from_format', 'html')
to_format = kwargs.get('to_format', 'tex')
from_format = kwargs.get("from_format", "html")
to_format = kwargs.get("to_format", "tex")
# create temp file
self.file_object = NamedTemporaryFile(suffix='.pdf')
self.file_object = NamedTemporaryFile(suffix=".pdf")
Comment on lines -38 to +40
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was made by black -- it auto-corrects single quotes to double quotes.


extra_args = (
'--smart',
'--standalone',
'-o', self.file_object.name
)
extra_args = ("--smart", "--standalone", "-o", self.file_object.name)
Comment on lines -40 to +42
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change was made by black -- it standardizes tuples and lists to be on a single line if they'll fit, with one exception.

If you put a trailing comma after self.file_object.name, then black will assume you want each item on its own line, and will reformat all of the items so they're each on their own line.

# generate it using pandoc
self.service.convert_text(html, to_format, format=from_format, extra_args=extra_args)
self.service.convert_text(
html, to_format, format=from_format, extra_args=extra_args
)
# return the file which is now populated with the docx forms
return self.file_object

Expand All @@ -52,18 +52,17 @@ class PandocDocxService(BasePandocService):
"""
Generate html to docx format
"""

def generate(self, html, **kwargs):
from_format = kwargs.get('from_format', 'html')
to_format = kwargs.get('to_format', 'docx')
from_format = kwargs.get("from_format", "html")
to_format = kwargs.get("to_format", "docx")
# create temp file
self.file_object = NamedTemporaryFile(suffix='.docx')
self.file_object = NamedTemporaryFile(suffix=".docx")

extra_args = (
'--smart',
'--standalone',
'-o', self.file_object.name
)
extra_args = ("--smart", "--standalone", "-o", self.file_object.name)
# generate it using pandoc
self.service.convert_text(html, to_format, format=from_format, extra_args=extra_args)
self.service.convert_text(
html, to_format, format=from_format, extra_args=extra_args
)
# return the file which is now populated with the docx forms
return self.file_object
Loading