Package Gnumed :: Package exporters :: Module export_immunization_metadata |
[frames] | no frames]

# Source Code for Module Gnumed.exporters.export_immunization_metadata

      1  # GNUmed vaccination metadata exporter
      2  #============================================================
      3  # $Source: /home/ncq/Projekte/cvs2git/vcs-mirror/gnumed/gnumed/client/exporters/export_immunization_metadata.py,v $
      4  # $Id: export_immunization_metadata.py,v 1.2 2006-02-26 18:31:28 ncq Exp $
      5  __version__ = "$Revision: 1.2 $"
      6  __author__ = "Karsten Hilbert"
      7  __license__ = 'GPL'
      8
      9  from Gnumed.pycommon import gmPG
     10  #============================================================
     11  # connect to database
     12  pool = gmPG.ConnectionPool()
     13
     14  filename = 'immunization-data.txt'
     15  print "Writing immunization metadata to:", filename
     16  f = file(filename, 'w')
     17
     18  vaccine_template = """
     19  Vaccine "%s" (%s)
     20    comment  : %s
     21    given via: %s (%s = %s)
     22    age range: %s days - %s days
     23  """
     24
     25  schedule_template = """
     26  Schedule "%s" (%s)
     27    indication : %s (%s)
     28    start @ age: %s days
     29    # of shots : %s
     30    comment    : %s
     31  """
     32
     33  #===========================
     34  # export vaccine information
     35  #===========================
     36
     37  # retrieve vaccines
     38  cmd = "select * from clin.v_vaccine"
     39  vaccine_rows, idx = gmPG.run_ro_query (
     40          link_obj = 'clinical',
     41          aQuery = cmd,
     42          get_col_idx = True
     43  )
     44  # error
     45  if vaccine_rows is None:
     46          print "error retrieving vaccine data"
     47  # display vaccine data
     48  else:
     49          f.write('Vaccines in the GNUmed database\n')
     50          f.write('-------------------------------\n')
     51          for vacc in vaccine_rows:
     52                  f.write(vaccine_template % (
     53                          vacc[idx['trade_name']],
     54                          vacc[idx['short_name']],
     55                          vacc[idx['comment']],
     56                          vacc[idx['l10n_route_description']],
     57                          vacc[idx['route_abbreviation']],
     58                          vacc[idx['route_description']],
     59                          vacc[idx['min_age']].day,
     60                          vacc[idx['max_age']].day
     61                  ))
     62                  if vacc[idx['is_live']] is None:
     63                          f.write("  type of vaccine not known\n")
     64                  elif vacc[idx['is_live']]:
     65                          f.write("  Cave: live vaccine\n")
     66                  else:
     67                          f.write("  non-live vaccine\n")
     68                  # get indications for this vaccine
     69                  cmd = """select * from clin.v_inds4vaccine where pk_vaccine = %s"""
     70                  indication_rows, ind_idx = gmPG.run_ro_query (
     71                          'clinical',
     72                          cmd,
     73                          True,
     74                          vacc[idx['pk_vaccine']]
     75                  )
     76                  # error
     77                  if indication_rows is None:
     78                          print "error retrieving vaccine indication data"
     79                  # display them
     80                  else:
     81                          f.write('  Indications:\n')
     82                          for ind in indication_rows:
     83                                  f.write('  - %s (%s)\n' % (ind[ind_idx['l10n_indication']], ind[ind_idx['indication']]))
     84
     85  #=============================
     86  # export vaccination schedules
     87  #=============================
     88
     89  # retrieve schedules
     90  cmd = "select * from clin.v_vacc_regimes"
     91  schedule_rows, idx = gmPG.run_ro_query (
     92          'clinical',
     93          cmd,
     94          True
     95  )
     96  if schedule_rows is None:
     97          print "error retrieving vaccination schedules"
     98  else:
     99          f.write('\n\nVaccination schedules in the GNUmed database\n')
    100          f.write(    '--------------------------------------------\n')
    101          for sched in schedule_rows:
    102                  if sched[idx['is_active']]:
    103                          act = 'active'
    104                  else:
    105                          act = 'inactive'
    106                  f.write(schedule_template % (
    107                          sched[idx['regime']],
    108                          act,
    109                          sched[idx['l10n_indication']],
    110                          sched[idx['indication']],
    111                          sched[idx['min_age_due']],
    112                          sched[idx['shots']],
    113                          sched[idx['comment']]
    114                  ))
    115                  # get shots for this schedule
    116                  cmd = "select * from clin.v_vacc_defs4reg where pk_regime=%s order by vacc_seq_no"
    117                  shot_rows, shots_idx = gmPG.run_ro_query (
    118                          'clinical',
    119                          cmd,
    120                          True,
    121                          sched[idx['pk_regime']]
    122                  )
    123                  if shot_rows is None:
    124                          print "error retrieving shots for regime"
    125                  else:
    126                          f.write('  Shots defined for this schedule:\n')
    127                          for shot in shot_rows:
    128                                  if shot[shots_idx['is_booster']]:
    129                                          f.write('  booster) start %s - %s days of age, refresh after %s (%s)\n' % (
    130                                                  shot[shots_idx['age_due_min']].day,
    131                                                  shot[shots_idx['age_due_max']].day,
    132                                                  shot[shots_idx['min_interval']].day,
    133                                                  shot[shots_idx['vacc_comment']]
    134                                          ))
    135                                  elif shot[shots_idx['vacc_seq_no']] == 1:
    136                                          f.write('  shot #%s) due between day %s and %s (%s)\n' % (
    137                                                  shot[shots_idx['vacc_seq_no']],
    138                                                  shot[shots_idx['age_due_min']].day,
    139                                                  shot[shots_idx['age_due_max']].day,
    140                                                  shot[shots_idx['vacc_comment']]
    141                                          ))
    142                                  else:
    143                                          f.write('  shot #%s) due between day %s and %s, minimum %s day after previous (%s)\n' % (
    144                                                  shot[shots_idx['vacc_seq_no']],
    145                                                  shot[shots_idx['age_due_min']].day,
    146                                                  shot[shots_idx['age_due_max']].day,
    147                                                  shot[shots_idx['min_interval']].day,
    148                                                  shot[shots_idx['vacc_comment']]
    149                                          ))
    150                  # get vaccines suitable for this schedule
    151                  cmd = """
    152  select trade_name, short_name
    153  from clin.vaccine
    154  where
    155          select () = ()
    156  """
    157
    158  f.close()
    159
    160  #============================================================
    161  # $Log: export_immunization_metadata.py,v $
    162  # Revision 1.2  2006-02-26 18:31:28  ncq
    163  # - improve templates
    164  # - export schedules, too
    165  #
    166  # Revision 1.1  2006/02/23 19:25:47  ncq
    167  # - export immunization metadata from the database into a file
    168  #
    169