Ejemplo n.º 1
0
def main(unused_argv):
    if not (FLAGS.api_name or FLAGS.input):
        raise app.UsageError('You must specify one of --api_name or --input')
    if FLAGS.api_name and FLAGS.input:
        raise app.UsageError(
            'You can only specify one of --api_name or --input')
    if not (FLAGS.output_dir or FLAGS.output_file):
        raise app.UsageError(
            'You must specify one of --output_dir or --output_file')
    if FLAGS.output_dir and FLAGS.output_file:
        raise app.UsageError(
            'You can only specify one of --output_dir or --output_file')

    if FLAGS.verbose:
        logging.basicConfig(level=logging.DEBUG)

    # Get the discovery document
    if FLAGS.api_name:
        if not FLAGS.api_version:
            raise app.UsageError(
                'You must specify --api_version with --api_name')
        content = GetApiDiscovery(FLAGS.api_name, FLAGS.api_version)
    else:
        f = open(FLAGS.input)
        content = f.read()
        f.close()
    discovery_doc = json.loads(content,
                               object_pairs_hook=collections.OrderedDict)

    package_writer = package_writer_foundry.GetPackageWriter(
        output_dir=FLAGS.output_dir,
        output_file=FLAGS.output_file,
        output_format=FLAGS.output_format)

    Generate(discovery_doc=discovery_doc,
             package_writer=package_writer,
             include_timestamp=FLAGS.include_timestamp,
             version_package=FLAGS.version_package,
             package_path=FLAGS.package_path,
             output_type=FLAGS.output_type,
             language=FLAGS.language,
             language_variant=FLAGS.language_variant)
    return 0
Ejemplo n.º 2
0
def main(unused_argv):
    if not FLAGS.discovery:
        raise app.UsageError('You must specify --discovery')
    if not (FLAGS.output_dir or FLAGS.output_file):
        raise app.UsageError(
            'You must specify one of --output_dir or --output_file')
    if not FLAGS.templates:
        raise app.UsageError('You must specify --templates')

    f = open(FLAGS.discovery)
    discovery_doc = json.loads(f.read())
    f.close()

    options = {
        # Include other files needed to compile (e.g. base jar files)
        'include_dependencies': False,
        # Include the timestamp in the generated library
        'include_timestamp': FLAGS.include_timestamp,
        # Put API version in the package
        'version_package': FLAGS.version_package,
    }
    if FLAGS.output_type == 'full':
        options['include_dependencies'] = True

    # try to determine what generator to use from the language variant.
    language_variants = Targets().VariationsForLanguage(FLAGS.language)
    if language_variants:
        features = language_variants.GetFeatures(FLAGS.language_variant)
        template_dir = features.template_dir
        generator_name = features.get('generator', FLAGS.language)
    else:
        # Not described by targets.json. The template dir should just be down
        # in the language specific folder
        template_dir = os.path.join(os.path.dirname(__file__), 'languages',
                                    FLAGS.language)
        features = None
        generator_name = FLAGS.language

    # Instantiate the right code generator
    lang_model = None
    try:
        if FLAGS.language == 'any':
            api = Api(discovery_doc)
            # TODO(user): A default language model should be built in to the
            #   templates
            lang_model = language_model.DocumentingLanguageModel()
        else:
            generator_class = generator_lookup.GetGeneratorByLanguage(
                generator_name)
            generator_instance = generator_class(discovery_doc)
            api = generator_instance.api
            lang_model = generator_instance.language_model

    except ValueError:
        raise app.UsageError('Unsupported language option: %s' %
                             FLAGS.language)

    api.VisitAll(lambda o: o.SetLanguageModel(lang_model))
    gen = TemplateExpander(api, options=options)

    if features:
        gen.SetFeatures(features)

    template_dir = os.path.join(template_dir, FLAGS.templates)
    if not os.path.isdir(template_dir):
        raise app.UsageError('Can not find template tree at: %s' %
                             template_dir)
    gen.SetTemplateDir(template_dir)

    # Get an output writer
    package_writer = package_writer_foundry.GetPackageWriter(
        output_dir=FLAGS.output_dir,
        output_file=FLAGS.output_file,
        output_format=FLAGS.output_format)

    # do it
    gen.GeneratePackage(package_writer)
    package_writer.DoneWritingArchive()
    return 0