Exemplo n.º 1
0
    def encode(self, module: abc.MutableMapping) -> str:
        """Extends the parent function, by adding a restriction.
        For PDS, if there are any GROUP elements, there must be at
        least one OBJECT element in the label.  Behavior here
        depends on the value of this encoder's convert_group_to_object
        property.
        """
        (obj_count, grp_count) = self.count_aggs(module)

        if grp_count > 0 and obj_count < 1:
            if self.convert_group_to_object:
                for k, v in module.items():
                    # First try to convert any GROUPs that would not
                    # be valid PDS GROUPs.
                    if isinstance(v, self.grpcls) and not self.is_PDSgroup(v):
                        module[k] = self.objcls(v)
                        break
                else:
                    # Then just convert the first GROUP
                    for k, v in module.items():
                        if isinstance(v, self.grpcls):
                            module[k] = self.objcls(v)
                            break
                    else:
                        raise ValueError("Couldn't convert any of the GROUPs "
                                         "to OBJECTs.")
            else:
                raise ValueError("This module has a GROUP element, but no "
                                 "OBJECT elements, which is not allowed by "
                                 "the PDS.  You could set "
                                 "*convert_group_to_object* to *True* on the "
                                 "encoder to try and convert a GROUP "
                                 "to an OBJECT.")

        s = super().encode(module)
        if self.tab_replace > 0:
            return s.replace("\t", (" " * self.tab_replace))
        else:
            return s
Exemplo n.º 2
0
 def items(self):
     """Explicit use of MutableMapping attributes."""
     return MutableMapping.items(self)