def tables_columns(): tables_sql = ( r"""SELECT tbl_name FROM sqlite_master WHERE (type='table' or type='view') and not (name in""" + SQLiteInternalTables() + r""") ORDER BY tbl_name""" ) connection_ok, tables = utils.sql_load_fr_db(tables_sql) if not connection_ok: textstring = u"""Cannot get data from sql """ + utils.returnunicode(tables_sql) utils.MessagebarAndLog.critical(bar_msg=u"Error, sql failed, see log message panel", log_msg=textstring) return [] tables_dict = {} tablenames = [col[0] for col in tables] for tablename in tablenames: columns_sql = """PRAGMA table_info (%s)""" % tablename connection_ok, columns = utils.sql_load_fr_db(columns_sql) if not connection_ok: textstring = u"""Cannot get data from sql """ + utils.returnunicode(columns_sql) utils.MessagebarAndLog.critical(bar_msg=u"Error, sql failed, see log message panel", log_msg=textstring) continue tables_dict[tablename] = tuple(sorted(tuple(columns), key=itemgetter(1))) return tables_dict
def w_qual_field_parameter_units(): sql = "select distinct parameter, unit from w_qual_field" connection_ok, result_dict = utils.get_sql_result_as_dict(sql) if not connection_ok: textstring = u"""Cannot get data from sql """ + utils.returnunicode(sql) utils.MessagebarAndLog.critical(bar_msg=u"Error, sql failed, see log message panel", log_msg=textstring) return {} return utils.returnunicode(result_dict, keep_containers=True)
def get_stored_settings(ms, settingskey): """ Reads the settings from settingskey and returns a tuple The settings string is assumed to look like this: objname;attr1:value1;attr2:value2/objname2;attr3:value3... :param ms: midvatten settings :param settingskey: the key to get from midvatten settings. :return: a tuple like ((objname', ((attr1, value1), (attr2, value2))), (objname2, ((attr3, value3), ...) """ settings_string_raw = ms.settingsdict.get(settingskey, None) if settings_string_raw is None: return [] settings_string = utils.returnunicode(settings_string_raw) objects_settings = settings_string.split(u'/') stored_settings = [] for object_settings in objects_settings: settings = object_settings.split(u';') object_name = settings[0] try: attributes = tuple([tuple(setting.split(u':')) for setting in settings[1:]]) except ValueError, e: utils.MessagebarAndLog.warning(log_msg=u"ExportFieldlogger: Getting stored settings didn't work: " + str(e)) continue stored_settings.append((object_name, attributes))
def alter_data(self, observation): observation = copy.deepcopy(observation) shift_specification = utils.returnunicode(self.dateshift_lineedit.text()) step_steplength = shift_specification.split(u' ') failed = False bar_msg = u'Dateshift specification wrong format, se log message panel' log_msg = (u'Dateshift specification must be made using format ' + '"step step_length", ex: "0 hours", "-1 hours", "-1 days" etc.\n' + 'Supported step lengths: microseconds, milliseconds, seconds, ' + 'minutes, hours, days, weeks.') if len(step_steplength) != 2: utils.MessagebarAndLog.warning(bar_msg=bar_msg, log_msg=log_msg) return Cancel() try: step = float(step_steplength[0]) steplength = step_steplength[1] except: utils.MessagebarAndLog.warning(bar_msg=bar_msg, log_msg=log_msg) return Cancel() test_shift = dateshift('2015-02-01', step, steplength) if test_shift == None: utils.MessagebarAndLog.warning(bar_msg=bar_msg, log_msg=log_msg) return Cancel() observation[u'date_time'] = dateshift(observation[u'date_time'], step, steplength) return observation
def get_settings(self): settings = ((u'parameter_list', self.parameter_list), (u'location_suffix', self.location_suffix), (u'sublocation_suffix', self.sublocation_suffix)) settings = tuple((k, v) for k, v in settings if v) return utils.returnunicode(settings, keep_containers=True)
def get_settings(self): settings = ((u'final_parameter_name', self.final_parameter_name), (u'input_type', self.input_type), (u'hint', self.hint), (u'location_suffix', self.location_suffix), (u'sublocation_suffix', self.sublocation_suffix)) settings = tuple((k, v) for k, v in settings if v) return utils.returnunicode(settings, keep_containers=True)
def save_stored_settings(ms, stored_settings, settingskey): """ Saves the current parameter settings into midvatten settings :param ms: midvattensettings :param stored_settings: a tuple like ((objname', ((attr1, value1), (attr2, value2))), (objname2, ((attr3, value3), ...) :return: stores a string like objname;attr1:value1;attr2:value2/objname2;attr3:value3... in midvatten settings """ if stored_settings is None: return stored_settings = utils.returnunicode(stored_settings, keep_containers=True) settings_list = [] for object_index, attrs in stored_settings: object_settings = [object_index] object_settings.extend([u':'.join((k, v)) for k, v in attrs if k and v]) if len(object_settings) > 1: settings_list.append(u';'.join(object_settings)) setting_string = u'/'.join(settings_list) ms.settingsdict[settingskey] = utils.returnunicode(setting_string) ms.save_settings()
def staff_list(): """ :return: A list of staff members from the staff table """ sql = "SELECT distinct staff from zz_staff" sql_result = utils.sql_load_fr_db(sql) connection_ok, result_list = sql_result if not connection_ok: textstring = """Failed to get existing staff from staff table from sql """ + sql qgis.utils.iface.messageBar().pushMessage("Error", textstring, 2, duration=10) return False, tuple() return True, utils.returnunicode(tuple([x[0] for x in result_list]), True)
def get_distinct_values(tablename, columnname): if not tablename or not columnname: return [] sql = '''SELECT distinct "%s" from "%s"'''%(columnname, tablename) connection_ok, result = utils.sql_load_fr_db(sql) if not connection_ok: textstring = u"""Cannot get data from sql """ + utils.returnunicode(sql) utils.MessagebarAndLog.critical( bar_msg=u"Error, sql failed, see log message panel", log_msg=textstring) return [] values = [col[0] for col in result] return values
def prepare_w_flow_data(observations): """ Produces a filestring with columns "obsid, instrumentid, flowtype, date_time, reading, unit, comment" and imports it :param obsdict: a dict like {obsid: {date_time: {parameter: value}}} :return: """ file_data_list = [[u'obsid', u'instrumentid', u'flowtype', u'date_time', u'reading', u'unit', u'comment']] instrumentids = utils.get_last_used_flow_instruments()[1] already_asked_instruments = {} for observation in observations: obsid = observation[u'obsid'] flowtype = observation[u'flowtype'] date_time = datetime.strftime(observation[u'date_time'], '%Y-%m-%d %H:%M:%S') unit = observation[u'unit'] instrumentid = already_asked_instruments.get(obsid, None) if instrumentid is None: instrumentids_for_obsid = instrumentids.get(obsid, None) if instrumentids_for_obsid is None: last_used_instrumentid = u'' else: last_used_instrumentid = sorted( [(_date_time, _instrumentid) for _flowtype, _instrumentid, _date_time in instrumentids[obsid] if (_flowtype == flowtype)]) if last_used_instrumentid: last_used_instrumentid = last_used_instrumentid[-1][1] else: last_used_instrumentid = u'' question = utils.NotFoundQuestion(dialogtitle=u'Submit instrument id', msg=u''.join([u'Submit the instrument id for the measurement:\n ', u', '.join([obsid, date_time, flowtype, unit])]), existing_list=[last_used_instrumentid], default_value=last_used_instrumentid, combobox_label=u'Instrument id:s in database.\nThe last used instrument id for the current obsid is prefilled:') answer = question.answer if answer == u'cancel': return Cancel() instrumentid = utils.returnunicode(question.value) already_asked_instruments[obsid] = instrumentid reading = observation[u'value'].replace(u',', u'.') comment = observation.get(u'comment', u'') file_data_list.append([obsid, instrumentid, flowtype, date_time, reading, unit, comment]) return file_data_list
def fill_list(self, combobox_var, parameter_var, parameter_list_dict): """ :param combobox_var: a QComboBox object :param parameter_var: a string parameter name :param parameter_list_dict: A dict like {u'Accvol': [(u'm3',)], u'Momflow': [(u'l/s',)]} :return: """ vals = parameter_list_dict.get(parameter_var, None) if vals is None: vals = list(sorted(set([val for vals_list in parameter_list_dict.values() for val in vals_list[0]]))) else: vals = list(vals[0]) combobox_var.clear() combobox_var.addItem(u'') combobox_var.addItems(utils.returnunicode(vals, keep_containers=True))
def ask_for_locale(self): locales = [PyQt4.QtCore.QLocale(PyQt4.QtCore.QLocale.Swedish, PyQt4.QtCore.QLocale.Sweden), PyQt4.QtCore.QLocale(PyQt4.QtCore.QLocale.English, PyQt4.QtCore.QLocale.UnitedStates)] locale_names = [localeobj.name() for localeobj in locales] locale_names.append(locale.getdefaultlocale()[0]) locale_names = list(set(locale_names)) question = utils.NotFoundQuestion(dialogtitle=u'User input needed', msg=u'Supply locale for the database.\nCurrently, only locale sv_SE has special meaning,\nall other locales will use english.', existing_list=locale_names, default_value=u'', button_names=[u'Cancel', u'Ok']) answer = question.answer submitted_value = utils.returnunicode(question.value) if answer == u'cancel': return answer elif answer == u'ok': return submitted_value
def select_file_and_parse_rows(row_parser): filenames = utils.select_files(only_one_file=False, extension="csv (*.csv)") if filenames is None or not filenames: return Cancel() observations = [] for filename in filenames: filename = utils.returnunicode(filename) with io.open(filename, 'r', encoding='utf-8') as f: #Skip header f.readline() observations.extend(row_parser(f)) #Remove duplicates observations = [dict(no_duplicate) for no_duplicate in set([tuple(possible_duplicate.items()) for possible_duplicate in observations])] return observations
def parse_rows(f): """ Parses rows from fieldlogger format into a dict :param f: File_data, often an open file or a list of rows without header :return: a list of dicts like [{date_time: x, sublocation: y, parametername: z, value: o}, ...] """ observations = [] for rownr, rawrow in enumerate(f): observation = {} row = utils.returnunicode(rawrow).rstrip(u'\r').rstrip(u'\n') if not row: continue cols = row.split(u';') observation[u'sublocation'] = cols[0] date = cols[1] time = cols[2] observation[u'date_time'] = datestring_to_date(u' '.join([date, time])) observation[u'value'] = cols[3] observation[u'parametername'] = cols[4] observations.append(observation) return observations
def combined_name(self): return utils.returnunicode(self._combined_name.text())
def distinct_unit(self): return utils.returnunicode(self._distinct_unit.currentText())
def unit_columns(self): return utils.returnunicode(self._unit_columns.currentText())
def unit_table(self): return utils.returnunicode(self._unit_table.currentText())
def distinct_parameter(self): return utils.returnunicode(self._distinct_parameter.currentText())
def parameter_columns(self): return utils.returnunicode(self._parameter_columns.currentText())
def final_parameter_name(self): return utils.returnunicode(self._final_parameter_name.text())
def sublocation_suffix(self, value): self._sublocation_suffix.setText(utils.returnunicode(value))
def sublocation_suffix(self): return utils.returnunicode(self._sublocation_suffix.text())
def hint(self, value): self._hint.setText(utils.returnunicode(value))
def hint(self): return utils.returnunicode(self._hint.text())
def final_parameter_name(self, value): self._final_parameter_name.setText(utils.returnunicode(value))
def combined_name(self, value): self._combined_name.setText(utils.returnunicode(value))
def parameter_table(self): return utils.returnunicode(self._parameter_table.currentText())
def input_type(self): return utils.returnunicode(self._input_type.currentText())
def depth(self): return utils.returnunicode(self.__depth.currentText())