1818import textwrap
1919from xml .dom import Node
2020import io
21+ import glob
2122
2223messages = {}
2324
@@ -735,65 +736,54 @@ def parseGenerator(node):
735736 doc += n1 .nodeValue .rstrip ("\r \n " ).lstrip ("\r \n " )
736737 messages [name ] = doc
737738
738- LANGUAGES = ['zh_CN' , 'zh_TW' , 'de' , 'fr' , 'ja' , 'ko' , 'es' , 'ru' ]
739-
740739def collectOptions (elem ):
741740 """Collect all option IDs from config.xml."""
742741 options = set ()
742+ optionsWithElems = {}
743743 for group in elem .getElementsByTagName ('group' ):
744744 for option in group .getElementsByTagName ('option' ):
745745 optionId = option .getAttribute ('id' )
746- if optionId :
746+ optionType = option .getAttribute ('type' )
747+ if optionId and optionType != 'obsolete' :
747748 options .add (optionId )
748- return options
749-
750- def collectOptionsWithElements (elem ):
751- """Collect all option IDs and their elements from config.xml."""
752- options = {}
753- for group in elem .getElementsByTagName ('group' ):
754- for option in group .getElementsByTagName ('option' ):
755- optionId = option .getAttribute ('id' )
756- if optionId :
757- options [optionId ] = option
758- return options
749+ optionsWithElems [optionId ] = option
750+ return (options ,optionsWithElems )
759751
760752def syncLocalizedConfig (elem , translationsDir , autoSync = False ):
761753 """Sync localized config_xx.xml files with original config.xml.
762-
754+
763755 Args:
764756 elem: The root element of config.xml
765757 translationsDir: Path to translations directory
766758 autoSync: If True, automatically sync; if False, only report differences
767759 """
768760 import os
769761 import shutil
770-
771- existingOptions = collectOptions (elem )
772- existingOptionsWithElements = collectOptionsWithElements (elem )
773- print ("Found %d options in config.xml" % len (existingOptions ))
774-
762+
763+ existingOptions , existingOptionsWithElements = collectOptions (elem )
764+ print ("Found %d active options in config.xml" % len (existingOptions ))
765+
775766 srcDir = os .path .dirname (translationsDir )
776767 if os .path .basename (srcDir ) == 'addon' :
777768 srcDir = os .path .dirname (srcDir )
778769 srcDir = os .path .join (srcDir , 'src' )
779-
780- for lang in LANGUAGES :
781- configFile = os .path .join (srcDir , "config_%s.xml" % lang )
782-
770+
771+ for configFile in sorted (glob .glob ("translations/config_*.xml" )):
772+
783773 if not os .path .exists (configFile ):
784- print ("Skipping %s: config file not found" % lang )
774+ print ("Skipping %s: config file not found" % configFile )
785775 continue
786-
787- print ("Processing %s..." % lang )
788-
776+
777+ print ("Processing %s..." % configFile )
778+
789779 try :
790780 with io .open (configFile , 'r' , encoding = 'utf8' ) as f :
791781 content = f .read ()
792782 langDoc = xml .dom .minidom .parseString (content )
793783 except Exception as e :
794784 print (" Error parsing %s: %s" % (configFile , e ))
795785 continue
796-
786+
797787 langOptions = set ()
798788 langOptionsWithElements = {}
799789 for group in langDoc .getElementsByTagName ('group' ):
@@ -802,64 +792,64 @@ def syncLocalizedConfig(elem, translationsDir, autoSync=False):
802792 if optionId :
803793 langOptions .add (optionId )
804794 langOptionsWithElements [optionId ] = option
805-
795+
806796 missingOptions = existingOptions - langOptions
807797 extraOptions = langOptions - existingOptions
808-
798+
809799 if missingOptions :
810800 print (" Missing %d options: %s" % (len (missingOptions ), ', ' .join (sorted (list (missingOptions ))[:5 ])))
811801 if len (missingOptions ) > 5 :
812802 print (" ... and %d more" % (len (missingOptions ) - 5 ))
813-
803+
814804 if extraOptions :
815805 print (" Extra %d options (not in original): %s" % (len (extraOptions ), ', ' .join (sorted (list (extraOptions ))[:5 ])))
816806 if len (extraOptions ) > 5 :
817807 print (" ... and %d more" % (len (extraOptions ) - 5 ))
818-
808+
819809 if not missingOptions and not extraOptions :
820810 print (" OK - all options synchronized" )
821811 continue
822-
812+
823813 if autoSync and (missingOptions or extraOptions ):
824814 print (" Auto-syncing..." )
825-
815+
826816 rootElement = langDoc .documentElement
827-
817+
828818 for optionId in extraOptions :
829819 optionElem = langOptionsWithElements [optionId ]
830820 parentGroup = optionElem .parentNode
831821 parentGroup .removeChild (optionElem )
832822 print (" Removed: %s" % optionId )
833-
823+
834824 for optionId in missingOptions :
835825 optionElem = existingOptionsWithElements [optionId ]
836826 importedElem = langDoc .importNode (optionElem , True )
837-
827+
838828 parentGroup = None
839829 for group in rootElement .getElementsByTagName ('group' ):
840830 parentGroup = group
841831 break
842-
832+
843833 if parentGroup :
844834 parentGroup .appendChild (importedElem )
845835 print (" Added: %s" % optionId )
846-
836+
847837 backupFile = configFile + ".bak"
848838 shutil .copy2 (configFile , backupFile )
849-
839+
850840 outputContent = langDoc .toprettyxml (indent = ' ' , encoding = 'utf-8' )
851841 outputStr = outputContent .decode ('utf-8' ) if isinstance (outputContent , bytes ) else outputContent
852-
842+
853843 lines = outputStr .split ('\n ' )
854844 filteredLines = [line for line in lines if line .strip ()]
855845 outputStr = '\n ' .join (filteredLines )
856-
846+
857847 with io .open (configFile , 'w' , encoding = 'utf8' ) as f :
858848 f .write (outputStr )
859-
849+
860850 print (" Backup saved to: %s" % backupFile )
861851 print (" File updated: %s" % configFile )
862-
852+
863853 print ("\n Sync %s!" % ("complete" if not autoSync else "and update complete" ))
864854
865855def main ():
@@ -1104,7 +1094,7 @@ def main():
11041094 parseGroupCDocs (n )
11051095 print ("}" )
11061096 elif (sys .argv [1 ] == "-sync" ):
1107- if len (sys .argv ) < 4 :
1097+ if len (sys .argv ) < 3 :
11081098 sys .exit ('Usage: %s -sync config.xml translations_dir [--auto]' % sys .argv [0 ])
11091099 translationsDir = sys .argv [3 ]
11101100 autoSync = '--auto' in sys .argv
0 commit comments