Example #1
0
    def _ShowCompiledFile(self, parsed_url, head, body):
        """Show the compiled version of a json file given the path to the compiled
    file.
    """

        api_model = model.Model()

        request_path = self._GetRequestPath(parsed_url)
        (file_root, file_ext) = os.path.splitext(request_path)
        (filedir, filename) = os.path.split(file_root)
        json_file_path = os.path.normpath(file_root + '.json')

        try:
            # Get main json file
            api_defs = json_schema.Load(json_file_path)
            namespace = api_model.AddNamespace(api_defs[0], json_file_path)
            type_generator = cpp_type_generator.CppTypeGenerator(
                'previewserver::api', namespace, namespace.unix_name)

            # Get json file depedencies
            for dependency in api_defs[0].get('dependencies', []):
                json_file_path = os.path.join(filedir, dependency + '.json')
                api_defs = json_schema.Load(json_file_path)
                referenced_namespace = api_model.AddNamespace(
                    api_defs[0], json_file_path)
                if referenced_namespace:
                    type_generator.AddNamespace(
                        referenced_namespace,
                        cpp_util.Classname(referenced_namespace.name).lower())

            # Generate code
            if file_ext == '.h':
                cpp_code = (h_generator.HGenerator(
                    namespace, type_generator).Generate().Render())
            elif file_ext == '.cc':
                cpp_code = (cc_generator.CCGenerator(
                    namespace, type_generator).Generate().Render())
            else:
                self.send_error(404, "File not found: %s" % request_path)
                return

            # Do highlighting on the generated code
            (highlighter_param,
             style_param) = self._GetHighlighterParams(parsed_url)
            head.Append('<style>' +
                        self.server.highlighters[highlighter_param].GetCSS(
                            style_param) + '</style>')
            body.Append(
                self.server.highlighters[highlighter_param].GetCodeElement(
                    cpp_code, style_param))
        except IOError:
            self.send_error(404, "File not found: %s" % request_path)
            return
        except (TypeError, KeyError, AttributeError, AssertionError,
                NotImplementedError) as error:
            body.Append('<pre>')
            body.Append('compiler error: ' + str(error))
            body.Append('Check server log for more details')
            body.Append('</pre>')
            raise
def _createCppBundleGenerator(file_path):
  json_object = json_schema.Load(file_path)
  model = Model()
  model.AddNamespace(json_object[0], file_path)
  cpp_bundle_generator = CppBundleGenerator(
      None, model, None, None, 'generated_api_schemas',
      None, None, None)
  return (cpp_bundle_generator, model)
Example #3
0
def load_schema(schema):
    schema_filename, schema_extension = os.path.splitext(schema)

    if schema_extension == '.json':
        api_defs = json_schema.Load(schema)
    elif schema_extension == '.idl':
        api_defs = idl_schema.Load(schema)
    else:
        sys.exit("Did not recognize file extension %s for schema %s" %
                 (schema_extension, schema))

    return api_defs
Example #4
0
  def LoadSchema(self, schema):
    schema_filename, schema_extension = os.path.splitext(schema)

    if schema_extension == '.json':
      api_defs = json_schema.Load(schema)
    elif schema_extension == '.idl':
      api_defs = idl_schema.Load(schema)
    else:
      sys.exit('Did not recognize file extension %s for schema %s' %
               (schema_extension, schema))
    if len(api_defs) != 1:
      sys.exit('File %s has multiple schemas. Files are only allowed to contain'
               'a single schema.' % schema)

    return api_defs
Example #5
0
    def LoadSchema(self, schema):
        '''Load a schema definition. The schema parameter must be a file name
    with the full path relative to the root.'''
        schema_filename, schema_extension = os.path.splitext(schema)

        schema_path = os.path.join(self._root, schema)
        if schema_extension == '.json':
            api_defs = json_schema.Load(schema_path)
        elif schema_extension == '.idl':
            api_defs = idl_schema.Load(schema_path)
        else:
            sys.exit('Did not recognize file extension %s for schema %s' %
                     (schema_extension, schema))

        return api_defs
Example #6
0
    def LoadSchema(self, schema):
        '''Load a schema definition. The schema parameter must be a file name
    with the full path relative to the root.'''
        _, schema_extension = os.path.splitext(schema)

        schema_path = os.path.join(self._root, schema)
        if schema_extension == '.json':
            api_defs = json_schema.Load(schema_path)
        elif schema_extension == '.idl':
            api_defs = idl_schema.Load(schema_path)
        else:
            sys.exit('Did not recognize file extension %s for schema %s' %
                     (schema_extension, schema))

        # TODO(devlin): This returns a list. Does it need to? Is it ever > 1?
        return api_defs
    def LoadSchema(self, schema):
        '''Load a schema definition. The schema parameter must be a file name
    without any path component - the file is loaded from the path defined by
    the real_path argument passed to the constructor.'''
        schema_filename, schema_extension = os.path.splitext(schema)

        schema_path = os.path.join(self._real_path, schema)
        if schema_extension == '.json':
            api_defs = json_schema.Load(schema_path)
        elif schema_extension == '.idl':
            api_defs = idl_schema.Load(schema_path)
        else:
            sys.exit('Did not recognize file extension %s for schema %s' %
                     (schema_extension, schema))

        return api_defs
Example #8
0
def handle_single_schema(filename, dest_dir, root, root_namespace):
  schema = os.path.normpath(filename)
  schema_filename, schema_extension = os.path.splitext(schema)
  path, short_filename = os.path.split(schema_filename)
  api_defs = json_schema.DeleteNocompileNodes(load_schema(schema))

  api_model = model.Model()

  for target_namespace in api_defs:
    referenced_schemas = target_namespace.get('dependencies', [])
    # Load type dependencies into the model.
    # TODO(miket): do we need this in IDL?
    for referenced_schema in referenced_schemas:
      referenced_schema_path = os.path.join(
          os.path.dirname(schema), referenced_schema + '.json')
      referenced_api_defs = json_schema.Load(referenced_schema_path)

      for namespace in referenced_api_defs:
        api_model.AddNamespace(namespace,
            os.path.relpath(referenced_schema_path, opts.root))

    # Gets the relative path from opts.root to the schema to correctly determine
    # the include path.
    relpath = os.path.relpath(schema, opts.root)
    namespace = api_model.AddNamespace(target_namespace, relpath)
    if not namespace:
      continue

    if short_filename != namespace.unix_name:
      sys.exit("Filename %s is illegal. Name files using unix_hacker style." %
               filename)

    # The output filename must match the input filename for gyp to deal with it
    # properly.
    out_file = namespace.unix_name
    type_generator = cpp_type_generator.CppTypeGenerator(
        root_namespace, namespace, namespace.unix_name)
    for referenced_namespace in api_model.namespaces.values():
      if referenced_namespace == namespace:
        continue
      type_generator.AddNamespace(
          referenced_namespace,
          referenced_namespace.unix_name)

    h_code = (h_generator.HGenerator(namespace, type_generator)
        .Generate().Render())
    cc_code = (cc_generator.CCGenerator(namespace, type_generator)
        .Generate().Render())

    if dest_dir:
      with open(
          os.path.join(dest_dir, namespace.source_file_dir, out_file + '.cc'),
          'w') as cc_file:
        cc_file.write(cc_code)
      with open(
          os.path.join(dest_dir, namespace.source_file_dir, out_file + '.h'),
          'w') as h_file:
        h_file.write(h_code)
    else:
      print '%s.h' % out_file
      print
      print h_code
      print
      print '%s.cc' % out_file
      print
      print cc_code