In [1]:
import json
import sys
from konfoo import *
In [2]:
import oyaml as yaml
In [3]:
def to_yaml(container, *attributes, **options):
flow_style = options.pop('flow_style', False)
return yaml.safe_dump(container.view_fields(*attributes, **options),
stream=sys.stdout,
default_flow_style=flow_style)
In [4]:
class Identifier(Structure):
def __init__(self):
super().__init__() # <- NEVER forget to call it first!
self.version = Decimal(8) # 1st field
self.id = Decimal(8) # 2nd field
self.length = Decimal(8) # 3rd field
self.module = Char() # 4th field
self.index_fields() # <- Indexes all fields (optional)
In [5]:
structure = Identifier()
In [6]:
structure.to_list()
Out[6]:
In [7]:
structure.to_csv()
Out[7]:
In [8]:
structure.to_json()
Out[8]:
In [9]:
to_yaml(structure, flow_style=False)
In [10]:
class Identifier(Structure):
def __init__(self):
super().__init__()
self.version = Decimal(8, align_to=4)
self.id = Decimal(8, align_to=4)
self.length = Decimal(8, align_to=4)
self.module = Char(align_to=4)
self.index_fields()
In [11]:
structure = Identifier()
In [12]:
structure.to_list('alignment')
Out[12]:
In [13]:
structure.to_csv('alignment.byte_size', 'alignment.bit_offset', fieldnames=('id', 'size', 'offset'))
Out[13]:
In [14]:
structure.to_json('alignment')
Out[14]:
In [15]:
class HeaderV1(Structure):
def __init__(self):
super().__init__()
self.type = Identifier()
self.index_fields()
In [16]:
header = HeaderV1()
In [17]:
header.to_list()
Out[17]:
In [18]:
header.to_csv()
Out[18]:
In [19]:
header.to_json()
Out[19]:
In [20]:
to_yaml(header, flow_style=False)
In [21]:
class HeaderV2(HeaderV1):
def __init__(self):
super().__init__()
self.size = Decimal32()
self.index_fields()
In [22]:
header = HeaderV2()
In [23]:
header.to_list()
Out[23]:
In [24]:
header.to_csv()
Out[24]:
In [25]:
header.to_json()
Out[25]:
In [26]:
to_yaml(header)
In [27]:
structure = Structure()
structure.version = Decimal(8, align_to=4)
structure.id = Decimal(8, align_to=4)
structure.length = Decimal(8, align_to=4)
structure.module = Char(align_to=4)
In [28]:
structure.to_list()
Out[28]:
In [29]:
structure.to_csv()
Out[29]:
In [30]:
structure.to_json()
Out[30]:
In [31]:
to_yaml(structure, flow_style=False)
In [32]:
structure = Structure(
version=Decimal(8, 4),
id=Decimal(8, 4),
length=Decimal(8, 4),
module=Char(4))
In [33]:
structure.to_list()
Out[33]:
In [34]:
structure.to_csv()
Out[34]:
In [35]:
structure.to_json()
Out[35]:
In [36]:
to_yaml(structure, flow_style=False)
In [37]:
structure = Structure(
type=Structure(version=Decimal(8, 4),
id=Decimal(8, 4),
length=Decimal(8, 4),
module=Char(4)),
size=Decimal32())
In [38]:
structure.to_list()
Out[38]:
In [39]:
structure.to_csv()
Out[39]:
In [40]:
structure.to_json()
Out[40]:
In [41]:
to_yaml(structure, flow_style=False)
In [42]:
structure = Structure(
version=Decimal(8, 4),
id=Decimal(8, 4),
length=Decimal(8, 4),
module=Char(4))
In [43]:
structure.to_list()
Out[43]:
In [44]:
structure.to_csv()
Out[44]:
In [45]:
structure.to_json()
Out[45]:
In [46]:
to_yaml(structure, flow_style=False)
In [47]:
structure.initialize_fields(dict(version=1, id=2, length=9, module='F'))
In [48]:
structure.initialize_fields({"version": 1, "id": 2, "length": 9, "module": "F"})
In [49]:
structure.to_list()
Out[49]:
In [50]:
structure.to_csv()
Out[50]:
In [51]:
structure.to_json()
Out[51]:
In [52]:
to_yaml(structure, flow_style=False)
In [53]:
structure
Out[53]:
In [54]:
structure.describe()
Out[54]:
In [55]:
json.dump(structure.describe(), sys.stdout, indent=2)
In [56]:
d3flare_json(structure.describe(), sys.stdout, indent=2)
In [57]:
structure.container_size()
Out[57]:
In [58]:
num_of_bytes, num_of_remaining_bits = structure.container_size()
In [59]:
num_of_bytes
Out[59]:
In [60]:
num_of_remaining_bits
Out[60]:
In [61]:
structure.to_list('index')
Out[61]:
In [62]:
structure.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))
Out[62]:
In [63]:
structure.to_json('index')
Out[63]:
In [64]:
structure.index_fields(index=Index())
Out[64]:
In [65]:
structure.index_fields()
Out[65]:
In [66]:
structure.to_list('index')
Out[66]:
In [67]:
structure.to_csv('index.byte', 'index.bit', 'index.address', fieldnames=('id', 'index', 'offset', 'address'))
Out[67]:
In [68]:
structure.to_json('index')
Out[68]:
In [69]:
structure.deserialize(bytes.fromhex('01020946f00f00'))
Out[69]:
In [70]:
structure.to_list()
Out[70]:
In [71]:
structure.to_csv()
Out[71]:
In [72]:
structure.to_json()
Out[72]:
In [73]:
to_yaml(structure, flow_style=False)
In [74]:
bytestream = bytearray()
bytestream.hex()
Out[74]:
In [75]:
structure.serialize(bytestream)
Out[75]:
In [76]:
bytes(structure).hex()
Out[76]:
In [77]:
bytestream.hex()
Out[77]:
In [78]:
len(structure)
Out[78]:
In [79]:
structure.version
Out[79]:
In [80]:
structure['version']
Out[80]:
In [81]:
structure.version.name
Out[81]:
In [82]:
structure.version.value
Out[82]:
In [83]:
structure.version.bit_size
Out[83]:
In [84]:
structure.version.alignment
Out[84]:
In [85]:
structure.version.alignment.byte_size
Out[85]:
In [86]:
structure.version.alignment.bit_offset
Out[86]:
In [87]:
structure.version.byte_order
Out[87]:
In [88]:
structure.version.byte_order.value
Out[88]:
In [89]:
structure.version.index
Out[89]:
In [90]:
structure.version.index.byte
Out[90]:
In [91]:
structure.version.index.bit
Out[91]:
In [92]:
structure.version.index.address
Out[92]:
In [93]:
structure.version.index.base_address
Out[93]:
In [94]:
structure.version.index.update
Out[94]:
In [95]:
[(name, member) for name, member in structure.items()]
Out[95]:
In [96]:
[name for name in structure.keys()]
Out[96]:
In [97]:
[member for member in structure.values()]
Out[97]:
In [98]:
structure.view_fields()
Out[98]:
In [99]:
structure.view_fields('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order.name')
Out[99]:
In [100]:
structure.to_json()
Out[100]:
In [101]:
print(structure.to_json(indent=2))
In [102]:
structure.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')
Out[102]:
In [103]:
print(structure.to_json('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order', indent=2))
In [104]:
structure.field_items()
Out[104]:
In [105]:
structure.to_list()
Out[105]:
In [106]:
structure.to_list('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order.name')
Out[106]:
In [107]:
structure.to_dict()
Out[107]:
In [108]:
structure.to_dict('name', 'bit_size', 'value', 'index', 'alignment', 'byte_order')
Out[108]:
In [109]:
structure.to_csv()
Out[109]:
In [110]:
structure.to_csv('name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))
Out[110]:
In [111]:
structure.write_csv('Structure.csv', 'name', 'bit_size', 'value', fieldnames=('id', 'type', 'size', 'value'))
In [112]:
structure.save('Structure.ini')
In [113]:
structure.load('Structure.ini')
In [ ]: