11#!/usr/bin/env python3
2- # coding: utf-8
32
43import os
54import argparse
65import sys
7- from multiprocessing import Pool
6+ import concurrent . futures
87from polib import pofile
9- from functools import partial
108from msg2po .core import (
119 CONFIG ,
1210 LanguageMap ,
@@ -37,8 +35,9 @@ def extract_po(pf: str, language_map: LanguageMap):
3735 pf is po file basename
3836 """
3937 po_path = os .path .join (CONFIG .po_dirname , pf )
40- print ("processing {}" .format (po_path ))
41- po = pofile (po_path ) # open once
38+ print (f"processing { po_path } " )
39+ # Open PO once, it's a heavy op
40+ po = pofile (po_path )
4241 trans_map = translation_entries (po )
4342 female_map = female_entries (po )
4443
@@ -47,38 +46,44 @@ def extract_po(pf: str, language_map: LanguageMap):
4746 for ef in sorted (trans_map ):
4847 enc = get_enc (po_path , ef )
4948 ef_extract_path = os .path .join (dst_dir , ef )
50- print ("Extracting {} from {} into {} with encoding {}" . format ( ef , po_path , ef_extract_path , enc ) )
49+ print (f "Extracting { ef } from { po_path } into { ef_extract_path } with encoding { enc } " )
5150 po2file (po , ef_extract_path , enc , ef , dst_dir = dst_dir , trans_map = trans_map , female_map = female_map )
5251
5352 enc = get_enc (po_path )
54- print ("Extracted {} into {} with encoding {}" . format ( po_path , dst_dir , enc ) )
53+ print (f "Extracted { po_path } into { dst_dir } with encoding { enc } " )
5554
5655
5756def main ():
5857 po_dir = CONFIG .po_dir
5958 language_map = LanguageMap ()
6059
61- # find PO files
62- po_files = []
63- for dir_name , subdir_list , file_list in os .walk (po_dir ):
64- for f in file_list :
65- if get_ext (f ) == "po" :
66- po_files .append (f )
67- if po_files == []:
68- print ("no PO files found in directory {}" .format (po_dir ))
60+ # Find PO files
61+ po_files = [f for _ , _ , files in os .walk (po_dir ) for f in files if get_ext (f ) == "po" ]
62+
63+ if not po_files :
64+ print (f"no PO files found in directory { po_dir } " )
6965 sys .exit (1 )
7066
7167 with cd (CONFIG .tra_dir ):
72- # extract PO files
73- pool = Pool ()
74- try :
75- r = pool .map_async (partial (extract_po , language_map = language_map ), po_files )
76- pool .close ()
77- codes = r .get () # noqa: F841 - need to get results to see if there's an exception
78- except KeyboardInterrupt :
79- pool .terminate ()
80- finally :
81- pool .join ()
68+ with concurrent .futures .ProcessPoolExecutor () as executor :
69+ futures = {executor .submit (extract_po , pf , language_map ): pf for pf in po_files }
70+
71+ for future in concurrent .futures .as_completed (futures ):
72+ pf = futures [future ]
73+ try :
74+ future .result ()
75+ except ValueError as e :
76+ print (f"ValueError in file { pf } : { e } " )
77+ executor .shutdown (wait = False , cancel_futures = True )
78+ sys .exit (1 )
79+ except KeyboardInterrupt :
80+ print ("Interrupted by user, terminating execution..." )
81+ executor .shutdown (wait = False , cancel_futures = True )
82+ sys .exit (1 )
83+ except Exception as e :
84+ print (f"Unhandled exception in file { pf } : { e } " )
85+ executor .shutdown (wait = False , cancel_futures = True )
86+ sys .exit (1 )
8287
8388
8489if __name__ == "__main__" :
0 commit comments