Python yaml.SafeDumper() Examples

The following are 30 code examples of yaml.SafeDumper(). 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: item_conversion.py    From smarthome with GNU General Public License v3.0 6 votes vote down vote up
def convert_yaml(data):
    """
    ***Converter Special ***

    Convert data structure to yaml format

    :param data: OrderedDict to convert
    :return: yaml formated data
    """

    ordered = (type(data).__name__ == 'OrderedDict')
    if ordered:
        sdata = _ordered_dump(data, Dumper=yaml.SafeDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=2, width=32768, allow_unicode=True, default_flow_style=False)
    else:
        sdata = yaml.dump(data, Dumper=yaml.SafeDumper, indent=indent_spaces, block_seq_indent=2, width=32768, allow_unicode=True, default_flow_style=False)
    sdata = _format_yaml_dump(sdata)

    return sdata 
Example #2
Source File: item_conversion.py    From smarthome with GNU General Public License v3.0 6 votes vote down vote up
def _ordered_dump(data, stream=None, Dumper=yaml.Dumper, **kwds):
    """
    Ordered yaml dumper
    Use this instead ot yaml.Dumper/yaml.SaveDumper to get an Ordereddict

    :param stream: stream to write to
    :param Dumper: yaml-dumper to use
    :**kwds: Additional keywords

    :return: OrderedDict structure
    """

    # usage example: ordered_dump(data, Dumper=yaml.SafeDumper)
    class OrderedDumper(Dumper):
        pass
    def _dict_representer(dumper, data):
        return dumper.represent_mapping(
            yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
            data.items())
    OrderedDumper.add_representer(OrderedDict, _dict_representer)
    return yaml.dump(data, stream, OrderedDumper, **kwds) 
Example #3
Source File: purge.py    From DeepSea with GNU General Public License v3.0 6 votes vote down vote up
def roles():
    """
    Remove the roles from the cluster/*.sls files
    """
    # Keep yaml human readable/editable
    friendly_dumper = yaml.SafeDumper
    friendly_dumper.ignore_aliases = lambda self, data: True

    cluster_dir = '/srv/pillar/ceph/cluster'
    for filename in os.listdir(cluster_dir):
        pathname = "{}/{}".format(cluster_dir, filename)
        content = None
        with open(pathname, "r") as sls_file:
            content = yaml.safe_load(sls_file)
        log.info("content {}".format(content))
        if 'roles' in content:
            content.pop('roles')
        with open(pathname, "w") as sls_file:
            sls_file.write(yaml.dump(content, Dumper=friendly_dumper,
                           default_flow_style=False)) 
Example #4
Source File: base.py    From agents-aea with Apache License 2.0 6 votes vote down vote up
def yaml_dump(data, stream: TextIO) -> None:
    """
    Dump data to a yaml file in an ordered way.

    :param data: the data to be dumped
    :param stream: the file pointer
    """

    def ordered_dump(data, stream=None, **kwds):
        class OrderedDumper(yaml.SafeDumper):
            """A wrapper for safe yaml loader."""

            pass

        def _dict_representer(dumper, data):
            return dumper.represent_mapping(
                yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items()
            )

        OrderedDumper.add_representer(OrderedDict, _dict_representer)
        return yaml.dump(data, stream, OrderedDumper, **kwds)  # nosec

    ordered_dump(data, stream) 
Example #5
Source File: purge.py    From DeepSea with GNU General Public License v3.0 6 votes vote down vote up
def default():
    """
    Remove the .../stack/defaults directory.  Preserve available_roles
    """
    # Keep yaml human readable/editable
    friendly_dumper = yaml.SafeDumper
    friendly_dumper.ignore_aliases = lambda self, data: True

    preserve = {}
    content = None
    pathname = "/srv/pillar/ceph/stack/default/{}/cluster.yml".format('ceph')
    with open(pathname, "r") as sls_file:
        content = yaml.safe_load(sls_file)
    preserve['available_roles'] = content['available_roles']
    stack_default = "/srv/pillar/ceph/stack/default"
    shutil.rmtree(stack_default)
    os.makedirs("{}/{}".format(stack_default, 'ceph'))
    with open(pathname, "w") as sls_file:
        sls_file.write(yaml.dump(preserve, Dumper=friendly_dumper,
                       default_flow_style=False))

    uid = pwd.getpwnam("salt").pw_uid
    gid = grp.getgrnam("salt").gr_gid
    for path in [stack_default, "{}/{}".format(stack_default, 'ceph'), pathname]:
        os.chown(path, uid, gid) 
Example #6
Source File: testing.py    From cloud-custodian with Apache License 2.0 6 votes vote down vote up
def write_policy_file(self, policy, format="yaml"):
        """ Write a policy file to disk in the specified format.

        Input a dictionary and a format. Valid formats are `yaml` and `json`
        Returns the file path.
        """
        fh = tempfile.NamedTemporaryFile(mode="w+b", suffix="." + format, delete=False)
        if format == "json":
            fh.write(json.dumps(policy).encode("utf8"))
        else:
            fh.write(yaml.dump(policy, encoding="utf8", Dumper=yaml.SafeDumper))

        fh.flush()
        self.addCleanup(os.unlink, fh.name)
        self.addCleanup(fh.close)
        return fh.name 
Example #7
Source File: memory.py    From pyrpl with GNU General Public License v3.0 5 votes vote down vote up
def save(data, stream=None, Dumper=yaml.SafeDumper,
             default_flow_style=False,
             encoding='utf-8',
             **kwds):
        class OrderedDumper(Dumper):
            pass
        def _dict_representer(dumper, data):
            return dumper.represent_mapping(
                yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                data.items())
        OrderedDumper.add_representer(OrderedDict, _dict_representer)
        OrderedDumper.add_representer(np.float64,
                    lambda dumper, data: dumper.represent_float(float(data)))
        OrderedDumper.add_representer(complex,
                    lambda dumper, data: dumper.represent_str(str(data)))
        OrderedDumper.add_representer(np.complex128,
                    lambda dumper, data: dumper.represent_str(str(data)))
        OrderedDumper.add_representer(np.ndarray,
                    lambda dumper, data: dumper.represent_list(list(data)))
        # I added the following two lines to make pyrpl compatible with pyinstruments. In principle they can be erased
        if isinstance(data, dict) and not isinstance(data, OrderedDict):
            data = OrderedDict(data)
        return yaml.dump(data,
                         stream=stream,
                         Dumper=OrderedDumper,
                         default_flow_style=default_flow_style,
                         encoding=encoding,
                         **kwds)

    # usage example:
    # load(stream, yaml.SafeLoader)
    # save(data, stream=f, Dumper=yaml.SafeDumper) 
Example #8
Source File: iscsi_upgrade.py    From DeepSea with GNU General Public License v3.0 5 votes vote down vote up
def set_igw_service_daemon():
    """
    Set igw_service_daemons pillar item with ceph-iscsi daemon
    """
    __opts__ = salt.config.client_config('/etc/salt/master')
    __grains__ = salt.loader.grains(__opts__)
    __opts__['grains'] = __grains__
    __utils__ = salt.loader.utils(__opts__)
    __salt__ = salt.loader.minion_mods(__opts__, utils=__utils__)
    local = salt.client.LocalClient(mopts=__opts__)

    filename = '/srv/pillar/ceph/stack/ceph/cluster.yml'
    contents = {}
    with open(filename, 'r') as yml:
        contents = yaml.safe_load(yml)
        if not contents:
            contents = {}
        contents['igw_service_daemons'] = ['rbd-target-api']
        friendly_dumper = yaml.SafeDumper
        friendly_dumper.ignore_aliases = lambda self, data: True
        with open(filename, 'w') as yml:
            yml.write(yaml.dump(contents,
                                Dumper=friendly_dumper,
                                default_flow_style=False))

    # refresh pillar
    master = __salt__['master.minion']()
    local.cmd(master, 'saltutil.pillar_refresh')
    return True 
Example #9
Source File: populate.py    From DeepSea with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, **kwargs):
        """
        Keep yaml human readable/editable.  Disable yaml references.
        """
        self.dumper = yaml.SafeDumper
        self.dumper.ignore_aliases = lambda self, data: True

        if 'overwrite' in kwargs:
            self.overwrite = kwargs['overwrite']
        else:
            self.overwrite = False 
Example #10
Source File: lovelace_migrate.py    From python-lovelace with MIT License 5 votes vote down vote up
def dump(self):
        """Dump YAML for the Lovelace UI."""
        def ordered_dump(data, stream=None, Dumper=yaml.Dumper, **kwargs):
            """YAML dumper for OrderedDict."""

            class OrderedDumper(Dumper):
                """Wrapper class for YAML dumper."""

                def ignore_aliases(self, data):
                    """Disable aliases in YAML dump."""
                    return True

                def increase_indent(self, flow=False, indentless=False):
                    """Increase indent on YAML lists."""
                    return super(OrderedDumper, self).increase_indent(
                        flow, False)

            def _dict_representer(dumper, data):
                """Function to represent OrderDict and derivitives."""
                return dumper.represent_mapping(
                    yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
                    data.items())

            OrderedDumper.add_multi_representer(OrderedDict, _dict_representer)
            return yaml.dump(data, stream, OrderedDumper, **kwargs)

        return ordered_dump(self, Dumper=yaml.SafeDumper,
                            default_flow_style=False).strip() 
Example #11
Source File: file_utils.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def write_yaml(root, file_name, data, overwrite=False):
    """
    Write dictionary data in yaml format.

    :param root: Directory name.
    :param file_name: Desired file name. Will automatically add .yaml extension if not given
    :param data: data to be dumped as yaml format
    :param overwrite: If True, will overwrite existing files
    """
    if not exists(root):
        raise MissingConfigException("Parent directory '%s' does not exist." % root)

    file_path = os.path.join(root, file_name)
    yaml_file_name = file_path if file_path.endswith(".yaml") else file_path + ".yaml"

    if exists(yaml_file_name) and not overwrite:
        raise Exception("Yaml file '%s' exists as '%s" % (file_path, yaml_file_name))

    try:
        with codecs.open(yaml_file_name, mode='w', encoding=ENCODING) as yaml_file:
            yaml.dump(data, yaml_file,
                      default_flow_style=False,
                      allow_unicode=True,
                      Dumper=YamlSafeDumper)
    except Exception as e:
        raise e 
Example #12
Source File: yamlwrapper.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def dump(*args, **kwargs):
    """Delegate to yaml dumps.
    """
    if kwargs is None:
        kwargs = {}
    kwargs['Dumper'] = Dumper
    return yaml.dump(*args, **kwargs) 
Example #13
Source File: site_yml.py    From ceph-ansible-copilot with GNU Lesser General Public License v2.1 5 votes vote down vote up
def write_yaml(yaml_file, yaml_data):

    with open(yaml_file, "w", 0) as out:
        ordered_dump(yaml_data,
                     Dumper=yaml.SafeDumper,
                     stream=out,
                     default_flow_style=False,
                     explicit_start=True) 
Example #14
Source File: serializer.py    From bazarr with GNU General Public License v3.0 5 votes vote down vote up
def get_yaml_dumper(context):
    """Return yaml dumper that handles all needed object types."""
    class CustomDumper(yaml.SafeDumper):
        """Custom YAML Dumper."""

        def default_representer(self, data):
            """Convert data to string."""
            if isinstance(data, int):
                return self.represent_int(data)
            if isinstance(data, float):
                return self.represent_float(data)
            return self.represent_str(str(data))

        def ordered_dict_representer(self, data):
            """Representer for OrderedDict."""
            return self.represent_mapping('tag:yaml.org,2002:map', data.items())

        def default_language_representer(self, data):
            """Convert language to string."""
            return self.represent_str(format_language(data, context['profile']))

        def default_quantity_representer(self, data):
            """Convert quantity to string."""
            return self.default_representer(format_quantity(data, context['profile']))

        def default_duration_representer(self, data):
            """Convert quantity to string."""
            return self.default_representer(format_duration(data, context['profile']))

    CustomDumper.add_representer(OrderedDict, CustomDumper.ordered_dict_representer)
    CustomDumper.add_representer(babelfish.Language, CustomDumper.default_language_representer)
    CustomDumper.add_representer(timedelta, CustomDumper.default_duration_representer)
    CustomDumper.add_representer(units.Quantity, CustomDumper.default_quantity_representer)

    return CustomDumper 
Example #15
Source File: __main__.py    From cmake_format with GNU General Public License v3.0 5 votes vote down vote up
def dump_config(args, config_dict, outfile):
  """
  Dump the default configuration to stdout
  """

  outfmt = args.dump_config
  config_dict.update(get_argdict(args))
  cfg = configuration.Configuration(**config_dict)
  # Don't dump default per-command configs
  for key in standard_funs.get_default_config():
    cfg.misc.per_command.pop(key, None)

  if outfmt == 'yaml':
    import yaml
    yaml_register_odict(yaml.SafeDumper)
    yaml_register_odict(yaml.Dumper)
    yaml.dump(cfg.as_odict(args.with_help, args.with_defaults),
              outfile, indent=2,
              default_flow_style=False, sort_keys=False)
    return
  if outfmt == 'json':
    json.dump(cfg.as_odict(args.with_help, args.with_defaults),
              outfile, indent=2)
    outfile.write('\n')
    return

  cfg.dump(outfile, with_help=args.with_help, with_defaults=args.with_defaults) 
Example #16
Source File: monkey.py    From maas with GNU Affero General Public License v3.0 5 votes vote down vote up
def fix_ordereddict_yaml_representer():
    """Fix PyYAML so an OrderedDict can be dumped."""

    def dumper(dumper, data):
        return dumper.represent_mapping("tag:yaml.org,2002:map", data.items())

    yaml.add_representer(OrderedDict, dumper, Dumper=yaml.Dumper)
    yaml.add_representer(OrderedDict, dumper, Dumper=yaml.SafeDumper) 
Example #17
Source File: remarshal.py    From remarshal with MIT License 5 votes vote down vote up
def encode_yaml(data, ordered, yaml_options):
    dumper = OrderedDumper if ordered else yaml.SafeDumper
    return yaml.dump(
        data,
        None,
        dumper,
        allow_unicode=True,
        default_flow_style=False,
        encoding=None,
        **yaml_options
    )


# === Main === 
Example #18
Source File: util.py    From molecule with MIT License 5 votes vote down vote up
def increase_indent(self, flow=False, indentless=False):
        return super(SafeDumper, self).increase_indent(flow, False) 
Example #19
Source File: util.py    From molecule with MIT License 5 votes vote down vote up
def safe_dump(data):
    """
    Dump the provided data to a YAML document and returns a string.

    :param data: A string containing an absolute path to the file to parse.
    :return: str
    """
    # TODO(retr0h): Do we need to encode?
    # yaml.dump(data) produces the document as a str object in both python
    # 2 and 3.
    return yaml.dump(
        data, Dumper=SafeDumper, default_flow_style=False, explicit_start=True
    ) 
Example #20
Source File: test_storage.py    From operator with Apache License 2.0 5 votes vote down vote up
def test_is_c_dumper(self):
        dumper = storage._SimpleDumper(io.StringIO(''))
        if getattr(yaml, 'CSafeDumper', None) is not None:
            self.assertIsInstance(dumper, yaml.CSafeDumper)
        else:
            self.assertIsInstance(dumper, yaml.SafeDumper) 
Example #21
Source File: push.py    From DeepSea with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, dryrun=False):
        """
        The source is proposals_dir and the destination is pillar_dir
        """
        self.proposals_dir = "/srv/pillar/ceph/proposals"
        self.pillar_dir = "/srv/pillar/ceph"
        self.dryrun = dryrun

        # Keep yaml human readable/editable
        self.friendly_dumper = yaml.SafeDumper
        self.friendly_dumper.ignore_aliases = lambda self, data: True 
Example #22
Source File: validation.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def ToYAML(self):
    """Print validated object as simplified YAML.

    Returns:
      Object as a simplified YAML string compatible with parsing using the
      SafeLoader.
    """
    return yaml.dump(self.ToDict(),
                     default_flow_style=False,
                     Dumper=yaml.SafeDumper) 
Example #23
Source File: utilities.py    From alibuild with GNU General Public License v3.0 5 votes vote down vote up
def yamlDump(s):
  class YamlOrderedDumper(yaml.SafeDumper):
    pass
  def represent_ordereddict(dumper, data):
    rep = []
    for k,v in data.items():
      k = dumper.represent_data(k)
      v = dumper.represent_data(v)
      rep.append((k, v))
    return yaml.nodes.MappingNode(u'tag:yaml.org,2002:map', rep)
  YamlOrderedDumper.add_representer(OrderedDict, represent_ordereddict)
  return yaml.dump(s, Dumper=YamlOrderedDumper) 
Example #24
Source File: pretty_yaml.py    From boundary-layer with Apache License 2.0 5 votes vote down vote up
def _build_dumper():
    class OrderedDumper(yaml.SafeDumper):
        pass

    def _dict_representer(dumper, data):
        return dumper.represent_mapping(
            yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
            data.items())

    OrderedDumper.add_representer(OrderedDict, _dict_representer)

    return OrderedDumper 
Example #25
Source File: default_handlers.py    From python-frontmatter with MIT License 5 votes vote down vote up
def export(self, metadata, **kwargs):
        """
        Export metadata as YAML. This uses yaml.SafeDumper by default.
        """
        kwargs.setdefault("Dumper", SafeDumper)
        kwargs.setdefault("default_flow_style", False)
        kwargs.setdefault("allow_unicode", True)

        metadata = yaml.dump(metadata, **kwargs).strip()
        return u(metadata)  # ensure unicode 
Example #26
Source File: _yaml.py    From peeringdb-py with Apache License 2.0 5 votes vote down vote up
def _init():
    dumper = yaml.SafeDumper
    for cls in get_backend().CUSTOM_FIELDS:
        dumper.add_representer(cls, default_representer)
    dumper.add_representer(YamlWrap, represent_wrapped) 
Example #27
Source File: base.py    From trains-agent with Apache License 2.0 5 votes vote down vote up
def represent_ordered_dict(dumper, data):
    """
    Serializes ``OrderedDict`` to YAML by its proper order.
    Registering this function to ``yaml.SafeDumper`` enables using ``yaml.safe_dump`` with ``OrderedDict``s.
    """
    return dumper.represent_mapping(yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, data.items()) 
Example #28
Source File: utils.py    From ambassador with Apache License 2.0 5 votes vote down vote up
def dump_yaml(obj: Any, **kwargs) -> str:
    global yaml_logged_dumper

    if not yaml_logged_dumper:
        yaml_logged_dumper = True

        # logger.info("YAML: using %s dumper" % ("Python" if (yaml_dumper == yaml.SafeDumper) else "C"))

    return yaml.dump(obj, Dumper=yaml_dumper, **kwargs) 
Example #29
Source File: odyldo.py    From hiyapyco with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        yaml.SafeDumper.__init__(self, *args, **kwargs)
        yaml.representer.SafeRepresenter.add_representer(OrderedDict, type(self)._odyrepr) 
Example #30
Source File: kube-secret-editor.py    From kube-secret-editor with MIT License 5 votes vote down vote up
def main():
    NoDatesSafeLoader.remove_implicit_resolver('tag:yaml.org,2002:timestamp')
    yaml.SafeDumper.orig_represent_str = yaml.SafeDumper.represent_str
    yaml.add_representer(str, repr_str, Dumper=yaml.SafeDumper)
    fname = sys.argv[1]
    edit(fname)