Package Gnumed :: Package pycommon :: Module gmGuiBroker
[frames] | no frames]

Source Code for Module Gnumed.pycommon.gmGuiBroker

  1   
  2   
  3   
  4  __doc__ = """GNUmed GUI element brokerage 
  5   
  6  This module provides wrappers for the equivalent of global 
  7  variables needed for a gnumed GUI client interface 
  8   
  9  @author: Dr. Horst Herb 
 10  @version: 0.2 
 11  @copyright: GPL v2 or later 
 12  """ 
 13   
 14  __author__ = "H.Herb <hherb@gnumed.net>, H.Berger <Hilmar.Berger@gmx.de>" 
 15  #=========================================================== 
 16  if __name__ == '__main__': 
 17          _ = lambda x:x 
 18   
 19  # FIXME !!! hack moved here from gmConf. This definitely must be replaced by some  
 20  # structure getting data from the backend 
 21  # FIXME: hardcoded color/width !?! move to DB (?) 
 22  config = {'main.use_notebook':1, 'main.shadow.colour':(131, 129, 131), 'main.shadow.width':10} 
 23   
 24  #=========================================================== 
25 -class GuiBroker:
26 "Wrapper for global objects needed by GNUMmed GUI clients" 27 28 #This class wraps all global gui objects (variables)for a gnumed 29 #application. The static (at application level)dictionary 30 #__objects can be accessed through the method addobject 31 #and getobject. 32 #So, if you need to access the main window frame, you would 33 #query an instance of GuiBroker for it. 34 35 __objects = {} 36 __keycounter=0 37 38
39 - def __init__(self):
40 pass
41 42
43 - def addobject(self, widget, key=None):
44 "Add an object to the gnumed gui object dictionary" 45 46 #An object can be anything (class, variable, widget) 47 #The "key" is a key expression (number, text) that 48 #allows you to retrieve the object. 49 #Convention for keys is the widget or variable name 50 #as a text string 51 #If key is not passed as parameter, a unique serial 52 #number is allocated as key and returned 53 54 if not key: 55 # create a new sequential key that doesn't exist yet 56 key = GuiBroker.__keycounter + 1 57 while key in GuiBroker.__objects: 58 key +=1 59 GuiBroker.__keycounter = key 60 GuiBroker.__objects[key]=widget 61 return key
62 63 64
65 - def getobject(self, key):
66 "allows to retrieve a gnumed gui element; see addobject() regarding the key parameter" 67 return GuiBroker.__objects[key]
68
69 - def has_key( self, key):
70 return key in GuiBroker.__objects
71 72 73
74 - def keylist(self):
75 " returns a list of all keys; see documentation for the dictionary data type" 76 return GuiBroker.__objects.keys()
77 78 79
80 - def valuelist(self):
81 "returns a list of all values; see documentation for the dictionary data type" 82 return GuiBroker.__objects.values()
83 84 85
86 - def itemlist(self):
87 "returns a list of all key:value pairs; see documentation for the dictionary data type" 88 return GuiBroker.__objects.items()
89 90 91
92 - def __getitem__(self, key):
93 "Allows retrieving the value via value = instance[key]" 94 return self.getobject(key)
95 96 97
98 - def __setitem__(self, key, object):
99 "Allows access in the style of instance[key]=value" 100 return self.addobject(object, key)
101 102 #=========================================================== 103 if __name__ == "__main__": 104 105 import sys 106 107 if len(sys.argv) < 2: 108 sys.exit() 109 110 if sys.argv[1] != 'test': 111 sys.exit() 112 113 # you can test this module by invoking it as main program 114 print('>>> gmGuiBroker.GuiBroker test') 115 test = GuiBroker() 116 117 print('>>> test.addobject("something", 3)') 118 var = test.addobject("something", 3) 119 print(var, "\n") 120 121 print('>>> test.addobject("something else without a specified key")') 122 var = test.addobject("something else without a specified key") 123 print(var, "\n") 124 125 print('>>> test.addobject(test)') 126 testreference = test.addobject(test) 127 print(testreference, "\n") 128 129 print('>>> test.addobject(100, "hundred)') 130 var = test.addobject(100, "hundred") 131 print(var, "\n") 132 133 print(">>> test.keylist()") 134 var = test.keylist() 135 print(var, "\n") 136 137 print(">>> test.valuelist()") 138 var = test.valuelist() 139 print(var, "\n") 140 141 print(">>> test.itemlist()") 142 var = test.itemlist() 143 print(var, "\n") 144 145 print(">>> test[3]") 146 var = test[3] 147 print(var, "\n") 148 149 print(">>> test[testreference].getobject('hundred')") 150 var = test[testreference].getobject('hundred') 151 print(var, "\n") 152 153 print(">>> var = test[testreference]") 154 var = test[testreference] 155 print(var, "\n") 156 157 print(">>> var = var['hundred']") 158 var = var['hundred'] 159 print(var, "\n") 160 161 print('>>> try: test.addobject["duplicate key", 3]') 162 print('>>> except KeyError: print("Duplicate keys not allowed!"') 163 try: test["duplicate key", 3] 164 except KeyError: print("Duplicate keys not allowed!") 165 166 print(">>> test['key']='value'") 167 test['key']='value' 168 print(test['key']) 169