From cb5ad2ed2d19e51fb2a908f4bfcf54f0d55b7b3f Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Wed, 5 Jul 2017 15:38:22 +0200 Subject: [PATCH] Added functions to try to open a DMS if it exists --- python/obitools3/dms/capi/obidms.pxd | 5 +++-- python/obitools3/dms/dms.pyx | 13 +++++++++++++ src/obidms.c | 21 ++++++++++++++++++++- src/obidms.h | 18 ++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/python/obitools3/dms/capi/obidms.pxd b/python/obitools3/dms/capi/obidms.pxd index 787c60a..be3fb00 100644 --- a/python/obitools3/dms/capi/obidms.pxd +++ b/python/obitools3/dms/capi/obidms.pxd @@ -9,8 +9,9 @@ cdef extern from "obidms.h" nogil: ctypedef OBIDMS_t* OBIDMS_p 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) + 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_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 be61f3b..5ab46cf 100644 --- a/python/obitools3/dms/dms.pyx +++ b/python/obitools3/dms/dms.pyx @@ -5,6 +5,7 @@ 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_get_full_path @@ -52,6 +53,18 @@ cdef class DMS(OBIWrapper): dms = OBIWrapper.new(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 : + raise Exception("Failed opening an OBIDMS") + dms = OBIWrapper.new(DMS, pointer) + return dms + def close(self) : ''' diff --git a/src/obidms.c b/src/obidms.c index 25a1b70..710eb20 100644 --- a/src/obidms.c +++ b/src/obidms.c @@ -362,7 +362,7 @@ OBIDMS_p obi_open_dms(const char* dms_path) if (realpath(complete_dms_path, dms->directory_path) == NULL) { obi_set_errno(OBIDMS_UNKNOWN_ERROR); - obidebug(1, "\nError getting the absolute path to the DMS directory"); + obidebug(1, "\nError getting the absolute path to the DMS directory (DMS does not exist)"); free(complete_dms_path); return NULL; } @@ -536,6 +536,25 @@ OBIDMS_p obi_open_dms(const char* dms_path) } +OBIDMS_p obi_test_open_dms(const char* dms_name) +{ + int exists; + + exists = obi_dms_exists(dms_name); + + switch (exists) + { + case 0: + return NULL; + case 1: + return obi_open_dms(dms_name); + }; + + obidebug(1, "\nError checking if an OBIDMS directory exists"); + return NULL; +} + + OBIDMS_p obi_dms(const char* dms_name) { int exists; diff --git a/src/obidms.h b/src/obidms.h index e984397..a7db2f5 100644 --- a/src/obidms.h +++ b/src/obidms.h @@ -175,6 +175,24 @@ OBIDMS_p obi_create_dms(const char* dms_name); OBIDMS_p obi_open_dms(const char* dms_path); +/** + * @brief Opens an existing OBITools Data Management instance (OBIDMS). + * + * @warning No error is printed or saved if the DMS does not exist. For it to be the case, use obi_open_dms(). + * + * @param dms_path A pointer to a C string containing the path to the database. + * + * @returns A pointer to an OBIDMS structure describing the opened DMS. + * @retval NULL if the DMS does not exist or if an error occurred. + * + * @see obi_open_dms() + * @see obi_close_dms() + * @since May 2017 + * @author Celine Mercier (celine.mercier@metabarcoding.org) + */ +OBIDMS_p obi_test_open_dms(const char* dms_name); + + /** * @brief Creates or opens a new OBIDMS instance. *