MosRecord#
- class hexrec.formats.mos.MosRecord(tag, address=0, data=b'', count=Ellipsis, checksum=Ellipsis, before=b'', after=b'', coords=(-1, -1), validate=True)[source]#
MOS Technology record object.
Attributes
Meta keys for equality checks.
Line parser regex.
Meta keys.
Methods
Computes the checksum field value.
Compute the count field value.
Shallow copy.
Creates a data record.
Creates an End Of File 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.
- LINE_REGEX = re.compile(b'^\x00*(?P<before>[^;]*);(?P<count>[0-9A-Fa-f]{2})(?P<address>[0-9A-Fa-f]{4})(?P<data>([0-9A-Fa-f]{2}){,255})(?P<checksum>[0-9A-Fa-f]{4})(?P<after>[^\\r\\n]*)\\r?\\n?\x00*$')#
Line parser regex.
- 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_data(address, data)[source]#
Creates a data record.
This is a mandatory class method to instantiate a data record.
- Parameters:
address (int) – Record address. If not supported, set zero.
data (bytes) – Record byte data.
- Returns:
BaseRecord
– Data record object.
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') >>> str(record) ':0312340061626391\r\n'
- classmethod create_eof(record_count)[source]#
Creates an End Of File record.
The End Of File record also carries the record count.
- Parameters:
record_count (int) – Number of preceding records.
- Returns:
MosRecord
– End Of File record object.
Examples
>>> from hexrec import MosFile >>> record = MosFile.Record.create_eof(123) >>> str(record) ';00007B007B\r\n\x00\x00\x00\x00\x00\x00'
- 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, eof=False, 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.
eof (bool) – Parsing an End Of File record.
validate (bool) – Perform validation checks.
- Returns:
BaseRecord
– Parsed record.- Raises:
ValueError – Syntax error.
Examples
>>> from hexrec import MosFile >>> record = MosFile.Record.parse(b';0000010001\r\n', eof=True) >>> record.tag <MosTag.EOF: 1> >>> MosFile.Record.parse(b';;0000010001\r\n', eof=True) 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', nuls=True)[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', nuls=True)[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