Skip to content

Commit

Permalink
Set up pre-commit (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
arkid15r authored Jun 6, 2024
1 parent 7c237bb commit f8f1057
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 365 deletions.
26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: check-ast
- id: check-builtin-literals
- id: check-yaml
- id: end-of-file-fixer
- id: fix-encoding-pragma
args:
- --remove
- id: mixed-line-ending
args:
- --fix=lf
- id: trailing-whitespace

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.4.7
hooks:
- id: ruff
- id: ruff-format

- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
4 changes: 2 additions & 2 deletions changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Changelog

5.0
------------------------
- Fork lingua as lingva.
- Fork lingua as lingva.
- Bump version to 5.0.

4.??
Expand All @@ -16,7 +16,7 @@ Changelog
4.15 - February 12, 2022
------------------------

- Fix file not found error with ``--list-files`` due to a ``\n`` appended
- Fix file not found error with ``--list-files`` due to a ``\n`` appended
to each filename.
`Patch #97 <https://github.com/wichert/lingua/pull/102>`_ from Johannes Raggam.

Expand Down
16 changes: 16 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,19 @@ norecursedirs = ".git tmp* .eggs bin build include lib share src"

[tool.setuptools.dynamic]
version = { attr = "lingva.__version__" }

[tool.isort]
known_first_party = ["lingva", "tests"]
line_length = 99
multi_line_output = 3
profile = "black"

[tool.ruff]
line-length = 99
target-version = "py311"

[tool.ruff.lint]
select = ["E4", "E7", "E9", "F"]

[tool.ruff.lint.flake8-errmsg]
max-string-length = 99
35 changes: 13 additions & 22 deletions src/lingva/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@
from collections import OrderedDict
except ImportError:
from ordereddict import OrderedDict
from operator import attrgetter

import os
import re
import sys
import tempfile
import time

from configparser import ConfigParser as SafeConfigParser
from operator import attrgetter

import click
import polib
from lingva.extractors import get_extractor
from lingva.extractors import register_extractors
from lingva.extractors.babel import register_babel_plugins
from lingva.extractors import EXTRACTORS
from lingva.extractors import EXTENSIONS

from lingva import __version__
from lingva.extractors import EXTENSIONS, EXTRACTORS, get_extractor, register_extractors
from lingva.extractors.babel import register_babel_plugins


def po_timestamp():
Expand Down Expand Up @@ -116,7 +115,7 @@ def list_files(files_from, sources):
if os.path.isfile(file):
yield file
elif os.path.isdir(file):
for (dirpath, dirnames, filenames) in os.walk(file):
for dirpath, dirnames, filenames in os.walk(file):
for file in filenames:
if get_extractor(file) is not None:
yield os.path.join(dirpath, file)
Expand Down Expand Up @@ -146,9 +145,7 @@ def strip_linenumbers(entry):
entry.occurrences = occurrences


def create_catalog(
width, copyright_holder, package_name, package_version, msgid_bugs_address
):
def create_catalog(width, copyright_holder, package_name, package_version, msgid_bugs_address):
catalog = POFile(wrapwidth=width)
catalog.copyright_holder = copyright_holder
catalog.package_name = package_name
Expand All @@ -174,8 +171,7 @@ def create_catalog(
def _register_extension(extension, extractor):
if extractor not in EXTRACTORS:
click.echo(
"Unknown extractor %s. Check --list-extractors for available options"
% extractor,
"Unknown extractor %s. Check --list-extractors for available options" % extractor,
err=True,
)
sys.exit(1)
Expand All @@ -187,7 +183,7 @@ def read_config(cfg_file):
config.read_file(cfg_file)
for section in config.sections():
if section == "extensions":
for (extension, extractor) in config.items(section):
for extension, extractor in config.items(section):
_register_extension(extension, extractor)
elif section.startswith("extractor:"):
extractor = section[10:]
Expand All @@ -202,8 +198,7 @@ def read_config(cfg_file):
EXTRACTORS[extractor].update_config(**extractor_config)
elif section.startswith("extension"):
click.echo(
"Use of %s section is obsolete. "
'Please use the "extensions" section.' % section,
"Use of %s section is obsolete. " 'Please use the "extensions" section.' % section,
err=True,
)
extension = section[10:]
Expand Down Expand Up @@ -286,9 +281,7 @@ def __init__(self, comment_tag, domain, keywords):
help="Add DIRECTORY to list of paths to check for input files",
)
@click.argument("sources", nargs=-1, type=click.Path(exists=True))
@click.option(
"--list-extractors", is_flag=True, help="List all known extraction plugins"
)
@click.option("--list-extractors", is_flag=True, help="List all known extraction plugins")
# Output options
@click.option(
"-o",
Expand Down Expand Up @@ -359,9 +352,7 @@ def __init__(self, comment_tag, domain, keywords):
default="1.0",
help="Package version to use in the generated POT file",
)
@click.option(
"--msgid-bugs-address", metavar="EMAIL", help="Email address bugs should be send to"
)
@click.option("--msgid-bugs-address", metavar="EMAIL", help="Email address bugs should be send to")
def main(
cfg_file,
files_from,
Expand Down
5 changes: 2 additions & 3 deletions src/lingva/extractors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from __future__ import print_function

from importlib.metadata import entry_points

import abc
import collections
import os
import re
import sys
from .compat import add_metaclass
from importlib.metadata import entry_points

from .compat import add_metaclass

Message = collections.namedtuple(
"Message", "msgctxt msgid msgid_plural flags comment tcomment location"
Expand Down
16 changes: 4 additions & 12 deletions src/lingva/extractors/babel.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
from importlib.metadata import entry_points

from .python import KEYWORDS
from .python import parse_keyword
from . import EXTRACTORS
from . import Message
from . import check_c_format
from . import check_python_format
from . import Extractor
from . import update_keywords
from . import EXTRACTORS, Extractor, Message, check_c_format, check_python_format, update_keywords
from .python import KEYWORDS, parse_keyword


class BabelExtractor(Extractor):
Expand All @@ -23,10 +17,8 @@ def __call__(self, filename, options, fileobj=None, firstline=0):
if fileobj is None:
fileobj = open(filename, "rb")
comment_tags = self.config["comment-tags"].split()
messages = self.extractor(
fileobj, list(self.keywords.keys()), comment_tags, self.config
)
for (lineno, function, args, comment) in messages:
messages = self.extractor(fileobj, list(self.keywords.keys()), comment_tags, self.config)
for lineno, function, args, comment in messages:
if not isinstance(args, (list, tuple)):
args = [args]
if function in self.keywords:
Expand Down
31 changes: 15 additions & 16 deletions src/lingva/extractors/python.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
from __future__ import print_function

import ast
import io
import sys
import tokenize
import warnings
from . import Extractor
from . import Message
from . import check_comment_flags
from . import check_c_format
from . import check_python_format
from . import Keyword
from . import update_keywords

from . import (
Extractor,
Keyword,
Message,
check_c_format,
check_comment_flags,
check_python_format,
update_keywords,
)

try:
basestring
Expand Down Expand Up @@ -84,7 +87,7 @@ def parse_translationstring(arguments, filename, firstline):
msgid = args[0]
if len(args) > 2 and isinstance(args[2], basestring):
default = args[2]
for (key, value) in kwargs.items():
for key, value in kwargs.items():
if not isinstance(value, basestring):
continue
if key == "msgid":
Expand Down Expand Up @@ -184,7 +187,7 @@ def __call__(self, token_stream, options, filename, firstline):
self.messages = []
self.handler = self.state_skip
try:
for (token_type, token, location, _) in token_stream:
for token_type, token, location, _ in token_stream:
self.process_token(token_type, token, location, token_stream)
except tokenize.TokenError as e:
print(
Expand Down Expand Up @@ -223,9 +226,7 @@ def process_comment(self, token, location):
if self.include_comments == "tagged":
comment = comment[len(self.comment_marker) :].strip()
(flags, comment) = check_comment_flags(comment)
if self.messages and self.messages[-1].location[1] == (
self.firstline + location[0]
):
if self.messages and self.messages[-1].location[1] == (self.firstline + location[0]):
last_message = self.messages[-1]
# Comment at the end of the line of a keyword call
new_comment = []
Expand Down Expand Up @@ -341,7 +342,7 @@ def add_argument(self, value, lineno):

def skip_iterable(self, start_token, end_token, token_stream):
depth = 1
for (token_type, token, loc, _) in token_stream:
for token_type, token, loc, _ in token_stream:
if token_type == tokenize.OP:
if token == start_token:
depth += 1
Expand All @@ -356,9 +357,7 @@ def skip_iterable(self, start_token, end_token, token_stream):

def process_keyword(self):
if self.keyword is not None:
msg = parse_keyword(
self.arguments, self.keyword, self.filename, self.firstline
)
msg = parse_keyword(self.arguments, self.keyword, self.filename, self.firstline)
else:
msg = parse_translationstring(self.arguments, self.filename, self.firstline)
if msg is None:
Expand Down
Loading

0 comments on commit f8f1057

Please sign in to comment.