Python ruamel.yaml.Loader() Examples

The following are 30 code examples of ruamel.yaml.Loader(). 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: utility.py    From planet with Apache License 2.0 6 votes vote down vote up
def load_config(logdir):
  """Load a configuration from the log directory.

  Args:
    logdir: The logging directory containing the configuration file.

  Raises:
    IOError: The logging directory does not contain a configuration file.

  Returns:
    Configuration object.
  """
  config_path = logdir and os.path.join(logdir, 'config.yaml')
  if not config_path or not tf.gfile.Exists(config_path):
    message = (
        'Cannot resume an existing run since the logging directory does not '
        'contain a configuration file.')
    raise IOError(message)
  with tf.gfile.GFile(config_path, 'r') as file_:
    config = yaml.load(file_, yaml.Loader)
    message = 'Resume run and write summaries and checkpoints to {}.'
    tf.logging.info(message.format(config.logdir))
  return config 
Example #2
Source File: check_urls.py    From apd-core with GNU General Public License v3.0 6 votes vote down vote up
def read_core_files():
    core_path = Path('../core')

    file_metas = []
    for item in core_path.glob('**/*'):
        if item.is_dir() or item.name.startswith('.'):
            continue

        category, file_name = item.parts[-2:]

        file = core_path / category / file_name
        with file.open() as file:
            data_info = yaml.load(file.read(), Loader=yaml.Loader)

        file_metas.append({
            'category': category,
            'file_name': file_name,
            'url': data_info['homepage'],
        })

    return file_metas 
Example #3
Source File: utility.py    From batch-ppo with Apache License 2.0 6 votes vote down vote up
def load_config(logdir):
  # pylint: disable=missing-raises-doc
  """Load a configuration from the log directory.

  Args:
    logdir: The logging directory containing the configuration file.

  Raises:
    IOError: The logging directory does not contain a configuration file.

  Returns:
    Configuration object.
  """
  config_path = logdir and os.path.join(logdir, 'config.yaml')
  if not config_path or not tf.gfile.Exists(config_path):
    message = (
        'Cannot resume an existing run since the logging directory does not '
        'contain a configuration file.')
    raise IOError(message)
  with tf.gfile.FastGFile(config_path, 'r') as file_:
    config = yaml.load(file_, Loader=yaml.Loader)
  message = 'Resume run and write summaries and checkpoints to {}.'
  tf.logging.info(message.format(config.logdir))
  return config 
Example #4
Source File: shyaml.py    From smarthome with GNU General Public License v3.0 6 votes vote down vote up
def yaml_load_roundtrip(filename):
    """
    Load contents of a yaml file into an dict structure for editing (using Roundtrip Loader)

    :param filename: name of the yaml file to load
    :return: data structure loaded from file
    """

    if not EDITING_ENABLED:
        return None

    y = None
    if not filename.lower().endswith('.yaml'):
        filename += YAML_FILE
    try:
        with open(filename, 'r') as stream:
            sdata = stream.read()
        sdata = sdata.replace('\n', '\n\n')
        y = yaml.load(sdata, yaml.RoundTripLoader)
    except Exception as e:
        logger.error("yaml_load_roundtrip: YAML-file load error: '%s'" % (e))
        y = {}
    return y 
Example #5
Source File: shyaml.py    From smarthome with GNU General Public License v3.0 6 votes vote down vote up
def _ordered_load(stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
    """
    Ordered yaml loader
    Use this instead ot yaml.loader/yaml.saveloader to get an Ordereddict

    :param stream: stream to read from
    :param Loader: yaml-loader to use
    :object_pairs_hook: ...

    :return: OrderedDict structure
    """

    # usage example: ordered_load(stream, yaml.SafeLoader)
    class OrderedLoader(Loader):
        pass
    def construct_mapping(loader, node):
        loader.flatten_mapping(node)
        return object_pairs_hook(loader.construct_pairs(node))
    OrderedLoader.add_constructor(
        yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
        construct_mapping)
    return yaml.load(stream, OrderedLoader) 
Example #6
Source File: config.py    From mackrl with Apache License 2.0 6 votes vote down vote up
def get_cfg(existing_cfg, _log):
    """

    """
    _sanity_check(existing_cfg, _log)
    import ntpath, os, ruamel.yaml as yaml
    with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])), 'r') as stream:
        try:
            ret = yaml.load(stream, Loader=yaml.Loader)
        except yaml.YAMLError as exc:
            assert False, "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])

    if not "batch_size_run" in ret:
        ret["batch_size_run"] = ret["batch_size"]

    if not "runner_test_batch_size" in ret:
        ret["runner_test_batch_size"] = ret["batch_size_run"]

    if not "name" in ret:
        ret["name"] = ntpath.basename(__file__).split(".")[0]

    return ret 
Example #7
Source File: config.py    From mackrl with Apache License 2.0 6 votes vote down vote up
def get_cfg(existing_cfg, _log):
    """

    """
    _sanity_check(existing_cfg, _log)
    import ntpath, os, ruamel.yaml as yaml
    with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])), 'r') as stream:
        try:
            ret = yaml.load(stream, Loader=yaml.Loader)
        except yaml.YAMLError as exc:
            assert False, "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])

    if not "batch_size_run" in ret:
        ret["batch_size_run"] = ret["batch_size"]

    if not "runner_test_batch_size" in ret:
        ret["runner_test_batch_size"] = ret["batch_size_run"]

    if not "name" in ret:
        ret["name"] = ntpath.basename(__file__).split(".")[0]

    return ret 
Example #8
Source File: config.py    From mackrl with Apache License 2.0 6 votes vote down vote up
def get_cfg(existing_cfg, _log):
    """

    """
    _sanity_check(existing_cfg, _log)
    import ntpath, os, ruamel.yaml as yaml
    with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])), 'r') as stream:
        try:
            ret = yaml.load(stream, Loader=yaml.Loader)
        except yaml.YAMLError as exc:
            assert False, "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])

    if not "batch_size_run" in ret:
        ret["batch_size_run"] = ret["batch_size"]

    if not "runner_test_batch_size" in ret:
        ret["runner_test_batch_size"] = ret["batch_size_run"]

    if not "name" in ret:
        ret["name"] = ntpath.basename(__file__).split(".")[0]

    return ret 
Example #9
Source File: config.py    From mackrl with Apache License 2.0 6 votes vote down vote up
def get_cfg(existing_cfg, _log):
    """
    generates
    """
    _sanity_check(existing_cfg, _log)
    import ntpath, os, ruamel.yaml as yaml
    with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])),
              'r') as stream:
        try:
            ret = yaml.load(stream, Loader=yaml.Loader)
        except yaml.YAMLError as exc:
            assert "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])

    if ret["coma_critic_use_sampling"] and "coma_critic_sample_size" not in ret and hasattr(ret, "batch_size_run"):
        ret["coma_critic_sample_size"] = ret["batch_size_run"] * 50
    return ret 
Example #10
Source File: config.py    From mackrl with Apache License 2.0 6 votes vote down vote up
def get_cfg(existing_cfg, _log):
    """
    generates
    """
    _sanity_check(existing_cfg, _log)
    import ntpath, os, ruamel.yaml as yaml
    with open(os.path.join(os.path.dirname(__file__), "{}.yml".format(ntpath.basename(__file__).split(".")[0])),
              'r') as stream:
        try:
            ret = yaml.load(stream, Loader=yaml.Loader)
        except yaml.YAMLError as exc:
            assert "Default config yaml for '{}' not found!".format(os.path.splitext(__file__)[0])

    #if ret["coma_critic_use_sampling"] and "coma_critic_sample_size" not in ret and hasattr(ret, "batch_size_run"):
    #    ret["coma_critic_sample_size"] = ret["batch_size_run"] * 50
    return ret 
Example #11
Source File: utility.py    From soccer-matlab with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def load_config(logdir):
  """Load a configuration from the log directory.

  Args:
    logdir: The logging directory containing the configuration file.

  Raises:
    IOError: The logging directory does not contain a configuration file.

  Returns:
    Configuration object.
  """
  config_path = logdir and os.path.join(logdir, 'config.yaml')
  if not config_path or not tf.gfile.Exists(config_path):
    message = (
        'Cannot resume an existing run since the logging directory does not '
        'contain a configuration file.')
    raise IOError(message)
  with tf.gfile.FastGFile(config_path, 'r') as file_:
    config = yaml.load(file_, Loader=yaml.Loader)
  message = 'Resume run and write summaries and checkpoints to {}.'
  tf.logging.info(message.format(config.logdir))
  return config 
Example #12
Source File: test_config.py    From workload-collocation-agent with Apache License 2.0 5 votes vote down vote up
def test_config_unsafe_object_creation():
    from ruamel import yaml
    import calendar

    test_config_path = testing.relative_module_path(__file__, 'test_config_unsafe.yaml')

    # Unsafe default loader allows any python object initialization
    data = yaml.load(open(test_config_path), Loader=yaml.Loader)
    assert 'time' in data
    assert isinstance(data['time'], calendar.Calendar)

    # With use safe version only to allow construct previously registered objects
    with pytest.raises(config.ConfigLoadError, match='could not determine a constructor'):
        config.load_config(test_config_path) 
Example #13
Source File: .sync-zenodo-metadata.py    From signac-flow with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sync(ctx, in_place=False, check=True):

    with open('CITATION.cff', 'rb') as file:
        citation = load(file.read(), Loader=Loader)
        authors = [
            Contributor.from_citation_author(**author)
            for author in citation['authors']]

    with open('contributors.yaml', 'rb') as file:
        contributors = load(file.read(), Loader=Loader)['contributors']
        contributors = [Contributor.from_citation_author(
            **contributor) for contributor in contributors]

    with open('.zenodo.json', 'rb') as file:
        zenodo = json.loads(file.read())
        zenodo_updated = zenodo.copy()
        zenodo_updated['creators'] = [a.as_zenodo_creator() for a in authors]
        zenodo_updated['contributors'] = [c.as_zenodo_contributor()
                                          for c in contributors if c not in authors]
        for key in ('version', 'keywords'):
            zenodo_updated[key] = citation[key]

    modified = json.dumps(zenodo, sort_keys=True) != json.dumps(zenodo_updated, sort_keys=True)
    if modified:
        if in_place:
            with open('.zenodo.json', 'wb') as file:
                file.write(json.dumps(zenodo_updated, indent=4, sort_keys=True).encode('utf-8'))
        else:
            click.echo(json.dumps(zenodo_updated, indent=4, sort_keys=True))
        if check:
            ctx.exit(1)
    else:
        click.echo("No changes.", err=True) 
Example #14
Source File: attr_dict.py    From dreamer with Apache License 2.0 5 votes vote down vote up
def load(cls, filename):
    assert str(filename).endswith('.yaml')
    with open(filename, 'r') as f:
      return cls(yaml.load(f, Loader=yaml.Loader)) 
Example #15
Source File: utility.py    From dreamer with Apache License 2.0 5 votes vote down vote up
def load_config(logdir):
  config_path = logdir and os.path.join(logdir, 'config.yaml')
  if not config_path or not tf.gfile.Exists(config_path):
    message = (
        'Cannot resume an existing run since the logging directory does not '
        'contain a configuration file.')
    raise IOError(message)
  with tf.gfile.GFile(config_path, 'r') as file_:
    config = yaml.load(file_, yaml.Loader)
    message = 'Resume run and write summaries and checkpoints to {}.'
    print(message.format(config.logdir))
  return config 
Example #16
Source File: test_kernel.py    From kerncraft with GNU Affero General Public License v3.0 5 votes vote down vote up
def setUp(self):
        with open(self._find_file('2d-5pt.c')) as f:
            self.twod_code = f.read()
        with open(self._find_file('3d-7pt.c')) as f:
            self.threed_code = f.read()
        with open(self._find_file('2d-5pt.yml')) as f:
            self.twod_description = yaml.load(f.read(), Loader=yaml.Loader)
        with open(self._find_file('copy-2d-linearized.c')) as f:
            self.twod_linear = f.read() 
Example #17
Source File: yaml2json.py    From packer-build with GNU General Public License v3.0 5 votes vote down vote up
def main():
    x = yaml.load(sys.stdin, Loader=yaml.Loader)
    del x['_anchors']
    print(json.dumps(x, sort_keys=True,
          indent=2, separators=(',', ': '))) 
Example #18
Source File: test_unit_import_workflow.py    From python-cwlgen with MIT License 5 votes vote down vote up
def setUpClass(cls):
        cls.path = test_dir + '/import_workflow.cwl'
        with open(cls.path) as mf:
            cls.wf_dict = ryaml.load(mf, Loader=ryaml.Loader)
        cls.wf = parse_cwl_dict(cls.wf_dict) 
Example #19
Source File: import_cwl.py    From python-cwlgen with MIT License 5 votes vote down vote up
def parse_cwl_string(cwlstr):
    cwl_dict = ryaml.load(cwlstr, Loader=ryaml.Loader)
    return parse_cwl_dict(cwl_dict) 
Example #20
Source File: import_cwl.py    From python-cwlgen with MIT License 5 votes vote down vote up
def parse_cwl(cwl_path):
    """
    Method that parses a CWL file and will a
    :class:`cwlgen.Workflow` or :class:`cwlgen.CommandLineTool`.
    Note: this will not import additional files.

    :param cwl_path: PATH to the CWL file
    :type cwl_path: str
    :return: :class:`cwlgen.Workflow` | :class:`cwlgen.CommandLineTool`
    """

    with open(cwl_path) as yaml_file:
        cwl_dict = ryaml.load(yaml_file, Loader=ryaml.Loader)
    return parse_cwl_dict(cwl_dict) 
Example #21
Source File: utils.py    From nni with MIT License 5 votes vote down vote up
def get_yml_content(file_path):
    '''Load yaml file content'''
    with open(file_path, 'r') as file:
        return yaml.load(file, Loader=yaml.Loader) 
Example #22
Source File: common_utils.py    From nni with MIT License 5 votes vote down vote up
def get_yml_content(file_path):
    '''Load yaml file content'''
    try:
        with open(file_path, 'r') as file:
            return yaml.load(file, Loader=yaml.Loader)
    except yaml.scanner.ScannerError as err:
        print_error('yaml file format error!')
        print_error(err)
        exit(1)
    except Exception as exception:
        print_error(exception)
        exit(1) 
Example #23
Source File: package_utils.py    From nni with MIT License 5 votes vote down vote up
def read_installed_package_meta():
    config_file = get_package_config_path()
    if os.path.exists(config_file):
        with open(config_file, 'r') as f:
            config = yaml.load(f, Loader=yaml.Loader)
    else:
        config = defaultdict(list)
    for t in ALGO_TYPES:
        if t not in config:
            config[t] = []
    return config 
Example #24
Source File: config.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def load_mod_config(config_path, loader=yaml.Loader):
    mod_config = load_config(config_path, loader)
    if mod_config is None or "mod" not in mod_config:
        import os
        os.remove(config_path)
        config_path = get_mod_config_path()
        return load_mod_config(config_path, loader)
    else:
        return mod_config 
Example #25
Source File: config.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def load_config(config_path, loader=yaml.Loader):
    if config_path is None:
        return {}
    if not os.path.exists(config_path):
        system_log.error(_(u"config.yml not found in {config_path}").format(config_path))
        return False
    with codecs.open(config_path, encoding="utf-8") as stream:
        config = yaml.load(stream, loader)
    return config 
Example #26
Source File: config.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def load_config(config_path, loader=yaml.Loader, verify_version=True):
    if not os.path.exists(config_path):
        system_log.error(_("config.yml not found in {config_path}").format(config_path))
        return False
    with codecs.open(config_path, encoding="utf-8") as stream:
        config = yaml.load(stream, loader)
    if verify_version:
        config = config_version_verify(config, config_path)
    return config 
Example #27
Source File: yaml_config.py    From autolab_core with Apache License 2.0 5 votes vote down vote up
def __ordered_load(self, stream, Loader=yaml.Loader, object_pairs_hook=OrderedDict):
        """Load an ordered dictionary from a yaml file.

        Note
        ----
        Borrowed from John Schulman.
        http://stackoverflow.com/questions/5121931/in-python-how-can-you-load-yaml-mappings-as-ordereddicts/21048064#21048064"
        """
        class OrderedLoader(Loader):
            pass
        OrderedLoader.add_constructor(
            yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
            lambda loader, node: object_pairs_hook(loader.construct_pairs(node)))
        return yaml.load(stream, OrderedLoader) 
Example #28
Source File: gapic_tasks.py    From artman with Apache License 2.0 5 votes vote down vote up
def execute(self, gapic_code_dir, grpc_code_dir, proto_code_dir, gapic_yaml):
        with open(gapic_yaml) as f:
            gapic_config = yaml.load(f, Loader=yaml.Loader)
        package_name = gapic_config.get('language_settings').get('csharp').get('package_name')
        package_root = '{0}/{1}'.format(gapic_code_dir, package_name)
        prod_dir = '{0}/{1}'.format(package_root, package_name)
        # Copy proto/grpc .cs files into prod directory
        self.exec_command(['sh', '-c', 'cp {0}/*.cs {1}'.format(proto_code_dir, prod_dir)])
        self.exec_command(['sh', '-c', 'cp {0}/*.cs {1}'.format(grpc_code_dir, prod_dir)]) 
Example #29
Source File: attr_dict.py    From planet with Apache License 2.0 5 votes vote down vote up
def load(cls, filename):
    assert str(filename).endswith('.yaml')
    with open(filename, 'r') as f:
      return cls(yaml.load(f, Loader=yaml.Loader)) 
Example #30
Source File: .sync-zenodo-metadata.py    From signac with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sync(ctx, in_place=False, check=True):

    with open('CITATION.cff', 'rb') as file:
        citation = load(file.read(), Loader=Loader)
        authors = [
            Contributor.from_citation_author(**author)
            for author in citation['authors']]

    with open('contributors.yaml', 'rb') as file:
        contributors = load(file.read(), Loader=Loader)['contributors']
        contributors = [Contributor.from_citation_author(
            **contributor) for contributor in contributors]

    with open('.zenodo.json', 'rb') as file:
        zenodo = json.loads(file.read())
        zenodo_updated = zenodo.copy()
        zenodo_updated['creators'] = [a.as_zenodo_creator() for a in authors]
        zenodo_updated['contributors'] = [c.as_zenodo_creator()
                                          for c in contributors if c not in authors]
        for key in ('version', 'keywords'):
            zenodo_updated[key] = citation[key]

    modified = json.dumps(zenodo, sort_keys=True) != json.dumps(zenodo_updated, sort_keys=True)
    if modified:
        if in_place:
            with open('.zenodo.json', 'wb') as file:
                file.write(json.dumps(zenodo_updated, indent=4, sort_keys=True).encode('utf-8'))
        else:
            click.echo(json.dumps(zenodo_updated, indent=4, sort_keys=True))
        if check:
            ctx.exit(1)
    else:
        click.echo("No changes.", err=True)