Package pyffi :: Package object_models :: Package xml :: Module basic
[hide private]
[frames] | no frames]

Source Code for Module pyffi.object_models.xml.basic

  1  """Implements base class for basic types.""" 
  2   
  3  # -------------------------------------------------------------------------- 
  4  # ***** BEGIN LICENSE BLOCK ***** 
  5  # 
  6  # Copyright (c) 2007-2011, Python File Format Interface 
  7  # All rights reserved. 
  8  # 
  9  # Redistribution and use in source and binary forms, with or without 
 10  # modification, are permitted provided that the following conditions 
 11  # are met: 
 12  # 
 13  #    * Redistributions of source code must retain the above copyright 
 14  #      notice, this list of conditions and the following disclaimer. 
 15  # 
 16  #    * Redistributions in binary form must reproduce the above 
 17  #      copyright notice, this list of conditions and the following 
 18  #      disclaimer in the documentation and/or other materials provided 
 19  #      with the distribution. 
 20  # 
 21  #    * Neither the name of the Python File Format Interface 
 22  #      project nor the names of its contributors may be used to endorse 
 23  #      or promote products derived from this software without specific 
 24  #      prior written permission. 
 25  # 
 26  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 27  # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 28  # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 29  # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 30  # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 31  # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 32  # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 33  # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 34  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 35  # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 36  # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 37  # POSSIBILITY OF SUCH DAMAGE. 
 38  # 
 39  # ***** END LICENSE BLOCK ***** 
 40  # -------------------------------------------------------------------------- 
 41   
 42  from pyffi.utils.graph import DetailNode 
 43   
44 -class BasicBase(DetailNode):
45 """Base class from which all basic types are derived. 46 47 The BasicBase class implements the interface for basic types. 48 All basic types are derived from this class. 49 They must override read, write, get_value, and set_value. 50 51 >>> import struct 52 >>> class UInt(BasicBase): 53 ... def __init__(self, template = None, argument = 0): 54 ... self.__value = 0 55 ... def read(self, version = None, user_version = None, f = None, 56 ... link_stack = [], argument = None): 57 ... self.__value, = struct.unpack('<I', f.read(4)) 58 ... def write(self, version = None, user_version = None, f = None, 59 ... block_index_dct = {}, argument = None): 60 ... f.write(struct.pack('<I', self.__value)) 61 ... def get_value(self): 62 ... return self.__value 63 ... def set_value(self, value): 64 ... self.__value = int(value) 65 >>> x = UInt() 66 >>> x.set_value('123') 67 >>> x.get_value() 68 123 69 >>> class Test(BasicBase): # bad: read, write, get_value, and set_value are 70 ... # not implemented 71 ... pass 72 >>> x = Test() # doctest: +ELLIPSIS 73 >>> x.set_value('123') # doctest: +ELLIPSIS 74 Traceback (most recent call last): 75 ... 76 NotImplementedError 77 """ 78 79 _is_template = False # is it a template type? 80 _has_links = False # does the type contain a Ref or a Ptr? 81 _has_refs = False # does the type contain a Ref? 82 _has_strings = False # does the type contain a string? 83 arg = None # default argument 84
85 - def __init__(self, template = None, argument = None, parent = None):
86 """Initializes the instance. 87 88 :param template: type used as template 89 :param argument: argument used to initialize the instance 90 (see the Struct class). 91 :param parent: The parent of this instance, that is, the instance this 92 instance is an attribute of.""" 93 # parent disabled for performance 94 #self._parent = weakref.ref(parent) if parent else None 95 pass
96 97 # string representation
98 - def __str__(self):
99 """Return string representation.""" 100 return str(self.get_value())
101
102 - def read(self, stream, data):
103 """Read object from file.""" 104 raise NotImplementedError
105
106 - def write(self, stream, data):
107 """Write object to file.""" 108 raise NotImplementedError
109 114 118
119 - def get_strings(self, data):
120 """Return all strings used by this object.""" 121 return []
122
123 - def get_refs(self, data=None):
124 """Return all references (excluding weak pointers) used by this 125 object.""" 126 return []
127
128 - def get_value(self):
129 """Return object value.""" 130 raise NotImplementedError
131
132 - def set_value(self, value):
133 """Set object value.""" 134 raise NotImplementedError
135
136 - def get_size(self, data=None):
137 """Returns size of the object in bytes.""" 138 raise NotImplementedError
139
140 - def get_hash(self, data=None):
141 """Returns a hash value (an immutable object) that can be used to 142 identify the object uniquely.""" 143 raise NotImplementedError
144
145 - def replace_global_node(self, oldbranch, newbranch, **kwargs):
146 """Replace a given branch.""" 147 pass
148 149 # 150 # user interface functions come next 151 # these functions are named after similar ones in the TreeItem example 152 # at http://doc.trolltech.com/4.3/itemviews-simpletreemodel.html 153 # 154 155 # DetailNode 156
157 - def get_detail_display(self):
158 """Return an object that can be used to display the instance.""" 159 return str(self.get_value())
160 161 # editor functions: default implementation assumes that the value is 162 # also suitable for an editor; override if not 163
164 - def get_editor_value(self):
165 """Return value suitable for editor.""" 166 return self.get_value()
167
168 - def set_editor_value(self, editorvalue):
169 """Set value from editor value.""" 170 return self.set_value(editorvalue)
171