Package pyffi :: Package object_models :: Package xml :: Module bit_struct :: Class BitStructBase
[hide private]
[frames] | no frames]

Class BitStructBase

source code

            object --+    
                     |    
utils.graph.DetailNode --+
                         |
                        BitStructBase
Known Subclasses:

Base class from which all file bitstruct types are derived.

The BitStructBase class implements the basic bitstruct interface: it will initialize all attributes using the class interface using the _attrs class variable, represent them as strings, and so on. The class variable _attrs must be declared every derived class interface.

Each item in the class _attrs list stores the information about the attribute as stored for instance in the xml file, and the _<name>_value_ instance variable stores the actual attribute instance.

Direct access to the attributes is implemented using a <name> property which invokes the get_attribute and set_attribute functions, as demonstrated below.

See the pyffi.XmlHandler class for a more advanced example.

>>> from pyffi.object_models.xml.basic import BasicBase
>>> from pyffi.object_models.xml.expression import Expression
>>> from pyffi.object_models.xml import BitStructAttribute as Attr
>>> class SimpleFormat(object):
...     @staticmethod
...     def name_attribute(name):
...         return name
>>> class Flags(BitStructBase):
...     _numbytes = 1
...     _attrs = [
...         Attr(SimpleFormat, dict(name = 'a', numbits = '3')),
...         Attr(SimpleFormat, dict(name = 'b', numbits = '1'))]
>>> SimpleFormat.Flags = Flags
>>> y = Flags()
>>> y.a = 5
>>> y.b = 1
>>> print(y) # doctest:+ELLIPSIS
<class 'pyffi.object_models.xml.bit_struct.Flags'> instance at 0x...
* a : 5
* b : 1
<BLANKLINE>
>>> y.to_int(None)
13
>>> y.from_int(9, None)
>>> print(y) # doctest:+ELLIPSIS
<class 'pyffi.object_models.xml.bit_struct.Flags'> instance at 0x...
* a : 1
* b : 1
<BLANKLINE>
Nested Classes [hide private]
  __metaclass__
This metaclass checks for the presence of a _attrs attribute.
Instance Methods [hide private]
 
__init__(self, template=None, argument=None, parent=None)
The constructor takes a tempate: any attribute whose type, or template type, is type(None) - which corresponds to TEMPLATE in the xml description - will be replaced by this type.
source code
 
deepcopy(self, block)
Copy attributes from a given block (one block class must be a subclass of the other).
source code
 
__str__(self)
str(x)
source code
 
read(self, stream, data)
Read structure from stream.
source code
 
from_int(self, value, data)
Set structure values from integer.
source code
 
to_int(self, data)
Get as integer.
source code
 
write(self, stream, data)
Write structure to stream.
source code
 
fix_links(self, data)
Fix links in the structure.
source code
 
get_links(self, data=None)
Get list of all links in the structure.
source code
 
get_strings(self, data)
Get list of all strings in the structure.
source code
 
get_refs(self, data=None)
Get list of all references in the structure.
source code
 
get_size(self, data=None)
Calculate the structure size in bytes.
source code
 
get_hash(self, data=None)
Calculate a hash for the structure, as a tuple.
source code
 
_get_filtered_attribute_list(self, data=None)
Generator for listing all 'active' attributes, that is, attributes whose condition evaluates True, whose version interval contaions C{version}, and whose user version is C{user_version}.
source code
 
get_attribute(self, name)
Get a basic attribute.
source code
 
set_attribute(self, value, name)
Set the value of a basic attribute.
source code
 
tree(self)
A generator for parsing all blocks in the tree (starting from and including C{self}).
source code
generator yielding DetailNodes
get_detail_child_nodes(self, edge_filter=(True, True))
Yield children of this structure.
source code
generator yielding strs
get_detail_child_names(self, edge_filter=(True, True))
Yield name of each child.
source code

Inherited from utils.graph.DetailNode: get_detail_child_edge_types, get_detail_display, get_detail_iterator, replace_global_node

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

Class Methods [hide private]
 
get_games(cls)
Get games for which this block is supported.
source code
 
get_versions(cls, game)
Get versions supported for C{game}.
source code
 
_get_attribute_list(cls)
Calculate the list of all attributes of this structure.
source code
 
_get_names(cls)
Calculate the list of all attributes names in this structure.
source code
Class Variables [hide private]
  _attrs = []
  _numbytes = 1
  _games = {}
  arg = None
hash(x)
  _attribute_list = []
  _has_links = False
  _has_refs = False
  _has_strings = False
  _is_template = False
  _names = []
  _struct = 'B'
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, template=None, argument=None, parent=None)
(Constructor)

source code 
The constructor takes a tempate: any attribute whose type, or template type, is type(None) - which corresponds to TEMPLATE in the xml description - will be replaced by this type. The argument is what the ARG xml tags will be replaced with.
Parameters:
  • template - If the class takes a template type argument, then this argument describes the template type.
  • argument - If the class takes a type argument, then it is described here.
  • parent - The parent of this instance, that is, the instance this array is an attribute of.
Overrides: object.__init__

deepcopy(self, block)

source code 
Copy attributes from a given block (one block class must be a subclass of the other). Returns self.

__str__(self)
(Informal representation operator)

source code 
str(x)
Overrides: object.__str__
(inherited documentation)

get_refs(self, data=None)

source code 
Get list of all references in the structure. Refs are links that point down the tree. For instance, if you need to parse the whole tree starting from the root you would use get_refs and not get_links, as get_links could result in infinite recursion.

_get_names(cls)
Class Method

source code 
Calculate the list of all attributes names in this structure. Skips duplicate names.

_get_filtered_attribute_list(self, data=None)

source code 

Generator for listing all 'active' attributes, that is, attributes whose condition evaluates True, whose version interval contaions C{version}, and whose user version is C{user_version}. None for C{version} or C{user_version} means that these checks are ignored. Duplicate names are skipped as well.

Note: Use data instead of version and user_version (old way will be deprecated).

tree(self)

source code 
A generator for parsing all blocks in the tree (starting from and including C{self}). By default, there is no tree structure, so returns self.

get_detail_child_nodes(self, edge_filter=(True, True))

source code 
Yield children of this structure.
Parameters:
  • edge_filter - The edge type to include.
Returns: generator yielding DetailNodes
Generator for detail tree child nodes.
Overrides: utils.graph.DetailNode.get_detail_child_nodes

get_detail_child_names(self, edge_filter=(True, True))

source code 
Yield name of each child.
Returns: generator yielding strs
Generator for detail tree child names.
Overrides: utils.graph.DetailNode.get_detail_child_names