File: validitycheck.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (92 lines) | stat: -rw-r--r-- 3,085 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
***************************************************************************
    validitycheck.py
    ---------------------
    Date                 : January 2019
    Copyright            : (C) 2019 by Nyall Dawson
    Email                : nyall dot dawson at gmail dot com
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************
"""

from qgis._core import QgsAbstractValidityCheck, QgsApplication


class CheckFactory:
    """
    Constructs QgsAbstractValidityChecks using a decorator.

    To use, Python based checks should use the decorator syntax:

    .. highlight:: python
    .. code-block:: python
        @check.register(type=QgsAbstractValidityCheck.TypeLayoutCheck)
        def my_layout_check(context, feedback):
            results = ...
            return results

    """

    def __init__(self):
        # unfortunately /Transfer/ annotation isn't working correct on validityCheckRegistry().addCheck(),
        # so we manually need to store a reference to all checks we register
        self.checks = []

    def register(self, type, *args, **kwargs):
        """
        Implements a decorator for registering Python based checks.

        :param type: check type, e.g. QgsAbstractValidityCheck.TypeLayoutCheck
        """

        def dec(f):
            check = CheckWrapper(check_type=type, check_func=f)
            self.checks.append(check)
            QgsApplication.validityCheckRegistry().addCheck(check)

        return dec


class CheckWrapper(QgsAbstractValidityCheck):
    """
    Wrapper object used to create new validity checks from @check.
    """

    def __init__(self, check_type, check_func):
        """
        Initializer for CheckWrapper.

        :param check_type: check type, e.g. QgsAbstractValidityCheck.TypeLayoutCheck
        :param check_func: test function, should return a list of QgsValidityCheckResult results
        """
        super().__init__()
        self._check_type = check_type
        self._results = []
        self._check_func = check_func

    def create(self):
        return CheckWrapper(check_type=self._check_type, check_func=self._check_func)

    def id(self):
        return self._check_func.__name__

    def checkType(self):
        return self._check_type

    def prepareCheck(self, context, feedback):
        self._results = self._check_func(context, feedback)
        if self._results is None:
            self._results = []
        return True

    def runCheck(self, context, feedback):
        return self._results


check = CheckFactory()