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 ctypedef OBIDMS_t* OBIDMS_p
OBIDMS_p obi_dms(const_char_p dms_name) 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) int obi_close_dms(OBIDMS_p dms)
char* obi_dms_get_dms_path(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) char* obi_dms_get_full_path(OBIDMS_p dms, const_char_p path_name)

View File

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

View File

@ -1,14 +1,16 @@
#cython: language_level=3 #cython: language_level=3
from libc.stdlib cimport free from libc.stdlib cimport free
from cpython.list cimport PyList_Size 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_close_dms, \
obi_dms_get_full_path obi_dms_get_full_path
from .capi.obitypes cimport const_char_p from .capi.obitypes cimport const_char_p
from obitools3.utils cimport bytes2str, \ from obitools3.utils cimport bytes2str, \
str2bytes, \ str2bytes, \
tobytes, \ tobytes, \
@ -18,115 +20,96 @@ from .object cimport OBIObjectClosedInstance
from pathlib import Path from pathlib import Path
from .view import view
__OBIDMS_COLUMN_CLASS__ = {}
cdef class DMS(OBIWrapper): cdef class DMS(OBIWrapper):
@staticmethod cdef inline OBIDMS_p pointer(self):
cdef type get_python_type(obitype_t obitype): return <OBIDMS_p>(self._pointer)
"""
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")
@staticmethod @staticmethod
cdef DMS new(object dms_name) : def new(object dms_name) :
cdef OBIDMS_p pointer cdef OBIDMS_p pointer
cdef DMS dms cdef DMS dms
cdef bytes dms_name_b = tobytes(dms_name) 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 : if pointer == NULL :
raise Exception("Failed opening or creating an OBIDMS") raise Exception("Failed creating an OBIDMS")
dms = OBIWrapper.new_wrapper(DMS, pointer) dms = OBIWrapper.new(DMS, pointer)
return dms return dms
# name property getter @staticmethod
@property def open(object dms_name) :
def name(self): cdef OBIDMS_p pointer
""" cdef DMS dms
Returns the name of the DMS instance cdef bytes dms_name_b = tobytes(dms_name)
pointer = obi_open_dms(<const_char_p> dms_name_b)
@rtype: bytes if pointer == NULL :
""" raise Exception("Failed opening an OBIDMS")
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer) dms = OBIWrapper.new(DMS, pointer)
return <bytes> pointer.dms_name return dms
def close(self) : def close(self) :
""" '''
Closes the DMS instance and free the associated memory Closes the DMS 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.
""" '''
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer) cdef OBIDMS_p pointer = self.pointer()
if pointer!=NULL: if self.active() :
OBIWrapper.close(self) OBIWrapper.close(self)
if (obi_close_dms(pointer)) < 0 : if (obi_close_dms(pointer)) < 0 :
raise Exception("Problem closing an OBIDMS") raise Exception("Problem closing an OBIDMS")
self._pointer=NULL
else:
raise OBIObjectClosedInstance()
def keys(self):
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
cdef const_char_p path = obi_dms_get_full_path(pointer,
b"VIEWS" # name property getter
) @property
def name(self) :
'''
Returns the name of the DMS instance
if path==NULL: @rtype: bytes
raise RuntimeError("Cannot retreive the Dataabase path") '''
return <bytes> self.pointer().dms_name
def keys(self) :
cdef const_char_p path = obi_dms_get_full_path(self.pointer(), b"VIEWS")
if path == NULL:
raise RuntimeError("Cannot retrieve the view database path")
p = Path(bytes2str(path)) p = Path(bytes2str(path))
free(path) free(path)
for v in p.glob("*.obiview"): for v in p.glob("*.obiview") :
yield str2bytes(v.stem) yield str2bytes(v.stem)
def values(self): def values(self) :
cdef bytes view_name cdef bytes view_name
for view_name in self.keys(): for view_name in self.keys():
yield self.get_view(view_name) yield self.get_view(view_name)
def items(self): def items(self) :
cdef bytes view_name cdef bytes view_name
for view_name in self.keys(): for view_name in self.keys():
yield (view_name,self.get_view(view_name)) yield (view_name, self.get_view(view_name))
def __contains__(self, key): def __contains__(self, key) :
cdef OBIDMS_p pointer = <OBIDMS_p>(self._pointer)
cdef str key_s = tostr(key) cdef str key_s = tostr(key)
cdef const_char_p path = obi_dms_get_full_path(pointer, cdef const_char_p path = obi_dms_get_full_path(self.pointer(), b"VIEWS")
b"VIEWS"
)
p = Path(bytes2str(path),key_s) p = Path(bytes2str(path),key_s)
free(path) free(path)
@ -134,15 +117,11 @@ cdef class DMS(OBIWrapper):
return p.with_suffix(".obiview").is_file() return p.with_suffix(".obiview").is_file()
cpdef int view_count(self): cpdef int view_count(self) :
return PyList_Size(list(self.keys())) return PyList_Size(list(self.keys()))
def get_view(self): def __len__(self) :
raise NotImplemented
def __len__(self):
return self.view_count() return self.view_count()
@ -150,11 +129,11 @@ cdef class DMS(OBIWrapper):
return self.get_view(view_name) return self.get_view(view_name)
def __iter__(self): def __iter__(self) :
return self.keys() return self.keys()
cpdef bint is_view_writable(self, object view_name): def get_view(self, object view_name) :
raise NotImplemented return view.View.open(self, view_name)