Python ruamel.yaml.round_trip_load() Examples

The following are 13 code examples of ruamel.yaml.round_trip_load(). 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 ruamel.yaml , or try the search function .
Example #1
Source File: cloudsync.py    From integrations with Apache License 2.0 6 votes vote down vote up
def sync(name, metrics):
    """Write markdown docs"""
    metrics_file = Path().resolve().parent / name / "metrics.yaml"
    cur = {}

    if metrics_file.exists():
        cur = yaml.round_trip_load(metrics_file.read_text())

    for m in metrics:
        entry = cur.setdefault(m.title, asdict(m))

        # If the fetched value exists override it, otherwise leave the current one alone.
        if m.description:
            entry["description"] = m.description
        if m.brief:
            entry["brief"] = m.brief
        if m.metric_type:
            entry["metric_type"] = m.metric_type

    with metrics_file.open("wt") as f:
        yaml.round_trip_dump(cur, f) 
Example #2
Source File: generate.py    From borgmatic with GNU General Public License v3.0 6 votes vote down vote up
def generate_sample_configuration(source_filename, destination_filename, schema_filename):
    '''
    Given an optional source configuration filename, and a required destination configuration
    filename, and the path to a schema filename in pykwalify YAML schema format, write out a
    sample configuration file based on that schema. If a source filename is provided, merge the
    parsed contents of that configuration into the generated configuration.
    '''
    schema = yaml.round_trip_load(open(schema_filename))
    source_config = None

    if source_filename:
        source_config = load.load_configuration(source_filename)

    destination_config = merge_source_configuration_into_destination(
        _schema_to_sample_configuration(schema), source_config
    )

    write_configuration(
        destination_filename,
        _comment_out_optional_configuration(_render_configuration(destination_config)),
    ) 
Example #3
Source File: config_utils.py    From paasta with Apache License 2.0 6 votes vote down vote up
def write_auto_config_data(
    service: str, extra_info: str, data: Dict[str, Any], soa_dir: str = DEFAULT_SOA_DIR
) -> Optional[str]:
    """
    Replaces the contents of an automated config file for a service, or creates the file if it does not exist.

    Returns the filename of the modified file, or None if no file was written.
    """
    service_dir = f"{soa_dir}/{service}"
    if not os.path.exists(service_dir):
        log.warning(
            f"Service {service} does not exist in configs, skipping auto config update"
        )
        return None
    subdir = f"{service_dir}/{AUTO_SOACONFIG_SUBDIR}"
    if not os.path.exists(subdir):
        os.mkdir(subdir)
    filename = f"{subdir}/{extra_info}.yaml"
    with open(filename, "w") as f:
        content = yaml.round_trip_load(
            HEADER_COMMENT.format(regular_filename=f"{service}/{extra_info}.yaml")
        )
        content.update(data)
        f.write(yaml.round_trip_dump(content))
    return filename 
Example #4
Source File: paasta_update_soa_memcpu.py    From paasta with Apache License 2.0 6 votes vote down vote up
def edit_soa_configs(filename, instance, cpu, mem, disk):
    if not os.path.exists(filename):
        filename = filename.replace("marathon", "kubernetes")
    if os.path.islink(filename):
        real_filename = os.path.realpath(filename)
        os.remove(filename)
    else:
        real_filename = filename
    try:
        with open(real_filename, "r") as fi:
            yams = fi.read()
            yams = yams.replace("cpus: .", "cpus: 0.")
            data = yaml.round_trip_load(yams, preserve_quotes=True)

        instdict = data[instance]
        if cpu:
            instdict["cpus"] = float(cpu)
        if mem:
            mem = max(128, round(float(mem)))
            instdict["mem"] = mem
        if disk:
            instdict["disk"] = round(float(disk))
        out = yaml.round_trip_dump(data, width=120)

        with open(filename, "w") as fi:
            fi.write(out)
    except FileNotFoundError:
        log.exception(f"Could not find {filename}")
    except KeyError:
        log.exception(f"Error in {filename}. Will continue") 
Example #5
Source File: config.py    From fleece with Apache License 2.0 6 votes vote down vote up
def import_config(args, input_file=None):
    if not input_file:
        input_file = sys.stdin
    source = input_file.read().strip()
    if source[0] == "{":
        # JSON input
        config = json.loads(source)
    else:
        # YAML input
        config = yaml.round_trip_load(source)

    STATE["stages"] = config["stages"]
    config["config"] = _encrypt_dict(config["config"])
    with open(args.config, "wt") as f:
        if config:
            yaml.round_trip_dump(config, f) 
Example #6
Source File: config.py    From fleece with Apache License 2.0 6 votes vote down vote up
def export_config(args, output_file=None):
    if not output_file:
        output_file = sys.stdout
    if os.path.exists(args.config):
        with open(args.config, "rt") as f:
            config = yaml.round_trip_load(f.read())
        STATE["stages"] = config["stages"]
        config["config"] = _decrypt_dict(config["config"])
    else:
        config = {
            "stages": {
                env["name"]: {"environment": env["name"], "key": "enter-key-name-here"}
                for env in STATE["awscreds"].environments
            },
            "config": {},
        }

    if args.json:
        output_file.write(json.dumps(config, indent=4))
    elif config:
        yaml.round_trip_dump(config, output_file) 
Example #7
Source File: convert_config.py    From borgmatic with GNU General Public License v3.0 5 votes vote down vote up
def main():  # pragma: no cover
    try:
        args = parse_arguments(*sys.argv[1:])
        schema = yaml.round_trip_load(open(validate.schema_filename()).read())
        source_config = legacy.parse_configuration(
            args.source_config_filename, legacy.CONFIG_FORMAT
        )
        source_config_file_mode = os.stat(args.source_config_filename).st_mode
        source_excludes = (
            open(args.source_excludes_filename).read().splitlines()
            if args.source_excludes_filename
            else []
        )

        destination_config = convert.convert_legacy_parsed_config(
            source_config, source_excludes, schema
        )

        generate.write_configuration(
            args.destination_config_filename, destination_config, mode=source_config_file_mode
        )

        display_result(args)
    except (ValueError, OSError) as error:
        print(error, file=sys.stderr)
        sys.exit(1) 
Example #8
Source File: settings-updater.py    From Community with GNU General Public License v3.0 5 votes vote down vote up
def load_settings(file_to_load):
    settings = None
    try:
        settings = yaml.round_trip_load(open(file_to_load, "r"), preserve_quotes=True)
    except Exception:
        log.exception("Exception loading %s: ", file_to_load)
    return settings 
Example #9
Source File: config.py    From uchroma with GNU Lesser General Public License v3.0 5 votes vote down vote up
def load_yaml(cls, filename: str):
        """
        Load a hierarchy of sparse objects from a YAML file.

        :param filename: The filename to open.
        :return: The configuration object hierarchy
        """
        def unpack(mapping, parent=None):
            """
            Recursively create Configuration objects with the parent
            correctly set, returning the top-most parent.
            """
            if mapping is None:
                return None

            children = config = None

            if not isinstance(mapping, cls):
                children = mapping.pop('children', None)
                config = cls(**cls._coerce_types(mapping), parent=parent)

            if children:
                for child in children:
                    unpack(child, parent=config)
            return config

        if filename in cls._yaml_cache:
            return cls._yaml_cache[filename]

        data = None
        with open(filename, 'r') as yaml_file:
            data = unpack(yaml.round_trip_load(yaml_file.read()))

        if data is not None:
            cls._yaml_cache[filename] = data

        return data 
Example #10
Source File: template_builder.py    From runtimes-common with Apache License 2.0 5 votes vote down vote up
def _resolve_and_publish(config_file, bucket):
    try:
        gcs_paths = []
        with open(config_file, 'r') as f:
            if 'yaml' not in config_file:
                logging.error('Please provide a valid yaml config file.')
                sys.exit(1)
            project_cfg = yaml.round_trip_load(f, preserve_quotes=True)
            project_name = project_cfg['project']
            for builder in project_cfg['builders']:
                cfg = os.path.abspath(str(builder['file']))
                name = builder['name']
                builder_name = project_name + '-' + name

                templated_file = _resolve_tags(cfg)
                logging.info(templated_file)
                gcs_paths.append(_publish_to_gcs(templated_file,
                                                 builder_name,
                                                 bucket))

        logging.info('Published Runtimes:')
        logging.info(gcs_paths)
    except ValueError as ve:
        logging.error('Error when parsing config! Check file formatting. \n{0}'
                      .format(ve))
    except KeyError as ke:
        logging.error('Config file is missing required field! \n{0}'
                      .format(ke)) 
Example #11
Source File: template_builder.py    From runtimes-common with Apache License 2.0 5 votes vote down vote up
def _resolve_tags(config_file):
    """
    Given a templated YAML cloudbuild config file, parse it, resolve image tags
    on each build step's image to the corresponding digest, and write new
    config with fully qualified images to temporary file for upload to GCS.

    Keyword arguments:
    config_file -- string representing path to
    templated cloudbuild YAML config file

    Return value:
    path to temporary file containing fully qualified config file, to be
    published to GCS.
    """
    with open(config_file, 'r') as infile:
        logging.info('Templating file: {0}'.format(config_file))
        try:
            config = yaml.round_trip_load(infile, preserve_quotes=True)

            for step in config.get('steps'):
                image = step.get('name')
                step['name'] = _resolve_tag(image)
                args = step.get('args', [])
                for i in range(0, len(args)):
                    arg = args[i]
                    m = re.search(IMAGE_REGEX, arg)
                    if m:
                        suffix = m.group()
                        prefix = re.sub(suffix, '', arg)
                        args[i] = prefix + _resolve_tag(suffix)

            return yaml.round_trip_dump(config)
        except yaml.YAMLError as e:
            logging.error(e)
            sys.exit(1) 
Example #12
Source File: cwldag.py    From cwl-airflow with Apache License 2.0 5 votes vote down vote up
def quick_load_cwl(self, cwl_file):
        with open(cwl_file, "r") as input_stream:
            cwl_data = yaml.round_trip_load(input_stream, preserve_quotes=True)
        return cwl_data 
Example #13
Source File: cwljobdispatcher.py    From cwl-airflow with Apache License 2.0 5 votes vote down vote up
def cwl_dispatch(self, json):
        try:
            cwlwf, it_is_workflow = load_cwl(self.dag.default_args["cwl_workflow"], self.dag.default_args)
            cwl_context = {"outdir": mkdtemp(dir=get_folder(os.path.abspath(self.tmp_folder)), prefix="dag_tmp_")}

            _jobloaderctx = jobloaderctx.copy()
            _jobloaderctx.update(cwlwf.metadata.get("$namespaces", {}))
            loader = Loader(_jobloaderctx)

            try:
                job_order_object = yaml.round_trip_load(io.StringIO(initial_value=dumps(json)))
                job_order_object, _ = loader.resolve_all(job_order_object,
                                                         file_uri(os.getcwd()) + "/",
                                                         checklinks=False)
            except Exception as e:
                _logger.error("Job Loader: {}".format(str(e)))

            job_order_object = init_job_order(job_order_object, None, cwlwf, loader, sys.stdout)

            cwl_context['promises'] = job_order_object

            logging.info(
                '{0}: Final job: \n {1}'.format(self.task_id, dumps(cwl_context, indent=4)))

            return cwl_context

        except Exception as e:
            _logger.info(
                'Dispatch Exception {0}: \n {1} {2}'.format(self.task_id, type(e), e))
            pass
        return None