Python yaml.safe_dump_all() Examples

The following are 13 code examples of yaml.safe_dump_all(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module yaml , or try the search function .
Example #1
Source File: provision.py    From marketplace-k8s-app-tools with Apache License 2.0 6 votes vote down vote up
def main():
  parser = ArgumentParser(description=_PROG_HELP)
  schema_values_common.add_to_argument_parser(parser)
  parser.add_argument('--deployer_image', required=True)
  parser.add_argument('--deployer_entrypoint', default=None)
  parser.add_argument('--deployer_service_account_name', required=True)
  parser.add_argument('--version_repo', default=None)
  parser.add_argument('--image_pull_secret', default=None)
  args = parser.parse_args()

  schema = schema_values_common.load_schema(args)
  values = schema_values_common.load_values(args)
  manifests = process(
      schema,
      values,
      deployer_image=args.deployer_image,
      deployer_entrypoint=args.deployer_entrypoint,
      version_repo=args.version_repo,
      image_pull_secret=args.image_pull_secret,
      deployer_service_account_name=args.deployer_service_account_name)
  print(yaml.safe_dump_all(manifests, default_flow_style=False, indent=2)) 
Example #2
Source File: namespace.py    From hokusai with MIT License 6 votes vote down vote up
def create_new_app_yaml(source_file, app_name):
  yaml_spec = YamlSpec(source_file).to_file()
  with open(yaml_spec, 'r') as stream:
    try:
      yaml_content = list(yaml.load_all(stream))
    except yaml.YAMLError as exc:
      raise HokusaiError("Cannot read source yaml file %s." % source_file)

  for c in yaml_content: update_namespace(c, clean_string(app_name))

  new_namespace = OrderedDict([
      ('apiVersion', 'v1'),
      ('kind', 'Namespace'),
      ('metadata', {
        'name': clean_string(app_name)
      })
    ])
  yaml_content = [new_namespace] + yaml_content

  with open(os.path.join(CWD, HOKUSAI_CONFIG_DIR, "%s.yml" % app_name), 'w') as output:
    output.write(YAML_HEADER)
    yaml.safe_dump_all(yaml_content, output, default_flow_style=False)

  print_green("Created %s/%s.yml" % (HOKUSAI_CONFIG_DIR, app_name)) 
Example #3
Source File: settings.py    From edx-load-tests with Apache License 2.0 6 votes vote down vote up
def dump(self, stream):
        """
        Dump the generated settings file contents to the given stream.

        Arguments:
            stream (file):
                An open writable file object to dump settings into.
        """
        # only dump the secrets yaml document if it is populated
        docs_to_dump = [self.data]
        if self.secrets:
            docs_to_dump.append(self.secrets)

        yaml.safe_dump_all(
            docs_to_dump,
            stream=stream,
            default_flow_style=False,  # Represent objects using indented blocks
                                       # rather than inline enclosures.
            explicit_start=True,  # Begin the first document with '---', per
                                  # our usual settings file syntax.
        ) 
Example #4
Source File: nlu_test.py    From RobotAIEngine with Apache License 2.0 6 votes vote down vote up
def _generage(self):
        """
        第一次产生测试yaml的方法
        :return:
        """
        file_list = glob.glob('./test/conf/*')
        for file_name in file_list:
            yaml_info_list = load_from_yaml(file_name)
            print file_name
            yaml_list = []
            for yaml_info in yaml_info_list:
                match_dict_list = Nlu_Framework.match(force_utf8_new(yaml_info['input']))
                result_dict = {"input": force_utf8_new(yaml_info['input']),
                               "output": match_dict_list[0]}
                yaml_list.append(result_dict)
            yaml_list = force_utf8_new(yaml_list)
            print yaml.safe_dump_all(yaml_list, allow_unicode=True, encoding='utf-8') 
Example #5
Source File: windows_services.py    From plaso with Apache License 2.0 5 votes vote down vote up
def CompileReport(self, mediator):
    """Compiles an analysis report.

    Args:
      mediator (AnalysisMediator): mediates interactions between analysis
          plugins and other components, such as storage and dfvfs.

    Returns:
      AnalysisReport: report.
    """
    # TODO: move YAML representation out of plugin and into serialization.
    lines_of_text = []
    if self._output_format == 'yaml':
      lines_of_text.append(
          yaml.safe_dump_all(self._service_collection.services))
    else:
      lines_of_text.append('Listing Windows Services')
      for service in self._service_collection.services:
        lines_of_text.append(self._FormatServiceText(service))
        lines_of_text.append('')

    lines_of_text.append('')
    report_text = '\n'.join(lines_of_text)
    return reports.AnalysisReport(plugin_name=self.NAME, text=report_text)

  # pylint: disable=unused-argument 
Example #6
Source File: test_test_controller.py    From armada with Apache License 2.0 5 votes vote down vote up
def test_test_controller_validation_failure_returns_400(
            self, mock_tiller, mock_manifest):
        rules = {'armada:test_manifest': '@'}
        self.policy.set_rules(rules)

        manifest_path = os.path.join(
            os.getcwd(), 'examples', 'keystone-manifest.yaml')
        with open(manifest_path, 'r') as f:
            payload = f.read()

        documents = list(yaml.safe_load_all(payload))
        documents[0]['schema'] = 'totally-invalid'
        invalid_payload = yaml.safe_dump_all(documents)

        resp = self.app.simulate_post('/api/v1.0/tests', body=invalid_payload)
        self.assertEqual(400, resp.status_code)

        m_tiller = mock_tiller.return_value
        m_tiller.__enter__.return_value = m_tiller

        resp_body = json.loads(resp.text)
        self.assertEqual(400, resp_body['code'])
        self.assertEqual(1, resp_body['details']['errorCount'])
        self.assertIn(
            {
                'message': (
                    'An error occurred while building chart group: '
                    'Could not build ChartGroup named '
                    '"keystone-infra-services".'),
                'error': True,
                'kind': 'ValidationMessage',
                'level': 'Error',
                'name': 'ARM001',
                'documents': []
            }, resp_body['details']['messageList'])
        self.assertEqual(
            (
                'Failed to validate documents or generate Armada '
                'Manifest from documents.'), resp_body['message'])
        m_tiller.__exit__.assert_called() 
Example #7
Source File: test_test_controller.py    From armada with Apache License 2.0 5 votes vote down vote up
def test_test_controller_validation_failure_returns_400(
            self, *_):
        rules = {'armada:tests_manifest': '@'}
        self.policy.set_rules(rules)

        manifest_path = os.path.join(os.getcwd(), 'examples',
                                     'keystone-manifest.yaml')
        with open(manifest_path, 'r') as f:
            payload = f.read()

        documents = list(yaml.safe_load_all(payload))
        documents[0]['schema'] = 'totally-invalid'
        invalid_payload = yaml.safe_dump_all(documents)

        resp = self.app.simulate_post('/api/v1.0/tests', body=invalid_payload)
        self.assertEqual(400, resp.status_code)

        resp_body = json.loads(resp.text)
        self.assertEqual(400, resp_body['code'])
        self.assertEqual(1, resp_body['details']['errorCount'])
        self.assertIn(
            {'message': (
                'An error occurred while generating the manifest: Could not '
                'find dependency chart helm-toolkit in armada/Chart/v1.'),
             'error': True,
             'kind': 'ValidationMessage',
             'level': 'Error',
             'name': 'ARM001',
             'documents': []},
            resp_body['details']['messageList'])
        self.assertEqual(('Failed to validate documents or generate Armada '
                          'Manifest from documents.'),
                         resp_body['message']) 
Example #8
Source File: writer.py    From artifacts with Apache License 2.0 5 votes vote down vote up
def FormatArtifacts(self, artifacts):
    """Formats artifacts to desired output format.

    Args:
      artifacts (list[ArtifactDefinition]): artifact definitions.

    Returns:
      str: formatted string of artifact definition.
    """
    # TODO: improve output formatting of yaml
    artifact_definitions = [artifact.AsDict() for artifact in artifacts]
    yaml_data = yaml.safe_dump_all(artifact_definitions)
    return yaml_data 
Example #9
Source File: renderer.py    From promenade with Apache License 2.0 5 votes vote down vote up
def _yaml_safe_dump_all(documents):
    f = io.StringIO()
    yaml.safe_dump_all(documents, f)
    return f.getvalue() 
Example #10
Source File: config.py    From ravestate with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def write(self, path: str):
        """
        Write all current config entries to a yaml file.

        * `path`: The file path to write. Will be overwritten!
        """
        with open(path, mode='w') as file:
            yaml.safe_dump_all((
                {"module": module_name, "config": config_entries}
                for module_name, config_entries in self.config_per_module.items()),
                file,
                default_flow_style=False) 
Example #11
Source File: set_ownership.py    From marketplace-k8s-app-tools with Apache License 2.0 5 votes vote down vote up
def dump(outfile, resources, included_kinds, app_name, app_uid, app_api_version,
         deployer_name, deployer_uid):

  def maybe_assign_ownership(resource):
    if resource["kind"] in _CLUSTER_SCOPED_KINDS:
      # Cluster-scoped resources cannot be owned by a namespaced resource:
      # https://kubernetes.io/docs/concepts/workloads/controllers/garbage-collection/#owners-and-dependents
      log.info("Application '{:s}' does not own cluster-scoped '{:s}/{:s}'",
               app_name, resource["kind"], resource["metadata"]["name"])

    # Deployer-owned resources should not be owned by the Application, as
    # they should be deleted with the deployer service account (not the app).
    if deployer_name and deployer_uid and should_be_deployer_owned(resource):
      log.info("ServiceAccount '{:s}' owns '{:s}/{:s}'", deployer_name,
               resource["kind"], resource["metadata"]["name"])
      resource = copy.deepcopy(resource)
      set_service_account_resource_ownership(
          account_uid=deployer_uid,
          account_name=deployer_name,
          resource=resource)
    elif included_kinds is None or resource["kind"] in included_kinds:
      log.info("Application '{:s}' owns '{:s}/{:s}'", app_name,
               resource["kind"], resource["metadata"]["name"])
      resource = copy.deepcopy(resource)
      set_app_resource_ownership(
          app_uid=app_uid,
          app_name=app_name,
          app_api_version=app_api_version,
          resource=resource)

    return resource

  to_be_dumped = [maybe_assign_ownership(resource) for resource in resources]
  yaml.safe_dump_all(to_be_dumped, outfile, default_flow_style=False, indent=2) 
Example #12
Source File: set_app_labels.py    From marketplace-k8s-app-tools with Apache License 2.0 5 votes vote down vote up
def write_resources(resources, outfile):
  yaml.safe_dump_all(resources, outfile, default_flow_style=False, indent=2) 
Example #13
Source File: separate_tester_resources.py    From marketplace-k8s-app-tools with Apache License 2.0 4 votes vote down vote up
def main():

  parser = ArgumentParser(description=_PROG_HELP)
  parser.add_argument(
      "--app_name", required=True, help="the name of the application instance")
  parser.add_argument(
      "--app_uid", required=True, help="the uid of the application instance")
  parser.add_argument(
      "--app_api_version",
      required=True,
      help="apiVersion of the Application CRD")
  parser.add_argument(
      "--manifests", required=True, help="the configuration for tests")
  parser.add_argument(
      "--out_manifests",
      required=True,
      help="the file to write non-test resources to")
  parser.add_argument(
      "--out_test_manifests",
      required=True,
      help="the file to write test resources to")
  args = parser.parse_args()

  if os.path.isfile(args.manifests):
    resources = load_resources_yaml(args.manifests)
  else:
    resources = []
    for filename in os.listdir(args.manifests):
      resources += load_resources_yaml(os.path.join(args.manifests, filename))

  test_resources = []
  nontest_resources = []
  for resource in resources:
    full_name = "{}/{}".format(resource['kind'],
                               deep_get(resource, 'metadata', 'name'))
    if deep_get(resource, 'metadata', 'annotations',
                GOOGLE_CLOUD_TEST) == 'test':
      print("INFO Tester resource: {}".format(full_name))
      set_app_resource_ownership(
          app_uid=args.app_uid,
          app_name=args.app_name,
          app_api_version=args.app_api_version,
          resource=resource)
      test_resources.append(resource)
    else:
      print("INFO Prod resource: {}".format(full_name))
      nontest_resources.append(resource)

  if nontest_resources:
    with open(args.out_manifests, "w", encoding='utf-8') as outfile:
      yaml.safe_dump_all(nontest_resources, outfile, default_flow_style=False)

  if test_resources:
    with open(args.out_test_manifests, "a", encoding='utf-8') as test_outfile:
      yaml.safe_dump_all(test_resources, test_outfile, default_flow_style=False)