Skip to content

Commit

Permalink
Use ruff formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Aug 26, 2024
1 parent 6614f3f commit 8b55055
Show file tree
Hide file tree
Showing 16 changed files with 12,497 additions and 12,186 deletions.
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 8b55055

Please sign in to comment.