diff options
author | René 'Necoro' Neumann <necoro@necoro.net> | 2009-08-14 15:54:22 +0200 |
---|---|---|
committer | René 'Necoro' Neumann <necoro@necoro.net> | 2009-08-14 15:54:22 +0200 |
commit | 0e6702e069aeabcd995b8968d0ebbc1593d59bf6 (patch) | |
tree | c41cd3af8bdb1a3bc0b20d95d7cf9c55934a5ab2 /portato/eix/parser.py | |
parent | 1e88fe3bee51cd04928499f8ec29e00cf129ad33 (diff) | |
download | portato-0e6702e069aeabcd995b8968d0ebbc1593d59bf6.tar.gz portato-0e6702e069aeabcd995b8968d0ebbc1593d59bf6.tar.bz2 portato-0e6702e069aeabcd995b8968d0ebbc1593d59bf6.zip |
Add header-object
Diffstat (limited to 'portato/eix/parser.py')
-rw-r--r-- | portato/eix/parser.py | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/portato/eix/parser.py b/portato/eix/parser.py index 676cdd5..90c569b 100644 --- a/portato/eix/parser.py +++ b/portato/eix/parser.py @@ -15,7 +15,8 @@ from __future__ import absolute_import, with_statement import os import struct -from .helper import debug +from ..helper import debug +from functools import partial from . import exceptions as ex @@ -30,7 +31,7 @@ def _get_bytes (file, length, expect_list = False): else: return struct.unpack("%sB" % length, s) -def number (file): +def number (file, skip = False): n = _get_bytes(file, 1) if n < 0xFF: @@ -49,10 +50,16 @@ def number (file): value = n << (count*8) if count > 0: - rest = _get_bytes(file, count, expect_list = True) - for i, r in enumerate(rest): - value += r << ((count - i - 1)*8) + if skip: + file.seek(count, os.SEEK_CUR) + return + + else: + rest = _get_bytes(file, count, expect_list = True) + + for i, r in enumerate(rest): + value += r << ((count - i - 1)*8) return value @@ -65,11 +72,15 @@ def vector (file, get_type, skip = False): else: return [get_type(file) for i in range(nelems)] +def typed_vector(type): + return partial(vector, get_type = type) + def string (file, skip = False): nelems = number(file) if skip: file.seek(nelems, os.SEEK_CUR) + return else: s = file.read(nelems) @@ -92,17 +103,32 @@ class LazyElement (object): self._value = None self.pos = file.tell() - get_type(skip=True) # skip it for the moment + get_type(file, skip=True) # skip it for the moment @property def value (self): if self._value is None: old_pos = self.file.tell() - self.file.seek(self.pos) - self._value = self.get_type(skip = False) - self.file.seek(old_pos) + self.file.seek(self.pos, os.SEEK_SET) + self._value = self.get_type(self.file, skip = False) + self.file.seek(old_pos, os.SEEK_SET) return self._value def __call__ (self): return self.value + +class header (object): + def __init__ (self, file): + def LE (t): + return LazyElement(t, file) + + self.version = LE(number) + self.ncats = LE(number) + self.overlays = LE(typed_vector(overlay)) + self.provide = LE(typed_vector(string)) + self.licenses = LE(typed_vector(string)) + self.keywords = LE(typed_vector(string)) + self.useflags = LE(typed_vector(string)) + self.slots = LE(typed_vector(string)) + self.sets = LE(typed_vector(string)) |