Findus23 / pyLanguagetool

Python Library and CLI for the LanguageTool JSON API
https://pylanguagetool.lw1.at/
MIT License
127 stars 10 forks source link

Make this work with pre-commit #49

Open lagerfeuer opened 10 months ago

lagerfeuer commented 10 months ago

It would be awesome to be able to add this tool to pre-commit hooks. Any plans on adding this?

See https://pre-commit.com/#new-hooks.

Roald87 commented 8 months ago

I thought this would be easy, but I can't make it work.

I added a .pre-commit-hooks.yaml to my fork of this repo and filled it with:

- id: pylanguagetool
  name: Spellcheck with Languagetool
  description: Checks the spelling
  entry: pylanguagetool 
  language: python

Then I ran the pre-commits with $pre-commit try-repo ../pyLanguagetool pylanguagetool --verbose --files path/to/some/markdown/file

I get the following error

Spellcheck with Languagetool.............................................Failed
- hook id: pylanguagetool
- duration: 0.25s
- exit code: 2

usage: pylanguagetool
       [-h]
       [-V]
       [-v]
       [-a API_URL]
       [--no-color]
       [-c]
       [-s]
       [-t {txt,html,md,markdown,rst,ipynb,json,xliff}]
       [-u]
       [-r]
       [--rule-categories]
       [-l LANG]
       [-m MOTHER_TONGUE]
       [-p PREFERRED_VARIANTS]
       [-e ENABLED_RULES]
       [-d DISABLED_RULES]
       [--enabled-categories ENABLED_CATEGORIES]
       [--disabled-categories DISABLED_CATEGORIES]
       [--enabled-only]
       [--picky]
       [--pwl PWL]
       [input file]
input file is required

Even if I try to pass a file directly by adjusting entry: pylanguagetool -- path/to/some/markdown/file in pre-commit-hooks.yaml, I get still get an error.

pylanguagetool: error: unrecognized arguments: path/to/some/markdown/file

Guess its not so easy :/

Findus23 commented 8 months ago

Okay, that really confused me, but I found the reason: Printing sys.argv shows that pre-commit properly calls pylanguagetool and the issue is therefore unrelated to it: ['/tmp/tmpj_q068oy/repo1uikxb3t/py_env-python3.11/bin/pylanguagetool', 'main.txt']

It turns out 2017 me didn't think this code block through: https://github.com/Findus23/pyLanguagetool/blob/b59d3f46ed937f6c619bcc082d4ecb0afa63d8b0/pylanguagetool/cli.py#L101-L126

The if not sys.stdin.isatty() section is so that echo "this is an test" | pylanguagetool also works without an input file by instead reading stdin. But the way pre-commit calls pylanguagetool also triggers this.

So with https://github.com/Findus23/pyLanguagetool/commit/63ac99735323f8c27bb9d36b971be1f5a5ba0581 pylanguagetool will now only read from stdin if no input file has been specified.

With this, the rest should be possible now. Please report back if there are issues and if not, I can release a new version.

BTW, in case you are planning to use languagetool in an automated way, I would strongly recommend pointing it to your own languagetool server and not hitting the main https://languagetool.org/ one with requests.

Roald87 commented 8 months ago

I made it work by cloning the latest version of the repo and adding the following .pre-commit-hooks.yaml file to the pylanguagetool repo

- id: pylanguagetool
  name: Spellcheck with Languagetool
  description: Checks the spelling
  entry: pylanguagetool
  additional_dependencies:
    - markdown2
    - beautifulsoup4
  language: python

then I ran it with

pre-commit try-repo ../pyLanguagetool pylanguagetool --verbose --files some/file.md

That worked.

What didn't work was if I add this to my pre-commit-config.yaml in the repo where I want to add Languagetool to.

  - repo: local
    hooks:
      - id: pylanguagetool
        files: '.*\.(md|markdown)'
        name: Languagetool spellcheck
        entry: pylanguagetool --input-type md -v
        language: python
        additional_dependencies: [pylanguagetool, beautifulsoup4, markdown2]

But I guess this doesn't work because this doesn't take the latest commit, but the latest release instead?

Roald87 commented 6 months ago

The pre-commit works with my fork https://github.com/Roald87/pyLanguagetool

I added the following to my .pre-commit-config.yaml in another repo

  - repo: https://github.com/Roald87/pyLanguagetool
    rev: 2cd8080
    hooks:
      - id: pylanguagetool
        files: '.*\.(md|markdown)'
        name: Languagetool spellcheck
        entry: pylanguagetool --input-type markdown

It works for a single file, but not if I try to run it on multiple files. I then get the error pylanguagetool: error: unrecognized arguments: folder/second-file.md. The first file is not mentioned in the error message, only from the second file onwards.

Roald87 commented 6 months ago

My fork now also works for multiple files

Roald87 commented 6 months ago

Note that this pre-commit doesn't work with pre-commit.ci, because it doesn't allow network access.

When you run it you get the error:

pre-commit.ci log

``` Languagetool spellcheck..................................................Failed - hook id: pylanguagetool - exit code: 1 Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 198, in _new_conn sock = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/util/connection.py", line 60, in create_connection for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/socket.py", line 963, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ socket.gaierror: [Errno -3] Temporary failure in name resolution The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 793, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 491, in _make_request raise new_e File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 467, in _make_request self._validate_conn(conn) File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 1099, in _validate_conn conn.connect() File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 616, in connect self.sock = sock = self._new_conn() ^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 205, in _new_conn raise NameResolutionError(self.host, self, e) from e urllib3.exceptions.NameResolutionError: : Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/adapters.py", line 486, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 847, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/util/retry.py", line 515, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='languagetool.org', port=443): Max retries exceeded with url: /api/v2/check (Caused by NameResolutionError(": Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution)")) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/bin/pylanguagetool", line 8, in sys.exit(main()) ^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/pylanguagetool/cli.py", line 260, in main response = api.check(check_text, **config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/pylanguagetool/api.py", line 190, in check r = requests.post(api_url + "check", data=post_parameters) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/sessions.py", line 703, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/adapters.py", line 519, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='languagetool.org', port=443): Max retries exceeded with url: /api/v2/check (Caused by NameResolutionError(": Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution)")) Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 198, in _new_conn sock = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/util/connection.py", line 60, in create_connection for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/lib/python3.12/socket.py", line 963, in getaddrinfo for res in _socket.getaddrinfo(host, port, family, type, proto, flags): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ socket.gaierror: [Errno -3] Temporary failure in name resolution The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 793, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 491, in _make_request raise new_e File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 467, in _make_request self._validate_conn(conn) File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 1099, in _validate_conn conn.connect() File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 616, in connect self.sock = sock = self._new_conn() ^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connection.py", line 205, in _new_conn raise NameResolutionError(self.host, self, e) from e urllib3.exceptions.NameResolutionError: : Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/adapters.py", line 486, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/connectionpool.py", line 847, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/urllib3/util/retry.py", line 515, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='languagetool.org', port=443): Max retries exceeded with url: /api/v2/check (Caused by NameResolutionError(": Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution)")) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/bin/pylanguagetool", line 8, in sys.exit(main()) ^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/pylanguagetool/cli.py", line 260, in main response = api.check(check_text, **config) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/pylanguagetool/api.py", line 190, in check r = requests.post(api_url + "check", data=post_parameters) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/sessions.py", line 703, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/pc/clone/zIzF0Ki2RpW1qZnxNoEZrQ/py_env-python3/lib/python3.12/site-packages/requests/adapters.py", line 519, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPSConnectionPool(host='languagetool.org', port=443): Max retries exceeded with url: /api/v2/check (Caused by NameResolutionError(": Failed to resolve 'languagetool.org' ([Errno -3] Temporary failure in name resolution)")) ```

To fix this add this to your .pre-commit-config.yaml . Then this hook will not run on pre-commit.ci.

ci:
  skip: [pylanguagetool]