SrecRecord#
- class hexrec.formats.srec.SrecRecord(tag, address=0, data=b'', count=Ellipsis, checksum=Ellipsis, before=b'', after=b'', coords=(-1, -1), validate=True)[source]#
Motorola S-record record object.
Attributes
Meta keys for equality checks.
Line parser regex, part 1.
Line parser regex, part 2.
Line parser regex, part 3.
Meta keys.
Methods
Computes the checksum field value.
Compute the count field value.
Shallow copy.
Creates a record count record.
Creates a data record.
Creates a header record.
Creates a start address record.
Interprets data bytes as integer.
Gets meta information.
Parses a record from bytes.
Prints a record.
Serializes onto a stream.
Converts into a byte string.
Converts into byte string tokens.
Updates the checksum field.
Updates the count field.
Validates consistency of attribute values.
- EQUALITY_KEYS: Sequence[str] = ['address', 'checksum', 'count', 'data', 'tag']#
Meta keys for equality checks.
Equality methods (
__eq__()
and__ne__()
) check against these meta keys only. Any other meta keys are just ignored.
- LINE1_REGEX = re.compile(b'^(?P<before>\\s*)[Ss](?P<tag>[0-9A-Fa-f])(?P<count>[0-9A-Fa-f]{2})')#
Line parser regex, part 1.
- LINE2_REGEX = [re.compile(b'^(?P<address>[0-9A-Fa-f]{4})'), re.compile(b'^(?P<address>[0-9A-Fa-f]{6})'), re.compile(b'^(?P<address>[0-9A-Fa-f]{8})')]#
Line parser regex, part 2.
- LINE3_REGEX = re.compile(b'^(?P<data>([0-9A-Fa-f]{2})*)(?P<checksum>[0-9A-Fa-f]{2})(?P<after>[^\\r\\n]*)\\r?\\n?$')#
Line parser regex, part 3.
- META_KEYS: Sequence[str] = ['address', 'after', 'before', 'checksum', 'coords', 'count', 'data', 'tag']#
Meta keys.
This sequence holds the meta keys for copying (see
copy()
).
- __bytes__()#
Serializes the record into bytes.
- Returns:
bytes – Byte serialization.
See also
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_end_of_file() >>> bytes(record) b':00000001FF\r\n'
>>> from hexrec import RawFile >>> record = RawFile.Record.create_data(0, b'abc') >>> bytes(record) b'abc'
- __eq__(other)#
Equality test.
This method returns true if self is considered equal to other.
As inequality is usually easier to check, this method is usually implemented as a trivial
not self != other
(__ne__()
).- Parameters:
other (
BaseRecord
) – Record to compare to.- Returns:
bool – self equals other.
See also
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile, RawFile >>> ihex1 = IhexFile.Record.create_data(0, b'abc') >>> ihex2 = IhexFile.Record.create_data(0, b'abc') >>> ihex1 is ihex2 False >>> ihex1 == ihex2 True >>> ihex3 = IhexFile.Record.create_data(0, b'xyz') >>> ihex1 == ihex3 False >>> raw = RawFile.Record.create_data(0, b'abc') >>> ihex1 == raw False
- __hash__ = None#
- __init__(tag, address=0, data=b'', count=Ellipsis, checksum=Ellipsis, before=b'', after=b'', coords=(-1, -1), validate=True)#
- __ne__(other)#
Ineuality test.
This method returns true if self is considered unequal to other.
Each attribute listed by
EQUALITY_KEYS
is compared between self and other. This method returns whether any attributes do not match.- Parameters:
other (
BaseRecord
) – Record to compare to.- Returns:
bool – self and other are unequal.
See also
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile, RawFile >>> ihex1 = IhexFile.Record.create_data(0, b'abc') >>> ihex2 = IhexFile.Record.create_data(0, b'abc') >>> ihex1 is ihex2 False >>> ihex1 != ihex2 False >>> ihex3 = IhexFile.Record.create_data(0, b'xyz') >>> ihex1 != ihex3 True >>> raw = RawFile.Record.create_data(0, b'abc') >>> ihex1 != raw True
- __repr__()#
String representation.
It returns a string representation of the record content, for human understanding only.
- Returns:
str – String representation.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_end_of_file() >>> repr(record) "<<class 'hexrec.formats.ihex.IhexRecord'> @... address:=0 after:=b'' before:=b'' checksum:=255 coords:=(-1, -1) count:=0 data:=b'' tag:=<IhexTag.END_OF_FILE: 1>>"
- __str__()#
Serializes the record into a string.
- Returns:
str – String serialization.
See also
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_end_of_file() >>> str(record) ':00000001FF\r\n'
>>> from hexrec import RawFile >>> record = RawFile.Record.create_data(0, b'abc') >>> str(record) 'abc'
- __weakref__#
list of weak references to the object (if defined)
- compute_checksum()[source]#
Computes the checksum field value.
It computes and returns the format-specific
checksum
value of a record.When not specialized, it returns
None
by default.- Returns:
int – Computed checksum value.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0, b'abc') >>> record.compute_checksum() 215
>>> from hexrec import RawFile >>> record = RawFile.Record.create_data(0, b'abc') >>> repr(record.compute_checksum()) 'None'
- compute_count()[source]#
Compute the count field value.
It computes and returns the format-specific
count
value of a record.When not specialized, it returns
None
by default.- Returns:
int – Computed checksum value.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0, b'abc') >>> record.compute_count() 3
>>> from hexrec import RawFile >>> record = RawFile.Record.create_data(0, b'abc') >>> repr(record.compute_count()) 'None'
- copy(validate=True)#
Shallow copy.
It calls the record constructor, passing meta to it.
- Parameters:
validate (bool) – Performs validation on instantiation (
__init__()
).- Returns:
BaseRecord
– Shallow copy.
See also
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record1 = IhexFile.Record.create_data(0x1234, b'abc') >>> record2 = record1.copy() >>> record1 is record2 False >>> record1 == record2 True
- classmethod create_count(count, tag=None)[source]#
Creates a record count record.
This is method instantiates a record count record, optionally choosing the desired count size.
- Parameters:
count (int) – Number of preceding data records.
tag (
SrecTag
) – Chosen record count tag. IfNone
, it uses the one returned bySrecTag.fit_count_tag()
.
- Returns:
SrecRecord
– Record count record object.
Examples
>>> from hexrec import SrecFile >>> record = SrecFile.Record.create_count(0x1234) >>> str(record) 'S5031234B6\r\n' >>> tag = SrecFile.Record.Tag.COUNT_24 >>> record = SrecFile.Record.create_count(0x1234, tag=tag) >>> str(record) 'S604001234B5\r\n'
- classmethod create_data(address, data, tag=None)[source]#
Creates a data record.
This is method instantiates a data record, optionally choosing the desired address size.
- Parameters:
address (int) – Record address.
data (bytes) – Record byte data.
tag (
SrecTag
) – Chosen data tag. IfNone
, it uses the one returned bySrecTag.fit_data_tag()
.
- Returns:
SrecRecord
– Data record object.
Examples
>>> from hexrec import SrecFile >>> record = SrecFile.Record.create_data(0x1234, b'abc') >>> str(record) 'S10612346162638D\r\n' >>> tag = SrecFile.Record.Tag.DATA_32 >>> record = SrecFile.Record.create_data(0x1234, b'abc', tag=tag) >>> str(record) 'S308000012346162638B\r\n'
- classmethod create_header(data=b'')[source]#
Creates a header record.
- Parameters:
data (bytes) – Header byte data.
- Returns:
IhexRecord
– Header record.- Raises:
ValueError – data size overflow.
Examples
>>> from hexrec import SrecFile >>> record = SrecFile.Record.create_header() >>> str(record) 'S0030000FC\r\n' >>> record = SrecFile.Record.create_header(b'HDR\0') >>> str(record) 'S0070000484452001A\r\n'
- classmethod create_start(address=0, tag=None)[source]#
Creates a start address record.
This is method instantiates a start address record, optionally choosing the desired address size.
- Parameters:
address (int) – Start address.
tag (
SrecTag
) – Chosen start tag. IfNone
, it uses the one matchingSrecTag.fit_start_tag()
.
- Returns:
SrecRecord
– Start address record object.
Examples
>>> from hexrec import SrecFile >>> record = SrecFile.Record.create_start(0x1234) >>> str(record) 'S9031234B6\r\n' >>> tag = SrecFile.Record.Tag.START_32 >>> record = SrecFile.Record.create_start(0x1234, tag=tag) >>> str(record) 'S70500001234B4\r\n'
- data_to_int(byteorder='big', signed=False)#
Interprets data bytes as integer.
It creates an integer from bytes of the
data
field.- Parameters:
byteorder ('big' or 'little') – Byte order (endianness): either
'big'
(default) or'little'
.signed (bool) – Signed integer (2-complement); default false.
- Returns:
int – Interpreted integer value.
See also
int.from_bytes()
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_extended_linear_address(0xABCD) >>> record.data b'\xab\xcd' >>> addrext = record.data_to_int() >>> addrext, hex(addrext) (43981, '0xabcd')
- get_meta()#
Gets meta information.
It returns all the object attributes whose keys are listed by
META_KEYS
.- Returns:
dict – Attribute values listed by
META_KEYS
.
See also
META_KEYS
set_meta()
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_end_of_file() >>> record.get_meta() {'address': 0, 'after': b'', 'before': b'', 'checksum': 255, 'coords': (-1, -1), 'count': 0, 'data': b'', 'tag': <IhexTag.END_OF_FILE: 1>}
- classmethod parse(line, validate=True)[source]#
Parses a record from bytes.
Please refer to the actual implementation provided by the record format for more details.
- Parameters:
line (bytes) – String of bytes to parse.
validate (bool) – Perform validation checks.
- Returns:
BaseRecord
– Parsed record.- Raises:
ValueError – Syntax error.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.parse(b':00000001FF\r\n') >>> record.tag <IhexTag.END_OF_FILE: 1> >>> IhexFile.Record.parse(b'::00000001FF\r\n') Traceback (most recent call last): ... ValueError: syntax error
- print(*args, stream=None, color=False, **kwargs)#
Prints a record.
The record is converted into tokens (eventually colorized) then joined and written onto a byte stream (stdout by default).
- Parameters:
args – Forwarded to the underlying call to
to_tokens()
.stream (
io.BytesIO
) – The byte stream where the record tokens are printed. IfNone
, stdout is selected.color (bool) – Tokens are colorized before printing.
kwargs – Forwarded to the underlying call to
to_tokens()
.
- Returns:
BaseRecord
– self.
See also
to_tokens()
colorize_tokens()
io.BytesIO
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0x1234, b'abc') >>> _ = record.print() :0312340061626391 >>> import io >>> stream = io.BytesIO() >>> _ = record.print(stream=stream, color=True) >>> stream.getvalue() b'\x1b[0m\x1b[33m:\x1b[34m03\x1b[31m1234\x1b[32m00\x1b[36m61\x1b[96m62\x1b[36m63\x1b[35m91\x1b[0m\r\n\x1b[0m'
- serialize(stream, *args, **kwargs)#
Serializes onto a stream.
This wraps a call to
to_bytestr()
andstream.write
.- Parameters:
stream (
io.BytesIO
) – Stream to write.args – Forwarded to
to_bytestr()
.kwargs – Forwarded to
to_bytestr()
.
- Returns:
BaseRecord
– self.
See also
to_bytestr()
io.BytesIO
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0x1234, b'abc') >>> import io >>> stream = io.BytesIO() >>> _ = record.serialize(stream, end=b'\n') >>> stream.getvalue() b':0312340061626391\n'
- to_bytestr(end=b'\\r\\n')[source]#
Converts into a byte string.
- Parameters:
args – Implementation specific.
kwargs – Implementation specific.
- Returns:
bytes – Byte string representation.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0x1234, b'abc') >>> record.to_bytestr(end=b'\n') b':0312340061626391\n'
- to_tokens(end=b'\\r\\n')[source]#
Converts into byte string tokens.
- Parameters:
args – Implementation specific.
kwargs – Implementation specific.
- Returns:
bytes – Mapping of token keys to token byte strings.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_data(0x1234, b'abc') >>> record.to_tokens(end=b'\n') {'before': b'', 'begin': b':', 'count': b'03', 'address': b'1234', 'tag': b'00', 'data': b'616263', 'checksum': b'91', 'after': b'', 'end': b'\n'}
- update_checksum()#
Updates the checksum field.
It updates the
checksum
attribute, assigning to it the value returned bycompute_checksum()
.- Returns:
BaseRecord
– self.
See also
checksum
compute_checksum()
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> IhexRecord = IhexFile.Record >>> record = IhexRecord(IhexRecord.Tag.END_OF_FILE, checksum=None) >>> record.compute_checksum() 255 >>> record.checksum is None True >>> _ = record.update_checksum() >>> record.checksum 255
- update_count()#
Updates the count field.
It updates the
count
attribute, assigning to it the value returned bycompute_count()
.- Returns:
BaseRecord
– self.
See also
count
compute_count()
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> Record = IhexFile.Record >>> Tag = Record.Tag >>> record = Record(Tag.DATA, data=b'abc', count=None, checksum=None) >>> record.compute_count() 3 >>> record.count is None True >>> _ = record.update_count() >>> record.count 3
- validate(checksum=True, count=True)[source]#
Validates consistency of attribute values.
All the record attributes are checked for consistency.
Please refer to the implementation for more details.
- Parameters:
checksum (bool) – Check the consistency of the
checksum
attribute.count (bool) – Check the consistency of the
count
attribute.
- Returns:
BaseRecord
– self.- Raises:
ValueError – Some targeted attributes are inconsistent.
Examples
NOTE: These examples are provided by
BaseRecord
. Inherited classes for specific formats may require an adaptation.>>> from hexrec import IhexFile >>> record = IhexFile.Record.create_end_of_file() >>> _ = record.validate() >>> record.data = b'abc' >>> _ = record.update_count().update_checksum().validate() Traceback (most recent call last): ... ValueError: unexpcted data