def main()

in generate_xml_from_google_services_json.py [0:0]


def main():
  parser = argparse.ArgumentParser(
      description=((
          'Converts a Firebase %s into %s similar to the Gradle plugin, or '
          'converts a Firebase %s into a %s suitible for use on desktop apps.' %
          (DEFAULT_INPUT_FILENAME, DEFAULT_OUTPUT_FILENAME,
           DEFAULT_PLIST_INPUT_FILENAME, DEFAULT_JSON_OUTPUT_FILENAME))))
  parser.add_argument('-i', help='Override input file name',
                      metavar='FILE', required=False)
  parser.add_argument('-o', help='Override destination file name',
                      metavar='FILE', required=False)
  parser.add_argument('-p', help=('Package ID to select within the set of '
                                  'packages in the input file.  If this is '
                                  'not specified, the first package in the '
                                  'input file is selected.'))
  parser.add_argument('-l', help=('List all package IDs referenced by the '
                                  'input file.  If this is specified, '
                                  'the output file is not created.'),
                      action='store_true', default=False, required=False)
  parser.add_argument('-f', help=('Print project fields from the input file '
                                  'in the form \'name=value\\n\' for each '
                                  'field.  If this is specified, the output '
                                  'is not created.'),
                      action='store_true', default=False, required=False)
  parser.add_argument(
      '--plist',
      help=(
          'Specifies a plist file to convert to a JSON configuration file. '
          'If this is enabled, the script will expect a .plist file as input, '
          'which it will convert into %s file.  The output file is '
          '*not* suitable for use with Firebase on Android.' %
          (DEFAULT_JSON_OUTPUT_FILENAME)),
      action='store_true',
      default=False,
      required=False)

  # python 2 on Windows doesn't handle unicode arguments well, so we need to
  # pre-process the command line arguments before trying to parse them.
  if platform.system() == 'Windows':
    sys.argv = argv_as_unicode_win32()

  args = parser.parse_args()

  if args.plist:
    input_filename = DEFAULT_PLIST_INPUT_FILENAME
    output_filename = DEFAULT_JSON_OUTPUT_FILENAME
  else:
    input_filename = DEFAULT_INPUT_FILENAME
    output_filename = DEFAULT_OUTPUT_FILENAME

  if args.i:
    # Encode the input string (type unicode) as a normal string (type str)
    # using the 'utf-8' encoding so that it can be worked with the same as
    # input names from other sources (like the defaults).
    input_filename_raw = args.i.encode('utf-8')
    # Decode the filename to a unicode string using the 'utf-8' encoding to
    # properly handle filepaths with unicode characters in them.
    input_filename = input_filename_raw.decode('utf-8')

  if args.o:
    output_filename = args.o

  with open(input_filename, 'r') as ifile:
    file_string = ifile.read()

  json_string = None
  if args.plist:
    json_string = convert_plist_to_json(file_string, input_filename)
    if json_string is None:
      return 1
    jsobj = json.loads(json_string)
  else:
    jsobj = json.loads(file_string)

  root = ElementTree.Element('resources')
  root.set('xmlns:tools', 'http://schemas.android.com/tools')

  project_info = jsobj.get('project_info')
  if project_info:
    gen_string(root, 'firebase_database_url', project_info.get('firebase_url'))
    gen_string(root, 'gcm_defaultSenderId', project_info.get('project_number'))
    gen_string(root, 'google_storage_bucket',
               project_info.get('storage_bucket'))
    gen_string(root, 'project_id', project_info.get('project_id'))

  if args.f:
    if not project_info:
      sys.stderr.write('No project info found in %s.' % input_filename)
      return 1
    for field, value in sorted(project_info.items()):
      sys.stdout.write('%s=%s\n' % (field, value))
    return 0

  packages = set()
  client_list = jsobj.get('client')
  if client_list:
    # Search for the user specified package in the file.
    selected_package_name = ''
    selected_client = client_list[0]
    find_package_name = args.p
    for client in client_list:
      package_name = client.get('client_info', {}).get(
          'android_client_info', {}).get('package_name', '')
      if not package_name:
        package_name = client.get('oauth_client', {}).get(
            'android_info', {}).get('package_name', '')
      if package_name:
        if not selected_package_name:
          selected_package_name = package_name
          selected_client = client
        if package_name == find_package_name:
          selected_package_name = package_name
          selected_client = client
        packages.add(package_name)

    if args.p and selected_package_name != find_package_name:
      sys.stderr.write('No packages found in %s which match the package '
                       'name %s\n'
                       '\n'
                       'Found the following:\n'
                       '%s\n' % (input_filename, find_package_name,
                                 '\n'.join(packages)))
      return 1

    client_api_key = selected_client.get('api_key')
    if client_api_key:
      client_api_key0 = client_api_key[0]
      gen_string(root, 'google_api_key', client_api_key0.get('current_key'))
      gen_string(root, 'google_crash_reporting_api_key',
                 client_api_key0.get('current_key'))

    client_info = selected_client.get('client_info')
    if client_info:
      gen_string(root, 'google_app_id', client_info.get('mobilesdk_app_id'))

    # Only include the first matching OAuth client ID per type.
    client_id_web_parsed = False
    client_id_android_parsed = False

    oauth_client_list = selected_client.get('oauth_client')
    if oauth_client_list:
      for oauth_client in oauth_client_list:
        client_type = oauth_client.get('client_type')
        client_id = oauth_client.get('client_id')
        if not (client_type and client_id): continue
        if (client_type == OAUTH_CLIENT_TYPE_WEB and
            not client_id_web_parsed):
          gen_string(root, 'default_web_client_id', client_id)
          client_id_web_parsed = True
        if (client_type == OAUTH_CLIENT_TYPE_ANDROID_APP and
            not client_id_android_parsed):
          gen_string(root, 'default_android_client_id', client_id)
          client_id_android_parsed = True

    services = selected_client.get('services')
    if services:
      ads_service = services.get('ads_service')
      if ads_service:
        gen_string(root, 'test_banner_ad_unit_id',
                   ads_service.get('test_banner_ad_unit_id'))
        gen_string(root, 'test_interstitial_ad_unit_id',
                   ads_service.get('test_interstitial_ad_unit_id'))
      analytics_service = services.get('analytics_service')
      if analytics_service:
        analytics_property = analytics_service.get('analytics_property')
        if analytics_property:
          gen_string(root, 'ga_trackingId',
                     analytics_property.get('tracking_id'))
      # enable this once we have an example if this service being present
      # in the json data:
      maps_service_enabled = False
      if maps_service_enabled:
        maps_service = services.get('maps_service')
        if maps_service:
          maps_api_key = maps_service.get('api_key')
          if maps_api_key:
            for k in range(0, len(maps_api_key)):
              # generates potentially multiple of these keys, which is
              # the same behavior as the java plugin.
              gen_string(root, 'google_maps_key',
                         maps_api_key[k].get('maps_api_key'))

  tree = ElementTree.ElementTree(root)

  indent(root)

  if args.l:
    for package in sorted(packages):
      if package:
        sys.stdout.write(package + '\n')
  else:
    path = os.path.dirname(output_filename)

    if path and not os.path.exists(path):
      os.makedirs(path)

    if not args.plist:
      tree.write(output_filename, 'utf-8', True)
    else:
      with open(output_filename, 'w') as ofile:
        ofile.write(json_string)

  return 0