File: gmGuiBroker.py

package info (click to toggle)
gnumed-server 21.11-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 45,408 kB
  • ctags: 3,891
  • sloc: sql: 1,210,640; python: 13,526; sh: 1,476; makefile: 19
file content (168 lines) | stat: -rw-r--r-- 4,366 bytes parent folder | download | duplicates (4)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

from __future__ import print_function

__doc__ = """GNUmed GUI element brokerage

This module provides wrappers for the equivalent of global
variables needed for a gnumed GUI client interface

@author: Dr. Horst Herb
@version: 0.2
@copyright: GPL v2 or later
"""

__author__ = "H.Herb <hherb@gnumed.net>, H.Berger <Hilmar.Berger@gmx.de>"
#===========================================================
if __name__ == '__main__':
	_ = lambda x:x

# FIXME !!! hack moved here from gmConf. This definitely must be replaced by some 
# structure getting data from the backend
# FIXME: hardcoded color/width !?! move to DB (?)
config = {'main.use_notebook':1, 'main.shadow.colour':(131, 129, 131), 'main.shadow.width':10}

#===========================================================
class GuiBroker:
	"Wrapper for global objects needed by GNUMmed GUI clients"

	#This class wraps all global gui objects (variables)for a gnumed
	#application. The static (at application level)dictionary
	#__objects can be accessed through the method addobject
	#and getobject.
	#So, if you need to access the main window frame, you would
	#query an instance of GuiBroker for it.

	__objects = {}
	__keycounter=0


	def __init__(self):
		pass


	def addobject(self, widget, key=None):
		"Add an object to the gnumed gui object dictionary"

		#An object can be anything (class, variable, widget)
		#The "key" is a key expression (number, text) that
		#allows you to retrieve the object.
		#Convention for keys is the widget or variable name
		#as a text string
		#If key is not passed as parameter, a unique serial
		#number is allocated as key and returned

		if not key:
		# create a new sequential key that doesn't exist yet
			key = GuiBroker.__keycounter + 1
			while key in GuiBroker.__objects:
				key +=1
		GuiBroker.__keycounter = key
		GuiBroker.__objects[key]=widget
		return key



	def getobject(self, key):
		"allows to retrieve a gnumed gui element; see addobject() regarding the key parameter"
		return GuiBroker.__objects[key]

	def has_key( self, key):
		return key in GuiBroker.__objects



	def keylist(self):
		" returns a list of all keys; see documentation for the dictionary data type"
		return GuiBroker.__objects.keys()



	def valuelist(self):
		"returns a list of all values; see documentation for the dictionary data type"
		return GuiBroker.__objects.values()



	def itemlist(self):
		"returns a list of all key:value pairs; see documentation for the dictionary data type"
		return GuiBroker.__objects.items()



	def __getitem__(self, key):
		"Allows retrieving the value via value = instance[key]"
		return self.getobject(key)



	def __setitem__(self, key, object):
		"Allows access in the style of instance[key]=value"
		return self.addobject(object, key)

#===========================================================
if __name__ == "__main__":

	import sys

	if len(sys.argv) < 2:
		sys.exit()

	if sys.argv[1] != u'test':
		sys.exit()

	# you can test this module by invoking it as main program
	print('>>> gmGuiBroker.GuiBroker test')
	test = GuiBroker()

	print('>>> test.addobject("something", 3)')
	var = test.addobject("something", 3)
	print(var, "\n")

	print('>>> test.addobject("something else without a specified key")')
	var = test.addobject("something else without a specified key")
	print(var, "\n")

	print('>>> test.addobject(test)')
	testreference = test.addobject(test)
	print(testreference, "\n")

	print('>>> test.addobject(100, "hundred)')
	var = test.addobject(100, "hundred")
	print(var, "\n")

	print(">>> test.keylist()")
	var = test.keylist()
	print(var, "\n")

	print(">>> test.valuelist()")
	var = test.valuelist()
	print(var, "\n")

	print(">>> test.itemlist()")
	var = test.itemlist()
	print(var, "\n")

	print(">>> test[3]")
	var = test[3]
	print(var, "\n")

	print(">>> test[testreference].getobject('hundred')")
	var = test[testreference].getobject('hundred')
	print(var, "\n")

	print(">>> var = test[testreference]")
	var = test[testreference]
	print(var, "\n")

	print(">>> var = var['hundred']")
	var = var['hundred']
	print(var, "\n")

	print('>>> try: test.addobject["duplicate key", 3]')
	print('>>> except KeyError: print("Duplicate keys not allowed!"')
	try: test["duplicate key", 3]
	except KeyError: print("Duplicate keys not allowed!")

	print(">>> test['key']='value'")
	test['key']='value'
	print(test['key'])