From 3ac6e85fb3562f9fe0efa99a0dd8313b9d8a1434 Mon Sep 17 00:00:00 2001 From: Eric Coissac Date: Mon, 26 Dec 2016 14:58:03 +0100 Subject: [PATCH] Big refactoring 4 --- .../dms/{_obidmscolumn.pxd => column.pxd} | 8 +- .../dms/{_obidmscolumn.pyx => column.pyx} | 17 ++- python/obitools3/dms/dms.pyx | 20 +++- python/obitools3/dms/view.pxd | 1 - python/obitools3/dms/view.pyx | 103 ++++-------------- 5 files changed, 51 insertions(+), 98 deletions(-) rename python/obitools3/dms/{_obidmscolumn.pxd => column.pxd} (88%) rename python/obitools3/dms/{_obidmscolumn.pyx => column.pyx} (92%) diff --git a/python/obitools3/dms/_obidmscolumn.pxd b/python/obitools3/dms/column.pxd similarity index 88% rename from python/obitools3/dms/_obidmscolumn.pxd rename to python/obitools3/dms/column.pxd index dc1be0d..94aa986 100644 --- a/python/obitools3/dms/_obidmscolumn.pxd +++ b/python/obitools3/dms/column.pxd @@ -15,9 +15,9 @@ from .capi.obitypes cimport obiversion_t, \ from .capi.obiutils cimport obi_format_date -from ._obiview cimport OBIView +from .view cimport View -cdef class OBIDMS_column: +cdef class Column: cdef bytes _alias # associated property: alias cdef OBIDMS_column_p* _pointer @@ -28,7 +28,7 @@ cdef class OBIDMS_column: @staticmethod cdef type get_subclass_type(OBIDMS_column_p column_p) -cdef class OBIDMS_column_line: +cdef class Column_line: cdef OBIDMS_column _column cdef OBIDMS_column_p _column_p @@ -38,4 +38,4 @@ cdef class OBIDMS_column_line: cpdef update(self, data) -cdef register_column_class(OBIType_t obitype,type classe) +cdef register_column_class(OBIType_t obitype,type classe, type python) diff --git a/python/obitools3/dms/_obidmscolumn.pyx b/python/obitools3/dms/column.pyx similarity index 92% rename from python/obitools3/dms/_obidmscolumn.pyx rename to python/obitools3/dms/column.pyx index 1d17d00..e021d8f 100644 --- a/python/obitools3/dms/_obidmscolumn.pyx +++ b/python/obitools3/dms/column.pyx @@ -1,12 +1,13 @@ #cython: language_level=3 -cdef dict __OBIDMS_COLUMN_CLASS__ = {} +from .dms cimport __OBIDMS_COLUMN_CLASS__ + cdef class Column : """ - The OBIDMS.Column class + The obitools3.dms.column.Column class wraps a c instance of a column in the context of a View """ - + # Note: should only be initialized through a subclass def __init__(self, View view, @@ -172,11 +173,15 @@ cdef class OBIDMS_column_line : ###################################################################################################### -cdef register_column_class(OBIType_t obitype,type classe): +cdef register_column_class(OBIType_t obitype, + type classe, + type python): """ Each sub class of `OBIDMS_column` needs to be registered after its declaration to declare its relationship with an `OBIType_t` """ - assert issubclass(classe,OBIDMS_column) + global __OBIDMS_COLUMN_CLASS__ + + assert issubclass(classe,Column) - __OBIDMS_COLUMN_CLASS__[obitype]=classe + __OBIDMS_COLUMN_CLASS__[obitype]=(classe,python) diff --git a/python/obitools3/dms/dms.pyx b/python/obitools3/dms/dms.pyx index a592560..347e5f8 100644 --- a/python/obitools3/dms/dms.pyx +++ b/python/obitools3/dms/dms.pyx @@ -36,13 +36,27 @@ from obitools3.utils cimport bytes2str, \ from .taxonomy cimport Taxonomy - - - +__OBIDMS_COLUMN_CLASS__ = {} cdef class DMS : + @staticmethod + cdef type get_column_class(obitype_t obitype): + """ + Internal function returning the python class representing + a column for a given obitype. + """ + return __OBIDMS_COLUMN_CLASS__[obitype][0] + + @staticmethod + cdef type get_python_type(obitype_t obitype): + """ + Internal function returning the python type representing + an instance for a given obitype. + """ + return __OBIDMS_COLUMN_CLASS__[obitype][1] + def __init__(self, object dms_name) : ''' Constructor of a obitools3.dms.DMS instance. diff --git a/python/obitools3/dms/view.pxd b/python/obitools3/dms/view.pxd index b7a9d72..e0c09ba 100644 --- a/python/obitools3/dms/view.pxd +++ b/python/obitools3/dms/view.pxd @@ -32,7 +32,6 @@ from obitools3.utils cimport tobytes, \ from .dms cimport DMS - cdef class View: cdef DMS _dms diff --git a/python/obitools3/dms/view.pyx b/python/obitools3/dms/view.pyx index 68edf70..ca74570 100644 --- a/python/obitools3/dms/view.pyx +++ b/python/obitools3/dms/view.pyx @@ -2,7 +2,8 @@ cdef class View : - + + def __init__(self,dms,int __internalCall__): if __internalCall__!=987654: @@ -116,97 +117,31 @@ cdef class View : return s - cpdef delete_column(self, column_name) : + cpdef delete_column(self, + object column_name) : if obi_view_delete_column(self._pointer, tobytes(column_name)) < 0 : raise Exception("Problem deleting column %s from a view", tostr(column_name)) # Update the dictionary of column objects: - (self._columns).pop(column_name) self.update_column_pointers() - cpdef add_column(self, - str column_name, - obiversion_t version_number=-1, - object alias=None, - object type=b'', - index_t nb_lines=0, - index_t nb_elements_per_line=1, - list elements_names=None, - object indexer_name=b"", - object associated_column_name=b"", - obiversion_t associated_column_version=-1, - object comments=b"", - bint create=True - ) : - - cdef bytes column_name_b - cdef bytes elements_names_b - cdef bytes alias_b - cdef object subclass - cdef OBIDMS_column_p column_p - - column_name_b = tobytes(column_name) - if alias is None : - alias_b = column_name_b - else : - alias_b = tobytes(alias) - - if elements_names is None : - elements_names_b = b"" - else : - elements_names_b = b';'.join([tobytes(i) for i in elements_names]) - - if type : # TODO make C function that does that - if type == 'OBI_INT' : - data_type = OBI_INT - elif type == 'OBI_FLOAT' : - data_type = OBI_FLOAT - elif type == 'OBI_BOOL' : - data_type = OBI_BOOL - elif type == 'OBI_CHAR' : - data_type = OBI_CHAR - elif type == 'OBI_QUAL' : - data_type = OBI_QUAL - elif type == 'OBI_STR' : - data_type = OBI_STR - elif type == 'OBI_SEQ' : - data_type = OBI_SEQ - else : - raise Exception("Invalid provided data type") - - if (obi_view_add_column(self._pointer, column_name_b, version_number, alias_b, - data_type, nb_lines, nb_elements_per_line, - elements_names_b, str2bytes(indexer_name), - str2bytes(associated_column_name), associated_column_version, - str2bytes(comments), create) < 0) : - raise Exception("Problem adding a column in a view") - - # Get the column pointer - column_p = obi_view_get_column(self._pointer, alias_b) - - # Open and store the subclass - subclass = OBIDMS_column.get_subclass_type(column_p) - (self._columns)[alias] = subclass(self, alias) - - cpdef change_column_alias(self, bytes current_alias, bytes new_alias): + cpdef rename_column(self, + object current_name, + object new_name): + cdef OBIDMS_column column - if (obi_view_create_column_alias(self._pointer, current_alias, new_alias) < 0) : - raise Exception("Problem changing a column alias") - # Update the dictionaries of column objects - self._columns[new_alias] = self._columns[current_alias] - column = self._columns[new_alias] - column._alias = new_alias - (self._columns).pop(current_alias) - - - cdef update_column_pointers(self): - cdef str column_n - cdef OBIDMS_column column - for column_n in self._columns : - column = self._columns[column_n] - column._pointer = obi_view_get_pointer_on_column_in_view(self._pointer, str2bytes(column_n)) - + cdef bytes current_name_b = tobytes(current_name) + cdef bytes new_name_b = tobyes(new_name) + + if (obi_view_create_column_alias(self._pointer, + tobytes(current_name_b), + tobytes(new_name_b)) < 0) : + raise Exception("Problem in renaming column %s to %s" % ( + bytes2str(current_name_b), + bytes2str(new_name_b))) + + cpdef OBIView_line_selection new_selection(self,list lines=None): return OBIView_line_selection(self,lines)