space_packet_parser.common
Common mixins
Attributes
Classes
Custom element that automatically applies namespace mappings. |
|
Generic class that provides a notion of equality based on all non-callable, non-dunder attributes |
|
ABC that requires to_xml_element and from_xml_element methods for parsing and serializing |
|
Defines an object that can be parsed from packet data. |
|
Mixin class for storing access to the raw value of a parsed data item. |
|
A class to represent a binary data item. |
|
A class to represent a parsed boolean data item. |
|
A class to represent a float data item. |
|
A class to represent a integer data item. |
|
A class to represent a string data item. |
|
Packet representing parsed data items. |
Functions
|
Extract nbits from the data starting from the least significant end. |
Module Contents
- space_packet_parser.common.logger
- class space_packet_parser.common.NamespaceAwareElement
Bases:
lxml.etree.ElementBaseCustom element that automatically applies namespace mappings.
- _nsmap: dict[str, str]
- _ns_prefix: str | None = None
- classmethod element_prefix()
Create the XPath element prefix
Notes
If the prefix is None, it indicates either an implicit namespace such as <SpaceSystem xmlns=”http://xtce-example-ns-uri”/>, where nsmap is {None: “http://xtce-example-ns-uri”, …} or no namespace awareness, such as <SpaceSystem/>, where nsmap does not contain any reference to a URI or prefix for the XTCE namespace (but may contain other namespace mappings).
If the prefix is anything other than None, it must be a string and must be present in the namespace mapping dict and represents a prefixed namespace, such as <xtce:SpaceSystem xmlns:xtce=”http://xtce-example-ns-uri”/> where nsmap would be {“xtce”: “http://xtce-example-ns-uri”, …} and ns_prefix would be xtce.
- classmethod add_namespace_to_xpath(xpath: str) str
Adds a namespace prefix to each element in an XPath expression.
- Parameters:
xpath (str) – The original XPath expression without namespace prefixes.
- Returns:
The updated XPath expression with namespace prefixes.
- Return type:
str
- find(path, namespaces=None)
Override find() to automatically use the stored namespace map.
- findall(path, namespaces=None)
Override findall() to automatically use the stored namespace map.
- iterfind(path, namespaces=None)
Override iterfind() to automatically use the stored namespace map.
- classmethod set_nsmap(nsmap: dict)
Store the namespace map for all elements of this type.
- get_nsmap()
Retrieve the stored namespace map.
- classmethod set_ns_prefix(ns_prefix: str | None)
Store the namespace map for all elements of this type.
- get_ns_prefix()
Retrieve the stored namespace map.
- class space_packet_parser.common.AttrComparable
Generic class that provides a notion of equality based on all non-callable, non-dunder attributes
- __eq__(other)
- class space_packet_parser.common.XmlObject
ABC that requires to_xml_element and from_xml_element methods for parsing and serializing a library object from an XML element object.
- classmethod from_xml(element: lxml.etree.Element, *, tree: lxml.etree.ElementTree | None, parameter_lookup: dict[str, any] | None, parameter_type_lookup: dict[str, any] | None, container_lookup: dict[str, any] | None) XmlObject
- Abstractmethod:
Create an object from an XML element
Notes
This abstract implementation has a signature that includes all possible parameters to this function across our XML object classes in order to satisfy Liskov Substitution. It also makes it clear that you _can_ pass this information in to from_xml but depending on the subtype implementation, it may be ignored.
- Parameters:
element (ElementTree.Element) – XML element from which to parse the object
tree (Optional[ElementTree.ElementTree]) – Full XML tree for parsing that requires access to other elements
parameter_lookup (Optional[dict[str, parameters.ParameterType]]) – Parameters dict for parsing that requires knowledge of existing parameters
parameter_type_lookup (Optional[dict[str, parameters.ParameterType]]) – Parameter type dict for parsing that requires knowledge of existing parameter types
container_lookup (Optional[dict[str, parameters.ContainerType]]) – Container type dict for parsing that requires knowledge of existing containers
- Return type:
cls
- abstract to_xml(*, elmaker: lxml.builder.ElementMaker) lxml.etree.Element
Create an XML element from the object self
- Parameters:
elmaker (ElementMaker) – ElementMaker for creating new XML elements with predefined namespace
- Returns:
XML Element object
- Return type:
ElementTree.Element
- class space_packet_parser.common.Parseable
Bases:
ProtocolDefines an object that can be parsed from packet data.
- parse(packet: SpacePacket) None
Parse this entry from the packet data and add the necessary items to the packet.
- space_packet_parser.common.BuiltinDataTypes
- class space_packet_parser.common._Parameter
Mixin class for storing access to the raw value of a parsed data item.
The raw value is the closest representation of the data item as it appears in the packet. e.g. bytes for binary data, int for integer data, etc. It has not been calibrated or adjusted in any way and is an easy way for user’s to debug the transformations that happened after the fact.
Notes
We need to override the __new__ method to store the raw value of the data item on immutable built-in types. So this is just a way of allowing us to inject our own attribute into the built-in types.
- class space_packet_parser.common.BinaryParameter
Bases:
_Parameter,bytesA class to represent a binary data item.
- class space_packet_parser.common.BoolParameter
Bases:
_Parameter,intA class to represent a parsed boolean data item.
- __repr__() str
Return repr(self).
- class space_packet_parser.common.FloatParameter
Bases:
_Parameter,floatA class to represent a float data item.
- class space_packet_parser.common.IntParameter
Bases:
_Parameter,intA class to represent a integer data item.
- class space_packet_parser.common.StrParameter
Bases:
_Parameter,strA class to represent a string data item.
- space_packet_parser.common.ParameterDataTypes
- class space_packet_parser.common.SpacePacket(*args, binary_data: bytes = b'', **kwargs)
Bases:
dictPacket representing parsed data items.
Container that stores the binary packet data (bytes) as an instance attribute and the parsed data items in a dictionary interface. A
SpacePacketgenerally begins as an empty dictionary that gets filled as the packet is parsed. To access the raw bytes of the packet, use theSpacePacket.binary_dataattribute.- Parameters:
*args (Mapping or Iterable) – Initial items to store in the packet, passed to the dict() constructor.
binary_data (bytes, optional) – The binary data for a single packet as a bytes object / subclass. This binary data is stored and used for parsing the data items. Internally we are tracking the parsing position within this binary_data object and trying to read specific bit ranges from it.
**kwargs (dict) – Additional packet items to store, passed to the dict() constructor.
- binary_data = b''
- _parsing_pos = 0
- property header: dict
The header content of the packet.
- property user_data: dict
The user data content of the packet.
- property raw_data: bytes
The raw binary data of the packet.
- _read_from_binary_as_bytes(nbits: int) bytes
Read a number of bits from the binary packet data as bytes.
Reads the minimum number of complete bytes required to capture nbits. Moves _parsing_pos cursor nbits forward, even if nbits is not an integer number of bytes.
- Parameters:
nbits (int) – Number of bits to read
- Returns:
Raw bytes from the packet data
- Return type:
bytes
- _read_from_binary_as_int(nbits: int) int
Read a number of bits from the binary packet data as an integer.
- Parameters:
nbits (int) – Number of bits to read
- Returns:
Integer representation of the bits read from the packet
- Return type:
int
- space_packet_parser.common._extract_bits(data: bytes, start_bit: int, nbits: int)
Extract nbits from the data starting from the least significant end.
If data = 00110101 11001010, start_bit = 2, nbits = 9, then the bits extracted are “110101110”. Those bits are turned into a Python integer and returned.
- Parameters:
data (bytes) – Data to extract bits from
start_bit (int) – Starting bit location within the data
nbits (int) – Number of bits to extract
- Returns:
Extracted bits as an integer
- Return type:
int