pyffi :: object_models :: xml :: struct_ :: StructBase :: Class StructBase
[hide private]
[frames] | no frames]

Class StructBase

source code

            object --+        
                     |        
utils.graph.DetailNode --+    
                         |    
    utils.graph.GlobalNode --+
                             |
                            StructBase
Known Subclasses:

Base class from which all file struct types are derived.

The StructBase class implements the basic struct 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 StructAttribute as Attr
>>> class SimpleFormat(object):
...     class UInt(BasicBase):
...         _is_template = False
...         def __init__(self, **kwargs):
...             BasicBase.__init__(self, **kwargs)
...             self.__value = 0
...         def get_value(self):
...             return self.__value
...         def set_value(self, value):
...             self.__value = int(value)
...     @staticmethod
...     def name_attribute(name):
...         return name
>>> class X(StructBase):
...     _is_template = False
...     _attrs = [
...         Attr(SimpleFormat, dict(name = 'a', type = 'UInt')),
...         Attr(SimpleFormat, dict(name = 'b', type = 'UInt'))]
>>> SimpleFormat.X = X
>>> class Y(X):
...     _is_template = False
...     _attrs = [
...         Attr(SimpleFormat, dict(name = 'c', type = 'UInt')),
...         Attr(SimpleFormat, dict(name = 'd', type = 'X', cond = 'c == 3'))]
>>> SimpleFormat.Y = Y
>>> y = Y()
>>> y.a = 1
>>> y.b = 2
>>> y.c = 3
>>> y.d.a = 4
>>> y.d.b = 5
>>> print(y) # doctest:+ELLIPSIS
<class 'pyffi.object_models.xml.struct_.Y'> instance at 0x...
* a : 1
* b : 2
* c : 3
* d :
    <class 'pyffi.object_models.xml.struct_.X'> instance at 0x...
    * a : 4
    * b : 5
<BLANKLINE>
>>> y.d = 1
Traceback (most recent call last):
    ...
TypeError: expected X but got int
>>> x = X()
>>> x.a = 8
>>> x.b = 9
>>> y.d = x
>>> print(y) # doctest:+ELLIPSIS
<class 'pyffi.object_models.xml.struct_.Y'> instance at 0x...
* a : 1
* b : 2
* c : 3
* d :
    <class 'pyffi.object_models.xml.struct_.X'> instance at 0x...
    * a : 8
    * b : 9
<BLANKLINE>
Nested Classes [hide private]
  __metaclass__
This metaclass checks for the presence of _attrs and _is_template attributes.
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
 
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
 
replace_global_node(self, oldbranch, newbranch, **kwargs)
Replace a particular branch in the graph.
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 contains C{version}, and whose user version is C{user_version}.
source code
 
get_attribute(self, name)
Get a (non-basic) attribute.
source code
 
set_attribute(self, value, name)
Set a (non-basic) attribute.
source code
 
get_basic_attribute(self, name)
Get a basic attribute.
source code
 
set_basic_attribute(self, value, name)
Set the value of a basic attribute.
source code
 
get_template_attribute(self, name)
Get a template attribute.
source code
 
set_template_attribute(self, value, name)
Set the value of a template 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 names of the children of this structure.
source code
 
get_global_display(self)
Construct a convenient name for the block itself.
source code
 
get_global_child_nodes(self, edge_filter=(True, True))
Generator which yields all children of this item in the global view, of given edge type (default is edges of type 0).
source code

Inherited from utils.graph.GlobalNode: get_global_child_edge_types, get_global_iterator

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

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]
  _is_template = False
  _attrs = []
  _games = {}
  arg = None
hash(x)
  _attribute_list = []
  _has_links = False
  _has_refs = False
  _has_strings = False
  _names = []
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.

replace_global_node(self, oldbranch, newbranch, **kwargs)

source code 
Replace a particular branch in the graph.
Overrides: utils.graph.DetailNode.replace_global_node
(inherited documentation)

_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 contains 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: version and user_version arguments are deprecated, use the data argument instead.

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 names of the children of this structure.
Returns: generator yielding strs
Generator for detail tree child names.
Overrides: utils.graph.DetailNode.get_detail_child_names

get_global_display(self)

source code 
Construct a convenient name for the block itself.
Returns:
A string.
Overrides: utils.graph.GlobalNode.get_global_display

get_global_child_nodes(self, edge_filter=(True, True))

source code 

Generator which yields all children of this item in the global view, of given edge type (default is edges of type 0).

Override this method.

Returns:
Generator for global node children.
Overrides: utils.graph.GlobalNode.get_global_child_nodes
(inherited documentation)