space_packet_parser.parser
Module for parsing CCSDS packets using packet definitions
Module Contents
Classes
Representation of a parsed parameter |
|
Class for parsing CCSDS packets |
Functions
|
Extract nbits from the data starting from the least significant end. |
Attributes
- space_packet_parser.parser.logger
- space_packet_parser.parser.CcsdsPacketHeaderElement
- space_packet_parser.parser.CCSDS_HEADER_DEFINITION
- space_packet_parser.parser.CCSDS_HEADER_LENGTH_BYTES = 6
- space_packet_parser.parser.Packet
- class space_packet_parser.parser.ParsedDataItem(name: str, raw_value: any, unit: str = None, derived_value: float or str = None, short_description: str = None, long_description: str = None)
Bases:
space_packet_parser.xtcedef.AttrComparable
Representation of a parsed parameter
- __repr__()
Return repr(self).
- exception space_packet_parser.parser.UnrecognizedPacketTypeError(*args, partial_data: dict = None)
Bases:
Exception
Error raised when we can’t figure out which kind of packet we are dealing with based on the header
- class space_packet_parser.parser.PacketParser(packet_definition: xtcedef.XtcePacketDefinition or csvdef.CsvPacketDefinition, word_size: int = None)
Class for parsing CCSDS packets
- static _parse_header(packet_data: bytes) dict
Parses the CCSDS standard header.
- Parameters:
packet_data (bytes) – 6 bytes of binary data.
- Returns:
header – Dictionary of header items.
- Return type:
dict
- _determine_packet_by_restrictions(parsed_header: dict) Tuple[str, list]
Examines a dictionary representation of a CCSDS header and determines which packet type applies. This packet type must be unique. If the header data satisfies the restrictions for more than one packet type definition, an exception is raised.
- Parameters:
parsed_header (dict) – Pre-parsed header data in dictionary form for evaluating restriction criteria. NOTE: Restriction criteria can ONLY be evaluated against header items. There is no reasonable way to start parsing all the BaseContainer inheritance restrictions without assuming that all restrictions will be based on header items, which can be parsed ahead of time due to the consistent nature of a CCSDS header.
- Returns:
str – Name of packet definition.
list – A list of Parameter objects
- static parse_packet(packet_data: bitstring.ConstBitStream, containers: dict, root_container_name: str = 'CCSDSPacket', **parse_value_kwargs) Packet
Parse binary packet data according to the self.packet_definition object
- Parameters:
packet_data (bitstring.BitString) – Binary packet data to parse into Packets
containers (dict) – Dictionary of named containers, including their inheritance information.
root_container_name (str, Optional) – Default is CCSDSPacket. Any root container may be specified.
- Returns:
A Packet object container header and data attributes.
- Return type:
Packet
- static legacy_parse_packet(packet_data: bitstring.ConstBitStream, entry_list: list, **parse_value_kwargs) Packet
Parse binary packet data according to the self.flattened_containers property
- Parameters:
packet_data (bitstring.BitString) – Binary packet data to parse into Packets
entry_list (list) – List of Parameter objects
- Returns:
A Packet object container header and data attributes.
- Return type:
Packet
- static print_progress(current_bytes: int, total_bytes: int or None, start_time_ns: int, current_packets: int, end: str = '\r', log: bool = False)
Prints a progress bar, including statistics on parsing rate.
- Parameters:
current_bytes (int) – Number of bytes parsed so far.
total_bytes (int) – Number of total bytes to parse (if known)
current_packets (int) – Number of packets parsed so far.
start_time_ns (int) – Start time on system clock, in nanoseconds.
end (str) – Print function end string. Default is r to create a dynamically updating loading bar.
log (bool) – If True, log the progress bar at INFO level.
- generator(binary_data: bitstring.ConstBitStream or BinaryIO or socket.socket, parse_bad_pkts: bool = True, skip_header_bits: int = 0, root_container_name='CCSDSPacket', ccsds_headers_only: bool = False, yield_unrecognized_packet_errors: bool = False, show_progress: bool = False, buffer_read_size_bytes: int | None = None)
Create and return a Packet generator that reads from a ConstBitStream or a filelike object or a socket.
Creating a generator object to return allows the user to create many generators from a single Parser and reduces memory usage.
- Parameters:
binary_data (bitstring.ConstBitStream or BinaryIO or socket.socket) – Binary data source to parse into Packets.
parse_bad_pkts (bool, Optional) – Default True. If True, when the generator encounters a packet with an incorrect length it will still yield the packet (the data will likely be invalid). If False, the generator will still write a debug log message but will otherwise silently skip the bad packet.
skip_header_bits (int, Optional) – If provided, the parser skips this many bits at the beginning of every packet. This allows dynamic stripping of additional header data that may be prepended to packets.
root_container_name (str, Optional) – The name of the root level (lowest level of container inheritance) SequenceContainer. This SequenceContainer is assumed to be inherited by every possible packet structure in the XTCE document and is the starting point for parsing. Default is ‘CCSDSPacket’.
ccsds_headers_only (bool, Optional) – If True, only parses the packet headers (does not use the provided packet definition).
yield_unrecognized_packet_errors (bool, Optional) – Default False. If False, UnrecognizedPacketTypeErrors are caught silently and parsing continues to the next packet. If True, the generator will yield an UnrecognizedPacketTypeError in the event of an unrecognized packet. Note: These exceptions are not raised by default but are instead returned so that the generator can continue. You can raise the exceptions if desired. Leave this as False unless you need to examine the partial data from unrecognized packets.
show_progress (bool, Optional) – If True, prints a status bar. Note that for socket sources, the percentage will be zero until the generator ends.
buffer_read_size_bytes (int, Optional) – Number of bytes to read from e.g. a BufferedReader or socket binary data source on each read attempt. Default is 4096 bytes from a socket, -1 (full read) from a file.
- Yields:
Packet or UnrecognizedPacketTypeError – Generator yields Packet objects containing the parsed packet data for each subsequent packet. If yield_unrecognized_packet_errors is True, it will yield an unraised exception object, which can be raised or used for debugging purposes.
- space_packet_parser.parser._extract_bits(data: bytes, start_bit: int, nbits: int)
Extract nbits from the data starting from the least significant end.
If data = b”abcdefgh”, start_bit = 2, nbits = 3, then the bits extracted are “cde” and those are turned into a Python integer and returned.
- Parameters:
data (bytes) – Data to extract bits from
start_bit (int) – Starting bit location
nbits (int) – Number of bits to extract
- Returns:
Extracted bits as an integer
- Return type:
int