From adf5cbef9715af67d8b805103770745c8de68cff Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Fri, 28 Jul 2017 09:55:43 +0200 Subject: [PATCH] Added DMS method to create a DMS if it doesn't already exists, otherwise opens it --- python/obitools3/commands/import.pyx | 7 ++--- python/obitools3/dms/capi/obidms.pxd | 1 + python/obitools3/dms/dms.pyx | 38 ++++++++++++++++++---------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/python/obitools3/commands/import.pyx b/python/obitools3/commands/import.pyx index 7badd0e..de684eb 100644 --- a/python/obitools3/commands/import.pyx +++ b/python/obitools3/commands/import.pyx @@ -147,11 +147,8 @@ def run(config): inputs = uopen(config['import']['filename']) # Create or open DMS - try: - d = DMS.test_open(config['obi']['defaultdms']) - except : - d = DMS.new(config['obi']['defaultdms']) - + d = DMS.open_or_new(config['obi']['defaultdms']) + get_quality = False NUC_SEQS_view = False if config['import']['seqinformat']=='fasta': diff --git a/python/obitools3/dms/capi/obidms.pxd b/python/obitools3/dms/capi/obidms.pxd index be3fb00..0671223 100644 --- a/python/obitools3/dms/capi/obidms.pxd +++ b/python/obitools3/dms/capi/obidms.pxd @@ -12,6 +12,7 @@ cdef extern from "obidms.h" nogil: OBIDMS_p obi_open_dms(const_char_p dms_path) OBIDMS_p obi_test_open_dms(const_char_p dms_path) OBIDMS_p obi_create_dms(const_char_p dms_path) + int obi_dms_exists(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) diff --git a/python/obitools3/dms/dms.pyx b/python/obitools3/dms/dms.pyx index 2392c66..48e8213 100644 --- a/python/obitools3/dms/dms.pyx +++ b/python/obitools3/dms/dms.pyx @@ -5,9 +5,9 @@ from libc.stdlib cimport free from cpython.list cimport PyList_Size from .capi.obidms cimport obi_open_dms, \ - obi_test_open_dms, \ obi_create_dms, \ obi_close_dms, \ + obi_dms_exists, \ obi_dms_get_full_path from .capi.obitypes cimport const_char_p @@ -29,6 +29,29 @@ cdef class DMS(OBIWrapper): cdef inline OBIDMS_p pointer(self): return (self._pointer) + @staticmethod + def open_or_new(object dms_name) : + cdef OBIDMS_p pointer + cdef DMS dms + cdef bytes dms_name_b = tobytes(dms_name) + if DMS.exists(dms_name_b) : + pointer = obi_open_dms( dms_name_b) + else : + pointer = obi_create_dms( dms_name_b) + if pointer == NULL : + raise Exception("Failed opening or creating an OBIDMS") + dms = OBIWrapper.new_wrapper(DMS, pointer) + return dms + + @staticmethod + def exists(object dms_name): + cdef bytes dms_name_b = tobytes(dms_name) + cdef int rep + rep = obi_dms_exists(dms_name_b) + if rep < 0 : + raise RuntimeError("Error checking if a DMS exists") + else : + return bool(rep) @staticmethod def new(object dms_name) : @@ -52,19 +75,6 @@ cdef class DMS(OBIWrapper): raise Exception("Failed opening an OBIDMS") dms = OBIWrapper.new_wrapper(DMS, pointer) return dms - - - @staticmethod - def test_open(object dms_name) : - cdef OBIDMS_p pointer - cdef DMS dms - cdef bytes dms_name_b = tobytes(dms_name) - pointer = obi_test_open_dms( dms_name_b) - if pointer != NULL : - dms = OBIWrapper.new_wrapper(DMS, pointer) - else : - dms = None - return dms def close(self) :