
function handling errors and exceptions, as it can't read the right value of the global obi_errno (Cython configuration problem?)
154 lines
4.4 KiB
Cython
154 lines
4.4 KiB
Cython
#cython: language_level=3
|
|
|
|
from obitools3.dms.capi.obitypes cimport is_a_DNA_seq, \
|
|
OBI_VOID, \
|
|
OBI_BOOL, \
|
|
OBI_CHAR, \
|
|
OBI_FLOAT, \
|
|
OBI_INT, \
|
|
OBI_QUAL, \
|
|
OBI_SEQ, \
|
|
OBI_STR, \
|
|
index_t
|
|
|
|
from obitools3.dms.capi.obierrno cimport OBI_LINE_IDX_ERROR, \
|
|
OBI_ELT_IDX_ERROR
|
|
#obi_errno
|
|
|
|
|
|
cdef obi_errno_to_exception(int obi_errno, index_t line_nb=-1, object elt_id=None, str error_message=None) :
|
|
global obi_errno
|
|
if obi_errno > 0 :
|
|
if obi_errno == OBI_LINE_IDX_ERROR :
|
|
raise IndexError(line_nb, None or error_message)
|
|
elif obi_errno == OBI_ELT_IDX_ERROR :
|
|
raise IndexError(elt_id, None or error_message)
|
|
else :
|
|
raise Exception(None or error_message)
|
|
|
|
|
|
cdef bytes str2bytes(str string):
|
|
"""
|
|
Short cut to convert ascii encoded python string (str) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
return string.encode('ascii')
|
|
|
|
cdef str bytes2str(bytes string):
|
|
"""
|
|
Short cut to convert bytes (C-strings) to ascii encoded python string (str).
|
|
|
|
@param string: the binary (C-string) string to be converted.
|
|
@type string: bytes
|
|
@return an ascii transcoded string
|
|
@rtype: str
|
|
"""
|
|
return string.decode('ascii')
|
|
|
|
cdef bytes tobytes(object string):
|
|
"""
|
|
Short cut to convert ascii encoded string (str or bytes) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: bytes or str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
if isinstance(string, bytes):
|
|
return string
|
|
return str2bytes(string)
|
|
|
|
|
|
cdef str tostr(object string):
|
|
"""
|
|
Short cut to convert ascii encoded string (str or bytes) to bytes
|
|
which can be easily converted to C-strings.
|
|
|
|
@param string: the python string to be converted.
|
|
@type string: bytes or str
|
|
@return a transcoded string
|
|
@rtype: bytes
|
|
"""
|
|
if isinstance(string, str):
|
|
return string
|
|
return bytes2str(string)
|
|
|
|
|
|
cdef obitype_t get_obitype_single_value(object value) :
|
|
|
|
cdef type value_type
|
|
cdef obitype_t value_obitype
|
|
|
|
if value is None :
|
|
return OBI_VOID
|
|
|
|
value_type = type(value)
|
|
value_obitype = OBI_VOID
|
|
|
|
if value_type == int :
|
|
value_obitype = OBI_INT
|
|
elif value_type == float :
|
|
value_obitype = OBI_FLOAT
|
|
elif value_type == bool :
|
|
value_obitype = OBI_BOOL
|
|
elif value_type == str or value_type == bytes :
|
|
if is_a_DNA_seq(tobytes(value)) :
|
|
value_obitype = OBI_SEQ
|
|
elif len(value) == 1 :
|
|
value_obitype = OBI_CHAR
|
|
elif (len(value) > 1) :
|
|
value_obitype = OBI_STR
|
|
else :
|
|
value_obitype = OBI_VOID
|
|
|
|
return value_obitype
|
|
|
|
|
|
cdef obitype_t update_obitype(obitype_t obitype, object new_value) :
|
|
|
|
cdef type new_type
|
|
|
|
new_type = type(new_value)
|
|
|
|
if obitype == OBI_INT :
|
|
if new_type == float :
|
|
return OBI_FLOAT
|
|
# TODO BOOL vers INT/FLOAT
|
|
elif new_type == str or new_type == bytes :
|
|
if obitype == OBI_SEQ and is_a_DNA_seq(tobytes(new_value)) :
|
|
pass
|
|
else :
|
|
return OBI_STR
|
|
|
|
return obitype
|
|
|
|
|
|
cdef obitype_t get_obitype_iterable_value(object value) :
|
|
|
|
cdef obitype_t value_obitype
|
|
|
|
value_obitype = OBI_VOID
|
|
|
|
for k in value :
|
|
if value_obitype == OBI_VOID :
|
|
value_obitype = get_obitype_single_value(value[k])
|
|
else :
|
|
value_obitype = update_obitype(value_obitype, value[k])
|
|
|
|
return value_obitype
|
|
|
|
|
|
cdef obitype_t get_obitype(object value) :
|
|
|
|
if type(value) == dict or type(value) == list or type(value) == tuple :
|
|
return get_obitype_iterable_value(value)
|
|
|
|
else :
|
|
return get_obitype_single_value(value)
|