Skip to content

Commit d8fa85e

Browse files
Gustrym-kuhn
authored andcommittedSep 6, 2017
[backport on 2.14] enable custom help in python expressions
1 parent 9117137 commit d8fa85e

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed
 

‎.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ matrix:
1010
- BUILD=qt4
1111
- QT_VERSION=4
1212
# - LLVM_VERSION=3.8
13+
dist: precise
1314
sudo: false
1415
cache:
1516
apt: true

‎python/core/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def func(self, values, feature, parent):
8383

8484
helptemplate = string.Template("""<h3>$name function</h3><br>$doc""")
8585
name = kwargs.get('name', function.__name__)
86-
helptext = function.__doc__ or ''
86+
helptext = kwargs.get('help_text') or function.__doc__ or ''
8787
helptext = helptext.strip()
8888
expandargs = False
8989

@@ -138,6 +138,7 @@ def wrapper(func):
138138
return register_function(func, args, group, **kwargs)
139139
return wrapper
140140

141+
141142
try:
142143
# Add a __nonzero__ method onto QPyNullVariant so we can check for null values easier.
143144
# >>> value = QPyNullVariant("int")

‎tests/src/python/test_qgsexpression.py

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

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

81+
def testHelp(self):
82+
QgsExpression.registerFunction(self.help_with_variable)
83+
html = ('<h3>help_with_variable function</h3><br>'
84+
'The help comes from a variable.')
85+
self.assertEqual(self.help_with_variable.helptext(), html)
86+
87+
QgsExpression.registerFunction(self.help_with_docstring)
88+
html = ('<h3>help_with_docstring function</h3><br>'
89+
'The help comes from the python docstring.')
90+
self.assertEqual(self.help_with_docstring.helptext(), html)
91+
6992
def testAutoArgsAreExpanded(self):
7093
function = self.expandargs
7194
args = function.params()
@@ -186,5 +209,6 @@ def testComment(self):
186209
result = exp.evaluate()
187210
self.assertEqual(exp_res, result)
188211

212+
189213
if __name__ == "__main__":
190214
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.