Python ruamel.yaml.RoundTripDumper() Examples

The following are 28 code examples of ruamel.yaml.RoundTripDumper(). 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: oc_obj.py    From ansible-redhat_openshift_utils with Apache License 2.0 6 votes vote down vote up
def create_tmp_file_from_contents(rname, data, ftype='yaml'):
        ''' create a file in tmp with name and contents'''

        tmp = Utils.create_tmpfile(prefix=rname)

        if ftype == 'yaml':
            # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
            # pylint: disable=no-member
            if hasattr(yaml, 'RoundTripDumper'):
                Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
            else:
                Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))

        elif ftype == 'json':
            Utils._write(tmp, json.dumps(data))
        else:
            Utils._write(tmp, data)

        # Register cleanup when module is done
        atexit.register(Utils.cleanup, [tmp])
        return tmp 
Example #2
Source File: oc_obj.py    From ansible-redhat_openshift_utils with Apache License 2.0 6 votes vote down vote up
def create(self, files=None, content=None):
        '''
           Create a config

           NOTE: This creates the first file OR the first conent.
           TODO: Handle all files and content passed in
        '''
        if files:
            return self._create(files[0])

        # pylint: disable=no-member
        # The purpose of this change is twofold:
        # - we need a check to only use the ruamel specific dumper if ruamel is loaded
        # - the dumper or the flow style change is needed so openshift is able to parse
        # the resulting yaml, at least until gopkg.in/yaml.v2 is updated
        if hasattr(yaml, 'RoundTripDumper'):
            content['data'] = yaml.dump(content['data'], Dumper=yaml.RoundTripDumper)
        else:
            content['data'] = yaml.safe_dump(content['data'], default_flow_style=False)

        content_file = Utils.create_tmp_files_from_contents(content)[0]

        return self._create(content_file['path'])

    # pylint: disable=too-many-function-args 
Example #3
Source File: oc_obj.py    From ansible-redhat_openshift_utils with Apache License 2.0 6 votes vote down vote up
def create_tmp_file_from_contents(rname, data, ftype='yaml'):
        ''' create a file in tmp with name and contents'''

        tmp = Utils.create_tmpfile(prefix=rname)

        if ftype == 'yaml':
            # AUDIT:no-member makes sense here due to ruamel.YAML/PyYAML usage
            # pylint: disable=no-member
            if hasattr(yaml, 'RoundTripDumper'):
                Utils._write(tmp, yaml.dump(data, Dumper=yaml.RoundTripDumper))
            else:
                Utils._write(tmp, yaml.safe_dump(data, default_flow_style=False))

        elif ftype == 'json':
            Utils._write(tmp, json.dumps(data))
        else:
            Utils._write(tmp, data)

        # Register cleanup when module is done
        atexit.register(Utils.cleanup, [tmp])
        return tmp 
Example #4
Source File: shyaml.py    From smarthome with GNU General Public License v3.0 6 votes vote down vote up
def yaml_save_roundtrip(filename, data, create_backup=False):
    """
    Dump yaml using the RoundtripDumper and correct linespacing in output file

    :param filename: name of the yaml file to save to
    :param data: data structure to save
    """

    if not EDITING_ENABLED:
        return
    sdata = yaml.dump(data, Dumper=yaml.RoundTripDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=block_seq_indent, width=12288, allow_unicode=True)
    sdata = _format_yaml_dump2( sdata )

    if not filename.lower().endswith('.yaml'):
        filename += YAML_FILE
    if create_backup:
        if os.path.isfile(filename):
            shutil.copy2(filename, filename+'.bak')

    with open(filename, 'w') as outfile:
        outfile.write( sdata ) 
Example #5
Source File: pipelined_model.py    From FATE with Apache License 2.0 6 votes vote down vote up
def update_component_meta(self, component_name, component_module_name, model_alias, model_proto_index):
        """
        update meta info yaml
        TODO: with lock
        :param component_name:
        :param component_module_name:
        :param model_alias:
        :param model_proto_index:
        :return:
        """
        with open(self.define_meta_path, "r", encoding="utf-8") as fr:
            define_index = yaml.safe_load(fr)
        with open(self.define_meta_path, "w", encoding="utf-8") as fw:
            define_index["component_define"] = define_index.get("component_define", {})
            define_index["component_define"][component_name] = define_index["component_define"].get(component_name, {})
            define_index["component_define"][component_name].update({"module_name": component_module_name})
            define_index["model_proto"] = define_index.get("model_proto", {})
            define_index["model_proto"][component_name] = define_index["model_proto"].get(component_name, {})
            define_index["model_proto"][component_name][model_alias] = define_index["model_proto"][component_name].get(model_alias, {})
            define_index["model_proto"][component_name][model_alias].update(model_proto_index)
            yaml.dump(define_index, fw, Dumper=yaml.RoundTripDumper) 
Example #6
Source File: schema_handlers.py    From flask-assistant with Apache License 2.0 5 votes vote down vote up
def create_entity_skeleton(self):
        print('Creating Template for Entities')
        template = os.path.join(self.template_dir, 'entities.yaml')
        message = """# Template file for entities\n\n"""

        skeleton = {}
        for intent in self.assist._intent_action_funcs:
            entity_map = self.assist._intent_mappings.get(intent)
            action_func = self.assist._intent_action_funcs[intent][0]
            args = inspect.getargspec(action_func).args

            # dont add API 'sys' entities to the template
            if entity_map:
                args = [a for a in args if 'sys.' not in entity_map.get(a, [])]

            for param in [p for p in args if p not in skeleton]:
                skeleton[param] = [None, None]

        with open(template, 'w') as f:
            f.write(message)
            f.write('#Format as below\n\n')
            f.write("# entity_name:\n")
            f.write("#  - entry1: list of synonyms \n")
            f.write("#  - entry2: list of synonyms \n\n")
            f.write("#For example:\n\n")
            f.write("# drink:\n")
            f.write("#  - water: ['aqua', 'h20'] \n")
            f.write("#  - coffee: ['joe', 'caffeine', 'espresso', 'late'] \n")
            f.write("#  - soda: ['pop', 'coke']\n\n\n\n")
            yaml.dump(skeleton, f, default_flow_style=False, Dumper=yaml.RoundTripDumper) 
Example #7
Source File: json2yaml.py    From packer-build with GNU General Public License v3.0 5 votes vote down vote up
def main():
    print('---')
    print(yaml.dump(json.load(sys.stdin), Dumper=yaml.RoundTripDumper)) 
Example #8
Source File: __init__.py    From compose_format with MIT License 5 votes vote down vote up
def format_string(self, data, replace=False, strict=True):
        data = self.reorder(load(data, RoundTripLoader), strict=strict)
        formatted = dump(data, Dumper=RoundTripDumper, indent=2, width=120)

        return formatted.strip() + '\n' 
Example #9
Source File: config.py    From jd4 with GNU Affero General Public License v3.0 5 votes vote down vote up
def save_config():
    def do_save_config():
        with open(_CONFIG_FILE, 'w', encoding='utf-8') as file:
            yaml.dump(config, file, Dumper=yaml.RoundTripDumper)

    await get_event_loop().run_in_executor(None, do_save_config) 
Example #10
Source File: yamlpointer.py    From strictyaml with MIT License 5 votes vote down vote up
def lines_after(self, document, how_many):
        return "\n".join(
            dump(document, Dumper=RoundTripDumper).split("\n")[
                self.end_line(document) : self.end_line(document) + how_many
            ]
        ) 
Example #11
Source File: yamlpointer.py    From strictyaml with MIT License 5 votes vote down vote up
def lines_before(self, document, how_many):
        return "\n".join(
            dump(document, Dumper=RoundTripDumper).split("\n")[
                self.start_line(document) - 1 - how_many : self.start_line(document) - 1
            ]
        ) 
Example #12
Source File: yamlpointer.py    From strictyaml with MIT License 5 votes vote down vote up
def lines(self, document):
        return "\n".join(
            dump(document, Dumper=RoundTripDumper).split("\n")[
                self.start_line(document) - 1 : self.end_line(document)
            ]
        ) 
Example #13
Source File: yamlpointer.py    From strictyaml with MIT License 5 votes vote down vote up
def end_line(self, document):
        slicedpart = self._slice_segment(self._indices, document, include_selected=True)
        return len(dump(slicedpart, Dumper=RoundTripDumper).rstrip().split("\n")) 
Example #14
Source File: yamlpointer.py    From strictyaml with MIT License 5 votes vote down vote up
def start_line(self, document):
        slicedpart = self._slice_segment(
            self._indices, document, include_selected=False
        )

        if slicedpart is None or slicedpart == {} or slicedpart == []:
            return 1
        else:
            return (
                len(dump(slicedpart, Dumper=RoundTripDumper).rstrip().split("\n")) + 1
            ) 
Example #15
Source File: controller.py    From auto-tikv with Apache License 2.0 5 votes vote down vote up
def set_tikvyml(knob_sessname, knob_val):
    knob_sess=knob_sessname.split('.')[0:-1]
    knob_name=knob_sessname.split('.')[-1]
    ymldir=os.path.join(ansibledir,"conf","tikv_newcfg.yml")
    tmpdir=os.path.join(ansibledir,"conf","tikv.yml")
    tmpf=open(tmpdir)
    tmpcontent=yaml.load(tmpf, Loader=yaml.RoundTripLoader)
    if(knob_set[knob_sessname]['type']=='enum'):
        idx=knob_val
        knob_val=knob_set[knob_sessname]['enumval'][idx]
    if(knob_set[knob_sessname]['type']=='bool'):
        if(knob_val==0):
            knob_val=False
        else:
            knob_val=True
    if(knob_name=='block-size'):
        knob_val=str(knob_val)+"KB"
    if(knob_name=='write-buffer-size' or knob_name=='max-bytes-for-level-base' or knob_name=='target-file-size-base'):
        knob_val=str(knob_val)+"MB"
    if(knob_name in tmpcontent[knob_sess[0]][knob_sess[1]]):        # TODO: only support 2 level of knob_sess currently
        tmpcontent[knob_sess[0]][knob_sess[1]][knob_name]=knob_val
    else:
        return('failed')
    print("set_tikvyml:: ",knob_sessname, knob_sess, knob_name, knob_val)
    ymlf=open(ymldir, 'w')
    yaml.dump(tmpcontent, ymlf, Dumper=yaml.RoundTripDumper)
    os.popen("rm "+tmpdir+" && "+"mv "+ymldir+" "+tmpdir)
    time.sleep(0.5)
    return('success') 
Example #16
Source File: config.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def dump_config(config_path, config, dumper=yaml.RoundTripDumper):
    with codecs.open(config_path, mode='w', encoding='utf-8') as stream:
        stream.write(to_utf8(yaml.dump(config, Dumper=dumper))) 
Example #17
Source File: config.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def dump_config(config_path, config, dumper=yaml.RoundTripDumper):
    with codecs.open(config_path, mode='w', encoding='utf-8') as file:
        file.write(yaml.dump(config, Dumper=dumper)) 
Example #18
Source File: __main__.py    From InplusTrader_Linux with MIT License 5 votes vote down vote up
def list():
    """
    List all mod configuration
    """
    config_path = get_default_config_path()
    config = load_config(config_path, loader=yaml.RoundTripLoader)

    print(yaml.dump(config['mod'], Dumper=yaml.RoundTripDumper)) 
Example #19
Source File: yamlutils.py    From scriptcwl with Apache License 2.0 5 votes vote down vote up
def yaml2string(wf, pack, relpath, wd):
    s = [u'#!/usr/bin/env cwl-runner',
         yaml.dump(wf.to_obj(pack=pack, relpath=relpath, wd=wd),
                   Dumper=yaml.RoundTripDumper)]
    return u'\n'.join(s) 
Example #20
Source File: yolo_file.py    From yolo with Apache License 2.0 5 votes vote down vote up
def render(self, **variables):
        # Render variables into the yolo file.
        template = jinja2.Template(
            yaml.dump(self._raw_content, Dumper=yaml.RoundTripDumper)
        )
        rendered_content = template.render(**variables)
        new_content = yaml.safe_load(rendered_content)
        return YoloFile(new_content) 
Example #21
Source File: yolo_file.py    From yolo with Apache License 2.0 5 votes vote down vote up
def to_fileobj(self):
        """Dump this `YoloFile` contents to file-like object."""
        fp = utils.StringIO()
        yaml.dump(self._raw_content, fp, encoding='utf-8',
                  Dumper=yaml.RoundTripDumper)
        fp.seek(0)
        return fp 
Example #22
Source File: pipelined_model.py    From FATE with Apache License 2.0 5 votes vote down vote up
def create_pipelined_model(self):
        if os.path.exists(self.model_path):
            raise Exception("Model creation failed because it has already been created, model cache path is {}".format(
                self.model_path
            ))
        else:
            os.makedirs(self.model_path, exist_ok=False)
        for path in [self.variables_index_path, self.variables_data_path]:
            os.makedirs(path, exist_ok=False)
        shutil.copytree(os.path.join(file_utils.get_project_base_directory(), "federatedml", "protobuf", "proto"), self.define_proto_path)
        with open(self.define_meta_path, "w", encoding="utf-8") as fw:
            yaml.dump({"describe": "This is the model definition meta"}, fw, Dumper=yaml.RoundTripDumper) 
Example #23
Source File: shyaml.py    From smarthome with GNU General Public License v3.0 5 votes vote down vote up
def yaml_dump_roundtrip(data):
    """
    Dump yaml to a string using the RoundtripDumper and correct linespacing in output file

    :param data: data structure to save
    """

    sdata = yaml.dump(data, Dumper=yaml.RoundTripDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=block_seq_indent, width=12288, allow_unicode=True)
    sdata = _format_yaml_dump2( sdata )
    return sdata 
Example #24
Source File: item_conversion.py    From smarthome with GNU General Public License v3.0 5 votes vote down vote up
def _yaml_save_roundtrip(filename, data):
    """
    Dump yaml using the RoundtripDumper and correct linespacing in output file
    """

    sdata = yaml.dump(data, Dumper=yaml.RoundTripDumper, version=yaml_version, indent=indent_spaces, block_seq_indent=2, width=32768, allow_unicode=True)

    ldata = sdata.split('\n')
    rdata = []
    for index, line in enumerate(ldata):
        # Fix for ruamel.yaml handling: Reinsert empty line before comment of next section
        if len(line.lstrip()) > 0 and line.lstrip()[0] == '#':
            indentcomment = len(line) - len(line.lstrip(' '))
            indentprevline = len(ldata[index-1]) - len(ldata[index-1].lstrip(' '))
            if indentprevline - indentcomment >= 2*indent_spaces:
                rdata.append('')
            rdata.append(line)
        # Fix for ruamel.yaml handling: Remove empty line with spaces that have been inserted
        elif line.strip() == '' and line != '':
            if ldata[index-1] != '':
                rdata.append(line)
        else:
            rdata.append(line)

    sdata = '\n'.join(rdata)
    if sdata[0] == '\n':
        sdata =sdata[1:]

    with open(filename+'.yaml', 'w') as outfile:
        outfile.write( sdata ) 
Example #25
Source File: oc_obj.py    From ansible-redhat_openshift_utils with Apache License 2.0 5 votes vote down vote up
def write(self):
        ''' write to file '''
        if not self.filename:
            raise YeditException('Please specify a filename.')

        if self.backup and self.file_exists():
            shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))

        # Try to set format attributes if supported
        try:
            self.yaml_dict.fa.set_block_style()
        except AttributeError:
            pass

        # Try to use RoundTripDumper if supported.
        if self.content_type == 'yaml':
            try:
                Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
            except AttributeError:
                Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
        elif self.content_type == 'json':
            Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
        else:
            raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
                                 'Please specify a content_type of yaml or json.')

        return (True, self.yaml_dict) 
Example #26
Source File: oc_obj.py    From ansible-redhat_openshift_utils with Apache License 2.0 5 votes vote down vote up
def write(self):
        ''' write to file '''
        if not self.filename:
            raise YeditException('Please specify a filename.')

        if self.backup and self.file_exists():
            shutil.copy(self.filename, '{}{}'.format(self.filename, self.backup_ext))

        # Try to set format attributes if supported
        try:
            self.yaml_dict.fa.set_block_style()
        except AttributeError:
            pass

        # Try to use RoundTripDumper if supported.
        if self.content_type == 'yaml':
            try:
                Yedit._write(self.filename, yaml.dump(self.yaml_dict, Dumper=yaml.RoundTripDumper))
            except AttributeError:
                Yedit._write(self.filename, yaml.safe_dump(self.yaml_dict, default_flow_style=False))
        elif self.content_type == 'json':
            Yedit._write(self.filename, json.dumps(self.yaml_dict, indent=4, sort_keys=True))
        else:
            raise YeditException('Unsupported content_type: {}.'.format(self.content_type) +
                                 'Please specify a content_type of yaml or json.')

        return (True, self.yaml_dict) 
Example #27
Source File: schema_handlers.py    From flask-assistant with Apache License 2.0 5 votes vote down vote up
def create_user_says_skeleton(self):
        template = os.path.join(self.template_dir, 'user_says.yaml')

        skeleton = {}
        for intent in self.assist._intent_action_funcs:
            # print(type(intent))
            entity_map_from_action = self.assist._intent_mappings.get(intent, {})

            d = yaml.compat.ordereddict()
            d['UserSays'] = [None, None]
            d['Annotations'] = [None, None]
            d['Events'] = [None]

            # d['Annotations'] = self.parse_annotations_from_action_mappings(intent)

            data = yaml.comments.CommentedMap(d)  # to preserve order w/o tags
            skeleton[intent] = data

        with open(template, 'a') as f:
            f.write('# Template for defining UserSays examples\n\n')
            f.write('# give-color-intent:\n\n')
            f.write('#  UserSays:\n')
            f.write('#    - My color is blue\n')
            f.write('#    - red is my favorite color\n\n')
            f.write('#  Annotations:\n')
            f.write('#    - blue: sys.color     # maps param value -> entity\n')
            f.write('#    - red: sys.color\n\n')
            f.write('#  Events:\n')
            f.write('#    - event1              # adds a triggerable event named \'event1\' to the intent\n\n\n\n')
            # f.write(header)
            yaml.dump(skeleton, f, default_flow_style=False, Dumper=yaml.RoundTripDumper) 
Example #28
Source File: update-uc.py    From rdoinfo with Apache License 2.0 4 votes vote down vote up
def update_uc():
    # uc = load_uc(filter_oslo_clients)
    uc = load_uc(filter_all_minus_tripleo)
    uc_projects = uc.keys()

    with open(RDO, 'rb') as infile:
        info = yaml.load(infile, Loader=yaml.RoundTripLoader)
    DEFAULT_RELEASES = info['package-default']['tags']
    RELEASES_PUPPET = info['package-configs']['rpmfactory-puppet']['tags']
    for pkg in info['packages']:
        project = pkg['project']
        if project in uc_projects:
            new_version = uc[project]
            # "Setting %s to version %s" % (project, new_version)
            if 'tags' in pkg:
                tags = pkg['tags']
                if 'version-locked' in tags or 'under-review' in tags:
                    print("Not updating %s, it is version-locked or under"
                          " review" % project)
                    continue
                prev_version = tags.get(UC_RELEASE)
                if prev_version:
                    prev_version = prev_version.get(SOURCE_BRANCH)
            else:
                if project.startswith('puppet'):
                    tags = copy.copy(RELEASES_PUPPET)
                else:
                    tags = copy.copy(DEFAULT_RELEASES)
                prev_version = None
            if 'tags' in pkg and UC_RELEASE not in pkg['tags']:
                print("Not updating %s, it is not included in release %s"
                      % (project, UC_RELEASE))
                continue
            tags[UC_RELEASE] = {SOURCE_BRANCH: new_version}
            if prev_version:
                if prev_version != new_version:
                    print("%s updated from %s to %s" %
                          (project, prev_version, new_version))
                else:
                    print("%s %s already up to date" %
                          (project, new_version))
            else:
                print("%s first time pin to %s" %
                      (project, new_version))

            pkg['tags'] = tags
            uc_projects.remove(project)
        else:
            # "%s not found in upper-constraints" % project
            pass

    # "Projects not in rdoinfo: %s" % string.join(uc_projects, ' ')

    with open(RDO, 'w') as outfile:
        outfile.write(yaml.dump(info, Dumper=yaml.RoundTripDumper, indent=2))