Skip to content

Commit

Permalink
Apply ruff format for idna-data make-libdata generated files
Browse files Browse the repository at this point in the history
  • Loading branch information
waketzheng committed Aug 26, 2024
1 parent 8b55055 commit d076221
Showing 1 changed file with 29 additions and 16 deletions.
45 changes: 29 additions & 16 deletions tools/idna-data
Original file line number Diff line number Diff line change
Expand Up @@ -485,11 +485,21 @@ def ucdrange(start, end):
return ('{:04X}..{:04X}'.format(start.value, end.value),
'{}..{}'.format(start.name, end.name))

def upper_hex(value):
num = hex(value)
return num[:2] + num[2:].upper()

def optimised_list(d):
yield '('
for value in intranges_from_list(d):
yield ' {},'.format(hex(value))
yield ' ),'
values = intranges_from_list(d)
if len(values) == 1:
for value in values:
# Respect ruff format style
yield '({},),'.format(upper_hex(value))
else:
yield '('
for value in values:
yield ' {},'.format(upper_hex(value))
yield ' ),'

def make_table(args, ucdata):

Expand Down Expand Up @@ -523,14 +533,14 @@ def make_table(args, ucdata):
def idna_libdata(ucdata):

yield '# This file is automatically generated by tools/idna-data\n'
yield '__version__ = \'{}\''.format(ucdata.version)
yield '__version__ = "{}"\n'.format(ucdata.version)

#
# Script classifications are used by some CONTEXTO rules in RFC 5891
#
yield 'scripts = {'
for script in SCRIPT_WHITELIST:
prefix = ' \'{}\': '.format(script)
prefix = ' "{}": '.format(script)
for line in optimised_list(ucdata.ucd_s[script]):
yield prefix + line
prefix = ''
Expand All @@ -542,7 +552,7 @@ def idna_libdata(ucdata):
yield 'joining_types = {'
for cp in ucdata.codepoints():
if cp.joining_type:
yield ' 0x{:x}: {},'.format(cp.value, ord(cp.joining_type))
yield ' 0x{:X}: {},'.format(cp.value, ord(cp.joining_type))
yield '}'

#
Expand All @@ -558,7 +568,7 @@ def idna_libdata(ucdata):
classes[status] = set()
classes[status].add(cp.value)
for status in ['PVALID', 'CONTEXTJ', 'CONTEXTO']:
prefix = ' \'{}\': '.format(status)
prefix = ' "{}": '.format(status)
for line in optimised_list(classes[status]):
yield prefix + line
prefix = ''
Expand All @@ -574,38 +584,41 @@ def uts46_ranges(ucdata):
status, mapping = UTS46_STATUSES[fields[0]]
if mapping:
mapping = ''.join(chr(int(codepoint, 16)) for codepoint in fields[1].split())
mapping = mapping.replace('\\', '\\\\').replace('\'', '\\\'')
mapping = mapping.replace('\\', '\\\\')
else:
mapping = None
if cp.value > 255 and (status, mapping) == last:
continue
last = (status, mapping)

if mapping is not None:
yield '(0x{:X}, \'{}\', \'{}\')'.format(cp.value, status, mapping)
if '"' in mapping:
yield '(0x{:X}, "{}", \'{}\')'.format(cp.value, status, mapping)
else:
yield '(0x{:X}, "{}", "{}")'.format(cp.value, status, mapping)
else:
yield '(0x{:X}, \'{}\')'.format(cp.value, status)
yield '(0x{:X}, "{}")'.format(cp.value, status)

def uts46_libdata(ucdata):

yield '# This file is automatically generated by tools/idna-data'
yield '# vim: set fileencoding=utf-8 :\n'
yield 'from typing import List, Tuple, Union\n\n'
yield 'from typing import List, Tuple, Union\n'
yield '"""IDNA Mapping Table from UTS46."""\n\n'

yield '__version__ = \'{}\''.format(ucdata.version)
yield '__version__ = "{}"\n'.format(ucdata.version)

idx = -1
for row in uts46_ranges(ucdata):
idx += 1
if idx % UTS46_SEGMENT_SIZE == 0:
if idx != 0:
yield ' ]\n'
yield 'def _seg_{}() -> List[Union[Tuple[int, str], Tuple[int, str, str]]]:\n return ['.format(idx // UTS46_SEGMENT_SIZE)
yield ' {},'.format(row)
yield '\ndef _seg_{}() -> List[Union[Tuple[int, str], Tuple[int, str, str]]]:\n return ['.format(idx // UTS46_SEGMENT_SIZE)
yield ' {},'.format(row)
yield ' ]\n'

yield 'uts46data = tuple('
yield '\nuts46data = tuple('

This comment has been minimized.

Copy link
@glensc

glensc Sep 16, 2024

nuts :)

yield ' _seg_0()'
for i in range(1, idx // UTS46_SEGMENT_SIZE + 1):
yield ' + _seg_{}()'.format(i)
Expand Down

0 comments on commit d076221

Please sign in to comment.