def build_non_reg_text(reg_xml):
    """ This builds the tree for the non-regulation text such as Appendices
    and the Supplement section. """
    doc_root = etree.fromstring(reg_xml)

    reg_part = int(doc_root.xpath('//REGTEXT')[0].attrib['PART'])
    last_section = doc_root.xpath('//REGTEXT/PART/SECTION[last()]')[0]

    section_type = None
    node_type = None
    current_section = None
    m_stack = NodeStack()

    for child in last_section.getchildren():
        if child.tag == 'HD':
            p_level = 1
            if 'Appendix' in child.text and 'Part' in child.text:
                section_type = 'APPENDIX'
                node_type = Node.APPENDIX
                current_section = headers.parseString(child.text).letter
            elif 'Supplement' in child.text and 'Part' in child.text:
                section_type = 'SUPPLEMENT'
                node_type = Node.INTERP
            else:
                p_level = 2
                if section_type == 'SUPPLEMENT' and 'Appendix' in child.text:
                    current_section = headers.parseString(child.text).letter
                else:
                    current_section = determine_next_section(m_stack, p_level)

            if p_level == 1:
                label = [str(reg_part), current_section]
            else:
                label = [current_section]

            n = Node(node_type=node_type, label=label, title=child.text)
            last = m_stack.peek()

            if len(last) == 0:
                m_stack.push_last((p_level, n))
            else:
                tree_utils.add_to_stack(m_stack, p_level, n)
        elif current_section and section_type == 'APPENDIX':
            if child.tag == 'EXTRACT':
                process_appendix(m_stack, current_section, child)
                tree_utils.unwind_stack(m_stack)
        elif current_section and section_type == 'SUPPLEMENT':
            if child.tag == 'EXTRACT':
                process_supplement(reg_part, m_stack, child)
                tree_utils.unwind_stack(m_stack)

    while m_stack.size() > 1:
        tree_utils.unwind_stack(m_stack)

    sections = []
    for level, section in m_stack.m_stack[0]:
        sections.append(section)

    return sections
 def set_letter(self, appendix):
     """Find (and set) the appendix letter"""
     for node in (c for c in appendix.getchildren()
                  if is_appendix_header(c)):
         text = tree_utils.get_node_text(node)
         if self.appendix_letter:
             logger.warning("Found two appendix headers: %s and %s",
                            self.appendix_letter, text)
         self.appendix_letter = headers.parseString(text).appendix
     return self.appendix_letter
 def set_letter(self, appendix):
     """Find (and set) the appendix letter"""
     for node in (c for c in appendix.getchildren()
                  if is_appendix_header(c)):
         text = tree_utils.get_node_text(node)
         if self.appendix_letter:
             logging.warning("Found two appendix headers: %s and %s",
                             self.appendix_letter, text)
         self.appendix_letter = headers.parseString(text).appendix
     return self.appendix_letter