-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_calculation_expressions.py
62 lines (48 loc) · 2.48 KB
/
search_calculation_expressions.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
import helpers
from volue.mesh import Connection, OwnershipRelationAttribute, TimeseriesAttribute
keyword = "Income"
local_expressions = {}
template_expressions = {}
def search_calculation_expressions(session: Connection.Session, target):
"""
Traverses the Mesh model recursively and finds all time series
attributes that have calculation expressions containing specific string.
"""
object = session.get_object(target, full_attribute_info=True)
for attr in object.attributes.values():
if isinstance(attr, OwnershipRelationAttribute):
for child_id in attr.target_object_ids:
search_calculation_expressions(session, child_id)
elif isinstance(attr, TimeseriesAttribute):
if keyword in attr.expression:
# Local expression is set on attribute level.
# Template expression is set on attribute definition level.
# By default all attributes inherit template expression from
# their attribute definition. If calculation expression is
# changed explicitly for a given attribute, then it is called
# a local expression.
if attr.is_local_expression:
local_expressions[attr.path] = attr.expression
else:
template_expressions[attr.definition.path] = attr.expression
def main(address, tls_root_pem_cert):
# For production environments create connection using: with_tls, with_kerberos, or with_external_access_token, e.g.:
# connection = Connection.with_tls(address, tls_root_pem_cert)
connection = Connection.insecure(address)
with connection.create_session() as session:
models = session.list_models()
for model in models:
print(f"\nModel: '{model.name}'")
search_calculation_expressions(session, model.id)
for path, expression in template_expressions.items():
print(
f"Attribute definition path: {path} has template expression:\n{expression}"
)
for path, expression in local_expressions.items():
print(f"Attribute path: {path} has local expression:\n{expression}")
# clear search results before traversing the next model
local_expressions.clear()
template_expressions.clear()
if __name__ == "__main__":
address, tls_root_pem_cert = helpers.get_connection_info()
main(address, tls_root_pem_cert)