Skip to content

Commit

Permalink
Merge branch 'denisroldan-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
mherrmann committed Aug 6, 2016
2 parents c6f3d87 + 060ace4 commit 5467dec
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions pypxlib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict
from datetime import date, time
from os.path import isfile
from pypxlib.pxlib_ctypes import *

import atexit
Expand All @@ -24,12 +25,21 @@ def next(self):
# Python 3:
__next__ = next

def __init__(self, file_path, encoding='cp850'):
def __init__(self, file_path, encoding='cp850', blob_file_path=None):
if not blob_file_path:
possible_blob_file = \
file_path.replace('.db', '.mb').replace('.DB', '.MB')
if isfile(possible_blob_file):
blob_file_path = possible_blob_file
self.file_path = file_path
self.encoding = encoding
self.pxdoc = PX_new()
if PX_open_file(self.pxdoc, file_path.encode(self.PX_ENCODING)) != 0:
raise PXError('Could not open file %s.' % self.file_path)
if blob_file_path:
blob_file_path_enc = blob_file_path.encode(self.PX_ENCODING)
if PX_set_blob_file(self.pxdoc, blob_file_path_enc) < 0:
raise PXError('Could not open BLOB file %s.' % blob_file_path)
self._fields_cached = None
def __enter__(self):
return self
Expand Down Expand Up @@ -67,7 +77,8 @@ def __delitem__(self, rownum):
self._check_rownum(rownum)
if PX_delete_record(self.pxdoc, rownum) == -1:
raise PXError(
'Could not delete row %d of file %s.' % (rownum, self.file_path)
'Could not delete row %d of file %s.' %
(rownum, self.file_path)
)
def __len__(self):
return self.pxdoc.contents.px_head.contents.px_numrecords
Expand Down Expand Up @@ -110,7 +121,7 @@ def from_type(cls, type_, index, encoding):
return DoubleField(index, type_)
if type_ == pxfLogical:
return LogicalField(index, type_)
if type_ in (pxfBLOb, pxfMemoBLOb, pxfMemoBLOb, pxfBCD, pxfBytes):
if type_ in (pxfBLOb, pxfMemoBLOb, pxfBCD, pxfBytes):
return BytesField(index, type_)
if type_ in (pxfOLE, pxfGraphic):
return BytesField(index, type_)
Expand Down Expand Up @@ -180,7 +191,7 @@ def _serialize_to(self, value, pxval_value):

class BytesField(Field):
def _deserialize(self, pxval_value):
return pxval_value.str.val[:pxval_value.str.len]
return pxval_value.str.val.data
def _serialize_to(self, value, pxval_value):
pxval_value.str.val = value
pxval_value.str.len = len(value)
Expand Down Expand Up @@ -243,7 +254,8 @@ def __setattr__(self, item, value):
else:
self[item] = value
def save(self):
result = PX_update_record(self._table.pxdoc, self._pxvals, self._rownum)
result = \
PX_update_record(self._table.pxdoc, self._pxvals, self._rownum)
if result == -1:
raise PXError('Could not update record.')
def __repr__(self):
Expand Down

0 comments on commit 5467dec

Please sign in to comment.