Skip to content

Commit fea9e92

Browse files
Gustrym-kuhn
authored andcommittedAug 25, 2017
enable custom help in python expressions
1 parent 74306c7 commit fea9e92

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
 

‎python/core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def func(self, values, feature, parent):
7878

7979
helptemplate = string.Template("""<h3>$name function</h3><br>$doc""")
8080
name = kwargs.get('name', function.__name__)
81-
helptext = function.__doc__ or ''
81+
helptext = kwargs.get('help_text') or function.__doc__ or ''
8282
helptext = helptext.strip()
8383
expandargs = False
8484

‎tests/src/python/test_qgsexpression.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ def special(values, feature, parent):
4242
def sqrt(values, feature, parent):
4343
pass
4444

45+
@qgsfunction(1, 'testing', register=False)
46+
def help_with_docstring(values, feature, parent):
47+
"""The help comes from the python docstring."""
48+
pass
49+
50+
help_text = 'The help comes from a variable.'
51+
52+
@qgsfunction(1, 'testing', register=False, help_text=help_text)
53+
def help_with_variable(values, feature, parent):
54+
"""This docstring is not used for the help."""
55+
pass
56+
4557
@qgsfunction(1, 'testing', register=False, usesgeometry=True)
4658
def geomtest(values, feature, parent):
4759
pass
@@ -67,6 +79,17 @@ def testAutoCountsCorrectArgs(self):
6779
args = function.params()
6880
self.assertEqual(args, 3)
6981

82+
def testHelp(self):
83+
QgsExpression.registerFunction(self.help_with_variable)
84+
html = ('<h3>help_with_variable function</h3><br>'
85+
'The help comes from a variable.')
86+
self.assertEqual(self.help_with_variable.helptext(), html)
87+
88+
QgsExpression.registerFunction(self.help_with_docstring)
89+
html = ('<h3>help_with_docstring function</h3><br>'
90+
'The help comes from the python docstring.')
91+
self.assertEqual(self.help_with_docstring.helptext(), html)
92+
7093
def testAutoArgsAreExpanded(self):
7194
function = self.expandargs
7295
args = function.params()

0 commit comments

Comments
 (0)
Please sign in to comment.