Add the OBIObject concept
This commit is contained in:
@ -190,19 +190,26 @@ cdef class OBIDMS_column_line_bool(OBIDMS_column_line) :
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
cdef class OBIDMS_column_bool(OBIDMS_column):
|
cdef class Column_bool(Column):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
cdef OBIDMS_column _new(OBIView view,
|
def new(OBIView view,
|
||||||
bytes column_name,
|
object column_name,
|
||||||
index_t nb_elements_per_line=1,
|
index_t nb_elements_per_line=1,
|
||||||
object elements_names=None,
|
object elements_names=None,
|
||||||
bytes comments=b""):
|
object comments=b""):
|
||||||
|
|
||||||
|
cdef bytes column_name_b = tobytes(column_name)
|
||||||
|
cdef bytes comments_b
|
||||||
cdef bytes elements_names_b
|
cdef bytes elements_names_b
|
||||||
cdef char* elements_names_p
|
cdef char* elements_names_p
|
||||||
cdef OBIDMS_column new_column
|
cdef OBIDMS_column new_column
|
||||||
|
|
||||||
|
if comments is not None:
|
||||||
|
comments_b = tobytes(comments)
|
||||||
|
else:
|
||||||
|
comments_b = b''
|
||||||
|
|
||||||
if elements_names is not None:
|
if elements_names is not None:
|
||||||
elements_names_b = b''.join([tobytes(x) for x in elements_names])
|
elements_names_b = b''.join([tobytes(x) for x in elements_names])
|
||||||
elements_names_p = elements_names_b
|
elements_names_p = elements_names_b
|
||||||
@ -210,7 +217,7 @@ cdef class OBIDMS_column_bool(OBIDMS_column):
|
|||||||
elements_names_p = NULL
|
elements_names_p = NULL
|
||||||
|
|
||||||
if (obi_view_add_column(view = view._pointer,
|
if (obi_view_add_column(view = view._pointer,
|
||||||
column_name = column_name,
|
column_name = column_name_b,
|
||||||
version_number = -1,
|
version_number = -1,
|
||||||
alias = NULL,
|
alias = NULL,
|
||||||
data_type = OBI_BOOL,
|
data_type = OBI_BOOL,
|
||||||
@ -220,7 +227,7 @@ cdef class OBIDMS_column_bool(OBIDMS_column):
|
|||||||
indexer_name = NULL,
|
indexer_name = NULL,
|
||||||
associated_column_name = NULL,
|
associated_column_name = NULL,
|
||||||
associated_column_version = -1,
|
associated_column_version = -1,
|
||||||
comments = comments,
|
comments = comments_b,
|
||||||
create = True)<0):
|
create = True)<0):
|
||||||
raise RuntimeError("Cannot create column %s in view %s" % (bytes2str(column_name),
|
raise RuntimeError("Cannot create column %s in view %s" % (bytes2str(column_name),
|
||||||
bytes2str(view.name)))
|
bytes2str(view.name)))
|
||||||
@ -229,24 +236,11 @@ cdef class OBIDMS_column_bool(OBIDMS_column):
|
|||||||
new_column = self._columns[column_name]
|
new_column = self._columns[column_name]
|
||||||
|
|
||||||
return new_column
|
return new_column
|
||||||
|
|
||||||
@staticmethod
|
def add_to_view(self,
|
||||||
def new(OBIView view,
|
OBIView view,
|
||||||
object column_name,
|
object column_name=None,
|
||||||
index_t nb_elements_per_line=1,
|
object comments=b""):
|
||||||
object elements_names=None,
|
|
||||||
object comments=b""):
|
|
||||||
|
|
||||||
return OBIDMS_column_bool._new(view,
|
|
||||||
tobytes(column_name),
|
|
||||||
nb_elements_per_line,
|
|
||||||
elements_names,
|
|
||||||
tobytes(comments))
|
|
||||||
|
|
||||||
cpdef add_to_view(self,
|
|
||||||
OBIView view,
|
|
||||||
object column_name=None,
|
|
||||||
object comments=b""):
|
|
||||||
|
|
||||||
cdef OBIDMS_column_p column_p = self._column_pp[0]
|
cdef OBIDMS_column_p column_p = self._column_pp[0]
|
||||||
cdef bytes alias
|
cdef bytes alias
|
||||||
@ -256,19 +250,25 @@ cdef class OBIDMS_column_bool(OBIDMS_column):
|
|||||||
else:
|
else:
|
||||||
alias = tobytes(column_name)
|
alias = tobytes(column_name)
|
||||||
|
|
||||||
obi_view_add_column(view = view._ponter,
|
if (obi_view_add_column(view = view._ponter,
|
||||||
column_name = column_p.header.name,
|
column_name = column_p.header.name,
|
||||||
version_number = column_p.header.version,
|
version_number = column_p.header.version,
|
||||||
alias = alias,
|
alias = alias,
|
||||||
data_type = OBI_BOOL,
|
data_type = OBI_BOOL,
|
||||||
nb_lines = column_p.header.lines_used,
|
nb_lines = column_p.header.lines_used,
|
||||||
nb_elements_per_line = column_p.header.nb_elements_per_line,
|
nb_elements_per_line = column_p.header.nb_elements_per_line,
|
||||||
elements_names = column_p.header.elements_names,
|
elements_names = column_p.header.elements_names,
|
||||||
indexer_name = NULL,
|
indexer_name = NULL,
|
||||||
associated_column_name = NULL,
|
associated_column_name = NULL,
|
||||||
associated_column_version = NULL,
|
associated_column_version = NULL,
|
||||||
comments = tobytes(comments),
|
comments = tobytes(comments),
|
||||||
create = False)
|
create = False) < 0):
|
||||||
|
raise RuntimeError("Cannot insert column %s (%s@%d) into view %s" %
|
||||||
|
( bytes2str(alias),
|
||||||
|
bytes2str(column_p.header.name),
|
||||||
|
column_p.header.version,
|
||||||
|
bytes2str(view.name)
|
||||||
|
))
|
||||||
|
|
||||||
cpdef object get_line(self, index_t line_nb):
|
cpdef object get_line(self, index_t line_nb):
|
||||||
cdef obibool_t value
|
cdef obibool_t value
|
||||||
@ -291,4 +291,4 @@ cdef class OBIDMS_column_bool(OBIDMS_column):
|
|||||||
raise Exception("Problem setting a value in a column")
|
raise Exception("Problem setting a value in a column")
|
||||||
|
|
||||||
|
|
||||||
register_column_class(OBI_BOOL,OBIDMS_column_bool)
|
register_column_class(OBI_BOOL,Column_bool,bool)
|
@ -4,11 +4,13 @@ from .capi.obidms cimport OBIDMS_p
|
|||||||
from .capi.obitypes cimport obiversion_t, \
|
from .capi.obitypes cimport obiversion_t, \
|
||||||
obitype_t, \
|
obitype_t, \
|
||||||
index_t
|
index_t
|
||||||
|
|
||||||
|
from .object cimport OBIObject
|
||||||
|
|
||||||
cdef dict __OBIDMS_COLUMN_CLASS__
|
cdef dict __OBIDMS_COLUMN_CLASS__
|
||||||
cdef dict __OBIDMS_VIEW_CLASS__
|
cdef dict __OBIDMS_VIEW_CLASS__
|
||||||
|
|
||||||
cdef class DMS:
|
cdef class DMS(OBIObject):
|
||||||
|
|
||||||
cdef OBIDMS_p _pointer
|
cdef OBIDMS_p _pointer
|
||||||
|
|
||||||
|
@ -33,7 +33,7 @@ from pathlib import Path
|
|||||||
__OBIDMS_COLUMN_CLASS__ = {}
|
__OBIDMS_COLUMN_CLASS__ = {}
|
||||||
__OBIDMS_VIEW_CLASS__= {}
|
__OBIDMS_VIEW_CLASS__= {}
|
||||||
|
|
||||||
cdef class DMS :
|
cdef class DMS(OBIObject):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
cdef type get_column_class(obitype_t obitype):
|
cdef type get_column_class(obitype_t obitype):
|
||||||
@ -64,6 +64,8 @@ cdef class DMS :
|
|||||||
@type dms_name: a `str` or a `bytes` instance
|
@type dms_name: a `str` or a `bytes` instance
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
OBIObject.__init__(self)
|
||||||
|
|
||||||
# Declarations
|
# Declarations
|
||||||
cdef bytes dms_name_b = tobytes(dms_name)
|
cdef bytes dms_name_b = tobytes(dms_name)
|
||||||
|
|
||||||
@ -95,16 +97,19 @@ cdef class DMS :
|
|||||||
"""
|
"""
|
||||||
return <bytes> self._pointer.dms_name
|
return <bytes> self._pointer.dms_name
|
||||||
|
|
||||||
cpdef close(self) :
|
def close(self) :
|
||||||
"""
|
"""
|
||||||
Closes the DNS instance and free the associated memory
|
Closes the DNS instance and free the associated memory
|
||||||
|
|
||||||
the `close` method is automatically called by the object destructor.
|
the `close` method is automatically called by the object destructor.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if (obi_close_dms(self._pointer)) < 0 :
|
if self._pointer!=NULL:
|
||||||
raise Exception("Problem closing an OBIDMS")
|
if (obi_close_dms(self._pointer)) < 0 :
|
||||||
|
raise Exception("Problem closing an OBIDMS")
|
||||||
|
else:
|
||||||
|
raise OBIObjectClosedInstance()
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
cdef const_char_p path = obi_dms_get_full_path(self._pointer,
|
cdef const_char_p path = obi_dms_get_full_path(self._pointer,
|
||||||
b"VIEWS"
|
b"VIEWS"
|
||||||
|
10
python/obitools3/dms/object.pxd
Normal file
10
python/obitools3/dms/object.pxd
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
cdef class OBIObject:
|
||||||
|
cdef dict _dependent_object
|
||||||
|
|
||||||
|
cpdef register(self, OBIObject object)
|
||||||
|
cpdef close(self)
|
||||||
|
|
||||||
|
cdef class OBIObjectClosedInstance(Exception):
|
||||||
|
pass
|
28
python/obitools3/dms/object.pyx
Normal file
28
python/obitools3/dms/object.pyx
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#cython: language_level=3
|
||||||
|
|
||||||
|
cdef class OBIObject:
|
||||||
|
|
||||||
|
cpdef register(self, OBIObject object):
|
||||||
|
self._dependent_object[id(object)]=object
|
||||||
|
|
||||||
|
cpdef close(self):
|
||||||
|
cdef OBIObject object
|
||||||
|
|
||||||
|
for object in self._dependent_object.values():
|
||||||
|
object.close()
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._dependent_object={}
|
||||||
|
|
||||||
|
def __dealloc__(self):
|
||||||
|
"""
|
||||||
|
Destructor of the DMS instance.
|
||||||
|
|
||||||
|
The destructor automatically call the `close` method and
|
||||||
|
therefore free all the associated memory.
|
||||||
|
"""
|
||||||
|
|
||||||
|
self.close()
|
||||||
|
|
||||||
|
cdef class OBIObjectClosedInstance(Exception):
|
||||||
|
pass
|
Reference in New Issue
Block a user