Big refactoring 4

This commit is contained in:
2016-12-26 14:58:03 +01:00
parent 5156f6bb9e
commit 3ac6e85fb3
5 changed files with 51 additions and 98 deletions

View File

@ -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)

View File

@ -1,10 +1,11 @@
#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
@ -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__
__OBIDMS_COLUMN_CLASS__[obitype]=classe
assert issubclass(classe,Column)
__OBIDMS_COLUMN_CLASS__[obitype]=(classe,python)

View File

@ -37,12 +37,26 @@ 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.

View File

@ -32,7 +32,6 @@ from obitools3.utils cimport tobytes, \
from .dms cimport DMS
cdef class View:
cdef DMS _dms

View File

@ -3,6 +3,7 @@
cdef class View :
def __init__(self,dms,int __internalCall__):
if __internalCall__!=987654:
@ -116,96 +117,30 @@ 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
cpdef rename_column(self,
object current_name,
object new_name):
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):
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 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)))
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 = <OBIDMS_column_p*> obi_view_get_pointer_on_column_in_view(self._pointer, str2bytes(column_n))
cpdef OBIView_line_selection new_selection(self,list lines=None):
return OBIView_line_selection(self,lines)