-
Notifications
You must be signed in to change notification settings - Fork 597
/
mavexpression.py
69 lines (61 loc) · 2.25 KB
/
mavexpression.py
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
#!/usr/bin/env python
'''
mavlink expression evaluation functions
Copyright Andrew Tridgell 2011
Released under GNU GPL version 3 or later
'''
import os
# these imports allow for mavgraph and mavlogdump to use maths expressions more easily
from math import *
from .mavextra import *
'''
Support having a $HOME/.pymavlink/mavextra.py for extra graphing functions
'''
home = os.getenv('HOME')
if home is not None:
extra = os.path.join(home, '.pymavlink', 'mavextra.py')
if os.path.exists(extra):
try:
import imp
mavuser = imp.load_source('pymavlink.mavuser', extra)
except ModuleNotFoundError:
# "imp" is removed in Python 3.12. Try to use importlib instead:
import sys
# from: https://docs.python.org/dev/whatsnew/3.12.html#removed
import importlib.util
import importlib.machinery
def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
sys.modules[module.__name__] = module
loader.exec_module(module)
return module
load_source('pymavlink.mavuser', extra)
from pymavlink.mavuser import *
def evaluate_expression(expression, vars, nocondition=False):
'''evaluation an expression'''
# first check for conditions which take the form EXPRESSION{CONDITION}
if expression[-1] == '}':
startidx = expression.rfind('{')
if startidx == -1:
return None
condition=expression[startidx+1:-1]
expression=expression[:startidx]
try:
v = eval(condition, globals(), vars)
except Exception:
return None
if not nocondition and not v:
return None
try:
v = eval(expression, globals(), vars)
except NameError:
return None
except ZeroDivisionError:
return None
except IndexError:
return None
return v