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

Make this work with pre-commit #49

Open
lagerfeuer opened this issue Dec 20, 2023 · 6 comments · May be fixed by #52
Open

Make this work with pre-commit #49

lagerfeuer opened this issue Dec 20, 2023 · 6 comments · May be fixed by #52

Comments

@lagerfeuer
Copy link

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
Copy link

Roald87 commented Feb 16, 2024

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 added a commit that referenced this issue Feb 17, 2024
@Findus23
Copy link
Owner

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:

def get_input_text(config):
"""
Return text from stdin, clipboard or file.
Returns:
Tuple[str, str]:
A tuple contain of the text and an optional file extension.
If the text does not come from a file, the extension part of the
tuple will be none.
"""
if not sys.stdin.isatty(): # if piped into script
lines = [line.rstrip() for line in sys.stdin.readlines() if line.rstrip()]
return "\n".join(lines), None # read text from pipe and remove empty lines
elif config["clipboard"]:
return get_clipboard(), None
else:
if config["input file"]:
extension = os.path.splitext(config["input file"])[1][1:] # get file extention without .
try:
with open(config["input file"], 'r') as myfile:
return myfile.read(), extension
except UnicodeDecodeError:
print("can't read text")
sys.exit(1)
return None, None

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 63ac997 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
Copy link

Roald87 commented Feb 18, 2024

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
Copy link

Roald87 commented Apr 22, 2024

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
Copy link

Roald87 commented Apr 30, 2024

My fork now also works for multiple files

@Roald87
Copy link

Roald87 commented May 3, 2024

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: <urllib3.connection.HTTPSConnection object at 0x72969e1f04d0>: 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("<urllib3.connection.HTTPSConnection object at 0x72969e1f04d0>: 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 <module>
    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("<urllib3.connection.HTTPSConnection object at 0x72969e1f04d0>: 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: <urllib3.connection.HTTPSConnection object at 0x7841fea0ce30>: 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("<urllib3.connection.HTTPSConnection object at 0x7841fea0ce30>: 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 <module>
    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("<urllib3.connection.HTTPSConnection object at 0x7841fea0ce30>: 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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants