new Cython DMS API

This commit is contained in:
Celine Mercier
2017-04-06 14:41:26 +02:00
parent 0dfb1eb3e6
commit b5b7995411
3 changed files with 65 additions and 98 deletions

View File

@ -9,6 +9,8 @@ cdef extern from "obidms.h" nogil:
ctypedef OBIDMS_t* OBIDMS_p
OBIDMS_p obi_dms(const_char_p dms_name)
OBIDMS_p obi_open_dms(const char* dms_path)
OBIDMS_p obi_create_dms(const char* dms_path)
int obi_close_dms(OBIDMS_p dms)
char* obi_dms_get_dms_path(OBIDMS_p dms)
char* obi_dms_get_full_path(OBIDMS_p dms, const_char_p path_name)

View File

@ -1,25 +1,11 @@
#cython: language_level=3
from .capi.obidms cimport OBIDMS_p
from .capi.obitypes cimport obiversion_t, \
obitype_t, \
index_t
from .object cimport OBIWrapper
cdef dict __OBIDMS_COLUMN_CLASS__
cdef dict __OBIDMS_VIEW_CLASS__
from .capi.obidms cimport OBIDMS_p
cdef class DMS(OBIWrapper):
@staticmethod
cdef type get_python_type(obitype_t obitype)
@staticmethod
cdef DMS new(object dms_name)
cpdef close(self)
cdef inline OBIDMS_p pointer(self)
cpdef int view_count(self)
cpdef bint is_view_writable(self, object view_name)

View File

@ -1,9 +1,11 @@
#cython: language_level=3
from libc.stdlib cimport free
from cpython.list cimport PyList_Size
from .capi.obidms cimport obi_dms, \
from .capi.obidms cimport obi_open_dms, \
obi_create_dms, \
obi_close_dms, \
obi_dms_get_full_path
@ -18,83 +20,70 @@ from .object cimport OBIObjectClosedInstance
from pathlib import Path
__OBIDMS_COLUMN_CLASS__ = {}
from .view import view
cdef class DMS(OBIWrapper):
@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, size_t pointer) :
# '''
# Constructor of a obitools3.dms.DMS instance.
#
# @param dms_name: The name of the DMS
# @type dms_name: a `str` or a `bytes` instance
# '''
#
# print("hmmmm")
cdef inline OBIDMS_p pointer(self):
return <OBIDMS_p>(self._pointer)
@staticmethod
cdef DMS new(object dms_name) :
def new(object dms_name) :
cdef OBIDMS_p pointer
cdef DMS dms
cdef bytes dms_name_b = tobytes(dms_name)
pointer = obi_dms(<const_char_p> dms_name_b)
pointer = obi_create_dms(<const_char_p> dms_name_b)
if pointer == NULL :
raise Exception("Failed opening or creating an OBIDMS")
dms = OBIWrapper.new_wrapper(DMS, pointer)
raise Exception("Failed creating an OBIDMS")
dms = OBIWrapper.new(DMS, pointer)
return dms
@staticmethod
def open(object dms_name) :
cdef OBIDMS_p pointer
cdef DMS dms
cdef bytes dms_name_b = tobytes(dms_name)
pointer = obi_open_dms(<const_char_p> dms_name_b)
if pointer == NULL :
raise Exception("Failed opening an OBIDMS")
dms = OBIWrapper.new(DMS, pointer)
return dms
def close(self) :
'''
Closes the DMS instance and free the associated memory
The `close` method is automatically called by the object destructor.
'''
cdef OBIDMS_p pointer = self.pointer()
if self.active() :
OBIWrapper.close(self)
if (obi_close_dms(pointer)) < 0 :
raise Exception("Problem closing an OBIDMS")
# name property getter
@property
def name(self) :
"""
'''
Returns the name of the DMS instance
@rtype: bytes
"""
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
return <bytes> pointer.dms_name
def close(self) :
"""
Closes the DMS instance and free the associated memory
The `close` method is automatically called by the object destructor.
"""
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
if pointer!=NULL:
OBIWrapper.close(self)
if (obi_close_dms(pointer)) < 0 :
raise Exception("Problem closing an OBIDMS")
self._pointer=NULL
else:
raise OBIObjectClosedInstance()
'''
return <bytes> self.pointer().dms_name
def keys(self) :
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
cdef const_char_p path = obi_dms_get_full_path(pointer,
b"VIEWS"
)
cdef const_char_p path = obi_dms_get_full_path(self.pointer(), b"VIEWS")
if path == NULL:
raise RuntimeError("Cannot retreive the Dataabase path")
raise RuntimeError("Cannot retrieve the view database path")
p = Path(bytes2str(path))
@ -106,27 +95,21 @@ cdef class DMS(OBIWrapper):
def values(self) :
cdef bytes view_name
for view_name in self.keys():
yield self.get_view(view_name)
def items(self) :
cdef bytes view_name
for view_name in self.keys():
yield (view_name, self.get_view(view_name))
def __contains__(self, key) :
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
cdef str key_s = tostr(key)
cdef const_char_p path = obi_dms_get_full_path(pointer,
b"VIEWS"
)
cdef const_char_p path = obi_dms_get_full_path(self.pointer(), b"VIEWS")
p = Path(bytes2str(path),key_s)
free(path)
@ -138,10 +121,6 @@ cdef class DMS(OBIWrapper):
return PyList_Size(list(self.keys()))
def get_view(self):
raise NotImplemented
def __len__(self) :
return self.view_count()
@ -154,7 +133,7 @@ cdef class DMS(OBIWrapper):
return self.keys()
cpdef bint is_view_writable(self, object view_name):
raise NotImplemented
def get_view(self, object view_name) :
return view.View.open(self, view_name)