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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
"""GNUmed macro primitives.
This module implements functions a macro can legally use.
"""
#=====================================================================
# $Source: /sources/gnumed/gnumed/gnumed/client/wxpython/gmMacro.py,v $
__version__ = "$Revision: 1.26 $"
__author__ = "K.Hilbert <karsten.hilbert@gmx.net>"
import sys, time, random, types
import wxversion
import wx
from Gnumed.pycommon import gmLog, gmI18N, gmGuiBroker, gmExceptions
from Gnumed.business import gmPerson
from Gnumed.wxpython import gmGuiHelpers, gmPlugin, gmPatSearchWidgets
_log = gmLog.gmDefLog
#=====================================================================
class cMacroPrimitives:
"""Functions a macro can legally use.
An instance of this class is passed to the GNUmed scripting
listener. Hence, all actions a macro can legally take must
be defined in this class. Thus we achieve some screening for
security and also thread safety handling.
"""
#-----------------------------------------------------------------
def __init__(self, personality = None):
if personality is None:
raise gmExceptions.ConstructorError, 'must specify personality'
self.__personality = personality
self.__attached = 0
self._get_source_personality = None
self.__user_done = False
self.__user_answer = 'no answer yet'
self.__pat = gmPerson.gmCurrentPatient()
self.__auth_cookie = str(random.random())
self.__pat_lock_cookie = str(random.random())
self.__lock_after_load_cookie = str(random.random())
#-----------------------------------------------------------------
# public API
#-----------------------------------------------------------------
def attach(self, personality = None):
if self.__attached:
_log.Log(gmLog.lErr, 'attach with [%s] rejected, already serving a client' % personality)
return (0, _('attach rejected, already serving a client'))
if personality != self.__personality:
_log.Log(gmLog.lErr, 'rejecting attach to personality [%s], only servicing [%s]' % (personality, self.__personality))
return (0, _('attach to personality [%s] rejected') % personality)
self.__attached = 1
self.__auth_cookie = str(random.random())
return (1, self.__auth_cookie)
#-----------------------------------------------------------------
def detach(self, auth_cookie=None):
if not self.__attached:
return 1
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'rejecting detach() with cookie [%s]' % auth_cookie)
return 0
self.__attached = 0
return 1
#-----------------------------------------------------------------
def force_detach(self):
if not self.__attached:
return 1
self.__user_done = False
# FIXME: use self.__sync_cookie for syncing with user interaction
wx.CallAfter(self._force_detach)
return 1
#-----------------------------------------------------------------
def version(self):
return "%s $Revision: 1.26 $" % self.__class__.__name__
#-----------------------------------------------------------------
def shutdown_gnumed(self, auth_cookie=None, forced=False):
"""Shuts down this client instance."""
if not self.__attached:
return 0
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated shutdown_gnumed()')
return 0
wx.CallAfter(self._shutdown_gnumed, forced)
return 1
#-----------------------------------------------------------------
def raise_gnumed(self, auth_cookie = None):
"""Raise ourselves to the top of the desktop."""
if not self.__attached:
return 0
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated raise_gnumed()')
return 0
return "cMacroPrimitives.raise_gnumed() not implemented"
#-----------------------------------------------------------------
def get_loaded_plugins(self, auth_cookie = None):
if not self.__attached:
return 0
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated get_loaded_plugins()')
return 0
gb = gmGuiBroker.GuiBroker()
return gb['horstspace.notebook.gui'].keys()
#-----------------------------------------------------------------
def raise_notebook_plugin(self, auth_cookie = None, a_plugin = None):
"""Raise a notebook plugin within GNUmed."""
if not self.__attached:
return 0
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated raise_notebook_plugin()')
return 0
# FIXME: use semaphore
wx.CallAfter(gmPlugin.raise_notebook_plugin, a_plugin)
return 1
#-----------------------------------------------------------------
def load_patient_from_external_source(self, auth_cookie = None):
"""Load external patient, perhaps create it.
Callers must use get_user_answer() to find get status information.
It is unsafe to proceed without knowing the completion state as
the controlled client may be waiting for user input from a
patient selection list.
"""
if not self.__attached:
return (0, _('request rejected, you are not attach()ed'))
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated load_patient_from_external_source()')
return (0, _('rejected load_patient_from_external_source(), not authenticated'))
if self.__pat.is_locked():
_log.Log(gmLog.lErr, 'patient is locked, cannot load from external source')
return (0, _('current patient is locked'))
self.__user_done = False
wx.CallAfter(self._load_patient_from_external_source)
self.__lock_after_load_cookie = str(random.random())
return (1, self.__lock_after_load_cookie)
#-----------------------------------------------------------------
def lock_loaded_patient(self, auth_cookie = None, lock_after_load_cookie = None):
if not self.__attached:
return (0, _('request rejected, you are not attach()ed'))
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated lock_load_patient()')
return (0, _('rejected lock_load_patient(), not authenticated'))
# FIXME: ask user what to do about wrong cookie
if lock_after_load_cookie != self.__lock_after_load_cookie:
_log.Log(gmLog.lWarn, 'patient lock-after-load request rejected due to wrong cookie [%s]' % lock_after_load_cookie)
return (0, 'patient lock-after-load request rejected, wrong cookie provided')
self.__pat.lock()
self.__pat_lock_cookie = str(random.random())
return (1, self.__pat_lock_cookie)
#-----------------------------------------------------------------
def lock_into_patient(self, auth_cookie = None, search_params = None):
if not self.__attached:
return (0, _('request rejected, you are not attach()ed'))
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated lock_into_patient()')
return (0, _('rejected lock_into_patient(), not authenticated'))
if self.__pat.is_locked():
_log.Log(gmLog.lErr, 'patient is already locked')
return (0, _('already locked into a patient'))
searcher = gmPerson.cPatientSearcher_SQL()
if type(search_params) == types.DictType:
idents = searcher.get_identities(search_dict=search_params)
else:
idents = searcher.get_identities(search_term=search_params)
if idents is None:
return (0, _('error searching for patient with [%s]/%s') % (search_term, search_dict))
if len(idents) == 0:
return (0, _('no patient found for [%s]/%s') % (search_term, search_dict))
# FIXME: let user select patient
if len(idents) > 1:
return (0, _('several matching patients found for [%s]/%s') % (search_term, search_dict))
if not gmPerson.set_active_patient(patient = idents[0]):
return (0, _('cannot activate patient [%s] (%s/%s)') % (str(idents[0]), search_term, search_dict))
self.__pat.lock()
self.__pat_lock_cookie = str(random.random())
return (1, self.__pat_lock_cookie)
#-----------------------------------------------------------------
def unlock_patient(self, auth_cookie = None, unlock_cookie = None):
if not self.__attached:
return (0, _('request rejected, you are not attach()ed'))
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated unlock_patient()')
return (0, _('rejected unlock_patient, not authenticated'))
# we ain't locked anyways, so succeed
if not self.__pat.is_locked():
return (1, '')
# FIXME: ask user what to do about wrong cookie
if unlock_cookie != self.__pat_lock_cookie:
_log.Log(gmLog.lWarn, 'patient unlock request rejected due to wrong cookie [%s]' % unlock_cookie)
return (0, 'patient unlock request rejected, wrong cookie provided')
self.__pat.unlock()
return (1, '')
#-----------------------------------------------------------------
def assume_staff_identity(self, auth_cookie = None, staff_name = "Dr.Jekyll", staff_creds = None):
if not self.__attached:
return 0
if auth_cookie != self.__auth_cookie:
_log.Log(gmLog.lErr, 'non-authenticated select_identity()')
return 0
return "cMacroPrimitives.assume_staff_identity() not implemented"
#-----------------------------------------------------------------
def get_user_answer(self):
if not self.__user_done:
return (0, 'still waiting')
self.__user_done = False
return (1, self.__user_answer)
#-----------------------------------------------------------------
# internal API
#-----------------------------------------------------------------
def _force_detach(self):
msg = _(
'Someone tries to forcibly break the existing\n'
'controlling connection. This may or may not\n'
'have legitimate reasons.\n\n'
'Do you want to allow breaking the connection ?'
)
can_break_conn = gmGuiHelpers.gm_show_question (
aMessage = msg,
aTitle = _('forced detach attempt')
)
if can_break_conn:
self.__user_answer = 1
else:
self.__user_answer = 0
self.__user_done = True
if can_break_conn:
self.__pat.unlock()
self.__attached = 0
return 1
#-----------------------------------------------------------------
def _shutdown_gnumed(self, forced=False):
top_win = wx.GetApp().GetTopWindow()
if forced:
top_win.Destroy()
else:
top_win.Close()
#-----------------------------------------------------------------
def _load_patient_from_external_source(self):
if gmPatSearchWidgets.load_patient_from_external_sources():
self.__user_answer = 1
else:
self.__user_answer = 0
self.__user_done = True
return 1
#=====================================================================
# main
#=====================================================================
if __name__ == '__main__':
_log.SetAllLogLevels(gmLog.lData)
gmI18N.activate_locale()
gmI18N.install_domain()
from Gnumed.pycommon import gmScriptingListener
import xmlrpclib
listener = gmScriptingListener.cScriptingListener(macro_executor = cMacroPrimitives(personality='unit test'), port=9999)
s = xmlrpclib.ServerProxy('http://localhost:9999')
print "should fail:", s.attach()
print "should fail:", s.attach('wrong cookie')
print "should work:", s.version()
print "should fail:", s.raise_gnumed()
print "should fail:", s.raise_notebook_plugin('test plugin')
print "should fail:", s.lock_into_patient('kirk, james')
print "should fail:", s.unlock_patient()
status, conn_auth = s.attach('unit test')
print "should work:", status, conn_auth
print "should work:", s.version()
print "should work:", s.raise_gnumed(conn_auth)
status, pat_auth = s.lock_into_patient(conn_auth, 'kirk, james')
print "should work:", status, pat_auth
print "should fail:", s.unlock_patient(conn_auth, 'bogus patient unlock cookie')
print "should work", s.unlock_patient(conn_auth, pat_auth)
data = {'firstname': 'jame', 'lastnames': 'Kirk', 'gender': 'm'}
status, pat_auth = s.lock_into_patient(conn_auth, data)
print "should work:", status, pat_auth
print "should work", s.unlock_patient(conn_auth, pat_auth)
print s.detach('bogus detach cookie')
print s.detach(conn_auth)
del s
listener.tell_thread_to_stop()
#=====================================================================
# $Log: gmMacro.py,v $
# Revision 1.26 2006/07/22 12:15:08 ncq
# - add missing import
#
# Revision 1.25 2006/07/22 10:04:51 ncq
# - cleanup
# - pre-init all attributes so connectors won't kill the GNUmed slave
# with stupid AttributeExceptions
# - add lock_loaded_patient()
#
# Revision 1.24 2006/07/21 14:47:19 ncq
# - cleanup
# - add (_)load_patient_from_external_source()
# - improve testing
#
# Revision 1.23 2006/05/04 09:49:20 ncq
# - get_clinical_record() -> get_emr()
# - adjust to changes in set_active_patient()
# - need explicit set_active_patient() after ask_for_patient() if wanted
#
# Revision 1.22 2005/11/28 23:07:34 ncq
# - add shutdown_gnumed()
#
# Revision 1.21 2005/11/27 22:08:38 ncq
# - patient searcher has somewhat changed so adapt
#
# Revision 1.20 2005/11/27 20:38:10 ncq
# - properly import wx
#
# Revision 1.19 2005/09/28 21:27:30 ncq
# - a lot of wx2.6-ification
#
# Revision 1.18 2005/09/28 15:57:48 ncq
# - a whole bunch of wx.Foo -> wx.Foo
#
# Revision 1.17 2005/09/27 20:44:59 ncq
# - wx.wx* -> wx.*
#
# Revision 1.16 2005/01/31 10:37:26 ncq
# - gmPatient.py -> gmPerson.py
#
# Revision 1.15 2004/09/13 09:38:29 ncq
# - allow to wait for user interaction in controlled GnuMed instance
# despite having to use wxCallAfter by waiting on a semaphore
#
# Revision 1.14 2004/07/24 17:13:25 ncq
# - main.plugins.gui now horstspace.notebook.gui
#
# Revision 1.13 2004/06/25 13:28:00 ncq
# - logically separate notebook and clinical window plugins completely
#
# Revision 1.12 2004/06/01 07:59:55 ncq
# - comments improved
#
# Revision 1.11 2004/03/20 19:48:07 ncq
# - adapt to flat id list from get_patient_ids
#
# Revision 1.10 2004/03/20 17:54:18 ncq
# - lock_into_patient now supports dicts and strings
# - fix unit test
#
# Revision 1.9 2004/03/12 13:22:38 ncq
# - comment on semaphore for GUI actions
#
# Revision 1.8 2004/03/05 11:22:35 ncq
# - import from Gnumed.<pkg>
#
# Revision 1.7 2004/02/25 09:46:22 ncq
# - import from pycommon now, not python-common
#
# Revision 1.6 2004/02/17 10:45:30 ncq
# - return authentication cookie from attach()
# - use that cookie in all RPCs
# - add assume_staff_identity()
#
# Revision 1.5 2004/02/12 23:57:22 ncq
# - now also use random cookie for attach/detach
# - add force_detach() with user feedback
# - add get_loaded_plugins()
# - implement raise_plugin()
#
# Revision 1.4 2004/02/05 23:52:05 ncq
# - remove spurious return 0
#
# Revision 1.3 2004/02/05 20:46:18 ncq
# - require attach() cookie for detach(), too
#
# Revision 1.2 2004/02/05 20:40:34 ncq
# - added attach()
# - only allow attach()ed clients to call methods
# - introduce patient locking/unlocking cookie
# - enhance unit test
#
# Revision 1.1 2004/02/05 18:10:44 ncq
# - actually minimally functional macro executor with test code
#
|