165 lines
4.7 KiB
Cython
165 lines
4.7 KiB
Cython
#cython: language_level=3
|
|
|
|
from libc.stdlib cimport malloc,free
|
|
from cpython.list cimport PyList_Size
|
|
|
|
from .capi.obidms cimport obi_dms, \
|
|
obi_close_dms, \
|
|
obi_dms_get_full_path
|
|
|
|
from .capi.obidmscolumn cimport obi_close_column, \
|
|
OBIDMS_column_p, \
|
|
OBIDMS_column_header_p
|
|
|
|
from .capi.obiutils cimport obi_format_date
|
|
|
|
from .capi.obiview cimport Obiview_p, \
|
|
Obiview_infos_p, \
|
|
Alias_column_pair_p, \
|
|
obi_view_map_file, \
|
|
obi_view_unmap_file
|
|
|
|
from .capi.obitypes cimport const_char_p, \
|
|
name_data_type, \
|
|
only_ATGC # discuss
|
|
|
|
from obitools3.utils cimport bytes2str, \
|
|
str2bytes, \
|
|
tobytes, \
|
|
tostr
|
|
|
|
from pathlib import Path
|
|
|
|
__OBIDMS_COLUMN_CLASS__ = {}
|
|
__OBIDMS_VIEW_CLASS__= {}
|
|
|
|
cdef class DMS :
|
|
|
|
@staticmethod
|
|
cdef type get_column_class(obitype_t obitype):
|
|
"""
|
|
Internal function returning the python class representing
|
|
a column for a given obitype.
|
|
"""
|
|
return __OBIDMS_COLUMN_CLASS__[obitype][0]
|
|
|
|
@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]
|
|
|
|
@staticmethod
|
|
cdef type get_view_class(bytes viewtype):
|
|
return __OBIDMS_VIEW_CLASS__[viewtype]
|
|
|
|
|
|
def __init__(self, object dms_name) :
|
|
'''
|
|
Constructor of a obitools3.dms.DMS instance.
|
|
|
|
@param dms_name: The name of the DMS
|
|
@type dms_name: a `str` or a `bytes` instance
|
|
'''
|
|
|
|
# Declarations
|
|
cdef bytes dms_name_b = tobytes(dms_name)
|
|
|
|
# Fill structure and create or open the DMS
|
|
self._pointer = obi_dms(<const_char_p> dms_name_b)
|
|
|
|
if self._pointer == NULL :
|
|
raise Exception("Failed opening or creating an OBIDMS")
|
|
|
|
|
|
def __dealloc__(self):
|
|
"""
|
|
Destructor of the DMS instance.
|
|
|
|
The destructor automatically call the `close` method and
|
|
therefore free all the associated memory.
|
|
"""
|
|
|
|
self.close()
|
|
self._pointer=NULL
|
|
|
|
# name property getter
|
|
@property
|
|
def name(self):
|
|
"""
|
|
Returns the name of the DMS instance
|
|
|
|
@rtype: bytes
|
|
"""
|
|
return <bytes> self._pointer.dms_name
|
|
|
|
cpdef close(self) :
|
|
"""
|
|
Closes the DNS instance and free the associated memory
|
|
|
|
the `close` method is automatically called by the object destructor.
|
|
"""
|
|
|
|
if (obi_close_dms(self._pointer)) < 0 :
|
|
raise Exception("Problem closing an OBIDMS")
|
|
|
|
def keys(self):
|
|
cdef const_char_p path = obi_dms_get_full_path(self._pointer,
|
|
b"VIEWS"
|
|
)
|
|
|
|
if path==NULL:
|
|
raise RuntimeError("Cannot retreive the Dataabase path")
|
|
|
|
p = Path(bytes2str(path))
|
|
|
|
free(path)
|
|
|
|
for v in p.glob("*.obiview"):
|
|
yield str2bytes(v.stem)
|
|
|
|
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 str key_s = tostr(key)
|
|
cdef const_char_p path = obi_dms_get_full_path(self._pointer,
|
|
b"VIEWS"
|
|
)
|
|
p = Path(bytes2str(path),key_s)
|
|
|
|
free(path)
|
|
|
|
return p.with_suffix(".obiview").is_file()
|
|
|
|
cpdef int view_count(self):
|
|
return PyList_Size(list(self.keys()))
|
|
|
|
def get_view(self):
|
|
raise NotImplemented
|
|
|
|
def __len__(self):
|
|
return self.view_count()
|
|
|
|
def __getitem__(self, object view_name):
|
|
return self.get_view(view_name)
|
|
|
|
def __iter__(self):
|
|
return self.keys()
|
|
|
|
cpdef bint is_view_writable(self, object view_name):
|
|
raise NotImplemented
|
|
|
|
|
|
|