def parse_content(self): """restructs nested sections: * consider the contents as Body section if no nested sections * merge content-type to Header section """ if len(self) > 0 and not get_children(self, Section): body = Body() transpose_subnodes(self, body) self += body body.dedent() if self.get("content_type"): headers = get_children(self, Headers) if not headers: header = Headers() header.headers.insert(0, "Content-Type: %s" % self["content_type"]) bodies = get_children(self, Body) if len(bodies) == 0: self.append(header) else: pos = self.index(bodies[0]) self.insert(pos, header) else: for header in headers: header.headers.insert(0, "Content-Type: %s" % self["content_type"])
def assert_having_only(self, classes): if isinstance(classes, (list, tuple)): class_names = [cls.__name__ for cls in classes] else: class_names = [classes.__name__] nodes = get_children(self, Section) assert len(nodes) == 0 or all( isinstance(node, classes) for node in nodes ), "%s section should have only following sections: %s" % (self.__class__.__name__, class_names)
def dedent(self): for subnode in get_children(self, (nodes.literal_block, nodes.paragraph)): content = dedent(subnode.astext()) subnode.replace_self(nodes.literal_block(text=content))
def assert_having_at_least_one(self, cls): nodes = get_children(self, cls) assert len(nodes) >= 1, "%s section should have at least one of %s sections" % ( self.__class__.__name__, cls.__name__, )
def assert_having_at_most_one(self, cls): nodes = get_children(self, cls) assert len(nodes) <= 1, "%s section should have at most one %s section" % ( self.__class__.__name__, cls.__name__, )
def assert_having_no_sections(self): nodes = get_children(self, Section) assert len(nodes) == 0, "%s section should not have any sections" % (self.__class__.__name__)
def parse_content(self): for node in get_children(self, Action): if node.get("uri") is None: node["uri"] = self["uri"]