()
Parses arguments and iterates over files.
Parses arguments and iterates over files.
Parses arguments and iterates over files .
def main():
    """Parses arguments and iterates over files."""

    # Set up argument parser.
    parser = argparse.ArgumentParser(description='Convert JSON files to JS.')
    parser.add_argument('--source_lang', default='en',
                        help='ISO 639-1 source language code')
    parser.add_argument('--output_dir', default='generated',
                        help='relative directory for output files')
    parser.add_argument('--key_file', default='json' + os.path.sep + 'keys.json',
                        help='relative path to input keys file')
    parser.add_argument('--template', default='template.soy')
    parser.add_argument('--path_to_jar',
                        default='..' + os.path.sep + 'apps' + os.path.sep
                        + '_soy',
                        help='relative path from working directory to '
                        'SoyToJsSrcCompiler.jar')
    parser.add_argument('files', nargs='+', help='input files')

    # Initialize global variables.
    global args
    args = parser.parse_args()

    # Make sure output_dir ends with slash.
    if (not args.output_dir.endswith(os.path.sep)):
      args.output_dir += os.path.sep

    # Read in keys.json, mapping descriptions (e.g., Maze.turnLeft) to
    # Closure keys (long hash numbers).
    key_file = open(args.key_file)
    key_dict = json.load(key_file)
    key_file.close()

    # Process each input file.
    print('Creating .xlf files...')
    processed_langs = []
    if len(args.files) == 1:
      # Windows does not expand globs automatically.
      args.files = glob.glob(args.files[0])
    for arg_file in args.files:
      (path_to_json, filename) = os.path.split(arg_file)
      if not filename.endswith('.json'):
        raise InputError(filename, 'filenames must end with ".json"')
      target_lang = filename[:filename.index('.')]
      if not target_lang in ('qqq', 'keys'):
        processed_langs.append(target_lang)
        _process_file(path_to_json, target_lang, key_dict)

    # Output command line for Closure compiler.
    if processed_langs:
      print('Creating .js files...')
      processed_lang_list = ','.join(processed_langs)
      subprocess.check_call([
          'java',
          '-jar', os.path.join(args.path_to_jar, 'SoyToJsSrcCompiler.jar'),
          '--locales', processed_lang_list,
          '--messageFilePathFormat', args.output_dir + '{LOCALE}.xlf',
          '--outputPathFormat', args.output_dir + '{LOCALE}.js',
          '--srcs', args.template])
      if len(processed_langs) == 1:
        print('Created ' + processed_lang_list + '.js in ' + args.output_dir)
      else:
        print('Created {' + processed_lang_list + '}.js in ' + args.output_dir)

      for lang in processed_langs:
        os.remove(args.output_dir + lang + '.xlf')
      print('Removed .xlf files.')
def main ( ) : # Set up argument parser. parser = argparse . ArgumentParser ( description = 'Convert JSON files to JS.' ) parser . add_argument ( '--source_lang' , default = 'en' , help = 'ISO 639-1 source language code' ) parser . add_argument ( '--output_dir' , default = 'generated' , help = 'relative directory for output files' ) parser . add_argument ( '--key_file' , default = 'json' + os . path . sep + 'keys.json' , help = 'relative path to input keys file' ) parser . add_argument ( '--template' , default = 'template.soy' ) parser . add_argument ( '--path_to_jar' , default = '..' + os . path . sep + 'apps' + os . path . sep + '_soy' , help = 'relative path from working directory to ' 'SoyToJsSrcCompiler.jar' ) parser . add_argument ( 'files' , nargs = '+' , help = 'input files' ) # Initialize global variables. global args args = parser . parse_args ( ) # Make sure output_dir ends with slash. if ( not args . output_dir . endswith ( os . path . sep ) ) : args . output_dir += os . path . sep # Read in keys.json, mapping descriptions (e.g., Maze.turnLeft) to # Closure keys (long hash numbers). key_file = open ( args . key_file ) key_dict = json . load ( key_file ) key_file . close ( ) # Process each input file. print ( 'Creating .xlf files...' ) processed_langs = [ ] if len ( args . files ) == 1 : # Windows does not expand globs automatically. args . files = glob . glob ( args . files [ 0 ] ) for arg_file in args . files : ( path_to_json , filename ) = os . path . split ( arg_file ) if not filename . endswith ( '.json' ) : raise InputError ( filename , 'filenames must end with ".json"' ) target_lang = filename [ : filename . index ( '.' ) ] if not target_lang in ( 'qqq' , 'keys' ) : processed_langs . append ( target_lang ) _process_file ( path_to_json , target_lang , key_dict ) # Output command line for Closure compiler. if processed_langs : print ( 'Creating .js files...' ) processed_lang_list = ',' . join ( processed_langs ) subprocess . check_call ( [ 'java' , '-jar' , os . path . join ( args . path_to_jar , 'SoyToJsSrcCompiler.jar' ) , '--locales' , processed_lang_list , '--messageFilePathFormat' , args . output_dir + '{LOCALE}.xlf' , '--outputPathFormat' , args . output_dir + '{LOCALE}.js' , '--srcs' , args . template ] ) if len ( processed_langs ) == 1 : print ( 'Created ' + processed_lang_list + '.js in ' + args . output_dir ) else : print ( 'Created {' + processed_lang_list + '}.js in ' + args . output_dir ) for lang in processed_langs : os . remove ( args . output_dir + lang + '.xlf' ) print ( 'Removed .xlf files.' )