Package Gnumed :: Package business :: Module gmArriba |
[frames] | no frames]

# Source Code for Module Gnumed.business.gmArriba

      1  """GNUmed <-> arriba coupling.
      2
      3  The API definition was provided by Thomas Scheithauer
      4  <thomas.scheithauer@gmx.net>.
      5
      6  The GNUmed project is NOT authorized to share the actual
      7  interface specs so you need to get in contact with Thomas in
      8  order to re-implement the interface.
      9
     10  http://www.arriba-hausarzt.de
     11
     12  Note that that official casing is "arriba" rather
     13  than "ARRIBA" or "Arriba".
     14  """
     15  #============================================================
     16  __license__ = "GPL"
     17  __author__ = "K.Hilbert <Karsten.Hilbert@gmx.net>"
     18
     19
     20  # std lib
     21  import os
     22  import sys
     23  import codecs
     24  import subprocess
     25  import logging
     26
     27
     28  # GNUmed libraries
     29  if __name__ == '__main__':
     30          sys.path.insert(0, '../../')
     31
     32  from Gnumed.pycommon import gmTools
     33  from Gnumed.pycommon import gmShellAPI
     34  from Gnumed.pycommon import gmDispatcher
     35
     36  from Gnumed.business import gmXdtMappings
     37
     38
     39  _log = logging.getLogger('gm.arriba')
     40  #============================================================
     41 -class cArriba(object):
     42
     43          _date_format = '%d%m%Y'
     44
     45 -        def __init__(self):
     46                  self.path_to_binary = None
     47                  self.pdf_result = None
     48                  self.xml_result = None
     49          #--------------------------------------------------------
     50          # internal helpers
     51          #--------------------------------------------------------
     52 -        def __detect_binary(self):
     53
     54                  if self.path_to_binary is not None:
     55                          return True
     56
     57                  found, cmd = gmShellAPI.find_first_binary(binaries = [
     58                          r'/usr/local/bin/arriba',
     59                          r'/usr/bin/arriba',
     60                          r'arriba',
     61                          r'arriba.exe'
     62  #                       r'c:\programs\freediams\freediams.exe',
     63  #                       r'freediams.exe'
     64                  ])
     65
     66                  if found:
     67                          self.path_to_binary = cmd
     68                          return True
     69
     70  #               try:
     71  #                       self.custom_path_to_binary
     72  #               except AttributeError:
     73  #                       _log.error('cannot find FreeDiams binary, no custom path set')
     74  #                       return False
     75
     76  #               found, cmd = gmShellAPI.detect_external_binary(binary = self.custom_path_to_binary)
     77  #               if found:
     78  #                       self.path_to_binary = cmd
     79  #                       return True
     80
     81                  _log.error('cannot find arriba binary')
     82                  return False
     83          #--------------------------------------------------------
     84 -        def __write_config_file(self, patient=None):
     85                  xml = """<?xml version="1.0" encoding="UTF-8"?>
     86
     87  <konsultation version="1.1" xmlns="http://gpzk.de/ns/arriba/start-konfiguration">
     88          <parameter>
     89                  %s
     90                  <idle-timeout>0</idle-timeout>
     91          </parameter>
     92          <speicherorte>
     93                  <status>%s</status>
     94                  <ergebnis-xml>%s</ergebnis-xml>
     95                  <ergebnis-pdf>%s</ergebnis-pdf>
     96          </speicherorte>
     97  </konsultation>
     98  """
     99                  if patient is None:
    100                          pat_xml = u''
    101                  else:
    102                          active_name = patient.get_active_name()
    103                          pat_xml = """<vorname>%s%s</vorname>
    104                  <nachname>%s</nachname>
    105                  <geschlecht>%s</geschlecht>
    106                  <geburtsdatum>%s</geburtsdatum>""" % (
    107                          active_name['firstnames'],
    108                          gmTools.coalesce(active_name['preferred'], u'', u' (%s)'),
    109                          active_name['lastnames'],
    110                          gmXdtMappings.map_gender_gm2xdt[patient['gender']],
    111                          patient.get_formatted_dob(format = cArriba._date_format, encoding = u'utf8', none_string = u'00009999')
    112                  )
    113
    114                  fname_cfg = gmTools.get_unique_filename(prefix = 'gm2arriba-', suffix = '.xml')
    115                  fname_status = gmTools.get_unique_filename(prefix = 'arriba2gm_status-', suffix = '.xml')
    116                  self.xml_result = gmTools.get_unique_filename(prefix = 'arriba2gm_result-', suffix = '.xml')
    117                  self.pdf_result = gmTools.get_unique_filename(prefix = 'arriba2gm_result-', suffix = '.pdf')
    118                  xml_file = codecs.open(fname_cfg, 'wb', 'utf8', 'replace')
    119                  xml_file.write (xml % (
    120                          pat_xml,
    121                          fname_status,
    122                          self.xml_result,
    123                          self.pdf_result
    124                  ))
    125                  xml_file.close()
    126
    127                  return fname_cfg
    128          #--------------------------------------------------------
    129          # external API
    130          #--------------------------------------------------------
    131 -        def run(self, patient=None, debug=False):
    132                  self.pdf_result = None
    133
    134                  cfg_file = self.__write_config_file(patient = patient)
    135
    136                  # FIXME: add user-configured path
    137                  if not self.__detect_binary():
    138                          return False
    139
    140                  args = (
    141                          self.path_to_binary,
    142                          '--file=%s' % cfg_file,
    143                          '--show-cli-pane=%s' % gmTools.bool2subst(debug, 'true', 'false')
    144                  )
    145
    146                  try:
    147                          subprocess.check_call(args = args, close_fds = True)
    148                  except (OSError, ValueError, subprocess.CalledProcessError):
    149                          _log.exception('there was a problem executing [%s]', self.path_to_binary)
    150                          gmDispatcher.send(signal = u'statustext', msg = _('Cannot run [arriba] !'), beep = True)
    151                          return False
    152
    153                  try:
    154                          open(self.pdf_result).close()
    155                  except:
    156                          _log.exception('error accessing [%s]', self.pdf_result)
    157                          gmDispatcher.send(signal = u'statustext', msg = _('No [arriba] result found in [%s].') % self.pdf_result, beep = False)
    158                          return False
    159
    160                  return True
    161  #============================================================
    162  # main
    163  #------------------------------------------------------------
    164  if __name__ == "__main__":
    165
    166          if len(sys.argv) < 2:
    167                  sys.exit()
    168
    169          if sys.argv[1] != 'test':
    170                  sys.exit()
    171
    172          from Gnumed.pycommon import gmI18N
    173          from Gnumed.pycommon import gmDateTime
    174          gmI18N.activate_locale()
    175          gmI18N.install_domain()
    176          gmDateTime.init()
    177
    178          from Gnumed.business import gmPerson
    179
    180          gmPerson.set_active_patient(patient = gmPerson.cIdentity(aPK_obj = 12))
    181
    182          arriba = cArriba()
    183          print arriba
    184          arriba.run(patient = gmPerson.gmCurrentPatient(), debug = True)
    185