Skip to content

Commit

Permalink
fix(pep8): fixed addressed issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiSchwarz-cnic committed May 2, 2022
1 parent ead43c0 commit f6e4acc
Showing 1 changed file with 47 additions and 33 deletions.
80 changes: 47 additions & 33 deletions build-unicode-tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
# <http://www.unicode.org/reports/tr46/index.html>.

from __future__ import division
import urllib.parse
import urllib.error
import urllib.request
from pprint import pprint
import struct
from functools import reduce
import sys
import re
import json
from functools import cmp_to_key
from builtins import object
from past.utils import old_div
from builtins import range
from builtins import map
from builtins import str
from builtins import chr
from past.builtins import cmp
from future import standard_library

standard_library.install_aliases()
from builtins import chr
from builtins import str
from builtins import map
from builtins import range
from past.utils import old_div
from builtins import object
from functools import cmp_to_key
import json
import re
import sys
import urllib.request, urllib.error, urllib.parse
from functools import reduce
import struct
from pprint import pprint

# NUM_UCHAR is the number of Unicode characters there are.
NUM_UCHAR = 0x10FFFF + 1
Expand All @@ -33,13 +35,19 @@ def download_unicode(version):
uribase = "http://www.unicode.org/Public/"
idna_tables = uribase + "idna/" + version
print("... " + idna_tables + "/IdnaTestV2.txt")
urllib.request.urlretrieve(idna_tables + "/IdnaTestV2.txt", "test/IdnaTest.txt")
urllib.request.urlretrieve(
idna_tables + "/IdnaTestV2.txt",
"test/IdnaTest.txt")
infd = urllib.request.urlopen(idna_tables + "/IdnaMappingTable.txt")
dgc = urllib.request.urlopen(
uribase + version + "/ucd/extracted/DerivedGeneralCategory.txt"
)
print("... " + idna_tables + "/IdnaMappingTable.txt")
print("... " + uribase + version + "/ucd/extracted/DerivedGeneralCategory.txt\n")
print(
"... " +
uribase +
version +
"/ucd/extracted/DerivedGeneralCategory.txt\n")
with open("idna-map.js", "w") as outfd:
build_unicode_map(infd, outfd, dgc)
infd.close()
Expand Down Expand Up @@ -88,7 +96,8 @@ def __init__(self, parts):
self.rule = parts[0]
# If there are two parts, the second part is the mapping in question.
if len(parts) > 1 and parts[1]:
self.chars = "".join([unichar(int(u, 16)) for u in parts[1].split(" ")])
self.chars = "".join([unichar(int(u, 16))
for u in parts[1].split(" ")])
else:
self.chars = ""

Expand All @@ -107,7 +116,7 @@ def build_map_string(self, string):
self.index = utf16len(string)
string = string + self.chars
else:
self.index = utf16len(string[0 : self.index])
self.index = utf16len(string[0: self.index])
return string

def build_int(self):
Expand Down Expand Up @@ -153,9 +162,8 @@ def build_unicode_map(idnaMapTable, out, derivedGeneralCategory):

print("... build up internal unicharMap")
# Build up the string to use to map the output
vals.sort(
key=cmp_to_key(lambda x, y: cmp(len(x.chars), len(y.chars))), reverse=True
)
vals.sort(key=cmp_to_key(lambda x, y: cmp(
len(x.chars), len(y.chars))), reverse=True)
mappedStr = reduce(lambda s, v: v.build_map_string(s), vals, "")

# Convert this to integers
Expand Down Expand Up @@ -203,17 +211,23 @@ def build_unicode_map(idnaMapTable, out, derivedGeneralCategory):
out.write("];\n")

# Now emit the block index map
out.write("var blockIdxes = new Uint%dArray([" % (8 if len(blocks) < 256 else 16))
out.write(
"var blockIdxes = new Uint%dArray([" %
(8 if len(blocks) < 256 else 16))
out.write(
",".join(
str(blocks.index(tuple(unicharMap[i : i + block_size])))
str(blocks.index(tuple(unicharMap[i: i + block_size])))
for i in range(0, 0x30000, block_size)
)
)
out.write("]);\n")

# And the string
out.write("var mappingStr = %s;\n" % json.dumps(mappedStr, ensure_ascii=False))
out.write(
"var mappingStr = %s;\n" %
json.dumps(
mappedStr,
ensure_ascii=False))

# Finish off with the function to actually look everything up
out.write(
Expand Down Expand Up @@ -257,7 +271,7 @@ def find_block_sizes(unicharMap):
def compute_block_size(unicharMap, block_size):
blocks = set()
for i in range(0, len(unicharMap), block_size):
block = tuple(unicharMap[i : i + block_size])
block = tuple(unicharMap[i: i + block_size])
blocks.add(block)
num = len(blocks)
if num < 256:
Expand Down Expand Up @@ -287,21 +301,21 @@ def build_body(mode, test_vector, func, expected):
return []
if mode == "T" or mode == "B":
lines.append(
'assert.throws(function () { %s("%s", true); });' % (func, test_vector)
)
'assert.throws(function () { %s("%s", true); });' %
(func, test_vector))
if mode == "N" or mode == "B":
lines.append(
'assert.throws(function () { %s("%s", false); });' % (func, test_vector)
)
'assert.throws(function () { %s("%s", false); });' %
(func, test_vector))
else:
if mode == "T" or mode == "B":
lines.append(
'assert.equal(%s("%s", true), "%s");' % (func, test_vector, expected)
)
'assert.equal(%s("%s", true), "%s");' %
(func, test_vector, expected))
if mode == "N" or mode == "B":
lines.append(
'assert.equal(%s("%s", false), "%s");' % (func, test_vector, expected)
)
'assert.equal(%s("%s", false), "%s");' %
(func, test_vector, expected))

return lines

Expand Down

0 comments on commit f6e4acc

Please sign in to comment.