1  """Implements base class for basic types.""" 
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23   
 24   
 25   
 26   
 27   
 28   
 29   
 30   
 31   
 32   
 33   
 34   
 35   
 36   
 37   
 38   
 39   
 40   
 41   
 42  from pyffi.utils.graph import DetailNode 
 43   
 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  
 80      _has_links = False  
 81      _has_refs = False  
 82      _has_strings = False  
 83      arg = None  
 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           
 94           
 95          pass 
  96   
 97       
 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   
111          """Fix links. Called when all objects have been read, and converts 
112          block indices into blocks.""" 
113          pass 
 114   
116          """Return all links referred to in this object.""" 
117          return [] 
 118   
120          """Return all strings used by this object.""" 
121          return [] 
 122   
124          """Return all references (excluding weak pointers) used by this 
125          object.""" 
126          return [] 
 127   
129          """Return object value.""" 
130          raise NotImplementedError 
 131   
133          """Set object value.""" 
134          raise NotImplementedError 
 135   
137          """Returns size of the object in bytes.""" 
138          raise NotImplementedError 
 139   
141          """Returns a hash value (an immutable object) that can be used to 
142          identify the object uniquely.""" 
143          raise NotImplementedError 
 144   
146          """Replace a given branch.""" 
147          pass 
 148   
149       
150       
151       
152       
153       
154   
155       
156   
158          """Return an object that can be used to display the instance.""" 
159          return str(self.get_value()) 
 160   
161       
162       
163   
165          """Return value suitable for editor.""" 
166          return self.get_value() 
 167   
169          """Set value from editor value.""" 
170          return self.set_value(editorvalue) 
  171