1919# To contact SUSE LLC about this file by physical or electronic mail, you may
2020# find current contact information at www.suse.com.
2121
22+ #
23+ # This script generates the list of supported languages in JSON format.
24+ #
25+
2226from argparse import ArgumentParser
2327from langtable import language_name
2428from pathlib import Path
2529import json
26- import re
2730import subprocess
28-
31+ import sys
2932
3033class Locale :
3134 language : str
@@ -76,20 +79,15 @@ def language(self):
7679 return self .path .stem
7780
7881
79- class Manifest :
80- """ This class takes care of updating the manifest file"""
82+ class Languages :
83+ """ This class takes care of generating the supported languages file"""
8184
82- def __init__ (self , path : Path ):
83- self .path = path
84- self .__read__ ()
85-
86- def __read__ (self ):
87- with open (self .path ) as file :
88- self .content = json .load (file )
85+ def __init__ (self ):
86+ self .content = dict ()
8987
9088 def update (self , po_files , lang2territory : str , threshold : int ):
9189 """
92- Updates the list of locales in the manifest file
90+ Generate the list of supported locales
9391
9492 It does not write the changes to file system. Use the write() function
9593 for that.
@@ -111,46 +109,43 @@ def update(self, po_files, lang2territory: str, threshold: int):
111109 if locale .territory is None :
112110 print (
113111 "could not find a territory for '{language}'"
114- .format (language = locale .language )
112+ .format (language = locale .language ),
113+ file = sys .stderr
115114 )
116115 elif po_file .coverage () < threshold :
117116 print (
118117 "not enough coverage for '{language}' ({coverage}%)"
119118 .format (
120119 language = locale .code (),
121- coverage = po_file .coverage ())
120+ coverage = po_file .coverage ()),
121+ file = sys .stderr
122122 )
123123 else :
124124 supported .append (locale )
125125
126126 languages = [loc .language for loc in supported ]
127- self .content ["locales" ] = dict ()
128127 for locale in supported :
129128 include_territory = languages .count (locale .language ) > 1
130- self .content ["locales" ][locale .code ()] = locale .name (
131- include_territory )
129+ self .content [locale .code ()] = locale .name (include_territory )
132130
133- def write (self ):
134- with open (self .path , "w+" ) as file :
135- json . dump ( self . content , file , indent = 4 , ensure_ascii = False )
131+ def dump (self ):
132+ json . dump (self .content , sys . stdout , indent = 4 , ensure_ascii = False ,
133+ sort_keys = True )
136134
137135
138- def update_manifest (args ):
139- """Command to update the manifest.json file """
140- manifest = Manifest ( Path ( args . manifest ) )
136+ def update_languages (args ):
137+ """Print the supported languages in JSON format """
138+ languages = Languages ( )
141139 paths = [path for path in Path (args .po_directory ).glob ("*.po" )]
142140 with open (args .territories ) as file :
143141 lang2territory = json .load (file )
144- manifest .update (paths , lang2territory , args .threshold )
145- manifest . write ()
142+ languages .update (paths , lang2territory , args .threshold )
143+ languages . dump ()
146144
147145
148146if __name__ == "__main__" :
149- parser = ArgumentParser (prog = "locales.py" )
150- parser .set_defaults (func = update_manifest )
151- parser .add_argument (
152- "manifest" , type = str , help = "Path to the manifest file" ,
153- )
147+ parser = ArgumentParser (prog = "update-languages.py" )
148+ parser .set_defaults (func = update_languages )
154149 parser .add_argument (
155150 "--po-directory" , type = str , help = "Directory containing the po files" ,
156151 default = "web/po"
0 commit comments