def ecfr_notice(title, cfr_part, notice, applies_to, act_title, act_section, with_version=False, without_notice=False): """ Generate RegML for a single notice from eCFR XML. """ # Get the notice the new one applies to with open(find_file(os.path.join(cfr_part, applies_to)), 'r') as f: reg_xml = f.read() parser = etree.XMLParser(huge_tree=True) xml_tree = etree.fromstring(reg_xml, parser) doc_number = xml_tree.find('.//{eregs}documentNumber').text # Validate the file relative to schema validator = get_validator(xml_tree) # Get the ecfr builder builder = Builder(cfr_title=title, cfr_part=cfr_part, doc_number=doc_number, checkpointer=None, writer_type='XML') # Fetch the notices from the FR API and find the notice we're # looking for builder.fetch_notices_json() print([n['document_number'] for n in builder.notices_json]) notice_json = next((n for n in builder.notices_json if n['document_number'] == notice)) # Build the notice notice = builder.build_single_notice(notice_json)[0] if 'changes' not in notice: print('There are no changes in this notice to apply.') return # We've successfully fetched and parsed the new notice. # Build a the reg tree and layers for the notice it applies to. old_tree = build_reg_tree(xml_tree) # Build the new reg tree from the old_tree + notice changes last_version = doc_number version = notice['document_number'] merged_changes = builder.merge_changes(version, notice['changes']) reg_tree = compile_regulation(old_tree, merged_changes) layer_cache = LayerCacheAggregator() layers = builder.generate_layers(reg_tree, [act_title, act_section], layer_cache) # Write the notice file if not without_notice: builder.write_notice(version, old_tree=old_tree, reg_tree=reg_tree, layers=layers, last_version=last_version) # Write the regulation file for the new notice if with_version: builder.write_regulation(new_tree, layers=layers)