From fc04a66869a53790c4940adc0c1d0fc51c086ef0 Mon Sep 17 00:00:00 2001 From: Binh Nguyen Date: Sat, 1 Feb 2014 12:52:44 -0600 Subject: [PATCH] Fixed debugging output --- pyaccuwage/__init__.py | 1 + pyaccuwage/fields.py | 45 +++++++++++++++++++++++++++++++++++++----- pyaccuwage/model.py | 14 ++++++++----- pyaccuwage/reader.py | 23 +++++++++++++++++++++ 4 files changed, 73 insertions(+), 10 deletions(-) create mode 100644 pyaccuwage/reader.py diff --git a/pyaccuwage/__init__.py b/pyaccuwage/__init__.py index 6ea4ccc..a869047 100644 --- a/pyaccuwage/__init__.py +++ b/pyaccuwage/__init__.py @@ -1,4 +1,5 @@ from record import * +from reader import RecordReader VERSION = (0, 2012, 0) diff --git a/pyaccuwage/fields.py b/pyaccuwage/fields.py index 655e464..4ec42f1 100644 --- a/pyaccuwage/fields.py +++ b/pyaccuwage/fields.py @@ -17,13 +17,14 @@ class ValidationError(Exception): class Field(object): creation_counter = 0 - def __init__(self, name=None, max_length=0, required=True, uppercase=True): + def __init__(self, name=None, max_length=0, required=True, uppercase=True, creation_counter=None): self.name = name self._value = None + self._orig_value = None self.max_length = max_length self.required = required self.uppercase = uppercase - self.creation_counter = Field.creation_counter + self.creation_counter = creation_counter or Field.creation_counter Field.creation_counter += 1 def validate(self): @@ -38,13 +39,17 @@ class Field(object): def __getvalue(self): return self._value + def __len__(self): + return self.max_length + value = property(__getvalue, __setvalue) def read(self, fp): - if fp.tell() + self.max_length <= fp.len: - data = fp.read(self.max_length) + data = fp.read(self.max_length) + if len(data) == self.max_length: + self._orig_value = data return self.parse(data) - return None + raise ValueError() def parse(self, s): self.value = s.strip() @@ -77,6 +82,36 @@ class Field(object): self.value = o['value'] return self + def debug(self, counter): + import textwrap + + value = (self._orig_value or str(self.value)) + + wrapper = textwrap.TextWrapper(replace_whitespace=False, drop_whitespace=False) + wrapper.width = 100 + value = wrapper.wrap(value) + #value = textwrap.wrap(value, 100) + #print value + value = list(map(lambda x:(" " * 9) + ('"' + x + '"'), value)) + #value[0] = '"' + value[0] + '"' + value.append(" " * 10 + ('_' * 10) * (wrapper.width / 10)) + value.append(" " * 10 + ('0123456789') * (wrapper.width / 10)) + value.append(" " * 10 + ''.join((map(lambda x:str(x) + (' ' * 9), range(wrapper.width / 10 ))))) + #value.append((" " * 59) + map(lambda x:("%x" % x), range(16)) + + start = counter['c'] + counter['c'] += len(self._orig_value or self.value) + end = counter['c'] + + return ( + str(start) + '-' + str(end-1) + + ' [' + + str(len(self._orig_value or '') or len(str(self.value))) + + '] ' + self.name + + '\n' + + '\n'.join(value) + ) + class TextField(Field): def validate(self): diff --git a/pyaccuwage/model.py b/pyaccuwage/model.py index 2cc4704..d628711 100644 --- a/pyaccuwage/model.py +++ b/pyaccuwage/model.py @@ -1,4 +1,4 @@ -from fields import Field, ValidationError +from fields import Field, TextField, ValidationError import copy import pdb @@ -29,7 +29,10 @@ class Model(object): self.__dict__[key] = value def get_fields(self): - fields = [] + identifier = TextField("record_identifier", max_length=len(self.record_identifier), creation_counter=-1) + identifier.value = self.record_identifier + fields = [identifier] + for key in self.__class__.__dict__.keys(): attr = getattr(self, key) if isinstance(attr, Field): @@ -53,8 +56,8 @@ class Model(object): custom_validator(f) def output(self): - result = ''.join([self.record_identifier] + - [field.get_data() for field in self.get_sorted_fields()]) + result = ''.join([field.get_data() for field in self.get_sorted_fields()]) + if hasattr(self, 'record_length') and len(result) != self.record_length: raise ValidationError("Record result length not equal to %d bytes (%d)" % (self.record_length, len(result))) #result = ''.join([self.record_identifier] + [field.get_data() for field in self.get_sorted_fields()]) @@ -63,7 +66,8 @@ class Model(object): return result def read(self, fp): - for field in self.get_sorted_fields(): + # Skip the first record, since that's an identifier + for field in self.get_sorted_fields()[1:]: field.read(fp) diff --git a/pyaccuwage/reader.py b/pyaccuwage/reader.py new file mode 100644 index 0000000..ee957a0 --- /dev/null +++ b/pyaccuwage/reader.py @@ -0,0 +1,23 @@ +class RecordReader(object): + def __init__(self, record_classes, identifier_field='record_identifier'): + self._ident_field = identifier_field + self._ident_len = 0 + self._types = {} + + for rc in record_classes: + identifier = getattr(rc, identifier_field) + self._types[identifier] = rc + print (identifier) + self._ident_len = max(self._ident_len, len(identifier)) + + def load(self, fd): + + while 1: + record_ident = fd.read(self._ident_len) + if not record_ident: + break + record = self._types[record_ident]() + record.read(fd) + yield record + +