From c629f6b1b7454111b3d8b3a055905c1f0d88ca21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Rolda=CC=81n?= Date: Fri, 22 Jul 2016 13:03:48 +0200 Subject: [PATCH 1/7] Removed unnecessary repetition --- pypxlib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index c5b0fd9..0697ea2 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -110,7 +110,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_) From a5def86c4948db4476645bace27a736b481084d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Rolda=CC=81n?= Date: Fri, 22 Jul 2016 13:06:04 +0200 Subject: [PATCH 2/7] Added External BLOB files support. Now BytesFields are deserialised as non-binary strings. --- pypxlib/__init__.py | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index 0697ea2..ab552ac 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -2,6 +2,7 @@ from datetime import date, time from pypxlib.pxlib_ctypes import * +import os.path import atexit PX_boot() @@ -24,12 +25,26 @@ def next(self): # Python 3: __next__ = next - def __init__(self, file_path, encoding='cp850'): + def __init__(self, file_path, blob_file_path=None, encoding='cp850'): self.file_path = file_path + self.blob_file_path = blob_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) + + # BLOB Support + # http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file + if not self.blob_file_path: + # If not blob_file_path given, we try to acces automatically the related MB file + possible_blob_file = str(file_path).replace('.db', '.mb').replace('.DB', '.MB') + if os.path.isfile(possible_blob_file): + self.blob_file_path = blob_file_path + + # If given blob_file_path or file was automatically found we try to open it + if self.blob_file_path and PX_set_blob_file(self.pxdoc, blob_file_path.encode(self.PX_ENCODING)) < 0: + raise PXError('Could not open BLOB file %s.' % self.blob_file_path) + self._fields_cached = None def __enter__(self): return self @@ -180,7 +195,13 @@ def _serialize_to(self, value, pxval_value): class BytesField(Field): def _deserialize(self, pxval_value): - return pxval_value.str.val[:pxval_value.str.len] + try: + # Legacy support + return pxval_value.str.val[:pxval_value.str.len] + except: + # TODO - Change hardcoded enconding. Consider use the encoding parameter of Table __init__ + return pxval_value.str.val.data.decode('cp850') + def _serialize_to(self, value, pxval_value): pxval_value.str.val = value pxval_value.str.len = len(value) From 91e26339ac86b75ef27feb11aa655c6e25c10eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Rolda=CC=81n?= Date: Fri, 22 Jul 2016 13:52:44 +0200 Subject: [PATCH 3/7] Removed byte decoding of ByteFields and solved bugfixes importing files. --- pypxlib/__init__.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index ab552ac..c84f41f 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -35,14 +35,15 @@ def __init__(self, file_path, blob_file_path=None, encoding='cp850'): # BLOB Support # http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file - if not self.blob_file_path: + if not blob_file_path: # If not blob_file_path given, we try to acces automatically the related MB file possible_blob_file = str(file_path).replace('.db', '.mb').replace('.DB', '.MB') if os.path.isfile(possible_blob_file): + blob_file_path = possible_blob_file self.blob_file_path = blob_file_path # If given blob_file_path or file was automatically found we try to open it - if self.blob_file_path and PX_set_blob_file(self.pxdoc, blob_file_path.encode(self.PX_ENCODING)) < 0: + if blob_file_path and PX_set_blob_file(self.pxdoc, blob_file_path.encode(self.PX_ENCODING)) < 0: raise PXError('Could not open BLOB file %s.' % self.blob_file_path) self._fields_cached = None @@ -200,8 +201,7 @@ def _deserialize(self, pxval_value): return pxval_value.str.val[:pxval_value.str.len] except: # TODO - Change hardcoded enconding. Consider use the encoding parameter of Table __init__ - return pxval_value.str.val.data.decode('cp850') - + return pxval_value.str.val.data def _serialize_to(self, value, pxval_value): pxval_value.str.val = value pxval_value.str.len = len(value) From a0b2cfbf5d438fd4c228d8aec24e78e9795db6da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Rolda=CC=81n?= Date: Tue, 26 Jul 2016 13:16:58 +0200 Subject: [PATCH 4/7] Code improvements in order to support BLOB Files --- pypxlib/__init__.py | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index c84f41f..fcccf53 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -1,8 +1,8 @@ from collections import OrderedDict from datetime import date, time +from os.path import isfile from pypxlib.pxlib_ctypes import * -import os.path import atexit PX_boot() @@ -26,26 +26,20 @@ def next(self): __next__ = next def __init__(self, file_path, blob_file_path=None, encoding='cp850'): + # BLOB Support - http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file + 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.blob_file_path = blob_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) - - # BLOB Support - # http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file - if not blob_file_path: - # If not blob_file_path given, we try to acces automatically the related MB file - possible_blob_file = str(file_path).replace('.db', '.mb').replace('.DB', '.MB') - if os.path.isfile(possible_blob_file): - blob_file_path = possible_blob_file - self.blob_file_path = blob_file_path - - # If given blob_file_path or file was automatically found we try to open it - if blob_file_path and PX_set_blob_file(self.pxdoc, blob_file_path.encode(self.PX_ENCODING)) < 0: - raise PXError('Could not open BLOB file %s.' % self.blob_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 From 082ed7f8b941eb72929e48d5a2e09b7437567796 Mon Sep 17 00:00:00 2001 From: Michael Herrmann Date: Sat, 6 Aug 2016 08:58:35 +0200 Subject: [PATCH 5/7] Reorder parameters of Table constructor to keep it backwards-compatible. --- pypxlib/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index fcccf53..c3ac1e4 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -25,7 +25,7 @@ def next(self): # Python 3: __next__ = next - def __init__(self, file_path, blob_file_path=None, encoding='cp850'): + def __init__(self, file_path, encoding='cp850', blob_file_path=None): # BLOB Support - http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file if not blob_file_path: possible_blob_file = file_path.replace('.db', '.mb').replace('.DB', '.MB') From a75999cf43b809b460eb661c52198c05e5f6cf33 Mon Sep 17 00:00:00 2001 From: Michael Herrmann Date: Sat, 6 Aug 2016 09:00:28 +0200 Subject: [PATCH 6/7] Obey 80-char line limit. --- pypxlib/__init__.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index c3ac1e4..7d05257 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -26,9 +26,9 @@ def next(self): __next__ = next def __init__(self, file_path, encoding='cp850', blob_file_path=None): - # BLOB Support - http://pxlib.sourceforge.net/documentation.php?manpage=PX_set_blob_file if not blob_file_path: - possible_blob_file = file_path.replace('.db', '.mb').replace('.DB', '.MB') + 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 @@ -77,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 @@ -194,7 +195,6 @@ def _deserialize(self, pxval_value): # Legacy support return pxval_value.str.val[:pxval_value.str.len] except: - # TODO - Change hardcoded enconding. Consider use the encoding parameter of Table __init__ return pxval_value.str.val.data def _serialize_to(self, value, pxval_value): pxval_value.str.val = value @@ -258,7 +258,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): From 060ace4cf07bc798cf080ac0ea67c5faeaea011d Mon Sep 17 00:00:00 2001 From: Michael Herrmann Date: Sat, 6 Aug 2016 09:02:38 +0200 Subject: [PATCH 7/7] Remove legacy support which most likely wasn't needed. --- pypxlib/__init__.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/pypxlib/__init__.py b/pypxlib/__init__.py index 7d05257..c11204a 100644 --- a/pypxlib/__init__.py +++ b/pypxlib/__init__.py @@ -191,11 +191,7 @@ def _serialize_to(self, value, pxval_value): class BytesField(Field): def _deserialize(self, pxval_value): - try: - # Legacy support - return pxval_value.str.val[:pxval_value.str.len] - except: - return pxval_value.str.val.data + return pxval_value.str.val.data def _serialize_to(self, value, pxval_value): pxval_value.str.val = value pxval_value.str.len = len(value)