Commit 68b6ab32 authored by Celine Mercier's avatar Celine Mercier

working on the cython wrapper. This doesn't compile

parent b0570ee4
from obitools3.obidms.obidmscolumn.capidmscolumn cimport OBIDMS_column
from obitools3.obidms.capitypes cimport OBIType_t, obiversion_t
cdef extern from *:
ctypedef char* const_char_p "const char*"
......@@ -11,14 +13,22 @@ cdef extern from "obidms.h" nogil:
ctypedef OBIDMS_t* OBIDMS_p
OBIDMS_p obi_create_dms(const_char_p dms_name)
OBIDMS_p obi_open_dms(const_char_p dms_name)
OBIDMS_p obi_dms(const_char_p dms_name)
int obi_close_dms(OBIDMS_p dms)
from obitools3.obidms.obidmscolumn.capidmscolumn cimport *
cdef class OBIDMS:
cdef OBIDMS_p pointer
cdef str dms_name
\ No newline at end of file
cdef str dms_name
cpdef dict list(self)
cpdef OBIDMS_column open_column(self,
str column_name,
bint create=*,
bint clone=*, bint clone_data=*,
obiversion_t version_number=*,
OBIType_t data_type=*,
size_t nb_lines=*,
size_t nb_elements_per_line=*,
str elements_names=*)
#cython: language_level=3
from obitools3.utils cimport bytes2str, str2bytes
from pathlib import Path
from .capidms cimport *
from obitools3.obidms.obidmscolumn.obidmscolumn_int.capidmscolumn_int import *
from obitools3.obidms.obidmscolumn.obidmscolumn_float.capidmscolumn_float import *
from obitools3.obidms.obidmscolumn.obidmscolumn_bool.capidmscolumn_bool import *
from obitools3.obidms.obidmscolumn.obidmscolumn_char.capidmscolumn_char import *
from obitools3.obidms.obidmscolumn.obidmscolumn_idx.capidmscolumn_idx import *
# 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
cdef class OBIDMS :
def __init__(self, dms_name) :
dms_name_b = dms_name.encode(encoding='UTF-8')
self.dms_name = dms_name
self.pointer = obi_dms(dms_name_b)
def __init__(self, str dms_name) : # TODO
# Declarations
cdef bytes dms_name_b
# def __del__(self) :
# Format the character string to send to C function
dms_name_b = str2bytes(dms_name)
# Fill structure and create or open the DMS
self.dms_name = dms_name
self.pointer = obi_dms(<const_char_p> dms_name_b)
# def __del__(self) : # TODO problem with closing dir breaking everything
# obi_close_dms(self.pointer)
def list(self):
cpdef dict list(self):
# Declarations
cdef object p
cdef dict dms = {}
cdef str column_name
cdef bytes column_name_b
cdef str data_type
cdef obiversion_t latest_version
cdef size_t line_count
p = Path(self.dms_name+'.obidms')
#dms = {}
print("{:<25} {:<25} {:<25} {:<25}".format('-Column name-','-Data type-','-Latest version number-', '-Line count of latest version-'))
for entry in p.iterdir():
if entry.suffix == ".obicol":
column_name = entry.stem
column_name_b = column_name.encode('utf-8')
#dms[column_name] = {}
data_type = (name_data_type(obi_column_get_data_type_from_name(self.pointer, column_name_b))).decode('utf-8')
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)))
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
#dms[column_name]['latest_version'] = latest_version
#dms[column_name]['line_count'] = line_count
dms[column_name]['data_type'] = data_type
dms[column_name]['latest_version'] = latest_version
dms[column_name]['line_count'] = line_count
print("{:<25} {:<25} {:<25} {:<25}".format(column_name, data_type, latest_version, line_count))
return dms
def open_column(self,
column_name,
bint create=False,
bint clone=False, bint clone_data=True,
obiversion_t version_number=-1,
type=0,
size_t nb_lines=0,
size_t nb_elements_per_line=1,
str elements_names=None):
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):
# Declarations
cdef OBIDMS_column column # TODO not sure object
cdef bytes column_name_b
column_name_b = column_name.encode(encoding='UTF-8')
# Format the character string to send to C function
column_name_b = str2bytes(column_name)
if not type :
# Get the data type if not provided
if not data_type :
if create :
print("A data type must be specified")
raise Exception("A data type must be specified")
else :
type = obi_column_get_data_type_from_name(self.pointer, column_name_b)
data_type = obi_column_get_data_type_from_name(self.pointer, column_name_b)
if type == 1 :
# Open the column with the right subclass depending on the data type and the mode (read-only or writable)
if data_type == 1 :
if (create or clone) :
column = OBIDMS_column_int_writable(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
else :
column = OBIDMS_column_int_read(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
elif type == 2 :
elif data_type == 2 :
if (create or clone) :
column = OBIDMS_column_float_writable(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
else :
column = OBIDMS_column_float_read(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
elif type == 3 :
elif data_type == 3 :
if (create or clone) :
column = OBIDMS_column_bool_writable(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
else :
column = OBIDMS_column_bool_read(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
elif type == 4 :
elif data_type == 4 :
if (create or clone) :
column = OBIDMS_column_char_writable(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
else :
column = OBIDMS_column_char_read(self, column_name,
create, clone, clone_data,
version_number, type,
version_number, data_type,
nb_lines, nb_elements_per_line,
elements_names)
......
from obitools3.obidms.capidms cimport *
from libc.stdint cimport *
cdef extern from "obitypes.h" nogil:
enum OBIType:
pass
enum OBIBool:
pass
ctypedef OBIType OBIType_t
ctypedef OBIBool obibool_t
ctypedef int32_t obiint_t
ctypedef double obifloat_t
ctypedef char obichar_t
ctypedef size_t obiidx_t
char* name_data_type(int data_type)
from obitools3.obidms.capidms cimport OBIDMS_p
from obitools3.obidms.capidms cimport obi_errno
from obitools3.obidms.capidms cimport OBIDMS
cdef extern from "obidmscolumn.h" nogil:
......@@ -23,7 +8,6 @@ cdef extern from "obidmscolumn.h" nogil:
pass
ctypedef OBIDMS_column_t* OBIDMS_column_p
ctypedef int32_t obiversion_t
OBIDMS_column_p obi_create_column(OBIDMS_p dms, const char* column_name, OBIType_t type, size_t nb_lines, size_t nb_elements_per_line, const char* elements_names)
size_t obi_column_get_nb_lines_used(OBIDMS_column_p column)
......@@ -41,8 +25,12 @@ cdef extern from "obidmscolumn.h" nogil:
cdef class OBIDMS_column:
cpdef object get_item(self, line_nb, element_name)
cpdef get_elements_names(OBIDMS_column column)
cpdef get_data_type(OBIDMS_column column)
cpdef get_nb_lines_used(OBIDMS_column column)
cdef OBIDMS_column_p pointer
cdef OBIDMS dms
cdef str data_type # keep as OBIType_t? both?
cdef str data_type # TODO keep as OBIType_t? both?
cdef str dms_name
cdef str column_name
#cython: language_level=3
from .capidmscolumn cimport *
cdef class OBIDMS_column:
#Should only be initialized through a subclass
# Should only be initialized through a subclass
def __init__(self,
OBIDMS dms,
column_name,
object column_name, # TODO
bint create,
bint clone, bint clone_data,
obiversion_t version_number,
......@@ -16,11 +13,22 @@ cdef class OBIDMS_column:
size_t nb_lines,
size_t nb_elements_per_line,
str elements_names):
# Declarations
cpdef bytes column_name_b
cpdef bytes dms_name_b
cpdef bytes elements_names_b
# Fill structure
self.dms = dms
self.data_type = (name_data_type(type)).decode('UTF-8')
self.column_name = column_name
# Format the character strings to send them to C functions
column_name_b = column_name.encode(encoding='UTF-8')
dms_name_b = self.dms.dms_name.encode(encoding='UTF-8')
# Create, clone or open column
if create :
if elements_names == None :
elements_names_b = column_name_b
......@@ -35,11 +43,23 @@ cdef class OBIDMS_column:
def __iter__(self):
# Declarations
cpdef list elements_names
cpdef str element_name
cpdef bint multiple_elements
cpdef 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 :
......@@ -50,23 +70,29 @@ cdef class OBIDMS_column:
line = self.get_item(line_nb, element_name)
yield line
def __setitem__(self, int line_nb, value):
def __setitem__(self, size_t line_nb, object value):
self.set_item(line_nb, "", value)
def __getitem__(self, int line_nb):
def __getitem__(self, size_t line_nb):
return self.get_item(line_nb, "")
cpdef object get_item(self, line_nb, element_name):
raise NotImplementedError
def get_elements_names(self):
cpdef get_elements_names(self):
cpdef bytes elements_names
elements_names = obi_column_get_elements_names(self.pointer)
return (elements_names.decode('UTF-8')).split(';')
def get_data_type(self):
cpdef get_data_type(self):
return self.data_type
def get_nb_lines_used(self):
cpdef get_nb_lines_used(self):
return obi_column_get_nb_lines_used(self.pointer)
\ No newline at end of file
from obitools3.obidms.obidmscolumn.capidmscolumn cimport *
from obitools3.obidms.capitypes cimport obiint_t
from obitools3.obidms.obidmscolumn.capidmscolumn cimport OBIDMS_column_p
from obitools3.obidms.obidmscolumn.capidmscolumn cimport OBIDMS_column
cdef extern from "obidmscolumn_int.h" nogil:
int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, char* element_name, obiint_t value);
obiint_t obi_column_get_obiint_with_elt_name(OBIDMS_column_p column, size_t line_nb, char* element_name);
cdef class OBIDMS_column_int(OBIDMS_column):
#cpdef get_item(self, size_t line_nb, str element_name)
cdef class OBIDMS_column_int_read(OBIDMS_column_int):
# cpdef set_item(self, size_t line_nb, str element_name, obiint_t value)
# cpdef close(self)
cdef class OBIDMS_column_int_writable(OBIDMS_column_int):
# cpdef set_item(self, size_t line_nb, str element_name, obiint_t value)
# cpdef close(self)
\ No newline at end of file
#cython: language_level=3
from .capidmscolumn_int cimport *
from obitools3.obidms.obidmscolumn.capidmscolumn cimport obi_close_column
from obitools3.obidms.obidmscolumn.capidmscolumn cimport obi_truncate_and_close_column
from obitools3.obidms.capidms cimport obi_errno
from cpython.int cimport PyInt_FromLong
cdef class OBIDMS_column_int(OBIDMS_column) :
def get_item(self, line_nb, element_name):
def object get_item(self, size_t line_nb, str element_name):
cdef obiint_t value
value = obi_column_get_obiint_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'))
if obi_errno > 0 :
raise IndexError(line_nb, element_name)
return value
return PyInt_FromLong(value)
cdef class OBIDMS_column_int_read(OBIDMS_column_int) :
def set_item(self, line_nb, element_name, value):
def set_item(self, size_t line_nb, str element_name, obiint_t value):
raise Exception('Column is read-only')
def close(self):
......@@ -24,7 +28,7 @@ cdef class OBIDMS_column_int_read(OBIDMS_column_int) :
cdef class OBIDMS_column_int_writable(OBIDMS_column_int) :
def set_item(self, line_nb, element_name, value):
def set_item(self, size_t line_nb, str element_name, obiint_t value):
return obi_column_set_obiint_with_elt_name(self.pointer, line_nb, element_name.encode('utf-8'), value)
def close(self):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment