Exemplo n.º 1
0
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
Exemplo n.º 2
0
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)
Exemplo n.º 3
0
    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))
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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()
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
    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
Exemplo n.º 10
0
    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
Exemplo n.º 11
0
    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))
Exemplo n.º 12
0
 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
Exemplo n.º 13
0
    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
Exemplo n.º 14
0
    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
Exemplo n.º 15
0
 def combined_name(self):
     return utils.returnunicode(self._combined_name.text())
Exemplo n.º 16
0
 def distinct_unit(self):
     return utils.returnunicode(self._distinct_unit.currentText())
Exemplo n.º 17
0
 def unit_columns(self):
     return utils.returnunicode(self._unit_columns.currentText())
Exemplo n.º 18
0
 def unit_table(self):
     return utils.returnunicode(self._unit_table.currentText())
Exemplo n.º 19
0
 def distinct_parameter(self):
     return utils.returnunicode(self._distinct_parameter.currentText())
Exemplo n.º 20
0
 def parameter_columns(self):
     return utils.returnunicode(self._parameter_columns.currentText())
Exemplo n.º 21
0
 def final_parameter_name(self):
     return utils.returnunicode(self._final_parameter_name.text())
Exemplo n.º 22
0
 def sublocation_suffix(self, value):
     self._sublocation_suffix.setText(utils.returnunicode(value))
Exemplo n.º 23
0
 def sublocation_suffix(self):
     return utils.returnunicode(self._sublocation_suffix.text())
Exemplo n.º 24
0
 def hint(self, value):
     self._hint.setText(utils.returnunicode(value))
Exemplo n.º 25
0
 def hint(self):
     return utils.returnunicode(self._hint.text())
Exemplo n.º 26
0
 def final_parameter_name(self, value):
     self._final_parameter_name.setText(utils.returnunicode(value))
Exemplo n.º 27
0
 def combined_name(self, value):
     self._combined_name.setText(utils.returnunicode(value))
Exemplo n.º 28
0
 def parameter_table(self):
     return utils.returnunicode(self._parameter_table.currentText())
Exemplo n.º 29
0
 def input_type(self):
     return utils.returnunicode(self._input_type.currentText())
Exemplo n.º 30
0
 def depth(self):
     return utils.returnunicode(self.__depth.currentText())