Modify __init__ and close method to deal with registration process
This commit is contained in:
@ -28,6 +28,8 @@ from obitools3.utils cimport bytes2str, \
|
||||
tobytes, \
|
||||
tostr
|
||||
|
||||
from .object cimport OBIObjectClosedInstance
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
__OBIDMS_COLUMN_CLASS__ = {}
|
||||
@ -75,18 +77,6 @@ cdef class DMS(OBIObject):
|
||||
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):
|
||||
@ -103,9 +93,13 @@ cdef class DMS(OBIObject):
|
||||
|
||||
the `close` method is automatically called by the object destructor.
|
||||
"""
|
||||
cdef OBIDMS_p pointer = self._pointer
|
||||
|
||||
if self._pointer!=NULL:
|
||||
if (obi_close_dms(self._pointer)) < 0 :
|
||||
OBIObject.close(self)
|
||||
self._pointer=NULL
|
||||
|
||||
if pointer!=NULL:
|
||||
if (obi_close_dms(pointer)) < 0 :
|
||||
raise Exception("Problem closing an OBIDMS")
|
||||
else:
|
||||
raise OBIObjectClosedInstance()
|
||||
|
@ -4,6 +4,7 @@ cdef class OBIObject:
|
||||
cdef dict _dependent_object
|
||||
|
||||
cpdef register(self, OBIObject object)
|
||||
cpdef unregister(self, OBIObject object)
|
||||
cpdef close(self)
|
||||
|
||||
cdef class OBIObjectClosedInstance(Exception):
|
||||
|
@ -5,12 +5,18 @@ cdef class OBIObject:
|
||||
cpdef register(self, OBIObject object):
|
||||
self._dependent_object[id(object)]=object
|
||||
|
||||
cpdef close(self):
|
||||
cdef OBIObject object
|
||||
cpdef unregister(self, OBIObject object):
|
||||
del self._dependent_object[id(object)]
|
||||
|
||||
for object in self._dependent_object.values():
|
||||
def close(self):
|
||||
cdef OBIObject object
|
||||
cdef list toclose = list(self._dependent_object.values())
|
||||
|
||||
for object in toclose:
|
||||
object.close()
|
||||
|
||||
assert len(dependent_object.values)==0
|
||||
|
||||
def __init__(self):
|
||||
self._dependent_object={}
|
||||
|
||||
|
@ -4,12 +4,14 @@ from ..capi.obiview cimport Obiview_p
|
||||
from ..capi.obitypes cimport index_t, \
|
||||
obitype_t
|
||||
|
||||
from ..object cimport OBIObject
|
||||
from ..dms cimport DMS
|
||||
|
||||
from ..column.column cimport Column
|
||||
|
||||
|
||||
|
||||
cdef class View:
|
||||
cdef class View(OBIObject):
|
||||
|
||||
cdef DMS _dms
|
||||
cdef Obiview_p _pointer
|
||||
|
@ -17,12 +17,16 @@ from .dms cimport __OBIDMS_VIEW_CLASS__
|
||||
from obitools3.utils cimport tobytes, \
|
||||
bytes2str
|
||||
|
||||
from ..object cimport OBIObjectClosedInstance
|
||||
|
||||
cdef class View :
|
||||
|
||||
cdef class View(OBIObject) :
|
||||
|
||||
|
||||
def __init__(self,dms,int __internalCall__):
|
||||
|
||||
OBIObject.__init__(self)
|
||||
|
||||
if __internalCall__!=987654:
|
||||
raise RuntimeError('OBIView constructor cannot be called directly')
|
||||
|
||||
@ -56,6 +60,8 @@ cdef class View :
|
||||
bytes2str(view_name_b))
|
||||
)
|
||||
|
||||
self._dms.register(view)
|
||||
|
||||
return view
|
||||
|
||||
@staticmethod
|
||||
@ -85,6 +91,8 @@ cdef class View :
|
||||
message = "Error : Cannot create view %s" % bytes2str(view_name_b)
|
||||
raise RuntimeError(message)
|
||||
|
||||
dms.register(view)
|
||||
|
||||
return view
|
||||
|
||||
@staticmethod
|
||||
@ -101,16 +109,25 @@ cdef class View :
|
||||
if view._pointer == NULL :
|
||||
raise RuntimeError("Error : Cannot open view %s" % bytes2str(view_name_b))
|
||||
|
||||
dms.register(view)
|
||||
|
||||
return view
|
||||
|
||||
def close(self):
|
||||
if (self._pointer != NULL):
|
||||
cdef Obiview_p pointer = self._pointer
|
||||
|
||||
if (pointer != NULL):
|
||||
self._dms.unregister(self)
|
||||
OBIObject.close(self)
|
||||
|
||||
self._pointer = NULL
|
||||
|
||||
if obi_save_and_close_view(self._pointer) < 0 :
|
||||
raise Exception("Problem closing view %s" %
|
||||
bytes2str(self.name))
|
||||
else:
|
||||
raise OBIObjectClosedInstance()
|
||||
|
||||
def __dealloc__(self):
|
||||
self.close()
|
||||
|
||||
def __repr__(self) :
|
||||
cdef str s = "{name:s}\n{comments:s}\n{line_count:d} lines\n".format(name = str(self.name),
|
||||
|
Reference in New Issue
Block a user