Commits

Anonymous committed 606bd45

Move code from GenericFieldSet to BasicFieldSet

Comments (0)

Files changed (2)

hachoir-core/hachoir_core/field/basic_field_set.py

 from hachoir_core.field import Field, FieldError
+from hachoir_core.stream import InputStream
+from hachoir_core.endian import BIG_ENDIAN, LITTLE_ENDIAN
 
 class ParserError(FieldError):
     """
     pass
 
 class BasicFieldSet(Field):
-    pass
+    is_field_set = True
+    endian = None
 
+    def __init__(self, parent, name, stream, description, size):
+        # Sanity checks (preconditions)
+        assert not parent or issubclass(parent.__class__, BasicFieldSet)
+        assert issubclass(stream.__class__, InputStream)
+
+        # Set field set size
+        if self.static_size:
+            assert isinstance(self.static_size, (int, long))
+            size = self.static_size
+
+        # Set Field attributes
+        self._parent = parent
+        self._name = name
+        self._size = size
+        self._description = description
+        self.stream = stream
+
+        # Set endian
+        if not self.endian:
+            assert parent and parent.endian
+            self.endian = parent.endian
+
+        if parent:
+            # This field set is one of the root leafs
+            self._address = parent._current_size
+            self.root = parent.root
+            assert id(self.stream) == id(parent.stream)
+        else:
+            # This field set is the root
+            self._address = 0
+            self.root = self
+
+        # Sanity checks (post-conditions)
+        assert self.endian in (BIG_ENDIAN, LITTLE_ENDIAN)
+        assert (self._size is None) or (0 < self._size)
+
+    def createFields(self):
+        raise NotImplementedError()
+    def __iter__(self):
+        raise NotImplementedError()
+    def __len__(self):
+        raise NotImplementedError()
+

hachoir-core/hachoir_core/field/generic_field_set.py

 from hachoir_core.event_handler import EventHandler
 from hachoir_core.dict import Dict, UniqKeyError
 from hachoir_core.endian import BIG_ENDIAN, LITTLE_ENDIAN
-from hachoir_core.stream import InputStream, InputStreamError
 from hachoir_core.error import error, warning, info, HACHOIR_ERRORS
 from hachoir_core.tools import lowerBound
 import hachoir_core.config as config
     - and maybe set endian and static_size class attributes.
     """
 
-    is_field_set = True
-    endian = None
     _event_handler = None
     _current_size = 0
 
         @param size: Size in bits. If it's None, size will be computed. You
             can also set size with class attribute static_size
         """
-
-        # Set field set size
-        if self.static_size is not None:
-            assert isinstance(self.static_size, (int, long))
-            size = self.static_size
-
-        # Make some tests on arguments
-        assert not parent or issubclass(parent.__class__, GenericFieldSet)
-        assert issubclass(stream.__class__, InputStream)
-        assert (size is None) or (0 < size)
-
-        # Call parent class constructor
-        self._parent = parent
-        self._name = name
-        self._size = size
-        self._description = description
-
-        # Set endian
-        if self.endian is None:
-            assert parent is not None and parent.endian is not None
-            self.endian = parent.endian
-        assert self.endian in (BIG_ENDIAN, LITTLE_ENDIAN)
-
+        BasicFieldSet.__init__(self, parent, name, stream, description, size)
         self._fields = Dict()
         self._field_generator = self.createFields()
         self._field_array_count = {}
         self._array_cache = {}
-        if parent:
-            # This field set is one of the root leafs
-            self._address = parent._current_size
-            self.root = parent.root
-            self.stream = parent.stream
-        else:
-            # This field set is the root
-            self._address = 0
-            self.root = self
-            assert issubclass(stream.__class__, InputStream)
-            self.stream = stream
+        if not parent:
             self._global_event_handler = None
 
     def array(self, key):
             if field:
                 yield field
 
-    def createFields(self):
-        """
-        DON'T CALL THIS FUNCTION DIRECTLY!
-        Use: __iter__() or __getitem__() to access fields.
-
-        This function have to be implemented in concrete field set.
-        """
-        raise NotImplementedError()
-
     def _isDone(self):
         return (self._field_generator is None)
     done = property(_isDone, doc="Boolean to know if parsing is done or not")
 
-
     #
     # FieldSet_SeekUtility
     #
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.