lang.py PEP8 improvements

This commit is contained in:
jxd1337 2023-10-28 10:50:39 +02:00
parent f748395bda
commit dc96b473cd

View File

@ -5,20 +5,22 @@ import glob
import sys import sys
import csv import csv
def get_lang(lang): def get_lang(lang):
out = {} out = {}
for ln in open('./src/lang/%s.rs'%lang, encoding='utf8'): for ln in open('./src/lang/%s.rs' % lang, encoding='utf8'):
ln = ln.strip() ln = ln.strip()
if ln.startswith('("'): if ln.startswith('("'):
k, v = line_split(ln) k, v = line_split(ln)
out[k] = v out[k] = v
return out return out
def line_split(line): def line_split(line):
toks = line.split('", "') toks = line.split('", "')
if len(toks) != 2: if len(toks) != 2:
print(line) print(line)
assert(0) assert (0)
# Replace fixed position. # Replace fixed position.
# Because toks[1] may be v") or v"), # Because toks[1] may be v") or v"),
k = toks[0][toks[0].find('"') + 1:] k = toks[0][toks[0].find('"') + 1:]
@ -38,17 +40,17 @@ def main():
def expand(): def expand():
for fn in glob.glob('./src/lang/*.rs'): for fn in glob.glob('./src/lang/*.rs'):
lang = os.path.basename(fn)[:-3] lang = os.path.basename(fn)[:-3]
if lang in ['en','template']: continue if lang in ['en', 'template']: continue
print(lang) print(lang)
dict = get_lang(lang) dict = get_lang(lang)
fw = open("./src/lang/%s.rs"%lang, "wt", encoding='utf8') fw = open("./src/lang/%s.rs" % lang, "wt", encoding='utf8')
for line in open('./src/lang/template.rs', encoding='utf8'): for line in open('./src/lang/template.rs', encoding='utf8'):
line_strip = line.strip() line_strip = line.strip()
if line_strip.startswith('("'): if line_strip.startswith('("'):
k, v = line_split(line_strip) k, v = line_split(line_strip)
if k in dict: if k in dict:
# embraced with " to avoid empty v # embraced with " to avoid empty v
line = line.replace('"%s"'%v, '"%s"'%dict[k]) line = line.replace('"%s"' % v, '"%s"' % dict[k])
else: else:
line = line.replace(v, "") line = line.replace(v, "")
fw.write(line) fw.write(line)
@ -60,7 +62,7 @@ def expand():
def to_csv(): def to_csv():
for fn in glob.glob('./src/lang/*.rs'): for fn in glob.glob('./src/lang/*.rs'):
lang = os.path.basename(fn)[:-3] lang = os.path.basename(fn)[:-3]
csvfile = open('./src/lang/%s.csv'%lang, "wt", encoding='utf8') csvfile = open('./src/lang/%s.csv' % lang, "wt", encoding='utf8')
csvwriter = csv.writer(csvfile) csvwriter = csv.writer(csvfile)
for line in open(fn, encoding='utf8'): for line in open(fn, encoding='utf8'):
line_strip = line.strip() line_strip = line.strip()
@ -71,14 +73,14 @@ def to_csv():
def to_rs(lang): def to_rs(lang):
csvfile = open('%s.csv'%lang, "rt", encoding='utf8') csvfile = open('%s.csv' % lang, "rt", encoding='utf8')
fw = open("./src/lang/%s.rs"%lang, "wt", encoding='utf8') fw = open("./src/lang/%s.rs" % lang, "wt", encoding='utf8')
fw.write('''lazy_static::lazy_static! { fw.write('''lazy_static::lazy_static! {
pub static ref T: std::collections::HashMap<&'static str, &'static str> = pub static ref T: std::collections::HashMap<&'static str, &'static str> =
[ [
''') ''')
for row in csv.reader(csvfile): for row in csv.reader(csvfile):
fw.write(' ("%s", "%s"),\n'%(row[0].replace('"', '\"'), row[1].replace('"', '\"'))) fw.write(' ("%s", "%s"),\n' % (row[0].replace('"', '\"'), row[1].replace('"', '\"')))
fw.write(''' ].iter().cloned().collect(); fw.write(''' ].iter().cloned().collect();
} }
''') ''')