entirely reworked cython wrapper
This commit is contained in:
@ -1,31 +1,44 @@
|
||||
#cython: language_level=3
|
||||
|
||||
from obitools3.utils cimport bytes2str, str2bytes
|
||||
from obitools3.obidms.obidmscolumn._obidmscolumn cimport OBIDMS_column
|
||||
from pathlib import Path
|
||||
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport OBIDMS_column
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport obiversion_t # TODO pourquoi je peux pas les declarer dans le pxd?
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport name_data_type
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport obi_column_get_data_type_from_name
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport obi_column_get_latest_version_from_name
|
||||
# from obitools3.obidms.obidmscolumn.capidmscolumn cimport obi_column_get_line_count_from_name
|
||||
#
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_int.capidmscolumn_int cimport OBIDMS_column_int_writable # TODO pourquoi pas cimport?
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_int.capidmscolumn_int cimport OBIDMS_column_int_read
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_float.capidmscolumn_float cimport OBIDMS_column_float_writable
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_float.capidmscolumn_float cimport OBIDMS_column_float_read
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_bool.capidmscolumn_bool cimport OBIDMS_column_bool_writable
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_bool.capidmscolumn_bool cimport OBIDMS_column_bool_read
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_char.capidmscolumn_char cimport OBIDMS_column_char_writable
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_char.capidmscolumn_char cimport OBIDMS_column_char_read
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_idx.capidmscolumn_idx cimport OBIDMS_column_idx_writable
|
||||
from obitools3.obidms.obidmscolumn.obidmscolumn_idx.capidmscolumn_idx cimport OBIDMS_column_idx_read
|
||||
from obitools3.utils cimport bytes2str, str2bytes
|
||||
|
||||
from .capi.obidms cimport obi_dms
|
||||
from .capi.obidmscolumn cimport obi_column_get_data_type_from_name, \
|
||||
obi_column_get_latest_version_from_name, \
|
||||
obi_column_get_line_count_from_name, \
|
||||
obi_column_get_nb_lines_used, \
|
||||
obi_column_get_elements_names, \
|
||||
obi_create_column, \
|
||||
obi_clone_column, \
|
||||
obi_open_column, \
|
||||
obi_close_column
|
||||
from .capi.obitypes cimport const_char_p, name_data_type
|
||||
|
||||
|
||||
from ._obidms cimport OBIDMS
|
||||
from ._obidms cimport OBIDMS_column
|
||||
|
||||
from ._obidmscolumn_int cimport OBIDMS_column_int, \
|
||||
OBIDMS_column_int_writable
|
||||
|
||||
from ._obidmscolumn_float cimport OBIDMS_column_float, \
|
||||
OBIDMS_column_float_writable
|
||||
|
||||
from ._obidmscolumn_bool cimport OBIDMS_column_bool, \
|
||||
OBIDMS_column_bool_writable
|
||||
|
||||
from ._obidmscolumn_char cimport OBIDMS_column_char, \
|
||||
OBIDMS_column_char_writable
|
||||
|
||||
from ._obidmscolumn_idx cimport OBIDMS_column_idx, \
|
||||
OBIDMS_column_idx_writable
|
||||
|
||||
|
||||
cdef class OBIDMS :
|
||||
|
||||
def __init__(self, str dms_name) : # TODO
|
||||
def __init__(self, str dms_name) :
|
||||
|
||||
# Declarations
|
||||
cdef bytes dms_name_b
|
||||
@ -61,7 +74,7 @@ cdef class OBIDMS :
|
||||
column_name = entry.stem
|
||||
column_name_b = str2bytes(column_name)
|
||||
dms[column_name] = {}
|
||||
data_type = bytes2str((name_data_type(obi_column_get_data_type_from_name(self.pointer, column_name_b)))
|
||||
data_type = bytes2str(name_data_type(obi_column_get_data_type_from_name(self.pointer, column_name_b)))
|
||||
latest_version = obi_column_get_latest_version_from_name(self.pointer, column_name_b)
|
||||
line_count = obi_column_get_line_count_from_name(self.pointer, column_name_b)
|
||||
dms[column_name]['data_type'] = data_type
|
||||
@ -71,18 +84,18 @@ cdef class OBIDMS :
|
||||
|
||||
return dms
|
||||
|
||||
cpdef OBIDMS_column open_column(self, # TODO j'arrive pas a le passer en cpdef
|
||||
str column_name, # TODO
|
||||
bint create=False,
|
||||
bint clone=False, bint clone_data=True,
|
||||
obiversion_t version_number=-1,
|
||||
OBIType_t data_type=0, # TODO
|
||||
size_t nb_lines=0,
|
||||
size_t nb_elements_per_line=1,
|
||||
str elements_names=None):
|
||||
cpdef OBIDMS_column open_column(self,
|
||||
str column_name,
|
||||
bint create=False,
|
||||
bint clone=False, bint clone_data=True,
|
||||
obiversion_t version_number=-1,
|
||||
OBIType_t data_type= <OBIType_t> 0,
|
||||
size_t nb_lines=0,
|
||||
size_t nb_elements_per_line=1,
|
||||
str elements_names=None):
|
||||
|
||||
# Declarations
|
||||
cdef OBIDMS_column column # TODO not sure object
|
||||
cdef OBIDMS_column column
|
||||
cdef bytes column_name_b
|
||||
|
||||
# Format the character string to send to C function
|
||||
@ -104,7 +117,7 @@ cdef class OBIDMS :
|
||||
nb_lines, nb_elements_per_line,
|
||||
elements_names)
|
||||
else :
|
||||
column = OBIDMS_column_int_read(self, column_name,
|
||||
column = OBIDMS_column_int(self, column_name,
|
||||
create, clone, clone_data,
|
||||
version_number, data_type,
|
||||
nb_lines, nb_elements_per_line,
|
||||
@ -118,7 +131,7 @@ cdef class OBIDMS :
|
||||
nb_lines, nb_elements_per_line,
|
||||
elements_names)
|
||||
else :
|
||||
column = OBIDMS_column_float_read(self, column_name,
|
||||
column = OBIDMS_column_float(self, column_name,
|
||||
create, clone, clone_data,
|
||||
version_number, data_type,
|
||||
nb_lines, nb_elements_per_line,
|
||||
@ -132,7 +145,7 @@ cdef class OBIDMS :
|
||||
nb_lines, nb_elements_per_line,
|
||||
elements_names)
|
||||
else :
|
||||
column = OBIDMS_column_bool_read(self, column_name,
|
||||
column = OBIDMS_column_bool(self, column_name,
|
||||
create, clone, clone_data,
|
||||
version_number, data_type,
|
||||
nb_lines, nb_elements_per_line,
|
||||
@ -146,13 +159,125 @@ cdef class OBIDMS :
|
||||
nb_lines, nb_elements_per_line,
|
||||
elements_names)
|
||||
else :
|
||||
column = OBIDMS_column_char_read(self, column_name,
|
||||
column = OBIDMS_column_char(self, column_name,
|
||||
create, clone, clone_data,
|
||||
version_number, data_type,
|
||||
nb_lines, nb_elements_per_line,
|
||||
elements_names)
|
||||
|
||||
|
||||
# elif data_type == 5 :
|
||||
# if (create or clone) :
|
||||
# column = OBIDMS_column_idx_writable(self, column_name,
|
||||
# create, clone, clone_data,
|
||||
# version_number, data_type,
|
||||
# nb_lines, nb_elements_per_line,
|
||||
# elements_names)
|
||||
# else :
|
||||
# column = OBIDMS_column_idx(self, column_name,
|
||||
# create, clone, clone_data,
|
||||
# version_number, data_type,
|
||||
# nb_lines, nb_elements_per_line,
|
||||
# elements_names)
|
||||
|
||||
else :
|
||||
raise Exception("Problem with the data type")
|
||||
|
||||
return column
|
||||
|
||||
|
||||
|
||||
cdef class OBIDMS_column :
|
||||
|
||||
# Should only be initialized through a subclass
|
||||
def __init__(self,
|
||||
OBIDMS dms,
|
||||
str column_name,
|
||||
bint create,
|
||||
bint clone, bint clone_data,
|
||||
obiversion_t version_number,
|
||||
OBIType_t type,
|
||||
size_t nb_lines,
|
||||
size_t nb_elements_per_line,
|
||||
str elements_names):
|
||||
|
||||
# Declarations
|
||||
cdef bytes column_name_b
|
||||
cdef bytes dms_name_b
|
||||
cdef bytes elements_names_b
|
||||
|
||||
# Fill structure
|
||||
self.dms = dms
|
||||
self.data_type = bytes2str(name_data_type(type))
|
||||
self.column_name = column_name
|
||||
|
||||
# Format the character strings to send them to C functions
|
||||
column_name_b = str2bytes(column_name)
|
||||
dms_name_b = str2bytes(self.dms.dms_name)
|
||||
|
||||
# Create, clone or open column
|
||||
if create :
|
||||
if elements_names == None :
|
||||
elements_names_b = column_name_b
|
||||
else :
|
||||
elements_names_b = str2bytes(elements_names)
|
||||
self.pointer = obi_create_column(self.dms.pointer, column_name_b, type, nb_lines, nb_elements_per_line, elements_names_b)
|
||||
else :
|
||||
if clone :
|
||||
self.pointer = obi_clone_column(self.dms.pointer, column_name_b, version_number, clone_data)
|
||||
else :
|
||||
self.pointer = obi_open_column(self.dms.pointer, column_name_b, version_number)
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
|
||||
# Declarations
|
||||
cdef list elements_names
|
||||
cdef str element_name
|
||||
cdef bint multiple_elements
|
||||
cdef object line # TODO
|
||||
cdef size_t lines_used
|
||||
cdef size_t line_nb
|
||||
|
||||
# Check if there are multiple elements per line anf if yes, get their names
|
||||
elements_names = self.get_elements_names()
|
||||
if len(elements_names) > 1 :
|
||||
multiple_elements = True
|
||||
else :
|
||||
element_name = elements_names[0]
|
||||
|
||||
# Yield each line
|
||||
lines_used = obi_column_get_nb_lines_used(self.pointer)
|
||||
for line_nb in xrange(lines_used):
|
||||
if multiple_elements :
|
||||
line = []
|
||||
for element_name in elements_names :
|
||||
line.append(self.get_item(line_nb, element_name))
|
||||
else :
|
||||
line = self.get_item(line_nb, element_name)
|
||||
yield line
|
||||
|
||||
|
||||
def __setitem__(self, size_t line_nb, object value):
|
||||
self.set_item(line_nb, "", value)
|
||||
|
||||
|
||||
def __getitem__(self, size_t line_nb):
|
||||
return self.get_item(line_nb, "")
|
||||
|
||||
cpdef object get_item(self, size_t line_nb, str element_name):
|
||||
raise NotImplementedError
|
||||
|
||||
cpdef list get_elements_names(self):
|
||||
cdef bytes elements_names
|
||||
elements_names = obi_column_get_elements_names(self.pointer)
|
||||
return (bytes2str(elements_names)).split(';')
|
||||
|
||||
|
||||
cpdef str get_data_type(self):
|
||||
return self.data_type
|
||||
|
||||
|
||||
cpdef size_t get_nb_lines_used(self):
|
||||
return obi_column_get_nb_lines_used(self.pointer)
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user