diff --git a/python/obitools3/obidms/_obidms.pxd b/python/obitools3/obidms/_obidms.pxd index c834681..52c977b 100644 --- a/python/obitools3/obidms/_obidms.pxd +++ b/python/obitools3/obidms/_obidms.pxd @@ -41,6 +41,6 @@ cdef class OBIDMS_column: cpdef list get_elements_names(self) cpdef str get_data_type(self) cpdef size_t get_nb_lines_used(self) -# cpdef str get_creation_date(self) + cpdef str get_creation_date(self) cpdef close(self) diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index 6f7818e..dbf82c6 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -6,17 +6,15 @@ from obitools3.utils cimport bytes2str, str2bytes from .capi.obidms cimport obi_dms, \ obi_close_dms -from .capi.obidmscolumn cimport obi_column_get_nb_lines_used, \ - obi_column_get_elements_names, \ - obi_column_get_header_from_name, \ +from .capi.obidmscolumn cimport obi_column_get_header_from_name, \ obi_unmap_header, \ obi_column_get_latest_version_from_name, \ obi_create_column, \ obi_clone_column, \ obi_open_column, \ obi_close_column, \ + obi_column_format_date, \ OBIDMS_column_header_p - #obi_column_get_formatted_creation_date from .capi.obitypes cimport const_char_p, \ name_data_type @@ -28,7 +26,7 @@ from ._obidmscolumn_int cimport OBIDMS_column_int, \ OBIDMS_column_int_writable, \ OBIDMS_column_int_multi_elts, \ OBIDMS_column_int_multi_elts_writable - + from ._obidmscolumn_float cimport OBIDMS_column_float, \ OBIDMS_column_float_writable, \ OBIDMS_column_float_multi_elts, \ @@ -87,11 +85,11 @@ cdef class OBIDMS : p = Path(self.dms_name+'.obidms') - print("{:<25} {:<25} {:<25} {:<25}".format('-Column name-', + print("{:<30} {:<12} {:<25} {:<30} {:<40}".format('-Column name-', '-Data type-', '-Latest version number-', - '-Line count of latest version-')) - #'-Creation date of latest version-')) + '-Line count of latest version-', + '-Creation date of latest version-')) for entry in p.iterdir(): if entry.suffix == ".obicol": column_name = entry.stem @@ -100,18 +98,16 @@ cdef class OBIDMS : header = obi_column_get_header_from_name(self.pointer, column_name_b) data_type = bytes2str(name_data_type(header.data_type)) line_count = header.line_count + creation_date = bytes2str(obi_column_format_date(header.creation_date)) + obi_unmap_header(header) # TODO check if error? but C will already warn and there's nothing to do latest_version = obi_column_get_latest_version_from_name(self.pointer, column_name_b) - -# creation_date = bytes2str(obi_column_get_formatted_creation_date_from_name(self.pointer, column_name_b)) #TODO (deprecated, use header) -# print(creation_date) - dms[column_name]['data_type'] = data_type dms[column_name]['latest_version'] = latest_version dms[column_name]['line_count'] = line_count -# dms[column_name]['creation_date'] = creation_date - print("{:<25} {:<25} {:<25} {:<25}".format(column_name, data_type, latest_version, line_count)) + dms[column_name]['creation_date'] = creation_date + # TODO : actually get all the informations in the header + print("{:<30} {:<12} {:<25} {:<30} {:<40}".format(column_name, data_type, latest_version, line_count, creation_date)) - obi_unmap_header(header) # TODO check if error? but C will already warn and there's nothing to do return dms @@ -272,7 +268,7 @@ cdef class OBIDMS_column : self.pointer = obi_clone_column(self.dms.pointer, column_name_b, version_number, clone_data) else : self.pointer = obi_open_column(self.dms.pointer, column_name_b, version_number) - + def __iter__(self): @@ -281,34 +277,44 @@ cdef class OBIDMS_column : cdef size_t line_nb # Yield each line - lines_used = obi_column_get_nb_lines_used(self.pointer) + lines_used = self.pointer.header.lines_used for line_nb in range(lines_used): yield self.get_line(line_nb) + def __setitem__(self, size_t line_nb, object value): self.set_line(line_nb, value) + def __getitem__(self, size_t line_nb): return self.get_line(line_nb) + # cpdef object get_item(self, size_t line_nb, str element_name): TODO # raise NotImplementedError + # cpdef set_item(self, size_t line_nb, str element_name, object value): TODO # raise NotImplementedError + cpdef list get_elements_names(self): return self.elements_names + cpdef str get_data_type(self): return self.data_type - cpdef size_t get_nb_lines_used(self): - return obi_column_get_nb_lines_used(self.pointer) -# cpdef str get_creation_date(self): -# return bytes2str(obi_column_get_formatted_creation_date(self.pointer)) + cpdef size_t get_nb_lines_used(self): + return self.pointer.header.lines_used + + + cpdef str get_creation_date(self): + return bytes2str(obi_column_format_date(self.pointer.header.creation_date)) + cpdef close(self): raise NotImplementedError + diff --git a/python/obitools3/obidms/capi/obidmscolumn.pxd b/python/obitools3/obidms/capi/obidmscolumn.pxd index 6e3f72e..29a2f79 100644 --- a/python/obitools3/obidms/capi/obidmscolumn.pxd +++ b/python/obitools3/obidms/capi/obidmscolumn.pxd @@ -13,11 +13,6 @@ from ..capi.obitypes cimport const_char_p, \ cdef extern from "obidmscolumn.h" nogil: - - struct OBIDMS_column_t: - pass - - ctypedef OBIDMS_column_t* OBIDMS_column_p struct OBIDMS_column_header_t: bint little_endian @@ -35,25 +30,22 @@ cdef extern from "obidmscolumn.h" nogil: ctypedef OBIDMS_column_header_t* OBIDMS_column_header_p + struct OBIDMS_column_t: + OBIDMS_column_header_p header + + ctypedef OBIDMS_column_t* OBIDMS_column_p + OBIDMS_column_p obi_create_column(OBIDMS_p dms, const_char_p column_name, OBIType_t type, size_t nb_lines, size_t nb_elements_per_line, const_char_p elements_names) - - size_t obi_column_get_nb_lines_used(OBIDMS_column_p column) - - const_char_p obi_column_get_elements_names(OBIDMS_column_p column) - - void obi_column_make_unwritable(OBIDMS_column_p column) - + OBIDMS_column_p obi_open_column(OBIDMS_p dms, const_char_p column_name, obiversion_t version_number) - - OBIType_t obi_column_get_type(OBIDMS_column_p column) - + int obi_close_column(OBIDMS_column_p column) OBIDMS_column_p obi_clone_column(OBIDMS_p dms, @@ -70,12 +62,9 @@ cdef extern from "obidmscolumn.h" nogil: const_char_p column_name) int obi_unmap_header(OBIDMS_column_header_p header) - -# char* obi_column_get_formatted_creation_date(OBIDMS_column_p column) - -# char* obi_column_get_formatted_creation_date_from_name(OBIDMS_p dms, -# const_char_p column_name) - + + char* obi_column_format_date(time_t date) + cdef extern from "obidmscolumn_int.h" nogil: int obi_column_set_obiint_with_elt_name(OBIDMS_column_p column, diff --git a/python/obitools3/unit_tests.py b/python/obitools3/unit_tests.py index 4d5ea3d..6829683 100644 --- a/python/obitools3/unit_tests.py +++ b/python/obitools3/unit_tests.py @@ -25,7 +25,7 @@ def create_test_column(dms, data_type_code, multiple_elements_per_line=False): data_types = DATA_TYPES data_type_code = data_type_code data_type_str = data_types[data_type_code-1] - col_name = "test_col_"+data_type_str + col_name = "unit_test_"+data_type_str if multiple_elements_per_line : elts_names = elements_names() diff --git a/src/obidmscolumn.c b/src/obidmscolumn.c index 37dd0b2..ac91b23 100644 --- a/src/obidmscolumn.c +++ b/src/obidmscolumn.c @@ -1317,31 +1317,7 @@ void obi_ini_to_NA_values(OBIDMS_column_p column, } -void obi_column_make_unwritable(OBIDMS_column_p column) // TODO this might be irrelevant -{ - column->writable = false; -} - - -size_t obi_column_get_line_count(OBIDMS_column_p column) -{ - return (column->header)->line_count; -} - - -size_t obi_column_get_nb_lines_used(OBIDMS_column_p column) -{ - return (column->header)->lines_used; -} - - -OBIType_t obi_column_get_data_type(OBIDMS_column_p column) -{ - return (column->header)->data_type; -} - - -OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* column_name) +OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* column_name) // ADD VERSION ARGUMENT { OBIDMS_column_header_p header; OBIDMS_column_directory_p column_directory; @@ -1436,12 +1412,6 @@ int obi_unmap_header(OBIDMS_column_header_p header) } -const char* obi_column_get_elements_names(OBIDMS_column_p column) -{ - return (column->header)->elements_names; -} - - // TODO to be rewritten in an optimized and safe way size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char* element_name) { @@ -1483,146 +1453,28 @@ size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char } -size_t obi_column_get_nb_elements_per_line(OBIDMS_column_p column) -{ - // TODO check that column is not NULL? - return (column->header)->nb_elements_per_line; -} - - -char* obi_column_get_formatted_creation_date(OBIDMS_column_p column) // TODO +char* obi_column_format_date(time_t date) { char* formatted_time; struct tm* tmp; formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char)); - tmp = localtime(&((column->header)->creation_date)); + tmp = localtime(&date); if (tmp == NULL) { obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError formatting the creation date of a column"); + obidebug(1, "\nError formatting a date"); return NULL; } if (strftime(formatted_time, FORMATTED_TIME_LENGTH, "%c", tmp) == 0) { obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError formatting the creation date of a column"); + obidebug(1, "\nError formatting a date"); return NULL; } return formatted_time; } - -char* obi_column_get_formatted_creation_date_from_name(OBIDMS_p dms, const char* column_name) // TODO -{ - OBIDMS_column_header_p header; - OBIDMS_column_directory_p column_directory; - char* column_file_name; - int column_file_descriptor; - int column_dir_file_descriptor; - size_t header_size; - obiversion_t version_number; - char* formatted_time; - struct tm* tmp; - - // Get the column directory structure associated to the column - column_directory = obi_open_column_directory(dms, column_name); - if (column_directory == NULL) - { - obidebug(1, "\nError opening a column directory structure"); - return NULL; - } - - // Get the file descriptor associated to the column directory - column_dir_file_descriptor = dirfd(column_directory->directory); - if (column_dir_file_descriptor < 0) - { - obi_set_errno(OBICOLDIR_UNKNOWN_ERROR); - obidebug(1, "\nError getting the file descriptor of a column directory"); - obi_close_column_directory(column_directory); - return NULL; - } - - // Calculate the header size - header_size = obi_get_platform_header_size(); - - // Get the latest version number - version_number = obi_get_latest_version_number(column_directory); - if (version_number < 0) - { - obidebug(1, "\nError getting the latest version number in a column directory"); - return NULL; - } - - // Get the column file name - column_file_name = build_column_file_name(column_name, version_number); - if (column_file_name == NULL) - { - return NULL; - } - - // Open the column file (READ-ONLY) - column_file_descriptor = openat(column_dir_file_descriptor, column_file_name, O_RDONLY); - if (column_file_descriptor < 0) - { - obidebug(1, "\nError opening a column file"); - obi_set_errno(OBICOL_UNKNOWN_ERROR); - free(column_file_name); - return NULL; - } - - // Fill the header structure - header = mmap(NULL, - header_size, - PROT_READ, - MAP_SHARED, - column_file_descriptor, - 0 - ); - - if (header == MAP_FAILED) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError mmapping the header of a column"); - close(column_file_descriptor); - free(column_file_name); - return NULL; - } - - // TODO Check endianness? - - // Format time - formatted_time = (char*) malloc(FORMATTED_TIME_LENGTH*sizeof(char)); - tmp = localtime(&(header->creation_date)); - if (tmp == NULL) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError formatting the creation date of a column"); - close(column_file_descriptor); - free(column_file_name); - return NULL; - } - if (strftime(formatted_time, FORMATTED_TIME_LENGTH, "%c", tmp) == 0) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError formatting the creation date of a column"); - close(column_file_descriptor); - free(column_file_name); - return NULL; - } - - // Munmap header - if (munmap(header, header->header_size) < 0) - { - obi_set_errno(OBICOL_UNKNOWN_ERROR); - obidebug(1, "\nError munmapping header after getting the creation date of a column"); - } - - free(column_file_name); - close(column_file_descriptor); - - return formatted_time; -} diff --git a/src/obidmscolumn.h b/src/obidmscolumn.h index 5fc8796..102c4b5 100644 --- a/src/obidmscolumn.h +++ b/src/obidmscolumn.h @@ -283,56 +283,6 @@ int obi_truncate_and_close_column(OBIDMS_column_p column); void obi_ini_to_NA_values(OBIDMS_column_p column, size_t first_line_nb, size_t nb_lines); // TO make private? -/** - * @brief Sets the 'writable' header attribute of an OBIDMS column to False. // TODO delete? - * - * @param column A pointer on an OBIDMS column - * - * @since July 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -void obi_column_make_unwritable(OBIDMS_column_p column); - - -/** - * @brief Recovers the line count of an OBIDMS column. - * - * @param column A pointer on an OBIDMS column. - * - * @returns The line count of the column. - * - * @since July 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -size_t obi_column_get_line_count(OBIDMS_column_p column); - - -/** - * @brief Recovers the number of lines used in an OBIDMS column. - * - * @param column A pointer on an OBIDMS column. - * - * @returns The number of lines used in the column. - * - * @since August 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -size_t obi_column_get_nb_lines_used(OBIDMS_column_p column); - - -/** - * @brief Recovers the data type of an OBIDMS column. - * - * @param column A pointer on an OBIDMS column. - * - * @returns The data type of the column. - * - * @since July 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -OBIType_t obi_column_get_data_type(OBIDMS_column_p column); - - /** * @brief Recovers the header of an OBIDMS column from the column name. * @@ -364,19 +314,6 @@ OBIDMS_column_header_p obi_column_get_header_from_name(OBIDMS_p dms, const char* int obi_unmap_header(OBIDMS_column_header_p header); -/** - * @brief Recovers the elements names of an OBIDMS column. - * - * @param column A pointer on an OBIDMS column. - * - * @returns The elements names. - * - * @since July 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -const char* obi_column_get_elements_names(OBIDMS_column_p column); - - /** * @brief Recovers the index of an element in an OBIDMS column from the element's name. * @@ -393,43 +330,16 @@ size_t obi_column_get_element_index_from_name(OBIDMS_column_p column, const char /** - * @brief Recovers the number of elements per line in an OBIDMS column. + * @brief Formats a date in a way that is easy to read. * - * @param column A pointer on an OBIDMS column. + * @param date A date. * - * @returns The number of elements per line. - * - * @since July 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -size_t obi_column_get_nb_elements_per_line(OBIDMS_column_p column); - - -/** - * @brief Recovers the creation date of an OBIDMS column. - * - * @param column A pointer on an OBIDMS column. - * - * @returns The creation date of the column. + * @returns The date formatted in a way that is easy to read. * * @since October 2015 * @author Celine Mercier (celine.mercier@metabarcoding.org) */ -char* obi_column_get_formatted_creation_date(OBIDMS_column_p column); - - -/** - * @brief Recovers the creation date of an OBIDMS column from the column name. - * - * @param dms A pointer on an OBIDMS. - * @param column_name The name of an OBIDMS column. - * - * @returns The creation date of the column. - * - * @since October 2015 - * @author Celine Mercier (celine.mercier@metabarcoding.org) - */ -char* obi_column_get_get_formatted_creation_date_from_name(OBIDMS_p dms, const char* column_name); +char* obi_column_format_date(time_t date); #endif /* OBIDMSCOLUMN_H_ */