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