Commit daacd0df by Eric Coissac

Strong refactoring 1

parent 3cedd00d
......@@ -10,7 +10,13 @@ from .capi.obidmscolumn cimport obi_close_column, \
OBIDMS_column_header_p
from .capi.obiutils cimport obi_format_date
from .capi.obiview cimport Obiview_p, \
Obiview_infos_p, \
Alias_column_pair_p, \
obi_view_map_file, \
obi_view_unmap_file
from .capi.obitypes cimport const_char_p, \
OBIType_t, \
OBI_INT, \
......@@ -23,11 +29,6 @@ from .capi.obitypes cimport const_char_p, \
name_data_type, \
only_ATGC # discuss
from ._obidms cimport OBIDMS, \
OBIDMS_column, \
OBIView, \
OBIView_line
from ._obitaxo cimport OBI_Taxonomy
......
#cython: language_level=3
from .capi.obidmscolumn cimport OBIDMS_column_p, \
OBIDMS_column_header_p, \
obi_close_column, \
obi_column_prepare_to_get_value
from .capi.obiview cimport Obiview_p, \
obi_view_get_pointer_on_column_in_view
from .capi.obitypes cimport obiversion_t, \
OBIType_t, \
index_t, \
name_data_type
from .capi.obiutils cimport obi_format_date
from ._obiview cimport OBIView
cdef class OBIDMS_column:
cdef bytes _alias # associated property: alias
cdef OBIDMS_column_p* _pointer
cdef OBIView _view
cpdef close(self)
@staticmethod
cdef type get_subclass_type(OBIDMS_column_p column_p)
cdef class OBIDMS_column_line:
cdef OBIDMS_column _column
cdef OBIDMS_column_p _column_p
cdef OBIDMS_column_p* _column_pp
cdef index_t _index
cdef int _len
cpdef update(self, data)
cdef register_column_class(OBIType_t obitype,type classe)
#cython: language_level=3
cdef dict __OBIDMS_COLUMN_CLASS__ = {}
cdef class Column :
"""
The OBIDMS.Column class
"""
# Note: should only be initialized through a subclass
def __init__(self,
View view,
object name):
'''
Create a new OBDMS column objected referring to a already created column
in the context of a view.
This constructor is normally only called by subclass constructor.
@param view: The view object containing the column.
@type view: OBIView
@param name: The name of the column in the view
@type name: a `str` or a `bytes`
'''
cdef OBIDMS_column_p* column_pp
# Check that the class is only created as a subclass instance
if type(self)==Column or not isinstance(self, Column):
raise RuntimeError('OBIDMS.Column constructor cannot be called directly')
column_pp = obi_view_get_pointer_on_column_in_view(view._pointer,
tobytes(column_alias))
# Fill structure
self._alias = column_alias
self._pointer = column_pp
self._view = view
def __len__(self):
'''
implements the len() function for the Column class
@rtype: `int`
'''
return self.lines_used
def __sizeof__(self):
'''
returns the size of the C object wrapped by the Column instance
'''
cdef OBIDMS_column_header_p header = self._pointer[0].header
return header.header_size + header.data_size
def __iter__(self):
cdef index_t line_nb
for line_nb in range(self.lines_used):
yield self[line_nb]
def __str__(self) :
cdef str to_print
to_print = ''
for line in self :
to_print = to_print + str(line) + "\n"
return to_print
def __repr__(self) :
return b"%s, original name: %s, version %d, data type: %d" % (
self._alias,
self.original_name,
self.version,
self.data_type
)
cpdef close(self):
if obi_close_column((self._pointer)[0]) < 0 :
raise Exception("Problem closing a column")
# Column alias property getter and setter
@property
def alias(self):
return self._alias
@alias.setter
def alias(self, new_alias): # @DuplicatedSignature
self._view.change_column_alias(self._alias, new_alias)
# elements_names property getter
@property
def elements_names(self):
return (((self._pointer)[0].header).elements_names).split(b';')
# nb_elements_per_line property getter
@property
def nb_elements_per_line(self):
return ((self._pointer)[0].header).nb_elements_per_line
# data_type property getter
@property
def data_type(self):
return name_data_type(((self._pointer)[0].header).returned_data_type)
# original_name property getter
@property
def original_name(self):
return ((self._pointer)[0].header).name
# version property getter
@property
def version(self):
return ((self._pointer)[0].header).version
# lines_used property getter
@property
def lines_used(self):
return (self._pointer)[0].header.lines_used
# comments property getter
@property
def comments(self):
return (self._pointer)[0].header.comments
# creation_date property getter
@property
def creation_date(self):
return obi_format_date((self._pointer)[0].header.creation_date)
@staticmethod
cdef type get_subclass_type(OBIDMS_column_p column_p) :
cdef type subclass
cdef OBIType_t col_type
col_type = column_p.header.returned_data_type
subclass = __OBIDMS_COLUMN_CLASS__[col_type]
return subclass
######################################################################################################
cdef class OBIDMS_column_line :
def __init__(self, OBIDMS_column column, index_t line_nb) :
self._index = line_nb
self._column = column
self._column_pp = column._pointer
self._column_p = NULL
self._len = self._column_pp[0].header.nb_elements_per_line
if obi_column_prepare_to_get_value(self._column_pp[0],line_nb) < 0:
raise IndexError("Cannot access to the line %d" % line_nb)
def __contains__(self, str element_name):
return (element_name in self._column.elements_names)
def __repr__(self) :
return str(self._column.get_line(self._index))
cpdef update(self, data):
if isinstance(data, dict):
data=data.items()
for key,value in data:
if key in self:
self[key]=value
######################################################################################################
cdef register_column_class(OBIType_t obitype,type classe):
"""
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)
__OBIDMS_COLUMN_CLASS__[obitype]=classe
#cython: language_level=3
from .capi.obiview cimport obi_get_bool_with_elt_name_and_col_p_in_view, \
obi_get_bool_with_elt_idx_and_col_p_in_view, \
obi_set_bool_with_elt_name_and_col_p_in_view, \
obi_set_bool_with_elt_idx_and_col_p_in_view
from cpython.bool cimport bool, PyBool_FromLong
from .capi.obitypes cimport index_t, \
const_char_p, \
OBIType_t, \
obibool_t, \
OBI_BOOL, \
OBIBool_NA
from .capi.obierrno cimport obi_errno
from ._obidmscolumn cimport OBIDMS_column, \
OBIDMS_column_line, \
OBIDMS_column_p, \
register_column_class
from .capi.obidmscolumn cimport obi_column_get_obibool_with_elt_name, \
obi_column_get_obibool_with_elt_idx, \
obi_column_set_obibool_with_elt_name, \
obi_column_set_obibool_with_elt_idx
from obitools3.utils cimport str2bytes, bytes2str
cdef class OBIDMS_column_line_bool(OBIDMS_column_line) :
cdef update_pointer(self)
@staticmethod
cdef bool obibool_t2bool(obibool_t value)
@staticmethod
cdef bool2obibool_t(bool value)
cpdef bool get_bool_item_by_name(self,bytes element_name)
cpdef bool get_bool_item_by_idx(self,index_t index)
cpdef set_bool_item_by_name(self,bytes element_name,bool value)
cpdef set_bool_item_by_idx(self,index_t index,bool value)
# cdef obibool_t [:] _data_view
cdef class OBIDMS_column_bool(OBIDMS_column):
cdef OBIDMS_column _new(OBIView view,
bytes column_name,
index_t nb_elements_per_line=1,
object elements_names=None,
bytes comments=b""):
cpdef object get_line(self, index_t line_nb)
cpdef set_line(self, index_t line_nb, object value)
#cython: language_level=3
from ._obidms cimport OBIView_line
from ._obiview cimport OBIView_line
cdef class OBI_Seq(dict) :
......
#cython: language_level=3
from .capi.obitypes cimport obiversion_t, \
index_t, \
only_ATGC
from .capi.obiview cimport Obiview_p, \
Obiview_infos_p, \
Alias_column_pair_p, \
......@@ -21,45 +25,55 @@ from .capi.obiview cimport Obiview_p, \
ID_COLUMN, \
DEFINITION_COLUMN, \
QUALITY_COLUMN
from ._obidmscolumn cimport OBIDMS_column_p, \
OBIDMS_column
from ._obidms cimport OBIDMS
from obitools3.utils cimport tobytes, \
bytes2str, \
tostr
cdef class OBIView:
cdef OBIDMS _dms
cdef Obiview_p _pointer
cdef dict _columns
cdef __init_columns__(self):
cdef __init_columns__(self)
cpdef OBIView clone(self,
str view_name,
OBIView_line_selection line_selection=*,
str comments=*)
view_name,
comments=*)
@staticmethod
cpdef OBIView new(OBIDMS dms,
str view_name,
str comments=*)
cdef OBIView _new(OBIDMS dms,
bytes view_name,
bytes comments=*)
@staticmethod
cpdef OBIView open(OBIDMS dms,
str view_name)
cdef OBIView _open(OBIDMS dms,
bytes view_name)
cpdef delete_column(self, str column_name)
cpdef delete_column(self, column_name)
cpdef add_column(self,
str column_name,
obiversion_t version_number=*,
str alias=*,
str type=*,
object alias=*,
object type=*,
index_t nb_lines=*,
index_t nb_elements_per_line=*,
list elements_names=*,
str indexer_name=*,
str associated_column_name=*,
object indexer_name=*,
object associated_column_name=*,
obiversion_t associated_column_version=*,
str comments=*,
object comments=*,
bint create=*
)
cpdef change_column_alias(self, str current_alias, str new_alias)
cpdef update_column_pointers(self)
)
cpdef change_column_alias(self, bytes current_alias, bytes new_alias)
cdef update_column_pointers(self)
cpdef OBIView_line_selection new_selection(self,
list lines=*)
......
......@@ -3,84 +3,100 @@
cdef class OBIView :
def __init__(self,int __internalCall__):
def __init__(self,dms,int __internalCall__):
if __internalCall__!=987654:
raise RuntimeError('OBIView constructor cannot be called directly')
self._dms = dms
self._pointer = NULL
self._columns = {}
cdef __init_columns__(self):
cdef size_t i
cdef str col_alias
cdef bytes col_alias
cdef OBIDMS_column_p column_p
cdef Obiview_p pointer = self._pointer
self._columns = {}
for i in range(pointer.infos.column_count) :
col_alias = bytes2str(pointer.infos.column_references[i].alias)
col_alias = pointer.infos.column_references[i].alias
column_p = <OBIDMS_column_p> (pointer.columns)[i]
subclass = OBIDMS_column.get_subclass_type(column_p)
self._columns[col_alias] = subclass(self, col_alias)
cpdef OBIView clone(self,
str view_name,
str comments=""):
view_name,
comments=b""):
cdef OBIView view = OBIView(987654)
cdef OBIView view = OBIView(self._dms,
987654)
view._pointer = obi_new_view(self._pointer.dms,
str2bytes(view_name),
tobytes(view_name),
self._pointer,
NULL,
str2bytes(comments))
tobytes(comments))
if view._pointer == NULL :
raise RuntimeError("Error : Cannot clone view %s into view %s"
% (str(self.name),
view_name)
tobytes(view_name))
)
view.__init_columns__(self)
view.__init_columns__()
return view
@staticmethod
cpdef OBIView new(OBIDMS dms,
str view_name,
str comments=""):
cdef OBIView _new(OBIDMS dms,
bytes view_name,
bytes comments=b""):
cdef OBIView view = OBIView(987654) # @DuplicatedSignature
cdef OBIView view = OBIView(dms,
987654) # @DuplicatedSignature
view._pointer = obi_new_view(dms._pointer,
str2bytes(view_name),
view_name,
NULL,
NULL,
str2bytes(comments))
comments)
if view._pointer == NULL :
raise RuntimeError("Error : Cannot create view %s" % view_name)
view.__init_columns__(self)
view.__init_columns__()
return view
@staticmethod
def new(OBIDMS dms,
view_name,
comments=b""):
return OBIView._new(dms,tobytes(view_name),tobytes(comments))
@staticmethod
cpdef OBIView open(OBIDMS dms,
str view_name):
cdef OBIView _open(OBIDMS dms,
bytes view_name):
cdef OBIView view = OBIView(987654) # @DuplicatedSignature
cdef OBIView view = OBIView(dms,
987654) # @DuplicatedSignature
view._pointer = obi_open_view(dms._pointer,
str2bytes(view_name))
view_name)
if view._pointer == NULL :
raise RuntimeError("Error : Cannot open view %s" % view_name)
raise RuntimeError("Error : Cannot open view %s" % bytes2str(view_name))
view.__init_columns__(self)
view.__init_columns__()
return view
@staticmethod
def open(OBIDMS dms, # @ReservedAssignment
view_name):
return OBIView._open(dms,tobytes(view_name))
def __dealloc__(self):
if (obi_save_and_close_view(self._pointer) < 0) :
......@@ -95,46 +111,45 @@ cdef class OBIView :
return s
cpdef delete_column(self, str column_name) :
cdef str column_n
if obi_view_delete_column(self._pointer, str2bytes(column_name)) < 0 :
raise Exception("Problem deleting a column from a view")
cpdef delete_column(self, 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,
str alias='',
str type='',
object alias=None,
object type=b'',
index_t nb_lines=0,
index_t nb_elements_per_line=1,
list elements_names=None,
str indexer_name="",
str associated_column_name="",
object indexer_name=b"",
object associated_column_name=b"",
obiversion_t associated_column_version=-1,
str comments="",
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 = str2bytes(column_name)
if alias == '' :
alias = column_name
column_name_b = tobytes(column_name)
if alias is None :
alias_b = column_name_b
else :
alias_b = str2bytes(alias)
alias_b = tobytes(alias)
if elements_names is None :
elements_names_b = str2bytes("")
elements_names_b = b""
else :
elements_names_b = str2bytes(';'.join(elements_names))
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' :
......@@ -169,9 +184,9 @@ cdef class OBIView :
(self._columns)[alias] = subclass(self, alias)
cpdef change_column_alias(self, str current_alias, str new_alias):
cpdef change_column_alias(self, bytes current_alias, bytes new_alias):
cdef OBIDMS_column column
if (obi_view_create_column_alias(self._pointer, str2bytes(current_alias), str2bytes(new_alias)) < 0) :
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]
......@@ -180,7 +195,7 @@ cdef class OBIView :
(self._columns).pop(current_alias)
cpdef update_column_pointers(self):
cdef update_column_pointers(self):
cdef str column_n
cdef OBIDMS_column column
for column_n in self._columns :
......@@ -227,6 +242,10 @@ cdef class OBIView :
return to_print
@property
def dms(self):
return self._dms
# line_count property getter
@property
def line_count(self):
......@@ -317,7 +336,7 @@ cdef class OBIView_line_selection(list):
view_name)
)
view.__init_columns__(self)
view.__init_columns__()
return view
......
......@@ -62,9 +62,7 @@ cdef extern from "obidmscolumn.h" nogil:
OBIDMS_column_p obi_open_column(OBIDMS_p dms,
const_char_p column_name,
obiversion_t version_number)
int obi_close_column(OBIDMS_column_p column)
OBIDMS_column_p obi_clone_column(OBIDMS_p dms,
OBIDMS_column_p line_selection,
const_char_p column_name,
......@@ -83,6 +81,8 @@ cdef extern from "obidmscolumn.h" nogil:
int obi_close_header(OBIDMS_column_header_p header)
int obi_select(OBIDMS_column_p line_selection_column, index_t line_to_grep)
int obi_column_prepare_to_get_value(OBIDMS_column_p column, index_t line_nb)
cdef extern from "obidmscolumn_int.h" nogil:
......
#cython: language_level=3
cdef extern from "obierrno.h":
int obi_errno
#cython: language_level=3
from .capi.obidmscolumn cimport OBIDMS_column_p
from .capi.obiview cimport Obiview_p
from .capi.obitypes cimport obiversion_t, OBIType_t, index_t
from ._obidmscolumn_int cimport OBIDMS_column_int, \
OBIDMS_column_multi_elts_int
from ._obidmscolumn_float cimport OBIDMS_column_float, \
OBIDMS_column_multi_elts_float