def load_weight_methods(self): self._trawl_wm_descs = dict() wmd_q = Lookups.select().where(Lookups.lookup_type == 'TRAWL_WEIGHT_METHOD') self._trawl_wm_descs = {wm.lookup_value: textwrap.fill(wm.description, 20) for wm in wmd_q} self._fg_wm_descs = dict() fg_wmd_q = Lookups.select().where(Lookups.lookup_type == 'FG_WEIGHT_METHOD') self._fg_wm_descs = {wm.lookup_value: textwrap.fill(wm.description, 20) for wm in fg_wmd_q}
def getData(self, data_name): """ Shortcut to get data from the DB that doesn't deserve its own property (Note, tried to use a dict to simplify this, but DB cursors were not updating) :return: Value found in DB """ if self._current_trip is None: self._logger.warning('Attempt to get data with null current trip.') return None data_name = data_name.lower() if data_name == 'partial_trip': return True if self._current_trip.partial_trip == 'T' else False elif data_name == 'fishing_days_count': return self._current_trip.fishing_days_count elif data_name == 'logbook_type': try: logbook_value = self._current_trip.logbook_type logbook_desc = Lookups.get( (Lookups.lookup_type == 'VESSEL_LOGBOOK_NAME') & (Lookups.lookup_value == logbook_value)).description return logbook_desc except Lookups.DoesNotExist: return None elif data_name == 'logbook_number': return self._current_trip.logbook_number elif data_name == 'fish_processed': return self._current_trip.fish_processed elif data_name == 'notes': return self._current_trip.notes else: self._logger.warning( 'Attempt to get unknown data name: {}'.format(data_name)) return None
def setData(self, data_name, data_val): """ Set misc data to the DB - should do this for all properties instead of individual pyqtSlots... :return: """ if self._current_trip is None: self._logger.warning('Attempt to set data with null current trip.') return data_name = data_name.lower() if data_name == 'partial_trip': self._current_trip.partial_trip = 'T' if data_val else 'F' elif data_name == 'fishing_days_count': self._current_trip.fishing_days_count = int( data_val) if data_val else None elif data_name == 'logbook_type': logbook_value = Lookups.get( (Lookups.lookup_type == 'VESSEL_LOGBOOK_NAME') & (Lookups.description == data_val)).lookup_value self._current_trip.logbook_type = logbook_value # data_val = logbook_value elif data_name == 'logbook_number': self._current_trip.logbook_number = data_val elif data_name == 'fish_processed': self._current_trip.fish_processed = data_val elif data_name == 'notes': self._current_trip.notes = data_val else: self._logger.warning( 'Attempt to set unknown data name: {}'.format(data_name)) return self._set_cur_prop(data_name, data_val) self._current_trip.save() logging.debug('Set {} to {}'.format(data_name, data_val))
def _get_lookups_orm(self, rebuild=False): """ Get lookups via peewee ORM :return: """ if self._lookups is not None and not rebuild: # only build this once unless rebuilt return self._lookups = dict() # of lists # http://peewee.readthedocs.org/en/latest/peewee/querying.html#query-operators lookups_q = Lookups.select().where((Lookups.active >> None) | (Lookups.active == 1)) for lu in lookups_q: key = lu.lookup_type if key in self._lookups: self._lookups[key].append({'desc': lu.description, 'value': lu.lookup_value}) else: self._lookups[key] = [{'desc': lu.description, 'value': lu.lookup_value}] if len(self._lookups) == 0: raise ConnectionError('Unable to get LOOKUPS from database, check observer DB') # Build fisheries list self._lookup_fisheries = list() for fishery in self._lookups['FISHERY']: self._lookup_fisheries.append(fishery['desc']) self._lookup_fisheries = sorted(self._lookup_fisheries)
def _get_fishery_ID(self, fishery_name): """ Lookup up fishery ID by fishery_name """ try: return Lookups.get((Lookups.lookup_type == 'FISHERY') & ( Lookups.description == fishery_name)).lookup_value except Lookups.DoesNotExist as e: self._logger.error(str(e)) return None
def lookup_fisheries(fisheries_str): """ Given fisheries, convert to list and return list of ID's from LOOKUPS @param fisheries_str: e.g. '15,16' @return: e.g. [608, 688] """ found_ids = [] try: fishery_ids = fisheries_str.strip().replace(' ', '').split(',') for fish in fishery_ids: fish_id = Lookups.get((Lookups.lookup_type == 'FISHERY') & ( Lookups.lookup_value == fish.strip())).lookup found_ids.append(fish_id) return found_ids except Lookups.DoesNotExist: # noinspection PyUnboundLocalVariable logging.error('*** Unable to look up fishery {} in group {} ' '(does not exist, check db.)'.format( fish, fisheries_str))
def lookup_geartypes(geartypes_str): """ Given gear types, convert to list and return list of ID's from LOOKUPS @param geartypes_str: e.g. '15,16' @return: e.g. [608, 688] """ found_ids = [] try: gear_ids = geartypes_str.strip().replace(' ', '').split(',') for geartype in gear_ids: gear_id = Lookups.get( ((Lookups.lookup_type == 'FG_GEAR_TYPE') | (Lookups.lookup_type == 'TRAWL_GEAR_TYPE')) & (Lookups.lookup_value == geartype.strip())).lookup found_ids.append(gear_id) return found_ids except Lookups.DoesNotExist: # noinspection PyUnboundLocalVariable logging.error('*** Unable to look up gear type {} in group {} ' '(does not exist, check db.)'.format( geartype, geartypes_str))
def lookup_protocols(protocols_str): """ Given protocols, convert to list and return list of ID's @param protocols_str: e.g. 'FL,WS' @return: e.g. [8, 19] """ found_ids = [] try: prot_abbrevs = protocols_str.strip().replace(' ', '').split(',') for pa in prot_abbrevs: prot_id = Lookups.get((Lookups.lookup_type == 'PROTOCOL') & ( Lookups.lookup_value == pa.strip())).lookup found_ids.append(prot_id) return found_ids except Lookups.DoesNotExist: # noinspection PyUnboundLocalVariable logging.error('*** Unable to look up protocol {} in group {} ' '(does not exist, check spreadsheet.)'.format( pa, protocols_str)) except Exception as e: logging.error(f'{e}')
def _get_catch_additional_basket_types_from_lookup_table(self) -> Dict[int, str]: """ The types of additional baskets (unweighed full, weighed partial, weighed full) are stored as entries in the LOOKUPS Table with a LOOKUP_TYPE of "CAB_BASKET_TYPE". These values will not change during a run of OPTECS. For convenience, load these values into a dictionary. The digits stored in LOOKUPS are of type string. Convert to integer here (i.e. the key field of this dictionary is of type integer). :return: """ # Provide a backup in case values aren't in LOOKUPS. default_dict = { ObserverCatchBaskets.LOOKUP_VALUE_CAB_BASKET_TYPE_UNWEIGHED_FULL: "Unweighed Full Basket", ObserverCatchBaskets.LOOKUP_VALUE_CAB_BASKET_TYPE_WEIGHED_PARTIAL: "Weighed Partial Basket", ObserverCatchBaskets.LOOKUP_VALUE_CAB_BASKET_TYPE_WEIGHED_FULL: "Weighed Full Basket" } basket_type_dict = {} try: cab_lookup_entries = Lookups.select(Lookups.lookup_value, Lookups.description).\ where(Lookups.lookup_type == ObserverCatchBaskets.CAB_BASKET_TYPE_LOOKUP_TYPE) for entry in cab_lookup_entries: basket_type_dict[int(entry.lookup_value)] = entry.description if len(basket_type_dict) < len(default_dict): self._logger.warning(f"Fewer entries than expected. Using default values.") self._logger.warning(f"Entries found in LOOKUPS: {basket_type_dict}.") basket_type_dict = default_dict except Exception as e: self._logger.warning("Could not read DB for CATCH_ADDITIONAL_BASKETS.BASKET_TYPE enumeration. " + f"Using default. Exception info: {e}.") basket_type_dict = default_dict self._logger.debug(f"Using these CATCH_ADDITIONAL_BASKET basket_type values: {basket_type_dict}.") return basket_type_dict
def _get_fishery_by_ID(fishery_id): try: return Lookups.get((Lookups.lookup_type == 'FISHERY') & ( Lookups.lookup_value == fishery_id)).description except Lookups.DoesNotExist as e: logging.error(e)
def __init__(self): self._rf = dict() # translate code to description self._rfc = [] # list of codes rf_q = Lookups.select().where(Lookups.lookup_type == 'ROCKFISH_HANDLING') self._rf = {rf.lookup_value: rf.description for rf in rf_q} self._rfc = (rf.lookup_value for rf in rf_q)
def __init__(self): self._sm_descs = dict() smd_q = Lookups.select().where(Lookups.lookup_type == 'SC_SAMPLE_METHOD') self._sm_descs = {sm.lookup_value: sm.description for sm in smd_q}