parameters: (po_dir, output_path, input_glossary, output_glossary, only_locale)
return_statement: return 0
docstring: Main loop.
  
      Uses poterminology from the Translate Toolkit to collect glossary entries for all files in 'po_dir' for the given 'only_locale'. If 'only_locale' = "all", processes all locales. Then reads the <input_glossary>, adds new entries that were obtained by the glossary generation if there are any gaps, and then writes the results to <output_glossary>.
docstring_summary: Main loop.
docstring_tokens: Main loop .
function: def generate_glossary(po_dir, output_path, input_glossary, output_glossary, only_locale):
      """Main loop.
  
      Uses poterminology from the Translate Toolkit to collect glossary entries for all files in 'po_dir' for the given 'only_locale'. If 'only_locale' = "all", processes all locales. Then reads the <input_glossary>, adds new entries that were obtained by the glossary generation if there are any gaps, and then writes the results to <output_glossary>.
      """
  
      # Find the locale files to process
      print('Locale: ' + only_locale)
      locales = []
      glossaries = defaultdict(list)
  
      if only_locale != 'all':
          locales.append(only_locale)
      else:
          # Get locales from the Transifex glossary file
          header_row = read_csv_file(input_glossary)[0]
          regex = re.compile('^(translation_)(.+)$')
          for header in header_row:
              match = regex.match(header)
              if match:
                  locales.append(match.group(2))
  
      temp_path = make_path(output_path, 'temp_glossary')
  
      for locale in locales:
          print('Processing locale: ' + locale)
          # Generate the pot glossary
          input_path = po_dir + '/*/' + locale + '.po'
          pot_path = os.path.join(temp_path, 'glossary_' + locale + '.po')
  
          try:
              # We need shell=True for the wildcards.
              poterminology_result = subprocess.run(
                  ['poterminology ' + input_path + ' -o ' + pot_path], capture_output=True, shell=True, text=True, check=True)
              if poterminology_result.returncode != 0:
                  print('Error running poterminology:\n  FILE: ' + input_path + '\n  OUTPUT PATH: ' +
                        output_path + '\n  ' + poterminology_result.stderr)
                  return False
  
          except subprocess.CalledProcessError:
              print('Failed to run poterminology:\n  FILE: ' + input_path + '\n  OUTPUT PATH: ' +
                    output_path + '\n  ' + poterminology_result.stderr)
              return False
  
          # Convert to csv for easy parsing
          csv_file = os.path.join(temp_path, 'glossary_' + locale + '.csv')
          subprocess.run(['po2csv', '--progress=none',
                          pot_path, csv_file], check=True)
          # The po file is no longer needed, delete it.
          os.remove(pot_path)
  
          # Some translations were removed but are still part of the glossary
          if not os.path.isfile(csv_file):
              print('No translations found for locale: ', locale)
              continue
  
          transifex_glossary = load_transifex_glossary(input_glossary, locale)
          extracted_glossary = load_extracted_glossary(csv_file, locale)
  
          # Add generated translation if necessary
          for key in transifex_glossary.keys():
              if transifex_glossary[key].translation == '' and key in extracted_glossary:
                  extracted_entry = extracted_glossary[key]
                  if extracted_entry.translation != '':
                      transifex_entry = transifex_glossary[key]
                      transifex_entry.translation = extracted_entry.translation
                      transifex_entry.translation_comment = 'AUTOGENERATED - PLEASE PROOFREAD!'
                      transifex_glossary[key] = transifex_entry
          glossaries[locale] = transifex_glossary
  
      # Now collect the date for the global csv file
      # Write header
      print('Writing results to ' + output_glossary)
      result = 'term,pos,comment,'
      for locale in locales:
          result = result + 'translation_' + locale + ','
          result = result + 'comment_' + locale + ','
      result = result[0:-1] + '\n'
  
      source_terms = load_transifex_source_terms(input_glossary)
      # Collect all translations for each source term
      for key in source_terms:
          result = result + '"%s","%s","%s",' % (source_terms[key].term.replace(
              '"', '""'), source_terms[key].wordclass.replace('"', '""'), source_terms[key].term_comment.replace('"', '""'))
          for locale in locales:
              glossary = glossaries[locale]
              translation = ''
              translation_comment = ''
              if key in glossary:
                  translation = glossary[key].translation.replace('"', '""')
                  translation_comment = glossary[
                      key].translation_comment.replace('"', '""')
              result = result + \
                  '"%s","%s",' % (translation, translation_comment)
          result = result[0:-1] + '\n'
  
      # Now write the file.
      with open(output_glossary, 'wt') as dest_file:
          dest_file.write(result)
  
      # Cleanup.
      delete_path(temp_path)
      if not os.listdir(output_path):
          os.rmdir(output_path)
      print('Done.')
      return 0
function_tokens: def generate_glossary ( po_dir , output_path , input_glossary , output_glossary , only_locale ) : # Find the locale files to process print ( 'Locale: ' + only_locale ) locales = [ ] glossaries = defaultdict ( list ) if only_locale != 'all' : locales . append ( only_locale ) else : # Get locales from the Transifex glossary file header_row = read_csv_file ( input_glossary ) [ 0 ] regex = re . compile ( '^(translation_)(.+)$' ) for header in header_row : match = regex . match ( header ) if match : locales . append ( match . group ( 2 ) ) temp_path = make_path ( output_path , 'temp_glossary' ) for locale in locales : print ( 'Processing locale: ' + locale ) # Generate the pot glossary input_path = po_dir + '/*/' + locale + '.po' pot_path = os . path . join ( temp_path , 'glossary_' + locale + '.po' ) try : # We need shell=True for the wildcards. poterminology_result = subprocess . run ( [ 'poterminology ' + input_path + ' -o ' + pot_path ] , capture_output = True , shell = True , text = True , check = True ) if poterminology_result . returncode != 0 : print ( 'Error running poterminology:\n  FILE: ' + input_path + '\n  OUTPUT PATH: ' + output_path + '\n  ' + poterminology_result . stderr ) return False except subprocess . CalledProcessError : print ( 'Failed to run poterminology:\n  FILE: ' + input_path + '\n  OUTPUT PATH: ' + output_path + '\n  ' + poterminology_result . stderr ) return False # Convert to csv for easy parsing csv_file = os . path . join ( temp_path , 'glossary_' + locale + '.csv' ) subprocess . run ( [ 'po2csv' , '--progress=none' , pot_path , csv_file ] , check = True ) # The po file is no longer needed, delete it. os . remove ( pot_path ) # Some translations were removed but are still part of the glossary if not os . path . isfile ( csv_file ) : print ( 'No translations found for locale: ' , locale ) continue transifex_glossary = load_transifex_glossary ( input_glossary , locale ) extracted_glossary = load_extracted_glossary ( csv_file , locale ) # Add generated translation if necessary for key in transifex_glossary . keys ( ) : if transifex_glossary [ key ] . translation == '' and key in extracted_glossary : extracted_entry = extracted_glossary [ key ] if extracted_entry . translation != '' : transifex_entry = transifex_glossary [ key ] transifex_entry . translation = extracted_entry . translation transifex_entry . translation_comment = 'AUTOGENERATED - PLEASE PROOFREAD!' transifex_glossary [ key ] = transifex_entry glossaries [ locale ] = transifex_glossary # Now collect the date for the global csv file # Write header print ( 'Writing results to ' + output_glossary ) result = 'term,pos,comment,' for locale in locales : result = result + 'translation_' + locale + ',' result = result + 'comment_' + locale + ',' result = result [ 0 : - 1 ] + '\n' source_terms = load_transifex_source_terms ( input_glossary ) # Collect all translations for each source term for key in source_terms : result = result + '"%s","%s","%s",' % ( source_terms [ key ] . term . replace ( '"' , '""' ) , source_terms [ key ] . wordclass . replace ( '"' , '""' ) , source_terms [ key ] . term_comment . replace ( '"' , '""' ) ) for locale in locales : glossary = glossaries [ locale ] translation = '' translation_comment = '' if key in glossary : translation = glossary [ key ] . translation . replace ( '"' , '""' ) translation_comment = glossary [ key ] . translation_comment . replace ( '"' , '""' ) result = result + '"%s","%s",' % ( translation , translation_comment ) result = result [ 0 : - 1 ] + '\n' # Now write the file. with open ( output_glossary , 'wt' ) as dest_file : dest_file . write ( result ) # Cleanup. delete_path ( temp_path ) if not os . listdir ( output_path ) : os . rmdir ( output_path ) print ( 'Done.' ) return 0