Skip to content

Commit

Permalink
Use ruff (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor authored Jul 13, 2024
1 parent b52428c commit a59a1e6
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 342 deletions.
3 changes: 0 additions & 3 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ updates:
dev-dependencies:
patterns:
- "pytest*"
- "flake8*"
- "black"
- "isort"
- "ruff"
- "refurb"

Expand Down
10 changes: 3 additions & 7 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
Expand All @@ -42,12 +42,8 @@ jobs:
- name: Install dependencies
run: poetry install

- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
poetry run flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
poetry run flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Lint with ruff
run: poetry run ruff check . --statistics

- name: Test with pytest
run: poetry run python -m pytest --cov=.
Expand Down
7 changes: 2 additions & 5 deletions alternative_encodings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from . import cp859
from . import cp866i
from . import romaji
from . import viscii
from . import cp859, cp866i, romaji, viscii


def register_all():
def register_all() -> None:
cp859.register()
cp866i.register()
romaji.register()
Expand Down
30 changes: 17 additions & 13 deletions alternative_encodings/common.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,45 @@
import codecs
from typing import TYPE_CHECKING, Union

if TYPE_CHECKING:
from _codecs import _EncodingMap

def get_codec(encoding_table, decoding_table):

def get_codec(encoding_table: Union[dict[int, int], "_EncodingMap"], decoding_table: str) -> type[codecs.Codec]:
class Codec(codecs.Codec):
def encode(self, input, errors="strict"):
return codecs.charmap_encode(input, errors, encoding_table)
def encode(self, input_string: str, errors: str = "strict") -> tuple[bytes, int]:
return codecs.charmap_encode(input_string, errors, encoding_table)

def decode(self, input, errors="strict"):
return codecs.charmap_decode(input, errors, decoding_table)
def decode(self, input_bytes: bytes, errors: str = "strict") -> tuple[str, int]:
return codecs.charmap_decode(input_bytes, errors, decoding_table)

return Codec


def get_incremental_encoder(encoding_table):
def get_incremental_encoder(encoding_table: Union[dict[int, int], "_EncodingMap"]) -> type[codecs.IncrementalEncoder]:
class IncrementalEncoder(codecs.IncrementalEncoder):
def encode(self, input, final=False):
return codecs.charmap_encode(input, self.errors, encoding_table)[0]
def encode(self, input_string: str, _final: bool = False) -> bytes: # noqa: FBT001, FBT002
return codecs.charmap_encode(input_string, self.errors, encoding_table)[0]

return IncrementalEncoder


def get_incremental_decoder(decoding_table):
def get_incremental_decoder(decoding_table: str) -> type[codecs.IncrementalDecoder]:
class IncrementalDecoder(codecs.IncrementalDecoder):
def decode(self, input, final=False):
return codecs.charmap_decode(input, self.errors, decoding_table)[0]
def decode(self, input_bytes: bytes, _final: bool = False) -> str: # noqa: FBT001, FBT002
return codecs.charmap_decode(input_bytes, self.errors, decoding_table)[0]

return IncrementalDecoder


def get_stream_writer(codec):
def get_stream_writer(codec: type[codecs.Codec]) -> type[codecs.Codec, codecs.StreamWriter]:
class StreamWriter(codec, codecs.StreamWriter):
pass

return StreamWriter


def get_stream_reader(codec):
def get_stream_reader(codec: type[codecs.Codec]) -> type[codecs.Codec, codecs.StreamReader]:
class StreamReader(codec, codecs.StreamReader):
pass

Expand Down
23 changes: 14 additions & 9 deletions alternative_encodings/cp859.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import codecs

from .common import get_codec, get_incremental_decoder, get_incremental_encoder, get_stream_reader, get_stream_writer
import contextlib

from .common import (
get_codec,
get_incremental_decoder,
get_incremental_encoder,
get_stream_reader,
get_stream_writer,
)

# Decoding Table

Expand All @@ -14,7 +21,7 @@
"`abcdefghijklmno"
"pqrstuvwxyz{|}~\x7f"
"ÇüéâäàåçêëèïîìÄÅ"
"ÉæÆôöòûùÿÖÜø£Ø׃"
"ÉæÆôöòûùÿÖÜø£Ø׃" # noqa: RUF001
"áíóúñѪº¿®¬œŒ¡«»"
"░▒▓│┤ÁÂÀ©╣║╗╝¢¥┐"
"└┴┬├─┼ãÃ╚╔╩╦╠═╬¤"
Expand Down Expand Up @@ -51,19 +58,17 @@
)


def search_function(encoding):
def search_function(encoding: str) -> codecs.CodecInfo | None:
if regentry.name == encoding:
return regentry

return None


def register():
def register() -> None:
codecs.register(search_function)


def unregister():
try:
def unregister() -> None:
with contextlib.suppress(AttributeError):
codecs.unregister(search_function)
except AttributeError:
pass
25 changes: 12 additions & 13 deletions alternative_encodings/cp866i.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import codecs
import encodings.cp866 as cp866
import contextlib
from encodings import cp866


# Codec APIs
class Codec(cp866.Codec):
def encode(self, input, errors="strict"):
input = input.replace("і", "i").replace("І", "I")
return super().encode(input, errors)
def encode(self, input_string: str, errors: str = "strict") -> bytes:
input_string = input_string.replace("і", "i").replace("І", "I") # noqa: RUF001
return super().encode(input_string, errors)


class IncrementalEncoder(cp866.IncrementalEncoder):
def encode(self, input, final=False):
input = input.replace("і", "i").replace("І", "I")
return super().encode(input, final)
def encode(self, input_string: str, final: bool = False) -> bytes: # noqa: FBT001, FBT002
input_string = input_string.replace("і", "i").replace("І", "I") # noqa: RUF001
return super().encode(input_string, final)


IncrementalDecoder = cp866.IncrementalDecoder
Expand All @@ -30,19 +31,17 @@ def encode(self, input, final=False):
)


def search_function(encoding):
def search_function(encoding: str) -> codecs.CodecInfo | None:
if regentry.name == encoding:
return regentry

return None


def register():
def register() -> None:
codecs.register(search_function)


def unregister():
try:
def unregister() -> None:
with contextlib.suppress(AttributeError):
codecs.unregister(search_function)
except AttributeError:
pass
33 changes: 16 additions & 17 deletions alternative_encodings/romaji.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import codecs
import contextlib
import re
from typing import Iterator
from collections.abc import Iterator
from encodings import cp437

import encodings.cp437 as cp437
import cutlet

katsu = cutlet.Cutlet(use_foreign_spelling=False)


def is_translatable(s: str):
def is_translatable(s: str) -> bool:
return katsu.romaji(s).strip() != s.strip()


Expand All @@ -19,8 +20,8 @@ def translate_with_spaces(s: str) -> Iterator[str]:
yield end


def iter_translate(input: str) -> Iterator[str]:
for line_with_ending in input.splitlines(keepends=True):
def iter_translate(input_string: str) -> Iterator[str]:
for line_with_ending in input_string.splitlines(keepends=True):
line, end = re.search(r"^(.*?)([\r\n]*)$", line_with_ending, flags=re.MULTILINE + re.DOTALL).groups()
if line:
parts = re.split(r"(\b[\w \,]+\b)", line)
Expand All @@ -30,18 +31,18 @@ def iter_translate(input: str) -> Iterator[str]:
yield end


def encode(input: str) -> str:
return "".join(iter_translate(input))
def encode(input_string: str) -> str:
return "".join(iter_translate(input_string))


class Codec(cp437.Codec):
def encode(self, input, errors="strict"):
return super().encode(encode(input), errors)
def encode(self, input_string: str, errors: str = "strict") -> bytes:
return super().encode(encode(input_string), errors)


class IncrementalEncoder(cp437.IncrementalEncoder):
def encode(self, input, final=False):
return super().encode(encode(input), final)
def encode(self, input_string: str, final: bool = False) -> bytes: # noqa: FBT001, FBT002
return super().encode(encode(input_string), final)


IncrementalDecoder = cp437.IncrementalDecoder
Expand All @@ -58,19 +59,17 @@ def encode(self, input, final=False):
)


def search_function(encoding):
def search_function(encoding: str) -> codecs.CodecInfo | None:
if regentry.name == encoding:
return regentry

return None


def register():
def register() -> None:
codecs.register(search_function)


def unregister():
try:
def unregister() -> None:
with contextlib.suppress(AttributeError):
codecs.unregister(search_function)
except AttributeError:
pass
21 changes: 13 additions & 8 deletions alternative_encodings/viscii.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import codecs

from .common import get_codec, get_incremental_decoder, get_incremental_encoder, get_stream_reader, get_stream_writer
import contextlib

from .common import (
get_codec,
get_incremental_decoder,
get_incremental_encoder,
get_stream_reader,
get_stream_writer,
)

# Decoding Table

Expand Down Expand Up @@ -51,19 +58,17 @@
)


def search_function(encoding):
def search_function(encoding: str) -> codecs.CodecInfo | None:
if regentry.name == encoding:
return regentry

return None


def register():
def register() -> None:
codecs.register(search_function)


def unregister():
try:
def unregister() -> None:
with contextlib.suppress(AttributeError):
codecs.unregister(search_function)
except AttributeError:
pass
Loading

0 comments on commit a59a1e6

Please sign in to comment.