def test_new_subpart_added(self):
        amended_label = Amendment('POST', '200-Subpart:B')
        self.assertTrue(changes.new_subpart_added(amended_label))

        amended_label = Amendment('PUT', '200-Subpart:B')
        self.assertFalse(changes.new_subpart_added(amended_label))

        amended_label = Amendment('POST', '200-Subpart:B-a-3')
        self.assertFalse(changes.new_subpart_added(amended_label))
def test_new_subpart_added():
    amended_label = Amendment('POST', '200-Subpart:B')
    assert changes.new_subpart_added(amended_label)

    amended_label = Amendment('PUT', '200-Subpart:B')
    assert not changes.new_subpart_added(amended_label)

    amended_label = Amendment('POST', '200-Subpart:B-a-3')
    assert not changes.new_subpart_added(amended_label)
    def test_new_subpart_added(self):
        amended_label = Amendment('POST', '200-Subpart:B')
        self.assertTrue(changes.new_subpart_added(amended_label))

        amended_label = Amendment('PUT', '200-Subpart:B')
        self.assertFalse(changes.new_subpart_added(amended_label))

        amended_label = Amendment('POST', '200-Subpart:B-a-3')
        self.assertFalse(changes.new_subpart_added(amended_label))
Beispiel #4
0
def process_amendments(notice, notice_xml):
    """Process changes to the regulation that are expressed in the notice."""
    all_amends = []     # will be added to the notice
    cfr_part = notice['cfr_parts'][0]
    notice_changes = changes.NoticeChanges()

    # process amendments in batches, based on their parent XML
    for amdparent in notice_xml.xpath('//AMDPAR/..'):
        context = [amdparent.get('PART') or cfr_part]
        amendments_by_section = defaultdict(list)
        normal_amends = []  # amendments not moving or adding a subpart
        for amdpar in amdparent.xpath('.//AMDPAR'):
            instructions = amdpar.xpath('./EREGS_INSTRUCTIONS')
            if not instructions:
                logger.warning('No <EREGS_INSTRUCTIONS>. Was this notice '
                               'preprocessed?')
                continue
            instructions = instructions[0]
            amendments = [amendment_from_xml(el) for el in instructions]
            context = [None if l is '?' else l
                       for l in instructions.get('final_context').split('-')]
            section_xml = find_section(amdpar)
            for amendment in amendments:
                all_amends.append(amendment)
                if isinstance(amendment, DesignateAmendment):
                    subpart_changes = process_designate_subpart(amendment)
                    if subpart_changes:
                        notice_changes.update(subpart_changes)
                elif new_subpart_added(amendment):
                    notice_changes.update(process_new_subpart(
                        notice, amendment, amdpar))
                elif section_xml is None:
                    normal_amends.append(amendment)
                else:
                    normal_amends.append(amendment)
                    amendments_by_section[section_xml].append(amendment)

        cfr_part = context[0]   # carry the part through to the next amdparent
        create_xmlless_changes(normal_amends, notice_changes)
        # Process amendments relating to a specific section in batches, too
        for section_xml, related_amends in amendments_by_section.items():
            for section in reg_text.build_from_section(cfr_part, section_xml):
                create_xml_changes(related_amends, section, notice_changes)

        for appendix in parse_appendix_changes(normal_amends, cfr_part,
                                               amdparent):
            create_xml_changes(normal_amends, appendix, notice_changes)

        interp = parse_interp_changes(normal_amends, cfr_part, amdparent)
        if interp:
            create_xml_changes(normal_amends, interp, notice_changes)

    if all_amends:
        notice['amendments'] = all_amends
        notice['changes'] = notice_changes.changes

    return notice
def fetch_amendments(notice_xml):
    """Process changes to the regulation that are expressed in the notice."""
    notice_changes = changes.NoticeChanges()

    if notice_xml.xpath('.//AMDPAR[not(EREGS_INSTRUCTIONS)]'):
        logger.warning(
            'No <EREGS_INSTRUCTIONS>. Was this notice preprocessed?')

    cache = ContentCache()
    authority_by_xml = {}
    for instruction_xml in notice_xml.xpath('.//EREGS_INSTRUCTIONS/*'):
        amendment = amendment_from_xml(instruction_xml)
        content = cache.content_of_change(instruction_xml)
        if instruction_xml.tag == 'MOVE_INTO_SUBPART':
            subpart_changes = process_designate_subpart(amendment)
            if subpart_changes:
                notice_changes.add_changes(amendment.amdpar_xml,
                                           subpart_changes)
        elif instruction_xml.tag == 'AUTHORITY':
            authority_by_xml[amendment.amdpar_xml] = instruction_xml.text
        elif changes.new_subpart_added(amendment):
            subpart_changes = {}
            for change in changes.create_subpart_amendment(content.struct):
                subpart_changes.update(change)
            notice_changes.add_changes(amendment.amdpar_xml, subpart_changes)
        elif content:
            content.amends.append(amendment)
        else:
            create_xmlless_change(amendment, notice_changes)

    for content in cache.by_xml.values():
        create_xml_changes(content.amends, content.struct, notice_changes)

    amendments = []
    for amdpar_xml in notice_xml.xpath('.//AMDPAR'):
        amendment = {"instruction": amdpar_xml.text}
        # There'll be at most one
        for inst_xml in amdpar_xml.xpath('./EREGS_INSTRUCTIONS'):
            context = inst_xml.get('final_context', '')
            amendment['cfr_part'] = context.split('-')[0]
        relevant_changes = notice_changes.changes_by_xml[amdpar_xml]
        if relevant_changes:
            amendment['changes'] = list(relevant_changes.items())
        if amdpar_xml in authority_by_xml:
            amendment['authority'] = authority_by_xml[amdpar_xml]

        amendments.append(amendment)

    return amendments