Python ruamel.yaml.comments.CommentedMap() Examples

The following are 30 code examples of ruamel.yaml.comments.CommentedMap(). 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.comments , or try the search function .
Example #1
Source File: workflow.py    From scriptcwl with Apache License 2.0 7 votes vote down vote up
def __init__(self, steps_dir=None, working_dir=None):
        self.working_dir = working_dir
        if self.working_dir:
            self.working_dir = os.path.abspath(self.working_dir)
            if not os.path.exists(self.working_dir):
                os.makedirs(self.working_dir)
        self.wf_steps = CommentedMap()
        self.wf_inputs = CommentedMap()
        self.wf_outputs = CommentedMap()
        self.step_output_types = {}
        self.steps_library = StepsLibrary(working_dir=working_dir)
        self.has_workflow_step = False
        self.has_scatter_requirement = False
        self.has_multiple_inputs = False

        self._wf_closed = False

        self.load(steps_dir) 
Example #2
Source File: test_comment_manipulation.py    From opsbro with MIT License 6 votes vote down vote up
def test_before_top_map_from_scratch(self):
        from ruamel.yaml.comments import CommentedMap
        data = CommentedMap()
        data['a'] = 1
        data['b'] = 2
        data.yaml_set_start_comment('Hello\nWorld\n')
        # print(data.ca)
        # print(data.ca._items)
        compare(data, """
            {comment} Hello
            {comment} World
            a: 1
            b: 2
            """.format(comment='#')) 
Example #3
Source File: dsl_test.py    From pypyr-cli with Apache License 2.0 6 votes vote down vote up
def test_step_cant_get_run_step_dynamically_round_trip(mocked_moduleloader):
    """Step can't get run_step method on the dynamically imported module
    with round trip yaml loaded context.
    """
    stepcache.step_cache.clear()
    with pytest.raises(AttributeError) as err_info:
        with patch_logger('pypyr.dsl', logging.ERROR) as mock_logger_error:
            with patch_logger('pypyr.cache.stepcache',
                              logging.ERROR) as mock_cache_logger_error:
                commented_context = CommentedMap({'name': 'mocked.step'})
                commented_context._yaml_set_line_col(1, 2)
                Step(commented_context, None)

    mocked_moduleloader.assert_called_once_with('mocked.step')
    mock_logger_error.assert_called_once_with(
        "Error at pipeline step mocked.step yaml line: 1, col: 2")
    mock_cache_logger_error.assert_called_once_with(
        "The step mocked.step in module 3 doesn't have a "
        "run_step(context) function.")

    assert str(err_info.value) == "'int' object has no attribute 'run_step'" 
Example #4
Source File: test_anchor.py    From opsbro with MIT License 6 votes vote down vote up
def test_anchor_assigned(self):
        from ruamel.yaml.comments import CommentedMap
        data = load("""
        a: &id002
          b: 1
          c: 2
        d: *id002
        e: &etemplate
          b: 1
          c: 2
        f: *etemplate
        """)
        d = data['d']
        assert isinstance(d, CommentedMap)
        assert d.yaml_anchor() is None  # got dropped as it matches pattern
        e = data['e']
        assert isinstance(e, CommentedMap)
        assert e.yaml_anchor().value == 'etemplate'
        assert e.yaml_anchor().always_dump is False 
Example #5
Source File: __init__.py    From compose_format with MIT License 6 votes vote down vote up
def reorder(data, strict=True):
        if type(data) is CommentedMap:
            order = ComposeFormat.order_map(list(data.keys()))
            keys = list(data.keys())

            while ComposeFormat.sorted_by_order(keys, order, strict) != keys:
                for a, b in zip(ComposeFormat.sorted_by_order(keys, order, strict), keys):
                    if a == b:
                        continue
                    data.move_to_end(b)
                    break
                keys = list(data.keys())
            for key, item in data.items():
                if key in ComposeFormat.NON_SORTABLE_ARRAYS:
                    continue
                ComposeFormat.reorder(item, strict)
            return data
        if type(data) is CommentedSeq:
            for i, value in enumerate(data):
                if type(value) is not CommentedMap:
                    data[i] = ComposeFormat.fix_sexadecimal_numbers(value)
            data.sort()
            return data
        return data 
Example #6
Source File: yamllocation.py    From strictyaml with MIT License 6 votes vote down vote up
def fork(self, strictindex, new_value):
        """
        Return a chunk referring to the same location in a duplicated document.

        Used when modifying a YAML chunk so that the modification can be validated
        before changing it.
        """
        forked_chunk = YAMLChunk(
            deepcopy(self._ruamelparsed),
            pointer=self.pointer,
            label=self.label,
            key_association=copy(self._key_association),
        )
        if self.is_scalar():
            # Necessary for e.g. EmptyDict, which reports as a scalar.
            forked_chunk.pointer.set(forked_chunk, '_ruamelparsed',
                                     CommentedMap())
            forked_chunk.pointer.set(forked_chunk, '_strictparsed',
                                     CommentedMap(), strictdoc=True)
        forked_chunk.contents[self.ruamelindex(strictindex)] = new_value.as_marked_up()
        forked_chunk.strictparsed()[strictindex] = deepcopy(new_value.as_marked_up())
        return forked_chunk 
Example #7
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def is_mapping(self):
        return isinstance(self._value, CommentedMap) 
Example #8
Source File: test_comment_manipulation.py    From opsbro with MIT License 5 votes vote down vote up
def test_before_nested_seq_from_scratch(self):
        from ruamel.yaml.comments import CommentedMap, CommentedSeq
        data = CommentedMap()
        datab = CommentedSeq()
        data['a'] = 1
        data['b'] = datab
        datab.append('c')
        datab.append('d')
        data['b'].yaml_set_start_comment('Hello\nWorld\n', indent=2)
        compare(data, """
        a: 1
        b:
          {comment} Hello
          {comment} World
          - c
          - d
        """.format(comment='#')) 
Example #9
Source File: compound.py    From strictyaml with MIT License 5 votes vote down vote up
def to_yaml(self, data):
        self._should_be_mapping(data)
        # TODO : Maximum minimum keys
        return CommentedMap(
            [
                (self._key_validator.to_yaml(key), self._value_validator.to_yaml(value))
                for key, value in data.items()
            ]
        ) 
Example #10
Source File: compound.py    From strictyaml with MIT License 5 votes vote down vote up
def to_yaml(self, data):
        self._should_be_mapping(data)
        # TODO : if keys not in list or required keys missing, raise exception.
        return CommentedMap(
            [
                (key, self._validator_dict[key].to_yaml(value))
                for key, value in data.items()
                if key not in self._defaults.keys()
                or key in self._defaults.keys()
                and value != self._defaults[key]
            ]
        ) 
Example #11
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def data(self):
        """
        Returns raw data representation of the document or document segment.

        Mappings are rendered as ordered dicts, sequences as lists and scalar values
        as whatever the validator returns (int, string, etc.).

        If no validators are used, scalar values are always returned as strings.
        """
        if isinstance(self._value, CommentedMap):
            mapping = OrderedDict()
            for key, value in self._value.items():
                """
                #if isinstance(key, YAML):
                    #mapping[key.data] = value.data if isinstance(value, YAML) else value
                """
                mapping[key.data] = value.data
            return mapping
        elif isinstance(self._value, CommentedSeq):
            return [item.data for item in self._value]
        else:
            return self._value 
Example #12
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def as_marked_up(self):
        """
        Returns ruamel.yaml CommentedSeq/CommentedMap objects
        with comments. This can be fed directly into a ruamel.yaml
        dumper.
        """
        return self._chunk.contents 
Example #13
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def items(self):
        if not isinstance(self._value, CommentedMap):
            raise TypeError("{0} not a mapping, cannot use .items()".format(repr(self)))
        return [(key, self._value[key]) for key, value in self._value.items()] 
Example #14
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def values(self):
        if not isinstance(self._value, CommentedMap):
            raise TypeError(
                "{0} not a mapping, cannot use .values()".format(repr(self))
            )
        return [self._value[key] for key, value in self._value.items()] 
Example #15
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def get(self, index, default=None):
        if not isinstance(self._value, CommentedMap):
            raise TypeError("{0} not a mapping, cannot use .get()".format(repr(self)))
        return self._value[index] if index in self._value.keys() else default 
Example #16
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def __contains__(self, item):
        if isinstance(self._value, CommentedSeq):
            return item in self._value
        elif isinstance(self._value, CommentedMap):
            return item in self.keys()
        else:
            return item in self._value 
Example #17
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def text(self):
        """
        Return string value of scalar, whatever value it was parsed as.
        """
        if isinstance(self._value, CommentedMap):
            raise TypeError("{0} is a mapping, has no text value.".format(repr(self)))
        if isinstance(self._value, CommentedSeq):
            raise TypeError("{0} is a sequence, has no text value.".format(repr(self)))
        return self._text 
Example #18
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def __gt__(self, val):
        if isinstance(self._value, CommentedMap) or isinstance(
            self._value, CommentedSeq
        ):
            raise TypeError("{0} not an orderable type.".format(repr(self._value)))
        return self._value > val 
Example #19
Source File: __main__.py    From maubot with GNU Affero General Public License v3.0 5 votes vote down vote up
def load_base() -> RecursiveDict[CommentedMap]:
        return RecursiveDict(config.load_base()["plugin_config"], CommentedMap) 
Example #20
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def scalar(self):
        if isinstance(self._value, (CommentedMap, CommentedSeq)):
            raise TypeError("{0} has no scalar value.".format(repr(self)))
        return self._value 
Example #21
Source File: yamllocation.py    From strictyaml with MIT License 5 votes vote down vote up
def is_mapping(self):
        return isinstance(self.contents, CommentedMap) 
Example #22
Source File: yamllocation.py    From strictyaml with MIT License 5 votes vote down vote up
def is_scalar(self):
        return not isinstance(self.contents, (CommentedMap, CommentedSeq)) 
Example #23
Source File: any_validator.py    From strictyaml with MIT License 5 votes vote down vote up
def schema_from_document(document):
    if isinstance(document, CommentedMap):
        return Map(
            {key: schema_from_document(value) for key, value in document.items()}
        )
    elif isinstance(document, CommentedSeq):
        return FixedSeq([schema_from_document(item) for item in document])
    else:
        return Str() 
Example #24
Source File: parser.py    From strictyaml with MIT License 5 votes vote down vote up
def generic_load(
    yaml_string, schema=None, label=u"<unicode string>", allow_flow_style=False
):
    if not utils.is_string(yaml_string):
        raise TypeError("StrictYAML can only read a string of valid YAML.")

    # We manufacture a class that has the label we want
    DynamicStrictYAMLLoader = type(
        "DynamicStrictYAMLLoader",
        (StrictYAMLLoader,),
        {"label": label, "allow_flow_style": allow_flow_style},
    )

    try:
        document = ruamelyaml.load(yaml_string, Loader=DynamicStrictYAMLLoader)
    except ruamelyaml.YAMLError as parse_error:
        if parse_error.context_mark is not None:
            parse_error.context_mark.name = label
        if parse_error.problem_mark is not None:
            parse_error.problem_mark.name = label

        raise parse_error

    # Document is just a (string, int, etc.)
    if type(document) not in (CommentedMap, CommentedSeq):
        document = yaml_string

    if schema is None:
        schema = Any()

    return schema(YAMLChunk(document, label=label)) 
Example #25
Source File: openapi.py    From dactyl with MIT License 5 votes vote down vote up
def deref_swag(self):
        """
        Walk the OpenAPI specification for $ref objects and resolve them to
        the values they reference. Assumes the entire spec is contained in a
        single file.
        """

        def deref_yaml(yaml_value):
            if "keys" in dir(yaml_value): # Dictionary-like type
                if "$ref" in yaml_value.keys():
                    # It's a reference; deref it
                    reffed_value = self.deref(yaml_value["$ref"])
                    # The referenced object may contain more references, so
                    # resolve those before returning
                    return deref_yaml(reffed_value)
                else:
                    # recurse through each key/value pair looking for refs
                    the_copy = YamlMap()
                    for k,v in yaml_value.items():
                        the_copy[k] = deref_yaml(v)
                    return the_copy
            elif "append" in dir(yaml_value): # List-like type
                # recurse through each item looking for refs
                the_copy = YamlSeq()
                for item in yaml_value:
                    the_copy.append(deref_yaml(item))
                return the_copy
            else: # Probably a basic type
                # base case: return the value
                return yaml_value

        self.swag = deref_yaml(self.swag) 
Example #26
Source File: dsl_test.py    From pypyr-cli with Apache License 2.0 5 votes vote down vote up
def test_complex_step_init_with_missing_name_round_trip():
    """Step can't get step name from the yaml pipeline."""
    with pytest.raises(PipelineDefinitionError) as err_info:
        with patch_logger('pypyr.dsl', logging.ERROR) as mock_logger_error:
            step_info = CommentedMap({})
            step_info._yaml_set_line_col(6, 7)
            Step(step_info, None)

    assert mock_logger_error.call_count == 1
    assert mock_logger_error.mock_calls == [
        call('Error at pipeline step yaml line: 6, col: 7'),
    ]

    assert str(err_info.value) == "step must have a name." 
Example #27
Source File: dsl_test.py    From pypyr-cli with Apache License 2.0 5 votes vote down vote up
def test_run_pipeline_steps_complex_round_trip(mock_invoke_step,
                                               mock_get_module):
    """Complex step with swallow false raises error."""
    complex_step_info = CommentedMap({
        'name': 'step1',
        'swallow': 0
    })

    complex_step_info._yaml_set_line_col(5, 6)

    step = Step(complex_step_info, None)

    context = get_test_context()
    original_len = len(context)

    with patch_logger('pypyr.dsl', logging.ERROR) as mock_logger_error:
        with pytest.raises(ValueError) as err_info:
            step.run_step(context)

            assert str(err_info.value) == "arb error here"

    mock_logger_error.assert_called_once_with(
        "Error while running step step1 at pipeline yaml line: 5, col: 6")

    # validate all the in params ended up in context as intended,
    # plus runErrors
    assert len(context) == original_len + 1
    assert context['runErrors'] == [{
        'col': 6,
        'customError': {},
        'description': 'arb error here',
        'exception': err_info.value,
        'line': 5,
        'name': 'ValueError',
        'step': step.name,
        'swallowed': False,
    }] 
Example #28
Source File: generate_bear_metadata.py    From coala-bears with GNU Affero General Public License v3.0 5 votes vote down vote up
def _create_sorted_commented_map(input_dict):
    if not input_dict:
        return CommentedMap()
    return CommentedMap(sorted(input_dict.items(),
                               key=lambda t: t[0])) 
Example #29
Source File: cfml_syntax.py    From sublimetext-cfml with MIT License 5 votes vote down vote up
def order_output(syntax):
    if isinstance(syntax, dict):
        ordered_syntax = CommentedMap()
        for key in ORDERED_KEYS:
            if key in syntax:
                ordered_syntax[key] = order_output(syntax[key])
        for key in sorted(syntax):
            if key not in ORDERED_KEYS:
                ordered_syntax[key] = order_output(syntax[key])
        return ordered_syntax
    if isinstance(syntax, list):
        return [order_output(item) for item in syntax]
    return syntax 
Example #30
Source File: test_anchor.py    From opsbro with MIT License 5 votes vote down vote up
def test_merge_accessible(self):
        from ruamel.yaml.comments import CommentedMap, merge_attrib
        data = load("""
        k: &level_2 { a: 1, b2 }
        l: &level_1 { a: 10, c: 3 }
        m:
          << : *level_1
          c: 30
          d: 40
        """)
        d = data['m']
        assert isinstance(d, CommentedMap)
        assert hasattr(d, merge_attrib)