def _BuildSchemaDefinitions(self): """Loop over the schemas in the discovery doc and build definitions.""" schemas = self.values.get('schemas') if schemas: for name, def_dict in schemas.iteritems(): # Upgrade the string format schema to a dict. if isinstance(def_dict, unicode): def_dict = simplejson.loads(def_dict) self._schemas[name] = self.DataTypeFromJson(def_dict, name)
def ApiFromDiscoveryDoc(self, path): """Load a discovery doc from a file and creates a library Api. Args: path: (str) The path to the document. Returns: An Api for that document. """ f = open(os.path.join(os.path.dirname(__file__), 'testdata', path)) discovery_doc = simplejson.loads(f.read()) f.close() return Api(discovery_doc)
def __init__(self, targets_path=None): """Constructor. Loads targets file. Args: targets_path: (str) Path to targets file. Defaults to './targets.json' Raises: ValueError: if the targets file does not contain the required sections. """ if not targets_path: targets_path = os.path.join(os.path.dirname(__file__), 'targets.json') targets_file = open(targets_path) self._targets_dict = simplejson.loads(targets_file.read()) targets_file.close() # Do some basic validation that this has the required fields if ('languages' not in self._targets_dict or 'platforms' not in self._targets_dict): raise ValueError('languages or platforms is not in targets.json')
def testMakeDefaultSchemaNameFromTheDictTag(self): """Use the outer tag as id for schemas which have no id in their dict.""" discovery_doc = simplejson.loads( """ { "name": "fake", "version": "v1", "schemas": { "should_use_id": { "id": "named", "type": "object", "properties": { "dummy": { "type": "string" } } }, "unnamed": { "type": "object", "properties": { "dummy": { "type": "string" } } } }, "resources": {} } """) gen = Api(discovery_doc) self.assertTrue('Named' in gen._schemas) self.assertTrue('Unnamed' in gen._schemas)
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 not (FLAGS.output_dir or FLAGS.output_file): raise app.UsageError( 'You must specify one of --output_dir or --output_file') # Get the discovery document if FLAGS.api_name: if not FLAGS.api_version: raise app.UsageError('You must specify --api_version with --api_name') api_path = 'apis/%s/%s/rest' % (FLAGS.api_name, FLAGS.api_version) discovery_url = 'https://%s/discovery/%s/%s' % ( FLAGS.discovery_server, FLAGS.discovery_version, api_path) http = httplib2.Http() unused_resp, content = http.request(discovery_url) discovery_doc = simplejson.loads(content) error = discovery_doc.get('error') if error: raise app.Error(error) else: f = open(FLAGS.input) discovery_doc = simplejson.loads(f.read()) f.close() options = { # Emit a manifest file like a source jar 'emit_manifest': False, # Include other files needed to compile (e.g. base jar files) 'include_dependencies': True, # WARNING - change to false before commit # Put all the sources into a source jar 'include_source_jar': False, # Include the timestamp in the generated library 'include_timestamp': FLAGS.include_timestamp, # Prefix paths in the output with the library name 'use_library_name_in_path': False, # Put API version in the package 'version_package': FLAGS.version_package, } if FLAGS.output_type == 'full': options['emit_manifest'] = True options['include_dependencies'] = True options['include_source_jar'] = True options['use_library_name_in_path'] = True # Instantiate the right code generator generators = { 'csharp': CSharpGenerator, 'go': GoGenerator, 'gwt': GwtGenerator, 'java': JavaGenerator, 'objc': ObjCGenerator, 'php': PHPGenerator, } if FLAGS.language in generators: generator = generators[FLAGS.language](discovery_doc, options=options) else: raise app.UsageError('Unsupported language option: %s' % FLAGS.language) # Get the path to the template set. language_variants = Targets().TargetsForLanguage(FLAGS.language) variant_features = language_variants[FLAGS.language_variant] template_dir = os.path.join( os.path.dirname(__file__), FLAGS.language, variant_features['path']) generator.SetTemplateDir(template_dir) generator.SetSurfaceFeatures(variant_features) # Get an output writer if FLAGS.output_dir: package_writer = FilesystemLibraryPackage(FLAGS.output_dir) else: out = open(FLAGS.output_file, 'w') package_writer = ZipLibraryPackage(out) if options.get('emit_manifest'): package_writer.IncludeMinimalJarManifest( created_by='1.0.0-googleapis-v1 (Google Inc.)') # do it generator.GeneratePackage(package_writer) package_writer.DoneWritingArchive() if FLAGS.output_file: out.close() return 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 not (FLAGS.output_dir or FLAGS.output_file): raise app.UsageError( 'You must specify one of --output_dir or --output_file') # Get the discovery document if FLAGS.api_name: if not FLAGS.api_version: raise app.UsageError( 'You must specify --api_version with --api_name') api_path = 'apis/%s/%s/rest' % (FLAGS.api_name, FLAGS.api_version) discovery_url = 'https://%s/discovery/%s/%s' % ( FLAGS.discovery_server, FLAGS.discovery_version, api_path) http = httplib2.Http() unused_resp, content = http.request(discovery_url) discovery_doc = simplejson.loads(content) error = discovery_doc.get('error') if error: raise app.Error(error) else: f = open(FLAGS.input) discovery_doc = simplejson.loads(f.read()) f.close() options = { # Emit a manifest file like a source jar 'emit_manifest': False, # Include other files needed to compile (e.g. base jar files) 'include_dependencies': True, # WARNING - change to false before commit # Put all the sources into a source jar 'include_source_jar': False, # Include the timestamp in the generated library 'include_timestamp': FLAGS.include_timestamp, # Prefix paths in the output with the library name 'use_library_name_in_path': False, # Put API version in the package 'version_package': FLAGS.version_package, } if FLAGS.output_type == 'full': options['emit_manifest'] = True options['include_dependencies'] = True options['include_source_jar'] = True options['use_library_name_in_path'] = True # Instantiate the right code generator generators = { 'csharp': CSharpGenerator, 'go': GoGenerator, 'gwt': GwtGenerator, 'java': JavaGenerator, 'objc': ObjCGenerator, 'php': PHPGenerator, } if FLAGS.language in generators: generator = generators[FLAGS.language](discovery_doc, options=options) else: raise app.UsageError('Unsupported language option: %s' % FLAGS.language) # Get the path to the template set. language_variants = Targets().TargetsForLanguage(FLAGS.language) variant_features = language_variants[FLAGS.language_variant] template_dir = os.path.join(os.path.dirname(__file__), FLAGS.language, variant_features['path']) generator.SetTemplateDir(template_dir) generator.SetSurfaceFeatures(variant_features) # Get an output writer if FLAGS.output_dir: package_writer = FilesystemLibraryPackage(FLAGS.output_dir) else: out = open(FLAGS.output_file, 'w') package_writer = ZipLibraryPackage(out) if options.get('emit_manifest'): package_writer.IncludeMinimalJarManifest( created_by='1.0.0-googleapis-v1 (Google Inc.)') # do it generator.GeneratePackage(package_writer) package_writer.DoneWritingArchive() if FLAGS.output_file: out.close() return 0