Persisted Globals - Writing

Persisted Globals - Writing
 1def var_type(value):
 2    """VBScript VarType equivalent - returns numeric type code."""
 3    if value is None:
 4        return 1  # vbNull
 5    elif isinstance(value, bool):
 6        return 11  # vbBoolean
 7    elif isinstance(value, int):
 8        return 2  # vbInteger
 9    elif isinstance(value, float):
10        return 5  # vbDouble
11    elif isinstance(value, str):
12        return 8  # vbString
13    elif isinstance(value, (datetime.date, datetime.datetime)):
14        return 7  # vbDate
15    else:
16        return 9  # vbObject
17
18
19# This example demonstrates how to write persisted impact.globals for a specific group name
20
21ot = impact.gui.output_toolbox
22
23ot.clear()
24
25dbs = impact.active_database.settings
26
27# this will either create or update a persisted impact.globals group
28dbglobals = dbs.persisted_globals("MyCompany.MySettings")
29
30# check the user can update the ipds_ddb.dbs
31if dbs.can_save:
32
33    # check the impact.globals group can be modified
34    if dbglobals.read_only:
35        impact.gui.show_message(ipShowMessageType.smtError, "The impact.globals are read only", None)
36
37    else:
38
39        # add/change/delete/clear impact.globals as required
40        dbglobals.add("Connection", "ODBCDIRECT")
41        dbglobals.add("MaxRecords", 1000)
42        dbglobals.add("Distance", 25.4)
43        dbglobals.add("Checked", True)
44        dbglobals.add("DateTime", datetime.datetime.now()())
45
46        dbglobals.item("Distance").value = 200.0001
47
48        ot.add("Count=" + str(dbglobals.count))
49
50        for dbglobal in dbglobals:
51            ot.add(dbglobal.name + " (" + str(var_type(dbglobal.value)) + ")" + "=" + str(dbglobal.value))
52
53        # it is VERY important that you save any changes you make otherwise they may be lost when the user disconnects
54        if dbs.save():
55            ot.add("Successfully saved database settings")
56else:
57    impact.gui.show_message(ipShowMessageType.smtError, "Connected User does not have permission to save database settings", None)