Skip to content

Commit 608cef9

Browse files
committed
generate-pot.py: Make it possible to process a single SC
1 parent 6432351 commit 608cef9

File tree

1 file changed

+50
-38
lines changed

1 file changed

+50
-38
lines changed

util/skycultures/generate-pot.py

+50-38
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import sys
2727
import json
2828
import polib
29+
import argparse
2930

3031
if sys.version_info[0] < 3:
3132
raise Exception("Please use Python 3 (current is {})".format(
@@ -40,6 +41,12 @@
4041
common_names = set()
4142
cons_ast_names = set()
4243

44+
def ensure_dir(file_path):
45+
'''Create a directory for a path if it doesn't exist yet'''
46+
directory = os.path.dirname(file_path)
47+
if not os.path.exists(directory):
48+
os.makedirs(directory)
49+
4350
def is_sky_culture_dir(d):
4451
if not os.path.isdir(os.path.join(SCDIR, d)):
4552
return False
@@ -65,22 +72,7 @@ def load_common_names():
6572
common_names.add(match.group(1))
6673
print(f"Loaded {len(common_names)} common names", file=sys.stderr)
6774

68-
def update_descriptions_pot():
69-
sclist = [d for d in os.listdir(SCDIR) if is_sky_culture_dir(d)]
70-
sclist.sort()
71-
72-
pot = polib.POFile(encoding='utf-8', check_for_duplicates=True)
73-
pot.metadata = {
74-
'Project-Id-Version': 'PACKAGE VERSION',
75-
'Report-Msgid-Bugs-To': '[email protected]',
76-
'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>',
77-
'Language-Team': 'LANGUAGE <[email protected]>',
78-
'Language': '',
79-
'MIME-Version': '1.0',
80-
'Content-Type': 'text/plain; charset=UTF-8',
81-
'Content-Transfer-Encoding': '8bit'
82-
}
83-
75+
def update_descriptions_pot(sclist, pot):
8476
for sky_culture in sclist:
8577
data_path = os.path.join(SCDIR, sky_culture)
8678
description_file = os.path.join(data_path, 'description.md')
@@ -142,24 +134,7 @@ def update_descriptions_pot():
142134
else:
143135
pot.append(entry)
144136

145-
pot.save(DESCPOTFILE)
146-
147-
def update_cultures_pot():
148-
sclist = [d for d in os.listdir(SCDIR) if is_sky_culture_dir(d)]
149-
sclist.sort()
150-
151-
pot = polib.POFile(encoding='utf-8', check_for_duplicates=True)
152-
pot.metadata = {
153-
'Project-Id-Version': 'PACKAGE VERSION',
154-
'Report-Msgid-Bugs-To': '[email protected]',
155-
'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>',
156-
'Language-Team': 'LANGUAGE <[email protected]>',
157-
'Language': '',
158-
'MIME-Version': '1.0',
159-
'Content-Type': 'text/plain; charset=UTF-8',
160-
'Content-Transfer-Encoding': '8bit'
161-
}
162-
137+
def update_cultures_pot(sclist, pot):
163138
def process_cons_or_asterism(array, obj_type, pot, sc_name):
164139
for obj in array:
165140
assert 'id' in obj
@@ -313,13 +288,50 @@ def process_names(objects, pot, sc_name):
313288
if 'common_names' in data:
314289
process_names(data['common_names'], pot, sc_name)
315290

316-
pot.save(SCPOTFILE)
291+
if __name__ == '__main__':
292+
parser = argparse.ArgumentParser()
293+
parser.add_argument("-s", "--sky-culture", help="Process only the specified sky culture")
294+
args = parser.parse_args()
295+
metadata_template = {
296+
'Project-Id-Version': 'PACKAGE VERSION',
297+
'Report-Msgid-Bugs-To': '[email protected]',
298+
'Last-Translator': 'FULL NAME <EMAIL@ADDRESS>',
299+
'Language-Team': 'LANGUAGE <[email protected]>',
300+
'Language': '',
301+
'MIME-Version': '1.0',
302+
'Content-Type': 'text/plain; charset=UTF-8',
303+
'Content-Transfer-Encoding': '8bit'
304+
}
305+
if args.sky_culture:
306+
sclist = [args.sky_culture]
307+
desc_pot = polib.POFile(encoding='utf-8', check_for_duplicates=True)
308+
desc_pot.metadata = metadata_template
309+
sc_pot = desc_pot # same file for everything
310+
desc_pot_file_path = None
311+
sc_pot_file_path = None
312+
common_pot_file_path = os.path.join(SCDIR, args.sky_culture, 'po', args.sky_culture+'.pot')
313+
ensure_dir(common_pot_file_path)
314+
else:
315+
sclist = [d for d in os.listdir(SCDIR) if is_sky_culture_dir(d)]
316+
sclist.sort()
317+
desc_pot = polib.POFile(encoding='utf-8', check_for_duplicates=True)
318+
desc_pot.metadata = metadata_template
319+
sc_pot = polib.POFile(encoding='utf-8', check_for_duplicates=True)
320+
sc_pot.metadata = metadata_template
321+
desc_pot_file_path = DESCPOTFILE
322+
sc_pot_file_path = SCPOTFILE
323+
common_pot_file_path = None
317324

318325

319-
if __name__ == '__main__':
320326
print("Loading common names...", file=sys.stderr)
321327
load_common_names()
322328
print("Updating descriptions .pot file...", file=sys.stderr)
323-
update_descriptions_pot()
329+
update_descriptions_pot(sclist, desc_pot)
330+
if desc_pot_file_path:
331+
desc_pot.save(desc_pot_file_path)
324332
print("Updating SC index .pot file...", file=sys.stderr)
325-
update_cultures_pot()
333+
update_cultures_pot(sclist, sc_pot)
334+
if sc_pot_file_path:
335+
sc_pot.save(sc_pot_file_path)
336+
if common_pot_file_path:
337+
sc_pot.save(common_pot_file_path)

0 commit comments

Comments
 (0)