Exemplo n.º 1
0
 def test_yaml_load_file(self):
     file_path = os.path.join(get_modules_dir(), 'veriso_ee', 'module.yml')
     expected = {
         u'displayname': u'VeriSO (EE/EN)',
         u'ilimodels': [{
             u'epsg': u'21781',
             u'ilimodel': u'DM01AVCH24D',
             u'referenceframe': u'LV03'
         },
             {
                 u'epsg': u'2056',
                 u'ilimodel': u'DM01AVCH24LV95D',
                 u'referenceframe': u'LV95'
             },
             {
                 u'epsg': u'21781',
                 u'ilimodel': u'DM01AVBE11D',
                 u'referenceframe': u'LV03'
             },
             {
                 u'epsg': u'2056',
                 u'ilimodel': u'DM01AVBE11LV95D',
                 u'referenceframe': u'LV95'
             }]
     }
     loaded_dict = yaml_load_file(file_path)
     loaded_dict = json.loads(json.dumps(loaded_dict))
     self.assertDictEqual(loaded_dict, expected)
    def do_show_complex_check(self, folder, check):

        modules_dir = os.path.join(get_modules_dir())
        module_dir = os.path.join(modules_dir, self.module, 'checks', folder)

        # Search first in the module, if the check doesn't exist, try in the
        # extended module
        if (os.path.exists(module_dir)):
            module = "veriso.modules.%s.checks.%s.%s" % (self.module, folder,
                                                         check["file"])
        else:
            module = "veriso.modules.%s.checks.%s.%s" % (self.module_extended,
                                                         folder, check["file"])

        try:
            module = dynamic_import(module)
            c = module.ComplexCheck(self.iface)
            c.run()
        except Exception:
            exc_type, exc_value, exc_traceback = sys.exc_info()
            self.message_bar.pushMessage(
                self.module_name,
                str(traceback.format_exc(exc_traceback)),
                Qgis.Critical,
                duration=0)
            return
Exemplo n.º 3
0
def get_checks_from_files(module_name, topic_dir, modules_dir=None):
    """

    :param module_name:
    :param topic_dir:
    :param modules_dir:
    :return:
    """

    if modules_dir is None:
        modules_dir = get_modules_dir()
    path = os.path.join(modules_dir, module_name, 'checks', topic_dir)
    package = 'veriso.modules.%s.checks.%s' % (module_name, topic_dir)
    checks = []

    files = os.listdir(path)
    # alpha-numeric sorting to assure the same order in the menus
    # see http://stackoverflow.com/a/2669523/1193450
    # files = sorted(files, key=lambda item: (
    #    int(item.partition(' ')[0]) if item[0].isdigit() else float('inf'),
    #    item))
    try:
        locale = QSettings().value('locale/userLocale')[0:2]
    except TypeError:
        locale = 'de'

    for f in files:
        check = {}
        if f.endswith(".py") and f != '__init__.py':
            filename = f[:-3]
            module = '%s.%s' % (package, filename)
            try:
                module = dynamic_import(module)
                check['id'] = filename
                check['file'] = filename
                check['name'] = module.ComplexCheck.get_name()
                if check['name'] is not None:
                    # name is defined in the class
                    check['shortcut'] = module.ComplexCheck.get_shortcut()
                else:
                    # let's see if there is a yaml file as our last resort
                    yaml_path = os.path.join(path, filename + '.yml')
                    check['name'], check['shortcut'] = get_info_from_yaml(
                        yaml_path)

                # if we made it to here, it's all good
                checks.append(check)
            except:
                continue
        elif f.endswith('_separator'):
            check['name'] = 'separator'
            # checks.append(check)
    checks = sorted(checks, key=lambda k: k["name"][locale])
    return checks
Exemplo n.º 4
0
    def init_gui(self):
        """Initialize the dialog:
        Set the current date.
        Accept only lower characters as project name (= database schema).
        Fill modules combobox.
        """
        today = QDateTime.currentDateTime()
        self.dateTimeEdit.setDateTime(today)
        self.dateTimeEdit.setCalendarPopup(True)

        # You are only allowed to use lower case characters as project name (
        # = database schema).
        self.lineEditDbSchema.setValidator(
            QRegExpValidator(QRegExp("^[a-z][a-z0-9_]+"),
                             self.lineEditDbSchema))

        # Fill out the modules combobox.
        try:
            modules_dir = os.path.join(get_modules_dir())
            modules = []

            for module_name in get_subdirs(modules_dir):
                module_file = os.path.join(modules_dir, module_name,
                                           'module.yml')
                if os.path.isfile(module_file):
                    module = yaml_load_file(module_file)
                    module['dirname'] = module_name
                    modules.append(module)

            if modules:
                sorted_modules_list = sorted(modules,
                                             key=lambda k: k['displayname'])
                self.cmbBoxAppModule.clear()
                for module in sorted_modules_list:
                    self.cmbBoxAppModule.addItem(str(module["displayname"]),
                                                 module)
                self.cmbBoxAppModule.insertItem(0, "", None)
                self.cmbBoxAppModule.setCurrentIndex(0)
        except Exception as e:
            message = "Error while parsing the available modules."
            self.message_bar.pushMessage("VeriSO",
                                         tr(message),
                                         QgsMessageBar.CRITICAL,
                                         duration=0)
            QgsMessageLog.logMessage(str(e), "VeriSO", QgsMessageLog.CRITICAL)
            return

        self.cmbBoxIliModelName.insertItem(0, "", None)

        return True
Exemplo n.º 5
0
def get_baselayers(module_name):
    """Reads all baselayer definitions from a yaml file.

    Returns
      A list of dictionaries with all baselayer definitions. Otherwise
      False.
    """
    filename = os.path.join(get_modules_dir(), module_name, "baselayer",
                            "baselayer.yml")
    try:
        baselayers = yaml_load_file(filename)
        return baselayers
    except Exception as e:
        QgsMessageLog.logMessage(str(e), module_name, Qgis.Critical)
        return
    def get_extended_module_name(self):
        """
        # Read into module.yml to see if the module extends another module
        :return: the name of the extended module
        """
        modules_dir = os.path.join(get_modules_dir())
        module_file = os.path.join(modules_dir, self.module_name.lower(),
                                   'module.yml')

        if os.path.isfile(module_file):
            module_yaml = yaml_load_file(module_file)

            if 'extends' in module_yaml:
                return module_yaml['extends']

        return None
Exemplo n.º 7
0
def get_checks_from_files(module_name, topic_dir, modules_dir=None):
    """

    :param module_name:
    :param topic_dir:
    :param modules_dir:
    :return:
    """

    if modules_dir is None:
        modules_dir = get_modules_dir()
    path = os.path.join(modules_dir, module_name, 'checks', topic_dir)
    package = 'veriso.modules.%s.checks.%s' % (module_name, topic_dir)
    checks = []

    files = os.listdir(path)
    # alpha-numeric sorting to assure the same order in the menus
    # see http://stackoverflow.com/a/2669523/1193450
    files = sorted(files, key=lambda item: (
        int(item.partition(' ')[0]) if item[0].isdigit() else float('inf'),
        item))
    for f in files:
        check = {}
        if f.endswith(".py") and f != '__init__.py':
            filename = f[:-3]
            module = '%s.%s' % (package, filename)
            try:
                module = dynamic_import(module)
                check['id'] = filename
                check['file'] = filename
                check['shortcut'] = module.ComplexCheck.get_shortcut()
                check['name'] = module.ComplexCheck.get_name()
                checks.append(check)
            except:
                continue
    return checks
Exemplo n.º 8
0
 def test_get_modules_dir(self):
     self.assertIs(True, os.path.isdir(get_modules_dir()))
Exemplo n.º 9
0
def get_check_topics(module_name):
    """Get all check topics (aka groups).

    Different languages are support. See the yaml file how to deal with it.

    Returns:
      A ordered dictionary with the topic name and corresponding check
      files (python). False if something went wrong.
    """

    topics_dir = os.path.join(get_modules_dir(), module_name, 'checks')

    checks = []
    for topic_dir in get_subdirs(topics_dir):
        topic_file = os.path.join(topics_dir, topic_dir, 'topic.yml')
        if os.path.isfile(topic_file):
            try:
                topic = yaml_load_file(topic_file)
                topic['topic_dir'] = topic_dir
                checks.append(topic)
            except VerisoError:
                raise
    try:
        locale = QSettings().value('locale/userLocale')[0:2]
    except TypeError:
        locale = 'de'
    try:
        topics = OrderedDict()
        checks = sorted(checks, key=lambda k: k['topic'][locale])
        for check in checks:
            topic = check["topic"]
            topic_dir = check["topic_dir"]

            # Check, if yaml file is multilingual.
            try:
                if topic in topics:
                    continue
                topics[topic] = check
                # TODO control this whe using checks_from_files
                # yaml is *not* multilingual.
            except:
                # yaml is multilingual.
                # If the language set in QGIS is not available in the
                # yaml file, the first language will be chosen

                # dinamically get the checks based on the available files
                checks_from_files = get_checks_from_files(
                    module_name, topic_dir)
                try:
                    my_topic = topic[locale]
                    my_check = OrderedDict()
                    my_check["topic"] = my_topic
                    # my_check["checks"] = check["checks"]
                    my_check["checks"] = checks_from_files
                    my_check["topic_dir"] = topic_dir
                    topics[my_topic] = my_check
                    # language found

                except:
                    # language *not* found
                    my_check = OrderedDict()
                    my_check["topic"] = list(topic.values())[0]
                    # my_check["checks"] = check["checks"]
                    my_check["checks"] = checks_from_files
                    my_check["topic_dir"] = topic_dir
                    topics[my_check["topic"]] = my_check
        return topics
    except Exception as e:
        print(str(e))
        raise VerisoError(str(e), e, tag=module_name)