Skip to content

Commit

Permalink
Reformatting via black
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiesel authored and MinmoTech committed Jun 27, 2023
1 parent 7414a6d commit b4625c2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 28 deletions.
17 changes: 9 additions & 8 deletions tools/db_from_tsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import os


tsv_path = sys.argv[1] if len(sys.argv) > 1 else 'kanji.tsv'
db_path = sys.argv[2] if len(sys.argv) > 2 else 'kanji.db'
tsv_path = sys.argv[1] if len(sys.argv) > 1 else "kanji.tsv"
db_path = sys.argv[2] if len(sys.argv) > 2 else "kanji.db"

db_path = os.path.abspath(db_path)

Expand All @@ -18,8 +18,7 @@
conn = sqlite3.connect(db_path)


create_sql = \
"""CREATE TABLE characters (
create_sql = """CREATE TABLE characters (
character TEXT NOT NULL PRIMARY KEY,
stroke_count INTEGER DEFAULT NULL,
onyomi TEXT DEFAULT "[]",
Expand All @@ -45,20 +44,22 @@
)"""

conn.execute(create_sql)
fields = [l.split()[0] for l in create_sql.split('\n') if l.startswith(' ')]
fields = [l.split()[0] for l in create_sql.split("\n") if l.startswith(" ")]


db_kanji = []

for l in open(tsv_path, 'r', encoding='utf-8'):
d = l.replace('\n', '').split('\t')
for l in open(tsv_path, "r", encoding="utf-8"):
d = l.replace("\n", "").split("\t")
if len(d) != 7:
continue
kanji = d[0].strip()
if len(kanji) != 1:
continue

insert_sql = F'INSERT into characters ({",".join(fields)}) values ({",".join("?"*len(fields))})'
insert_sql = (
f'INSERT into characters ({",".join(fields)}) values ({",".join("?"*len(fields))})'
)
conn.executemany(insert_sql, db_kanji)


Expand Down
48 changes: 37 additions & 11 deletions tools/db_to_tsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,59 @@
import os


db_path = sys.argv[1] if len(sys.argv) > 1 else 'kanji.db'
tsv_path = sys.argv[2] if len(sys.argv) > 2 else 'kanji.tsv'
db_path = sys.argv[1] if len(sys.argv) > 1 else "kanji.db"
tsv_path = sys.argv[2] if len(sys.argv) > 2 else "kanji.tsv"

db_path = os.path.abspath(db_path)

con = sqlite3.connect(db_path)
crs = con.cursor()

crs.execute('SELECT * FROM characters')
crs.execute("SELECT * FROM characters")
data = crs.fetchall()


f = open(tsv_path, 'w', encoding='utf-8')
f = open(tsv_path, "w", encoding="utf-8")


def fw(*args):
if '\t' in ''.join(args):
raise ValueError('TSV ERROR')
if "\t" in "".join(args):
raise ValueError("TSV ERROR")

f.write("\t".join(args))
f.write("\n")

f.write('\t'.join(args))
f.write('\n')

fw('Kanji', 'Meanings', 'Primitive Alternatives', 'Primitives', 'Heisig Keyword (1-5)', 'Heisig Keyword (6+)', 'Primitive Keywords', 'Heisig Story', 'Heisig Comment', 'Radicals')
fw(
"Kanji",
"Meanings",
"Primitive Alternatives",
"Primitives",
"Heisig Keyword (1-5)",
"Heisig Keyword (6+)",
"Primitive Keywords",
"Heisig Story",
"Heisig Comment",
"Radicals",
)


def j2c(d):
return ', '.join(json.loads(d))
return ", ".join(json.loads(d))


for d in data:
fw(d[0], j2c(d[5]), d[12], d[10], d[15] or '', d[16] or '', j2c(d[11]), d[17] or '', d[18] or '', d[19])
fw(
d[0],
j2c(d[5]),
d[12],
d[10],
d[15] or "",
d[16] or "",
j2c(d[11]),
d[17] or "",
d[18] or "",
d[19],
)

f.close()
14 changes: 5 additions & 9 deletions tools/kanjivg_cleanup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
import os

kanjivg_dir = os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'..',
'addon',
'kanjivg'
os.path.dirname(os.path.abspath(__file__)), "..", "addon", "kanjivg"
)

for fname in os.listdir(kanjivg_dir):

if fname.endswith('.svg'):
with open(os.path.join(kanjivg_dir, fname), 'r') as f:
if fname.endswith(".svg"):
with open(os.path.join(kanjivg_dir, fname), "r") as f:
svg = f.read()

idx = svg.find('<svg')
idx = svg.find("<svg")
if idx > 0:
svg = svg[idx:]

with open(os.path.join(kanjivg_dir, fname), 'w') as f:
with open(os.path.join(kanjivg_dir, fname), "w") as f:
f.write(svg)

0 comments on commit b4625c2

Please sign in to comment.