|
| 1 | +import string |
1 | 2 | from qgis._core import *
|
| 3 | + |
| 4 | + |
| 5 | +def register_function(function, arg_count, group, usesgeometry=False, **kwargs): |
| 6 | + """ |
| 7 | + Register a Python function to be used as a expression function. |
| 8 | +
|
| 9 | + Functions should take (values, feature, parent) as args: |
| 10 | +
|
| 11 | + Example: |
| 12 | + def myfunc(values, feature, parent): |
| 13 | + pass |
| 14 | +
|
| 15 | + They can also shortcut naming feature and parent args by using *args |
| 16 | + if they are not needed in the function. |
| 17 | +
|
| 18 | + Example: |
| 19 | + def myfunc(values, *args): |
| 20 | + pass |
| 21 | +
|
| 22 | + Functions should return a value compatible with QVariant |
| 23 | +
|
| 24 | + Eval errors can be raised using parent.setEvalErrorString("Error message") |
| 25 | +
|
| 26 | + :param function: |
| 27 | + :param arg_count: |
| 28 | + :param group: |
| 29 | + :param usesgeometry: |
| 30 | + :return: |
| 31 | + """ |
| 32 | + class QgsExpressionFunction(QgsExpression.Function): |
| 33 | + def __init__(self, func, name, args, group, helptext='', usesgeometry=False): |
| 34 | + QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry) |
| 35 | + self.function = func |
| 36 | + |
| 37 | + def func(self, values, feature, parent): |
| 38 | + try: |
| 39 | + return self.function(values, feature, parent) |
| 40 | + except Exception as ex: |
| 41 | + parent.setEvalErrorString(ex.message) |
| 42 | + |
| 43 | + helptemplate = string.Template("""<h3>$name function</h3><br>$doc""") |
| 44 | + name = kwargs.get('name', function.__name__) |
| 45 | + helptext = function.__doc__ or '' |
| 46 | + helptext = helptext.strip() |
| 47 | + if arg_count == 0 and not name[0] == '$': |
| 48 | + name = '${0}'.format(name) |
| 49 | + |
| 50 | + if QgsExpression.isFunctionName(name): |
| 51 | + if not QgsExpression.unregisterFunction(name): |
| 52 | + raise TypeError("Unable to unregister function") |
| 53 | + |
| 54 | + function.__name__ = name |
| 55 | + helptext = helptemplate.safe_substitute(name=name, doc=helptext) |
| 56 | + f = QgsExpressionFunction(function, name, arg_count, group, helptext, usesgeometry) |
| 57 | + |
| 58 | + # This doesn't really make any sense here but does when used from a decorator context |
| 59 | + # so it can stay. |
| 60 | + register = kwargs.get('register', True) |
| 61 | + if register: |
| 62 | + QgsExpression.registerFunction(f) |
| 63 | + return f |
| 64 | + |
| 65 | + |
| 66 | +def qgsfunction(args, group, **kwargs): |
| 67 | + """ |
| 68 | + Decorator function used to define a user expression function. |
| 69 | +
|
| 70 | + Example: |
| 71 | + @qgsfunction(2, 'test'): |
| 72 | + def add(values, feature, parent): |
| 73 | + pass |
| 74 | +
|
| 75 | + Will create and register a function in QgsExpression called 'add' in the |
| 76 | + 'test' group that takes two arguments. |
| 77 | +
|
| 78 | + or not using feature and parent: |
| 79 | +
|
| 80 | + Example: |
| 81 | + @qgsfunction(2, 'test'): |
| 82 | + def add(values, *args): |
| 83 | + pass |
| 84 | + """ |
| 85 | + |
| 86 | + def wrapper(func): |
| 87 | + usesgeometry = kwargs.get('usesgeometry', False) |
| 88 | + return register_function(func, args, group, usesgeometry, **kwargs) |
| 89 | + return wrapper |
0 commit comments