openrvdas.logger.transforms.parse_transform
No module-level documentation available.
1#!/usr/bin/env python3 2 3 4from logger.transforms.transform import Transform # noqa: E402 5from logger.utils.das_record import DASRecord # noqa: E402 6from logger.utils import record_parser # noqa: E402 7 8 9################################################################################ 10class ParseTransform(Transform): 11 """Parse a "<data_id> <timestamp> <message>" record and return 12 corresponding dict of values (or JSON or DASRecord if specified).""" 13 14 def __init__(self, record_format=None, field_patterns=None, metadata=None, 15 definition_path=record_parser.DEFAULT_DEFINITION_PATH, 16 return_json=False, return_das_record=False, 17 metadata_interval=None, strip_unprintable=False, quiet=False, 18 prepend_data_id=False, delimiter=':', **kwargs): 19 """ 20 ``` 21 record_format 22 If not None, a custom record format to use for parsing records. 23 The default, defined in logger/utils/record_parser.py, is 24 '{data_id:w} {timestamp:ti} {field_string}'. 25 26 field_patterns 27 If not None, a list of parse patterns to be tried instead 28 of looking for device definitions along the definition path. 29 30 metadata 31 If field_patterns is not None, the metadata to send along with 32 data records. 33 34 definition_path 35 Wildcarded path matching YAML definitions for devices. Used 36 only if 'field_patterns' is None 37 38 return_json 39 Return a JSON-encoded representation of the dict 40 instead of a dict itself. 41 42 return_das_record 43 Return a DASRecord object. 44 45 metadata_interval 46 If not None, include the description, units and other metadata 47 pertaining to each field in the returned record if those data 48 haven't been returned in the last metadata_interval seconds. 49 50 strip_unprintable 51 Strip out and ignore any leading or trailing non-printable binary 52 characters in the string to be parsed. 53 54 quiet - if not False, don't complain when unable to parse a record. 55 56 prepend_data_id 57 If true prepend the instrument data_id to field_names in the record. 58 59 delimiter 60 The string to insert between data_id and field_name when prepend_data_id is true. 61 Defaults to ':'. 62 Not used if prepend_data_id is false. 63 64 ``` 65 """ 66 super().__init__(**kwargs) # processes 'quiet' and type hints 67 68 self.parser = record_parser.RecordParser( 69 record_format=record_format, 70 field_patterns=field_patterns, 71 metadata=metadata, 72 definition_path=definition_path, 73 return_json=return_json, 74 return_das_record=return_das_record, 75 metadata_interval=metadata_interval, 76 strip_unprintable=strip_unprintable, 77 quiet=quiet, 78 prepend_data_id=prepend_data_id, 79 delimiter=delimiter) 80 81 ############################ 82 def transform(self, record: str) -> DASRecord: 83 """Parse record and return DASRecord.""" 84 # See if it's something we can process, and if not, try digesting 85 if not self.can_process_record(record): # inherited from BaseModule() 86 return self.digest_record(record) # inherited from BaseModule() 87 88 return self.parser.parse_record(record)
class
ParseTransform(logger.transforms.transform.Transform):
11class ParseTransform(Transform): 12 """Parse a "<data_id> <timestamp> <message>" record and return 13 corresponding dict of values (or JSON or DASRecord if specified).""" 14 15 def __init__(self, record_format=None, field_patterns=None, metadata=None, 16 definition_path=record_parser.DEFAULT_DEFINITION_PATH, 17 return_json=False, return_das_record=False, 18 metadata_interval=None, strip_unprintable=False, quiet=False, 19 prepend_data_id=False, delimiter=':', **kwargs): 20 """ 21 ``` 22 record_format 23 If not None, a custom record format to use for parsing records. 24 The default, defined in logger/utils/record_parser.py, is 25 '{data_id:w} {timestamp:ti} {field_string}'. 26 27 field_patterns 28 If not None, a list of parse patterns to be tried instead 29 of looking for device definitions along the definition path. 30 31 metadata 32 If field_patterns is not None, the metadata to send along with 33 data records. 34 35 definition_path 36 Wildcarded path matching YAML definitions for devices. Used 37 only if 'field_patterns' is None 38 39 return_json 40 Return a JSON-encoded representation of the dict 41 instead of a dict itself. 42 43 return_das_record 44 Return a DASRecord object. 45 46 metadata_interval 47 If not None, include the description, units and other metadata 48 pertaining to each field in the returned record if those data 49 haven't been returned in the last metadata_interval seconds. 50 51 strip_unprintable 52 Strip out and ignore any leading or trailing non-printable binary 53 characters in the string to be parsed. 54 55 quiet - if not False, don't complain when unable to parse a record. 56 57 prepend_data_id 58 If true prepend the instrument data_id to field_names in the record. 59 60 delimiter 61 The string to insert between data_id and field_name when prepend_data_id is true. 62 Defaults to ':'. 63 Not used if prepend_data_id is false. 64 65 ``` 66 """ 67 super().__init__(**kwargs) # processes 'quiet' and type hints 68 69 self.parser = record_parser.RecordParser( 70 record_format=record_format, 71 field_patterns=field_patterns, 72 metadata=metadata, 73 definition_path=definition_path, 74 return_json=return_json, 75 return_das_record=return_das_record, 76 metadata_interval=metadata_interval, 77 strip_unprintable=strip_unprintable, 78 quiet=quiet, 79 prepend_data_id=prepend_data_id, 80 delimiter=delimiter) 81 82 ############################ 83 def transform(self, record: str) -> DASRecord: 84 """Parse record and return DASRecord.""" 85 # See if it's something we can process, and if not, try digesting 86 if not self.can_process_record(record): # inherited from BaseModule() 87 return self.digest_record(record) # inherited from BaseModule() 88 89 return self.parser.parse_record(record)
Parse a "
ParseTransform( record_format=None, field_patterns=None, metadata=None, definition_path='logger/devices/*.yaml,contrib/devices/*.yaml', return_json=False, return_das_record=False, metadata_interval=None, strip_unprintable=False, quiet=False, prepend_data_id=False, delimiter=':', **kwargs)
15 def __init__(self, record_format=None, field_patterns=None, metadata=None, 16 definition_path=record_parser.DEFAULT_DEFINITION_PATH, 17 return_json=False, return_das_record=False, 18 metadata_interval=None, strip_unprintable=False, quiet=False, 19 prepend_data_id=False, delimiter=':', **kwargs): 20 """ 21 ``` 22 record_format 23 If not None, a custom record format to use for parsing records. 24 The default, defined in logger/utils/record_parser.py, is 25 '{data_id:w} {timestamp:ti} {field_string}'. 26 27 field_patterns 28 If not None, a list of parse patterns to be tried instead 29 of looking for device definitions along the definition path. 30 31 metadata 32 If field_patterns is not None, the metadata to send along with 33 data records. 34 35 definition_path 36 Wildcarded path matching YAML definitions for devices. Used 37 only if 'field_patterns' is None 38 39 return_json 40 Return a JSON-encoded representation of the dict 41 instead of a dict itself. 42 43 return_das_record 44 Return a DASRecord object. 45 46 metadata_interval 47 If not None, include the description, units and other metadata 48 pertaining to each field in the returned record if those data 49 haven't been returned in the last metadata_interval seconds. 50 51 strip_unprintable 52 Strip out and ignore any leading or trailing non-printable binary 53 characters in the string to be parsed. 54 55 quiet - if not False, don't complain when unable to parse a record. 56 57 prepend_data_id 58 If true prepend the instrument data_id to field_names in the record. 59 60 delimiter 61 The string to insert between data_id and field_name when prepend_data_id is true. 62 Defaults to ':'. 63 Not used if prepend_data_id is false. 64 65 ``` 66 """ 67 super().__init__(**kwargs) # processes 'quiet' and type hints 68 69 self.parser = record_parser.RecordParser( 70 record_format=record_format, 71 field_patterns=field_patterns, 72 metadata=metadata, 73 definition_path=definition_path, 74 return_json=return_json, 75 return_das_record=return_das_record, 76 metadata_interval=metadata_interval, 77 strip_unprintable=strip_unprintable, 78 quiet=quiet, 79 prepend_data_id=prepend_data_id, 80 delimiter=delimiter)
record_format
If not None, a custom record format to use for parsing records.
The default, defined in logger/utils/record_parser.py, is
'{data_id:w} {timestamp:ti} {field_string}'.
field_patterns
If not None, a list of parse patterns to be tried instead
of looking for device definitions along the definition path.
metadata
If field_patterns is not None, the metadata to send along with
data records.
definition_path
Wildcarded path matching YAML definitions for devices. Used
only if 'field_patterns' is None
return_json
Return a JSON-encoded representation of the dict
instead of a dict itself.
return_das_record
Return a DASRecord object.
metadata_interval
If not None, include the description, units and other metadata
pertaining to each field in the returned record if those data
haven't been returned in the last metadata_interval seconds.
strip_unprintable
Strip out and ignore any leading or trailing non-printable binary
characters in the string to be parsed.
quiet - if not False, don't complain when unable to parse a record.
prepend_data_id
If true prepend the instrument data_id to field_names in the record.
delimiter
The string to insert between data_id and field_name when prepend_data_id is true.
Defaults to ':'.
Not used if prepend_data_id is false.
def
transform(self, record: str) -> logger.utils.das_record.DASRecord:
83 def transform(self, record: str) -> DASRecord: 84 """Parse record and return DASRecord.""" 85 # See if it's something we can process, and if not, try digesting 86 if not self.can_process_record(record): # inherited from BaseModule() 87 return self.digest_record(record) # inherited from BaseModule() 88 89 return self.parser.parse_record(record)
Parse record and return DASRecord.