def __search_metadata( self, selector: SeriesSelector) -> Generator[Metadata, None, None]: connection = self.connect() cursor = connection.cursor() dictionary_cursor = None if self._config.dictionary_query is not None: dictionary_cursor = self.connect().cursor() cursor.execute(self._config.list_query) series_name_index = None for i, name in enumerate(self._config.list_columns): if name == "series name": series_name_index = i if series_name_index is None: raise InvalidMetadataError('column "series name" not found') for row in cursor: selector = SeriesSelector(selector.source, row[series_name_index]) metadata = Metadata(selector) for i, name in enumerate(self._config.list_columns): if i == series_name_index: continue if row[i] is None: continue metadata.set_field( name, self._metadata_value_mapper.from_source(name, row[i])) if metadata.dictionary_name is not None and dictionary_cursor is not None: metadata.dictionary = self.__query_dictionary( dictionary_cursor, metadata.dictionary_name) yield metadata
def get_metadata(self, selector: SeriesSelector) -> Metadata: """Read metadata from the DB-API connection.""" metadata = Metadata(selector) if self._config.metadata_query is None: return metadata connection = self.connect() cursor = connection.cursor() query = self._config.metadata_query params = [selector.name] if self._config.query_string_parameters: query = query.format(*params) params = [] cursor.execute(query, params) row = cursor.fetchone() if row: for i, name in enumerate(self._config.metadata_columns): if row[i] is not None: metadata.set_field( name, self._metadata_value_mapper.from_source(name, row[i])) if metadata.dictionary_name is not None: metadata.dictionary = self.__query_dictionary( cursor, metadata.dictionary_name) return metadata
def get_metadata(self, selector: SeriesSelector) -> Metadata: """Read metadata, taking any configured metadata mapping into account.""" metadata = Metadata(selector) if self.__loaders.metadata is None: return metadata with self.__loaders.metadata.open() as metadata_file: reader = csv.DictReader(metadata_file) for row in reader: if self.__metadata_mapper.from_kukur("series name") not in row: raise InvalidMetadataError( 'column "series name" not found') if (row[self.__metadata_mapper.from_kukur("series name")] != selector.name): continue for field, _ in metadata: if self.__metadata_mapper.from_kukur(field) in row: try: value = row[self.__metadata_mapper.from_kukur( field)] metadata.set_field( field, self.__metadata_value_mapper.from_source( field, value), ) except ValueError: pass if metadata.dictionary_name is not None: metadata.dictionary = self.__get_dictionary( metadata.dictionary_name) return metadata
def search(self, selector: SeriesSelector) -> Generator[Metadata, None, None]: """Search for series matching the given selector.""" if self.__loaders.metadata is None: return with self.__loaders.metadata.open() as metadata_file: reader = csv.DictReader(metadata_file) for row in reader: if self.__metadata_mapper.from_kukur("series name") not in row: raise InvalidMetadataError( 'column "series name" not found') series_name = row[self.__metadata_mapper.from_kukur( "series name")] metadata = None if selector.name is not None: if series_name == selector.name: metadata = Metadata( SeriesSelector(selector.source, series_name)) else: metadata = Metadata( SeriesSelector(selector.source, series_name)) if metadata is not None: for field, _ in metadata: if self.__metadata_mapper.from_kukur(field) in row: try: value = row[self.__metadata_mapper.from_kukur( field)] metadata.set_field( field, self.__metadata_value_mapper.from_source( field, value), ) except ValueError: pass if metadata.dictionary_name is not None: metadata.dictionary = self.__get_dictionary( metadata.dictionary_name) yield metadata