Python ruamel.yaml.comments.CommentedSeq() Examples

The following are 24 code examples of ruamel.yaml.comments.CommentedSeq(). 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: __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 #2
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 #3
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 #4
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 #5
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 #6
Source File: utils.py    From strictyaml with MIT License 5 votes vote down vote up
def ruamel_structure(data, validator=None):
    """
    Take dicts and lists and return a ruamel.yaml style
    structure of CommentedMaps, CommentedSeqs and
    data.

    If a validator is presented and the type is unknown,
    it is checked against the validator to see if it will
    turn it back in to YAML.
    """
    if isinstance(data, dict):
        if len(data) == 0:
            raise exceptions.CannotBuildDocumentsFromEmptyDictOrList(
                "Document must be built with non-empty dicts and lists"
            )
        return CommentedMap(
            [
                (ruamel_structure(key), ruamel_structure(value))
                for key, value in data.items()
            ]
        )
    elif isinstance(data, list):
        if len(data) == 0:
            raise exceptions.CannotBuildDocumentsFromEmptyDictOrList(
                "Document must be built with non-empty dicts and lists"
            )
        return CommentedSeq([ruamel_structure(item) for item in data])
    elif isinstance(data, bool):
        return u"yes" if data else u"no"
    elif isinstance(data, (int, float)):
        return str(data)
    else:
        if not is_string(data):
            raise exceptions.CannotBuildDocumentFromInvalidData(
                (
                    "Document must be built from a combination of:\n"
                    "string, int, float, bool or nonempty list/dict\n\n"
                    "Instead, found variable with type '{}': '{}'"
                ).format(type(data).__name__, data)
            )
        return data 
Example #7
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 #8
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 #9
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def is_sequence(self):
        return isinstance(self._value, CommentedSeq) 
Example #10
Source File: representation.py    From strictyaml with MIT License 5 votes vote down vote up
def __lt__(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 #11
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 #12
Source File: operator.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def is_list(obj):
    """
    Check if object is a list or the ruamel.yaml equivalent of a list
    """
    return isinstance(obj, (list, CommentedSeq)) 
Example #13
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 #14
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 #15
Source File: compound.py    From strictyaml with MIT License 5 votes vote down vote up
def to_yaml(self, data):
        self._should_be_list(data)

        if len(set(data)) < len(data):
            raise YAMLSerializationError(
                (
                    "Expecting all unique items, "
                    "but duplicates were found in '{}'.".format(data)
                )
            )

        return CommentedSeq([self._validator.to_yaml(item) for item in data]) 
Example #16
Source File: compound.py    From strictyaml with MIT License 5 votes vote down vote up
def to_yaml(self, data):
        self._should_be_list(data)
        # TODO : Different length string
        return CommentedSeq(
            [validator.to_yaml(item) for item, validator in zip(data, self._validators)]
        ) 
Example #17
Source File: compound.py    From strictyaml with MIT License 5 votes vote down vote up
def to_yaml(self, data):
        self._should_be_list(data)
        return CommentedSeq([self._validator.to_yaml(item) for item in data]) 
Example #18
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 #19
Source File: test_comment_manipulation.py    From opsbro with MIT License 5 votes vote down vote up
def test_before_top_seq_from_scratch(self):
        from ruamel.yaml.comments import CommentedSeq
        data = CommentedSeq()
        data.append('a')
        data.append('b')
        data.yaml_set_start_comment('Hello\nWorld\n')
        print(round_trip_dump(data))
        compare(data, """
        {comment} Hello
        {comment} World
        - a
        - b
        """.format(comment='#'))

    # nested variants 
Example #20
Source File: test_anchor.py    From opsbro with MIT License 5 votes vote down vote up
def test_anchor_on_sequence(self):
        # as reported by Bjorn Stabell
        # https://bitbucket.org/ruamel/yaml/issue/7/anchor-names-not-preserved
        from ruamel.yaml.comments import CommentedSeq
        data = load("""
        nut1: &alice
         - 1
         - 2
        nut2: &blake
         - some data
         - *alice
        nut3:
         - *blake
         - *alice
        """)
        l = data['nut1']
        assert isinstance(l, CommentedSeq)
        assert l.yaml_anchor() is not None
        assert l.yaml_anchor().value == 'alice' 
Example #21
Source File: test_operator.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def delete_all_annotations(obj):
    if isinstance(obj, (dict, CommentedMap)):
        obj.get("metadata", {}).pop("annotations", None)
        for v in obj.values():
            delete_all_annotations(v)
    elif isinstance(obj, (list, CommentedSeq)):
        for item in obj:
            delete_all_annotations(item) 
Example #22
Source File: operator.py    From atomic-reactor with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def set_related_images(self):
        """
        Find pullspecs in predefined locations and put all of them in the
        .spec.relatedImages section (if it already exists, clear it first)
        """
        named_pullspecs = self._named_pullspecs()

        by_name = OrderedDict()
        conflicts = []

        for new in named_pullspecs:
            # Keep track only of the first instance with a given name.
            # Ideally, existing relatedImages should come first in the list,
            # otherwise error messages could be confusing.
            old = by_name.setdefault(new.name, new)
            # Check for potential conflict (same name, different image)
            if new.image != old.image:
                msg = ("{old.description}: {old.image} X {new.description}: {new.image}"
                       .format(old=old, new=new))
                conflicts.append(msg)

        if conflicts:
            raise RuntimeError("{} - Found conflicts when setting relatedImages:\n{}"
                               .format(self.path, "\n".join(conflicts)))

        related_images = (self.data.setdefault("spec", CommentedMap())
                                   .setdefault("relatedImages", CommentedSeq()))
        del related_images[:]

        for p in by_name.values():
            log.debug("%s - Set relatedImage %s (from %s): %s",
                      self.path, p.name, p.description, p.image)
            related_images.append(p.as_yaml_object()) 
Example #23
Source File: yamlpointer.py    From strictyaml with MIT License 4 votes vote down vote up
def _slice_segment(self, indices, segment, include_selected):
        slicedpart = deepcopy(segment)

        if len(indices) == 0 and not include_selected:
            slicedpart = None
        else:
            if len(indices) > 0:
                if indices[0][0] in ("val", "key"):
                    index = indices[0][1][0]
                else:
                    index = indices[0][1]
                start_popping = False

                if isinstance(segment, CommentedMap):
                    for key in segment.keys():
                        if start_popping:
                            slicedpart.pop(key)

                        if index == key:
                            start_popping = True

                            if isinstance(segment[index], (CommentedSeq, CommentedMap)):
                                slicedpart[index] = self._slice_segment(
                                    indices[1:],
                                    segment[index],
                                    include_selected=include_selected,
                                )

                            if not include_selected and len(indices) == 1:
                                slicedpart.pop(key)

                if isinstance(segment, CommentedSeq):
                    for i, value in enumerate(segment):
                        if start_popping:
                            del slicedpart[-1]

                        if i == index:
                            start_popping = True

                            if isinstance(segment[index], (CommentedSeq, CommentedMap)):
                                slicedpart[index] = self._slice_segment(
                                    indices[1:],
                                    segment[index],
                                    include_selected=include_selected,
                                )

                            if not include_selected and len(indices) == 1:
                                slicedpart.pop(index)

        return slicedpart 
Example #24
Source File: step.py    From scriptcwl with Apache License 2.0 4 votes vote down vote up
def __init__(self, fname):
        fname = str(fname)
        if fname.startswith('http://') or fname.startswith('https://'):
            self.run = fname
            self.from_url = True
        else:
            self.run = os.path.abspath(fname)
            self.from_url = False

        bn = os.path.basename(fname)
        self.name = os.path.splitext(bn)[0]
        self.python_name = python_name(self.name)

        self.step_inputs = {}
        self.input_names = []
        self.input_types = {}
        self.optional_input_names = []
        self.optional_input_types = {}
        self.output_names = []
        self.output_types = {}
        self.is_workflow = False
        self.is_scattered = False
        self.scattered_inputs = []
        self.python_names = {}

        document_loader, processobj, metadata, uri = load_cwl(fname)
        s = processobj

        self.command_line_tool = s
        valid_classes = ('CommandLineTool', 'Workflow', 'ExpressionTool')
        if 'class' in s and s['class'] in valid_classes:
            self.is_workflow = s['class'] == 'Workflow'
            for inp in s['inputs']:
                # Due to preprocessing of cwltool the id has become an
                # absolute iri, for ease of use we keep only the fragment
                short_id = iri2fragment(inp['id'])
                if self._input_optional(inp):
                    self.optional_input_names.append(short_id)
                    self.optional_input_types[short_id] = inp['type']
                    self.python_names[python_name(short_id)] = short_id
                else:
                    self.input_names.append(short_id)
                    self.input_types[short_id] = inp['type']
                    self.python_names[python_name(short_id)] = short_id

            for o in s['outputs']:
                short_id = iri2fragment(o['id'])
                self.output_names.append(short_id)
                self.output_types[short_id] = o['type']
                self.python_names[python_name(short_id)] = short_id
        else:
            if isinstance(s, CommentedSeq):
                msg = 'Not loading "{}", because it is a packed workflow.'
                raise PackedWorkflowException(msg.format(self.run))
            else:
                msg = '"{}" is a unsupported'
                raise NotImplementedError(msg.format(self.name))