Skip to content

Commit

Permalink
Merge pull request #186 from kjd/format-code
Browse files Browse the repository at this point in the history
Format code using `ruff`
  • Loading branch information
kjd authored Aug 28, 2024
2 parents 7b49ee8 + 99e7933 commit 3a26791
Show file tree
Hide file tree
Showing 17 changed files with 12,447 additions and 12,199 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ jobs:
python-version: ${{ matrix.python-version }}
allow-prereleases: true
- name: Install dependencies
run: python -m pip install mypy
run: python -m pip install ruff mypy
- name: Check code style with ruff
run: |
ruff format --check idna tests
ruff check idna tests
- name: Check types with mypy
run: mypy --strict idna

Expand Down
3 changes: 2 additions & 1 deletion idna/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .package_data import __version__
from .core import (
IDNABidiError,
IDNAError,
Expand All @@ -20,8 +19,10 @@
valid_string_length,
)
from .intranges import intranges_contain
from .package_data import __version__

__all__ = [
"__version__",
"IDNABidiError",
"IDNAError",
"InvalidCodepoint",
Expand Down
58 changes: 31 additions & 27 deletions idna/codec.py
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
from .core import encode, decode, alabel, ulabel, IDNAError
import codecs
import re
from typing import Any, Tuple, Optional
from typing import Any, Optional, Tuple

_unicode_dots_re = re.compile('[\u002e\u3002\uff0e\uff61]')
from .core import IDNAError, alabel, decode, encode, ulabel

_unicode_dots_re = re.compile("[\u002e\u3002\uff0e\uff61]")

class Codec(codecs.Codec):

def encode(self, data: str, errors: str = 'strict') -> Tuple[bytes, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
class Codec(codecs.Codec):
def encode(self, data: str, errors: str = "strict") -> Tuple[bytes, int]:
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return b"", 0

return encode(data), len(data)

def decode(self, data: bytes, errors: str = 'strict') -> Tuple[str, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
def decode(self, data: bytes, errors: str = "strict") -> Tuple[str, int]:
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return '', 0
return "", 0

return decode(data), len(data)


class IncrementalEncoder(codecs.BufferedIncrementalEncoder):
def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[bytes, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return b'', 0
return b"", 0

labels = _unicode_dots_re.split(data)
trailing_dot = b''
trailing_dot = b""
if labels:
if not labels[-1]:
trailing_dot = b'.'
trailing_dot = b"."
del labels[-1]
elif not final:
# Keep potentially unfinished label until the next call
del labels[-1]
if labels:
trailing_dot = b'.'
trailing_dot = b"."

result = []
size = 0
Expand All @@ -54,32 +56,33 @@ def _buffer_encode(self, data: str, errors: str, final: bool) -> Tuple[bytes, in
size += len(label)

# Join with U+002E
result_bytes = b'.'.join(result) + trailing_dot
result_bytes = b".".join(result) + trailing_dot
size += len(trailing_dot)
return result_bytes, size


class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
def _buffer_decode(self, data: Any, errors: str, final: bool) -> Tuple[str, int]:
if errors != 'strict':
raise IDNAError('Unsupported error handling \"{}\"'.format(errors))
if errors != "strict":
raise IDNAError('Unsupported error handling "{}"'.format(errors))

if not data:
return ('', 0)
return ("", 0)

if not isinstance(data, str):
data = str(data, 'ascii')
data = str(data, "ascii")

labels = _unicode_dots_re.split(data)
trailing_dot = ''
trailing_dot = ""
if labels:
if not labels[-1]:
trailing_dot = '.'
trailing_dot = "."
del labels[-1]
elif not final:
# Keep potentially unfinished label until the next call
del labels[-1]
if labels:
trailing_dot = '.'
trailing_dot = "."

result = []
size = 0
Expand All @@ -89,7 +92,7 @@ def _buffer_decode(self, data: Any, errors: str, final: bool) -> Tuple[str, int]
size += 1
size += len(label)

result_str = '.'.join(result) + trailing_dot
result_str = ".".join(result) + trailing_dot
size += len(trailing_dot)
return (result_str, size)

Expand All @@ -103,7 +106,7 @@ class StreamReader(Codec, codecs.StreamReader):


def search_function(name: str) -> Optional[codecs.CodecInfo]:
if name != 'idna2008':
if name != "idna2008":
return None
return codecs.CodecInfo(
name=name,
Expand All @@ -115,4 +118,5 @@ def search_function(name: str) -> Optional[codecs.CodecInfo]:
streamreader=StreamReader,
)


codecs.register(search_function)
10 changes: 6 additions & 4 deletions idna/compat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from .core import *
from .codec import *
from typing import Any, Union

from .core import decode, encode


def ToASCII(label: str) -> bytes:
return encode(label)


def ToUnicode(label: Union[bytes, bytearray]) -> str:
return decode(label)

def nameprep(s: Any) -> None:
raise NotImplementedError('IDNA 2008 does not utilise nameprep protocol')

def nameprep(s: Any) -> None:
raise NotImplementedError("IDNA 2008 does not utilise nameprep protocol")
Loading

0 comments on commit 3a26791

Please sign in to comment.