XtekRecord#

class hexrec.formats.xtek.XtekRecord(*super_init_args, addrlen=8, validate=True, **super_init_kwargs)[source]#

Tektronix Extended record object.

Attributes

EQUALITY_KEYS

Meta keys for equality checks.

LINE1_REGEX

Line parser regex, part 1.

LINE2_REGEX

Line parser regex, part 2.

META_KEYS

Meta keys.

Methods

__init__

compute_address_max

Calculates the maximum address.

compute_checksum

Computes the checksum field value.

compute_count

Compute the count field value.

compute_data_max

Calculates the maximum data size.

copy

Shallow copy.

create_data

Creates a data record.

create_eof

Creates an End Of File record.

data_to_int

Interprets data bytes as integer.

get_address_max

Calculates the maximum address.

get_data_max

Calculates the maximum data size.

get_meta

Gets meta information.

parse

Parses a record from bytes.

print

Prints a record.

serialize

Serializes onto a stream.

to_bytestr

Converts into a byte string.

to_tokens

Converts into byte string tokens.

update_checksum

Updates the checksum field.

update_count

Updates the count field.

validate

Validates consistency of attribute values.

EQUALITY_KEYS: Sequence[str] = ['address', 'checksum', 'count', 'data', 'tag', 'addrlen']#

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>[^%]*)%(?P<count>[0-9A-Fa-f]{2})(?P<tag>[68])(?P<checksum>[0-9A-Fa-f]{2})(?P<addrlen>[1-9A-Fa-f])')#

Line parser regex, part 1.

LINE2_REGEX = [re.compile(b'^(?P<address>[0-9A-Fa-f]{1})(?P<data>([0-9A-Fa-f]{2}){,124})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{2})(?P<data>([0-9A-Fa-f]{2}){,123})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{3})(?P<data>([0-9A-Fa-f]{2}){,123})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{4})(?P<data>([0-9A-Fa-f]{2}){,122})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{5})(?P<data>([0-9A-Fa-f]{2}){,122})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{6})(?P<data>([0-9A-Fa-f]{2}){,121})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{7})(?P<data>([0-9A-Fa-f]{2}){,121})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{8})(?P<data>([0-9A-Fa-f]{2}){,120})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{9})(?P<data>([0-9A-Fa-f]{2}){,120})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{10})(?P<data>([0-9A-Fa-f]{2}){,119})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{11})(?P<data>([0-9A-Fa-f]{2}){,119})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{12})(?P<data>([0-9A-Fa-f]{2}){,118})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{13})(?P<data>([0-9A-Fa-f]{2}){,118})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{14})(?P<data>([0-9A-Fa-f]{2}){,117})(?P<after>[^\\r\\n]*)\\r?\\n$'), re.compile(b'^(?P<address>[0-9A-Fa-f]{15})(?P<data>([0-9A-Fa-f]{2}){,117})(?P<after>[^\\r\\n]*)\\r?\\n$')]#

Line parser regex, part 2.

META_KEYS: Sequence[str] = ['address', 'after', 'before', 'checksum', 'coords', 'count', 'data', 'tag', 'addrlen']#

Meta keys.

This sequence holds the meta keys for copying (see copy()).

Tag#

alias of XtekTag

__bytes__()#

Serializes the record into bytes.

Returns:

bytes – Byte serialization.

See also

to_bytestr()

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:

boolself equals other.

See also

__ne__()

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__(*super_init_args, addrlen=8, validate=True, **super_init_kwargs)[source]#
__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:

boolself and other are unequal.

See also

__eq__()

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

to_bytestr()

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)

classmethod compute_address_max(addrlen)[source]#

Calculates the maximum address.

It calculates the maximum address field given the number of nibbles.

Parameters:

addrlen (int) – Address length, in nibbles (4-bit units).

Returns:

int – Maximum address value.

Raises:

ValueError – invalid addrlen.

Examples

>>> from hexrec import XtekFile
>>> XtekRecord = XtekFile.Record
>>> hex(XtekRecord.compute_address_max(4))
'0xffff'
>>> hex(XtekRecord.compute_address_max(6))
'0xffffff'
>>> hex(XtekRecord.compute_address_max(8))
'0xffffffff'
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'
classmethod compute_data_max(addrlen)[source]#

Calculates the maximum data size.

It calculates the maximum data field size given the number of nibbles

Parameters:

addrlen (int) – Address length, in nibbles (4-bit units).

Returns:

int – Maximum data size.

Raises:

ValueError – invalid addrlen.

Examples

>>> from hexrec import XtekFile
>>> XtekRecord = XtekFile.Record
>>> XtekRecord.compute_data_max(4)
122
>>> XtekRecord.compute_data_max(6)
121
>>> XtekRecord.compute_data_max(8)
120
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.

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, addrlen=8)[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.

  • addrlen (int) – Address length, in nibbles (4-bit units).

Returns:

XtekRecord – Data record object.

Raises:

ValueError – invalid parameter.

Examples

>>> from hexrec import XtekFile
>>> record = XtekFile.Record.create_data(0x1234, b'abc')
>>> str(record)
'%14635800001234616263\r\n'
classmethod create_eof(start=0, addrlen=8)[source]#

Creates an End Of File record.

The End Of File record also carries the start address.

Parameters:
  • start (int) – Start address.

  • addrlen (int) – Address length, in nibbles (4-bit units).

Returns:

XtekRecord – End Of File record object.

Examples

>>> from hexrec import XtekFile
>>> record = XtekFile.Record.create_eof(start=0x12345678)
>>> str(record)
'%0E842812345678\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_address_max()[source]#

Calculates the maximum address.

It calculates the maximum address field for the calling tag. If the address field is not supported, it returns None.

Returns:

int – Maximum address value, or None.

Examples

>>> from hexrec import XtekFile
>>> XtekRecord = XtekFile.Record
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=4)
>>> hex(record.get_address_max())
'0xffff'
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=6)
>>> hex(record.get_address_max())
'0xffffff'
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=8)
>>> hex(record.get_address_max())
'0xffffffff'
get_data_max()[source]#

Calculates the maximum data size.

It calculates the maximum data field size given the number of nibbles

Returns:

int – Maximum data size.

Raises:

ValueError – invalid addrlen.

Examples

>>> from hexrec import XtekFile
>>> XtekRecord = XtekFile.Record
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=4)
>>> record.get_data_max()
122
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=6)
>>> record.get_data_max()
121
>>> record = XtekRecord.create_data(0xFFFF, b'abc', addrlen=8)
>>> record.get_data_max()
120
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. If None, stdout is selected.

  • color (bool) – Tokens are colorized before printing.

  • kwargs – Forwarded to the underlying call to to_tokens().

Returns:

BaseRecordself.

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() and stream.write.

Parameters:
Returns:

BaseRecordself.

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 by compute_checksum().

Returns:

BaseRecordself.

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 by compute_count().

Returns:

BaseRecordself.

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:

BaseRecordself.

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