Ejemplo n.º 1
0
  def TearDownClass(cls):
    """Called after tests in an individual class have run."""

    # Check that all the test classes in module run
    if not cls._object_by_class_name:
      return
    for clazz in cls._latest_object.GetClasses():
      if clazz.__name__ not in cls._object_by_class_name:
        return

    # Collect all modules imported from file
    deps = set()
    for load_module_mock in cls._object_by_class_name.values():
      for module in load_module_mock.imported_modules:
        deps.add(cls._latest_object.GetDepString(module))

    filtered_deps = []
    if deps:
      deps = sorted(deps)
      for i in range(len(deps) - 1):
        if not deps[i + 1].startswith(deps[i]):
          filtered_deps.append(deps[i])
      filtered_deps.append(deps[-1])

    stream = io.StringIO()
    yaml_printer.YamlPrinter(stream).AddRecord(filtered_deps)
    deps = stream.getvalue()

    cls._latest_object.Finalize(deps)
Ejemplo n.º 2
0
 def _WriteSchema(self, message_name, spec):
   """Writes the schema in spec to the _GetSchemaFilePath() file."""
   tmp = io.StringIO()
   tmp.write('$schema: "http://json-schema.org/draft-06/schema#"\n\n')
   yaml_printer.YamlPrinter(
       name='yaml',
       projector=resource_projector.IdentityProjector(),
       out=tmp).Print(spec)
   content = re.sub('\n *{}\n'.format(_YAML_WORKAROUND), '\n', tmp.getvalue())
   file_path = self._GetSchemaFilePath(message_name)
   log.info('Generating JSON schema [{}].'.format(file_path))
   with files.FileWriter(file_path) as w:
     w.write(content)
Ejemplo n.º 3
0
def GetExportSchema(api, message_name, message_spec):
    """Returns the export/import YAML schema text for message_spec in api.

  Args:
    api: An API registry object.
    message_name: The message name in the api.
    message_spec: An arg_utils.GetRecursiveMessageSpec() message spec.

  Returns:
    The export/import YAML schema text for message_spec in api.
  """
    spec = _GenerateSchema(api, message_name, message_spec)
    tmp = io.StringIO()
    tmp.write('$schema: "http://json-schema.org/draft-06/schema#"\n\n')
    yaml_printer.YamlPrinter(name='yaml',
                             projector=resource_projector.IdentityProjector(),
                             out=tmp).Print(spec)
    return re.sub('\n *{}\n'.format(_YAML_WORKAROUND), '\n', tmp.getvalue())
Ejemplo n.º 4
0
 def testPrintOrderedDictPreserveOrder(self):
   yaml_printer.YamlPrinter(
       name='yaml',
       projector=resource_projector.IdentityProjector(),
   ).Print(self.ordered_dict_resource)
   self.AssertOutputEquals(textwrap.dedent("""\
       ---
       allowed:
       - IPProtocol: tcp
         ports:
         - '2376'
       description: ''
       name: allow-gae-builder
       id: '123456789'
       network: default
       sourceRanges:
       - 0.0.0.0/0
       kind: compute#firewall
       creationTimestamp: '2015-05-20T08:14:24.654-07:00'
       """))
Ejemplo n.º 5
0
 def Transform(self, registration):
     """Transform a registration into a YAML output."""
     yaml = yp.YamlPrinter()
     self._TransformKnownFields(yaml, registration)
     self._TransformRemainingFields(yaml, registration)
Ejemplo n.º 6
0
 def _PrintAsYaml(self, record):
     buffer = io.StringIO()
     printer = yp.YamlPrinter(buffer)
     printer.Print(record)
     return buffer.getvalue()