Skip to content

Commit

Permalink
Apply Black to locale (#1513)
Browse files Browse the repository at this point in the history
This applies Black 20.8b1 to po file stats script.
Uses: black --target-version=py36
  • Loading branch information
wenzeslaus committed Apr 7, 2021
1 parent d06c8e8 commit e9dfbc4
Showing 1 changed file with 39 additions and 34 deletions.
73 changes: 39 additions & 34 deletions locale/grass_po_stats.py
Expand Up @@ -29,15 +29,15 @@ def read_po_files(inputdirpath):
os.chdir(inputdirpath)
languages = {}
for pofile in sorted(glob.glob("*.po")):
lang = pofile.split('_')[1:]
lang = pofile.split("_")[1:]
# check if are two definitions like pt_br
if len(lang) == 2:
lang = ['_'.join(lang)]
lang = lang[0].split('.')[0]
lang = ["_".join(lang)]
lang = lang[0].split(".")[0]

# if keys is not in languages add it and the file's name
if lang not in languages:
languages[lang] = [pofile, ]
languages[lang] = [pofile]
# add only files name
else:
languages[lang].append(pofile)
Expand All @@ -49,33 +49,33 @@ def read_msgfmt_statistics(msg, lgood, lfuzzy, lbad):
"""Return a dictionary, and the good, fuzzy and bad values from a string"""
langdict = {}
# split the output
out = msg.split(b',')
out = msg.split(b",")
# TODO maybe check using regex
# check for each answer
for o in out:
o = o.lower().strip()
# each answer is written into dictionary and
# the value add to variable for the sum
if b'untranslated' in o:
val = int(o.split(b' ')[0])
langdict['bad'] = val
if b"untranslated" in o:
val = int(o.split(b" ")[0])
langdict["bad"] = val
lbad += val
elif b'fuzzy' in o:
val = int(o.split(b' ')[0])
langdict['fuzzy'] = val
elif b"fuzzy" in o:
val = int(o.split(b" ")[0])
langdict["fuzzy"] = val
lfuzzy += val
else:
val = int(o.split(b' ')[0])
langdict['good'] = val
val = int(o.split(b" ")[0])
langdict["good"] = val
lgood += val
return langdict, lgood, lfuzzy, lbad


def langDefinition(fil):
f = codecs.open(fil, encoding='utf-8', errors='replace', mode='r')
f = codecs.open(fil, encoding="utf-8", errors="replace", mode="r")
for l in f.readlines():
if '"Language-Team:' in l:
lang = l.split(' ')[1:-1]
lang = l.split(" ")[1:-1]
break
f.close()
if len(lang) == 2:
Expand All @@ -92,40 +92,45 @@ def get_stats(languages, directory):
output = {}
# TO DO TOTALS OF ENGLISH WORD FOR EACH FILE
# all the total string in english
output['totals'] = {}
output["totals"] = {}
# all the information about each lang
output['langs'] = {}
output["langs"] = {}
# for each language
for lang, pofilelist in languages.items():
output['langs'][lang] = {}
output['langs'][lang]['total'] = {}
output['langs'][lang]['name'] = langDefinition(os.path.join(directory, pofilelist[0]))
output["langs"][lang] = {}
output["langs"][lang]["total"] = {}
output["langs"][lang]["name"] = langDefinition(
os.path.join(directory, pofilelist[0])
)
# variables to create sum for each language
lgood = 0
lfuzzy = 0
lbad = 0
# for each file
for flang in pofilelist:

fpref = flang.split('_')[0]
fpref = flang.split("_")[0]
# run msgfmt for statistics
# TODO check if it's working on windows
process = subprocess.Popen(['msgfmt', '--statistics',
os.path.join(directory,flang)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process = subprocess.Popen(
["msgfmt", "--statistics", os.path.join(directory, flang)],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
msg = process.communicate()[1].strip()
# check if some errors occurs
if msg.find(b'error') != -1:
if msg.find(b"error") != -1:
# TODO CHECK IF grass.warning()
print("WARNING: file <%s> has some problems: <%s>" % (flang, msg))
continue
output['langs'][lang][fpref], lgood, lfuzzy, lbad = \
read_msgfmt_statistics(msg, lgood, lfuzzy, lbad)
output["langs"][lang][fpref], lgood, lfuzzy, lbad = read_msgfmt_statistics(
msg, lgood, lfuzzy, lbad
)
# write the sum and total of each file
output['langs'][lang]['total']['good'] = lgood
output['langs'][lang]['total']['fuzzy'] = lfuzzy
output['langs'][lang]['total']['bad'] = lbad
output['langs'][lang]['total']['total'] = lgood + lfuzzy + lbad
output["langs"][lang]["total"]["good"] = lgood
output["langs"][lang]["total"]["fuzzy"] = lfuzzy
output["langs"][lang]["total"]["bad"] = lbad
output["langs"][lang]["total"]["total"] = lgood + lfuzzy + lbad
return output


Expand All @@ -135,7 +140,7 @@ def writejson(stats, outfile):
# write a string with pretty style
outjson = os.linesep.join([l.rstrip() for l in fjson.splitlines()])
# write out file
fout = open(outfile, 'w')
fout = open(outfile, "w")
fout.write(outjson)
fout.write(os.linesep)
fout.close()
Expand All @@ -155,6 +160,6 @@ def main(in_dirpath, out_josonpath):


if __name__ == "__main__":
directory = 'po/'
outfile = os.path.join(os.environ['GISBASE'], 'translation_status.json')
directory = "po/"
outfile = os.path.join(os.environ["GISBASE"], "translation_status.json")
sys.exit(main(directory, outfile))

0 comments on commit e9dfbc4

Please sign in to comment.