1
|
import os
|
2
|
import sys
|
3
|
import glob
|
4
|
import traceback
|
5
|
|
6
|
from PyQt4.QtCore import QCoreApplication
|
7
|
from qgis.core import QgsApplication, QgsMessageLog
|
8
|
|
9
|
def load_user_expressions(path):
|
10
|
"""
|
11
|
Load all user expressions from the given paths
|
12
|
"""
|
13
|
|
14
|
modules = glob.glob(path + "/*.py")
|
15
|
names = [os.path.basename(f)[:-3] for f in modules]
|
16
|
for name in names:
|
17
|
if name == "__init__":
|
18
|
continue
|
19
|
|
20
|
|
21
|
try:
|
22
|
__import__("expressions.{0}".format(name), locals(), globals())
|
23
|
except:
|
24
|
error = traceback.format_exc()
|
25
|
msgtitle = QCoreApplication.translate("UserExpressions", "User expressions")
|
26
|
msg = QCoreApplication.translate("UserExpressions", "The user expression {0} is not valid").format(name)
|
27
|
QgsMessageLog.logMessage(msg +"\n"+ error, msgtitle, QgsMessageLog.WARNING)
|
28
|
|
29
|
|
30
|
userpythonhome = os.path.join(QgsApplication.qgisSettingsDirPath(), "python")
|
31
|
expressionspath = os.path.join(userpythonhome, "expressions")
|
32
|
startuppy = os.path.join(userpythonhome, "startup.py")
|
33
|
|
34
|
sys.path.append(userpythonhome)
|
35
|
|
36
|
|
37
|
if os.path.exists(startuppy):
|
38
|
execfile(startuppy, locals(), globals())
|
39
|
|
40
|
if not os.path.exists(expressionspath):
|
41
|
os.makedirs(expressionspath)
|
42
|
|
43
|
initfile = os.path.join(expressionspath, "__init__.py")
|
44
|
if not os.path.exists(initfile):
|
45
|
open(initfile, "w").close()
|
46
|
|
47
|
try:
|
48
|
import expressions
|
49
|
|
50
|
expressions.load = load_user_expressions
|
51
|
expressions.load(expressionspath)
|
52
|
expressions.template = """\"\"\"
|
53
|
Define new functions using @qgsfunction. feature and parent must always be the
|
54
|
last args. Use args=-1 to pass a list of values as arguments
|
55
|
\"\"\"
|
56
|
|
57
|
from qgis.core import *
|
58
|
from qgis.gui import *
|
59
|
|
60
|
@qgsfunction(args='auto', group='Custom')
|
61
|
def func(value1, feature, parent):
|
62
|
return value1
|
63
|
"""
|
64
|
except ImportError:
|
65
|
|
66
|
|
67
|
|
68
|
pass
|