pyx12.x12context¶
Parse a ANSI X12 data file.
Maintain context state Start saving context and segments Interface to read and alter segments
TODO: Attach errors to returned dicts
Classes¶
Capture the segment data and X12 definition for a loop subtree |
|
Capture the X12 definition for a loop subtree |
|
Capture the segment data and X12 definition |
|
Read an X12 input stream |
Module Contents¶
- class pyx12.x12context.X12DataNode(x12_node, seg_data, ntype='seg')[source]¶
Capture the segment data and X12 definition for a loop subtree Alter relational data Iterate over contents
- Parameters:
x12_node (Any)
seg_data (pyx12.segment.Segment | None)
ntype (str)
- seg_data: pyx12.segment.Segment | None[source]¶
- parent: X12DataNode | None[source]¶
- children: list[X12DataNode][source]¶
- abstractmethod iterate_segments()[source]¶
Iterate over this node and children, return any segments found
- Return type:
collections.abc.Iterator[dict[str, Any]]
- abstractmethod iterate_loop_segments()[source]¶
Iterate over this node and children, return loop start and loop end and any segments found
- Return type:
collections.abc.Iterator[dict[str, Any]]
- abstractmethod get_value(x12_path)[source]¶
- Returns:
the element value at the relative X12 path
- Return type:
string
- Parameters:
x12_path (str)
- abstractmethod set_value(x12_path, val)[source]¶
Set the value of simple element at the first found segment at the given path :param x12_path: An X12 path :type x12_path: string :param val: The new element value :type val: string
- exists(x12_path_str)[source]¶
Does at least one child at the x12 path exist? :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: True if found :rtype: boolean
- select(x12_path_str)[source]¶
Get a slice of sub-nodes at the relative X12 path. Note: All interaction/modification with a X12DataNode tree (having a loop root) is done in place. :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Iterator on the matching sub-nodes, relative to the instance. :rtype: L{node<x12context.X12DataNode>}
- Parameters:
x12_path_str (str)
- Return type:
- first(x12_path_str)[source]¶
Get the first sub-node matching the relative X12 path. Note: All interaction/modification with a X12DataNode tree (having a loop root) is done in place. :param x12_path_str: Relative X12 path - ie 2400/2430 :type x12_path_str: string :return: The matching sub-node, relative to the instance. :rtype: L{node<x12context.X12DataNode>}
- Parameters:
x12_path_str (str)
- Return type:
X12DataNode | None
- count(x12_path_str)[source]¶
Get a count of sub-nodes at the relative X12 path. :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Count of matching sub-nodes :rtype: int
- abstractmethod get_first_matching_segment(x12_path_str)[source]¶
Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.
- Parameters:
x12_path_str (string) – Relative X12 Path
- Returns:
First matching data segment
- Return type:
L{node<segment.Segment>}
- Raises:
X12PathError – On blank or invalid path
- class pyx12.x12context.X12LoopDataNode(x12_node, end_loops=None, parent=None)[source]¶
Bases:
X12DataNodeCapture the X12 definition for a loop subtree Alter relational data Iterate over contents
Construct an X12LoopDataNode
- Parameters:
x12_node (Any)
end_loops (list[Any] | None)
parent (X12DataNode | None)
- get_value(x12_path_str)[source]¶
Returns the element value at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None. If multiple values exist, this function returns the first.
- Parameters:
x12_path_str (string) – Relative X12 Path
- Returns:
the element value at the relative X12 path
- Return type:
string
- Raises:
X12PathError – On blank or invalid path
- set_value(x12_path_str, val)[source]¶
Set the value of simple element at the first found segment at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :param val: The new element value :type val: string
- iterate_segments()[source]¶
Iterate over this node and children
- Return type:
collections.abc.Iterator[dict[str, Any]]
- iterate_loop_segments()[source]¶
Iterate over this node and children, return loop start and loop end
- Return type:
collections.abc.Iterator[dict[str, Any]]
- add_segment(seg_data)[source]¶
Add the segment to this loop node iif the segment is the anchor for a child loop, also adds the loop
- Parameters:
seg_data (L{node<segment.Segment>} or string) – Segment data
- Returns:
New segment, or None if failed
- Return type:
L{node<x12context.X12SegmentDataNode>}
- Raises:
pyx12.errors.X12PathError – If invalid segment
TODO: Check counts?
- add_loop(seg_data)[source]¶
Add a new loop in the correct location :param seg_data: Segment data :type seg_data: L{node<segment.Segment>} or string :return: New loop_data_node, or None if failed :rtype: L{node<x12context.X12LoopDataNode>}
- Parameters:
seg_data (pyx12.segment.Segment | str)
- Return type:
- add_node(data_node)[source]¶
Add a X12DataNode instance The x12_map_node of the given data_node must be a direct child of this object’s x12_map_node :param data_node: The child loop node to add :type data_node : L{node<x12context.X12DataNode>} :raises errors.X12PathError: On blank or invalid path
- Parameters:
data_node (X12DataNode)
- Return type:
None
- delete_segment(seg_data)[source]¶
- Delete the given segment from this loop node
Do not delete the first segment in a loop
Does not descend into child loops
Only delete the first found matching segment
- Parameters:
seg_data (L{node<segment.Segment>} or string) – Segment data
- Returns:
True if found and deleted, else False
- Return type:
Boolean
TODO: Check counts?
- delete_node(x12_path_str)[source]¶
Delete the first node at the given relative path. If the path is not a valid relative path, return False If multiple values exist, this function deletes the first.
- Returns:
True if found and deleted, else False
- Return type:
Boolean
- Raises:
X12PathError – On blank or invalid path
- Parameters:
x12_path_str (str)
TODO: Check counts?
- get_first_matching_segment(x12_path_str)[source]¶
Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.
- Parameters:
x12_path_str (string) – Relative X12 Path
- Returns:
First matching data segment
- Return type:
L{node<segment.Segment>}
- Raises:
X12PathError – On blank or invalid path
- class pyx12.x12context.X12SegmentDataNode(x12_node, seg_data, parent=None, start_loops=None, end_loops=None)[source]¶
Bases:
X12DataNodeCapture the segment data and X12 definition Alter relational data Iterate over contents
- Parameters:
x12_node (Any)
seg_data (pyx12.segment.Segment)
parent (X12DataNode | None)
start_loops (list[Any] | None)
end_loops (list[Any] | None)
- handle_errh_errors(errh)[source]¶
Attach validation errors to segment node
TODO: move errors to parent loops if necessary
- Parameters:
errh (Any)
- Return type:
None
- get_value(x12_path_str)[source]¶
Get the value of the first found element at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :return: the element value at the relative X12 path :rtype: string
- set_value(x12_path_str, val)[source]¶
Set the value of simple element at the first found segment at the given path :param x12_path_str: Relative X12 Path :type x12_path_str: string :param val: The new element value :type val: string
- get_first_matching_segment(x12_path_str)[source]¶
Get first found Segment at the given relative path. If the path is not a valid relative path or if the given segment index does not exist, the function returns None.
- Parameters:
x12_path_str (string) – Relative X12 Path
- Returns:
First matching data segment
- Return type:
L{node<segment.Segment>}
- Raises:
X12PathError – On blank or invalid path
- iterate_segments()[source]¶
Iterate on this node, return the segment
- Return type:
collections.abc.Iterator[dict[str, Any]]
- iterate_loop_segments()[source]¶
Iterate over this node and children, return loop start and loop end and any segments found
- Return type:
collections.abc.Iterator[dict[str, Any]]
- select(x12_path_str)[source]¶
Segment nodes have no sub-nodes so return None :param x12_path_str: Relative X12 path - 2400/2430 :type x12_path_str: string :return: Iterator on the matching sub-nodes, relative to the instance. :rtype: L{node<x12context.X12DataNode>}
- Parameters:
x12_path_str (str)
- Return type:
- class pyx12.x12context.X12ContextReader(param, errh, src_file_obj, xslt_files=None, map_path=None)[source]¶
Read an X12 input stream Keep context when needed
- Parameters:
param (Any) – pyx12.param instance
errh (Any) – Error Handler object
src_file_obj (string) – Source document
xslt_files (Any)
map_path (str | None)
- Return type:
boolean
- map_index_if: pyx12.map_index.map_index[source]¶
- iter_segments(loop_id=None)[source]¶
Simple segment or tree iterator :return: X12 Data Node - simple segment or tree :rtype: L{node<x12context.X12DataNode>}
- Parameters:
loop_id (str | None)
- Return type:
- register_error_callback(callback, err_type)[source]¶
Future: Callbacks for X12 validation errors
- Parameters:
callback (Any)
err_type (str)
- Return type:
None
- property seg_term: str | None[source]¶
Current X12 segment terminator :rtype: string
- Type:
return
- Return type:
str | None
- property ele_term: str | None[source]¶
Current X12 element terminator :rtype: string
- Type:
return
- Return type:
str | None