Persisted Globals - Writing

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