regulations.generator.layers package

Submodules

regulations.generator.layers.base module

class regulations.generator.layers.base.InlineLayer[source]

Bases: regulations.generator.layers.base.LayerBase

Represents a layer which replaces text by looking at offsets

apply_layer(text, label_id)[source]

Entry point when processing the regulation tree. Given the node’s text and its label_id, yield all replacement text

attach_metadata(node)[source]

Noop

inline_replacements(text_index, original_text)[source]

Apply multiple inline layers to given text (e.g. links, highlighting, etc.)

replacement_for(original, data)[source]

Given the original text and the relevant data from a layer, create a (string) replacement, by, for example, running the data through a template

class regulations.generator.layers.base.LayerBase[source]

Bases: object

Base class for most layers; each layer contains information which is added on top of the regulation, such as definitions, internal citations, keyterms, etc.

attach_metadata(node)[source]

Attach metadata to the provided node

data_source

Data is pulled from the API; this field indicates the name of the endpoint to pull data from

inline_replacements(text_index, original_text)[source]

Return triplets of (original text, replacement text, offsets)

shorthand

A short description for this layer. This is used in query strings and the like to define which layers should be used

class regulations.generator.layers.base.ParagraphLayer[source]

Bases: regulations.generator.layers.base.LayerBase

Represents a layer which applies meta data to nodes

inline_replacements(text_index, original_text)[source]

Noop

class regulations.generator.layers.base.Replacement(original, replacement, locations)

Bases: tuple

locations

Alias for field number 2

original

Alias for field number 0

replacement

Alias for field number 1

class regulations.generator.layers.base.SearchReplaceLayer[source]

Bases: regulations.generator.layers.base.LayerBase

Represents a layer which replaces text by searching for and replacing a specific substring. Also accounts for the string appearing multiple times (via the ‘locations’ field)

attach_metadata(node)[source]

Noop

inline_replacements(text_index, original_text)[source]

Entry point when processing the regulation tree. Given the node’s label_id, attempt to find relevant layer data in self.layer

replacements_for(text, data)[source]

Given the original text and the relevant data from a layer, create a (string) replacement, by, for example, running the data through a template. Returns a generator

regulations.generator.layers.defined module

regulations.generator.layers.definitions module

regulations.generator.layers.diff_applier module

class regulations.generator.layers.diff_applier.DiffApplier(diff_json, label_requested)[source]

Bases: object

Diffs between two versions of a regulation are represented in our particular JSON format. This class applies that diff to the older version of the regulation, generating HTML that clearly shows the changes between old and new.

ADDED_OP = 'added'
DELETE = u'delete'
DELETED_OP = 'deleted'
EQUAL = u'equal'
INSERT = u'insert'
MODIFIED_OP = 'modified'
add_all(text)[source]

Mark all the text passed in as deleted.

add_nodes_to_tree(original, adds)[source]

Add all the nodes from new_nodes into the original tree.

apply_diff(original, label, component='text')[source]

Here we delete or add whole nodes in addition to passing to apply_diff_changes when text has been modified

apply_diff_changes(original, diff_list)[source]

Account for modified text

deconstruct_text(original)[source]
delete_all(text)[source]

Mark all the text passed in as deleted.

delete_text(start, end)[source]
get_text()[source]
classmethod has_moved(label_op, seen_count)[source]

A label is moved if it’s been deleted in one position but added int another

insert_text(pos, new_text)[source]
is_child_of_requested(label)[source]

Return true if the label is a child of the requested label.

relevant_added(label)[source]

Get the operations that add nodes, for the requested section/pargraph.

remove_moved_labels(label_ops)[source]

If a label has been moved, we will display it in the new position

set_child_labels(node)[source]

As we display removed, added, and unchanged nodes, the children of a node will contain all three types. Pull the ‘child_ops’ data to derive the correct order of these combined children

tree_changes(original_tree)[source]

Apply additions to the regulation tree.

regulations.generator.layers.external_citation module

regulations.generator.layers.footnotes module

class regulations.generator.layers.footnotes.FootnotesLayer(layer, version=None)[source]

Bases: regulations.generator.layers.base.ParagraphLayer

Assembles the footnotes for this node, if available

attach_metadata(node)[source]

Return a tuple of ‘footnotes’ and collection of footnotes. Footnotes are “collected” from the node and its children. .. note:

This does not handle the case where the same note reference
is used in multiple children.
data_source = 'formatting'
shorthand = 'footnotes'

regulations.generator.layers.formatting module

regulations.generator.layers.graphics module

regulations.generator.layers.internal_citation module

regulations.generator.layers.interpretations module

regulations.generator.layers.key_terms module

regulations.generator.layers.layers_applier module

class regulations.generator.layers.layers_applier.LayersApplier[source]

Bases: object

Most layers replace content. We try to do this intelligently here, so that layers don’t step over each other.

HTML_TAG_REGEX = <_sre.SRE_Pattern object>
apply_layers(original_text)[source]
enqueue(layer_element)[source]
enqueue_from_list(elements_list)[source]
location_replace(xml_node, original, replacement, locations)[source]
replace_all(original, replacement)[source]

Replace all occurrences of original with replacement. This is HTML aware; it effectively looks at all of the text in between HTML tags

replace_at(original, replacement, locations)[source]

Replace the occurrences of original at all the locations with replacement.

regulations.generator.layers.location_replace module

class regulations.generator.layers.location_replace.LocationReplace[source]

Bases: object

Applies location based layers to XML nodes. We use XML so that we only take into account the original text when we’re doing a replacement.

static find_all_offsets(pattern, text, offset=0)[source]

Don’t use regular expressions as they are a tad slow

location_replace(xml_node, original, replacement, locations)[source]

For the xml_node, replace the locations instances of orginal with replacement. @todo: This doesn’t appear to be used anymore?

location_replace_text(text, original, replacement, locations)[source]

Given plain text, do replacements

update_offset_starter()[source]

As we’re navigating the XML node, we need to keep track of how many offsets we’ve already seen.

update_offsets(original, text)[source]

Offsets change everytime we replace the text, since we add more characters. Update the offsets.

regulations.generator.layers.meta module

class regulations.generator.layers.meta.MetaLayer(layer_data)[source]

Bases: regulations.generator.layers.base.ParagraphLayer

attach_metadata(node)[source]

Return a pair of field-name (meta) + the layer data

data_source = 'meta'
shorthand = 'meta'

regulations.generator.layers.paragraph_markers module

regulations.generator.layers.toc_applier module

regulations.generator.layers.tree_builder module

class regulations.generator.layers.tree_builder.AddQueue[source]

Bases: object

Maintain a sorted list of nodes to add. This maintains a sorted queue of (label, node) tuples.

delete(label)[source]
find(label)[source]
insert(item)[source]
insert_all(items)[source]
sort()[source]
regulations.generator.layers.tree_builder.add_child(parent_node, node)[source]

Add a child node to a parent, maintaining the order of the children.

regulations.generator.layers.tree_builder.add_node_to_tree(node, parent_label, tree_hash)[source]

Add the node to the tree by adding it to it’s parent in order.

regulations.generator.layers.tree_builder.all_children_are_roman(parent_node)[source]

Return true if all the children of the parent node have roman labels

regulations.generator.layers.tree_builder.build_label(node)[source]
regulations.generator.layers.tree_builder.build_tree_hash(tree)[source]

Build a hash map of a tree’s nodes, so that we don’t have to keep walking the tree.

regulations.generator.layers.tree_builder.make_label_sortable(label, roman=False)[source]

Make labels sortable, but converting them as appropriate. Also, appendices have labels that look like 30(a), we make those appropriately sortable.

regulations.generator.layers.tree_builder.parent_in_tree(parent_label, tree_hash)[source]

Return True if the parent of node_label is in the tree

regulations.generator.layers.tree_builder.parent_label(node)[source]

This is not perfect. It can not handle children of subparts, for example

regulations.generator.layers.tree_builder.roman_nums()[source]

Generator for roman numerals.

regulations.generator.layers.utils module

regulations.generator.layers.utils.convert_to_python(data)[source]

Convert raw data (e.g. from json conversion) into the appropriate Python objects

regulations.generator.layers.utils.is_contained_in(child, parent)[source]

Return True if child is a child node of the parent.

Node labels are hierarchical paths, with segments separated by ‘-‘. As an edge case, a node label is also a child of itself.

regulations.generator.layers.utils.render_template(template, context)[source]

Module contents