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']]: 
 63                          f.write("  Cave: live vaccine\n") 
 64                  else: 
 65                          f.write("  non-live vaccine\n") 
 66                  # get indications for this vaccine 
 67                  cmd = """select * from clin.v_inds4vaccine where pk_vaccine = %s""" 
 68                  indication_rows, ind_idx = gmPG.run_ro_query ( 
 69                          'clinical', 
 70                          cmd, 
 71                          True, 
 72                          vacc[idx['pk_vaccine']] 
 73                  ) 
 74                  # error 
 75                  if indication_rows is None: 
 76                          print "error retrieving vaccine indication data" 
 77                  # display them 
 78                  else: 
 79                          f.write('  Indications:\n') 
 80                          for ind in indication_rows: 
 81                                  f.write('  - %s (%s)\n' % (ind[ind_idx['l10n_indication']], ind[ind_idx['indication']])) 
 82   
 83  #============================= 
 84  # export vaccination schedules 
 85  #============================= 
 86   
 87  # retrieve schedules 
 88  cmd = "select * from clin.v_vacc_regimes" 
 89  schedule_rows, idx = gmPG.run_ro_query ( 
 90          'clinical', 
 91          cmd, 
 92          True 
 93  ) 
 94  if schedule_rows is None: 
 95          print "error retrieving vaccination schedules" 
 96  else: 
 97          f.write('\n\nVaccination schedules in the GNUmed database\n') 
 98          f.write(    '--------------------------------------------\n') 
 99          for sched in schedule_rows: 
100                  if sched[idx['is_active']]: 
101                          act = 'active' 
102                  else: 
103                          act = 'inactive' 
104                  f.write(schedule_template % ( 
105                          sched[idx['regime']], 
106                          act, 
107                          sched[idx['l10n_indication']], 
108                          sched[idx['indication']], 
109                          sched[idx['min_age_due']], 
110                          sched[idx['shots']], 
111                          sched[idx['comment']] 
112                  )) 
113                  # get shots for this schedule 
114                  cmd = "select * from clin.v_vacc_defs4reg where pk_regime=%s order by vacc_seq_no" 
115                  shot_rows, shots_idx = gmPG.run_ro_query ( 
116                          'clinical', 
117                          cmd, 
118                          True, 
119                          sched[idx['pk_regime']] 
120                  ) 
121                  if shot_rows is None: 
122                          print "error retrieving shots for regime" 
123                  else: 
124                          f.write('  Shots defined for this schedule:\n') 
125                          for shot in shot_rows: 
126                                  if shot[shots_idx['is_booster']]: 
127                                          f.write('  booster) start %s - %s days of age, refresh after %s (%s)\n' % ( 
128                                                  shot[shots_idx['age_due_min']].day, 
129                                                  shot[shots_idx['age_due_max']].day, 
130                                                  shot[shots_idx['min_interval']].day, 
131                                                  shot[shots_idx['vacc_comment']] 
132                                          )) 
133                                  elif shot[shots_idx['vacc_seq_no']] == 1: 
134                                          f.write('  shot #%s) due between day %s and %s (%s)\n' % ( 
135                                                  shot[shots_idx['vacc_seq_no']], 
136                                                  shot[shots_idx['age_due_min']].day, 
137                                                  shot[shots_idx['age_due_max']].day, 
138                                                  shot[shots_idx['vacc_comment']] 
139                                          )) 
140                                  else: 
141                                          f.write('  shot #%s) due between day %s and %s, minimum %s day after previous (%s)\n' % ( 
142                                                  shot[shots_idx['vacc_seq_no']], 
143                                                  shot[shots_idx['age_due_min']].day, 
144                                                  shot[shots_idx['age_due_max']].day, 
145                                                  shot[shots_idx['min_interval']].day, 
146                                                  shot[shots_idx['vacc_comment']] 
147                                          )) 
148                  # get vaccines suitable for this schedule 
149                  cmd = """ 
150  select trade_name, short_name 
151  from clin.vaccine 
152  where 
153          select () = () 
154  """ 
155   
156  f.close() 
157   
158  #============================================================ 
159  # $Log: export_immunization_metadata.py,v $ 
160  # Revision 1.2  2006-02-26 18:31:28  ncq 
161  # - improve templates 
162  # - export schedules, too 
163  # 
164  # Revision 1.1  2006/02/23 19:25:47  ncq 
165  # - export immunization metadata from the database into a file 
166  # 
167