def _write_quiplog(self): """Write QUIP XML logfile using ChangeHistory entries. """ channel = self.fv.get_channelInfo(self.chname) if channel is None: return history_plgname = 'ChangeHistory' try: history_obj = self.fv.gpmon.getPlugin(history_plgname) except Exception: self.logger.error( '{0} plugin is not loaded. No {1} will be ' 'written.'.format(history_plgname, self.logfile)) return if channel.name not in history_obj.name_dict: self.logger.error( '{0} channel not found in {1}. No {2} will be ' 'written.'.format(channel.name, history_plgname, self.logfile)) return file_dict = history_obj.name_dict[channel.name] # Insert change history into QUIP log, ordered by image name, # and then by timestamp. for imname in sorted(file_dict): entries = file_dict[imname] for timestamp in sorted(entries): bnch = entries[timestamp] date_str, time_str = timestamp.split(' ') QUIP_LOG.add_entry(date_str, time_str, imname, imname, bnch.DESCRIP, 'status') output_xml(QUIP_LOG.xml_dict(), self.logfile)
def _write_quiplog(self): """Write QUIP XML logfile using ChangeHistory entries. """ channel = self.fv.get_channelInfo(self.chname) if channel is None: return history_plgname = 'ChangeHistory' try: history_obj = self.fv.gpmon.getPlugin(history_plgname) except Exception: self.logger.error( f'{history_plgname} plugin is not loaded. ' f'No {self.logfile} will be written.') return if channel.name not in history_obj.name_dict: self.logger.error( f'{channel.name} channel not found in {history_plgname}. ' f'No {self.logfile} will be written.') return file_dict = history_obj.name_dict[channel.name] # Insert change history into QUIP log, ordered by image name, # and then by timestamp. for imname in sorted(file_dict): entries = file_dict[imname] for timestamp in sorted(entries): bnch = entries[timestamp] date_str, time_str = timestamp.split(' ') QUIP_LOG.add_entry(date_str, time_str, imname, imname, bnch.DESCRIP, 'status') output_xml(QUIP_LOG.xml_dict(), self.logfile)
def test_output_xml(tmpdir): filename = str(tmpdir.join('simple.xml')) xmldict = {'foo': 'bar'} output_xml(xmldict, filename) with open(filename) as f: lines = f.readlines() assert len(lines) == 2 assert lines[0] == '<?xml version="1.0" ?>\n' assert lines[1] == '<foo>bar</foo>\n'
def save_images(self): """Save selected images and output XML files. """ output_images = [] res_dict = self.treeview.get_selected() clobber = self.settings.get('clobber', False) self.treeview.clear_selection() # Automatically disables Save button # If user gives empty string, no suffix. if self.suffix: sfx = '_' + self.suffix else: sfx = '' # Also include channel name in suffix. This is useful if user likes to # open the same image in multiple channels. if self.settings.get('include_chname', True): sfx += '_' + self.chname # Process each selected file. Each can have multiple edited extensions. for infile in res_dict: f_pfx = os.path.splitext(infile)[0] # prefix f_ext = '.fits' # Only FITS supported oname = f_pfx + sfx + f_ext outfile = os.path.join(self.outdir, oname) self.w.status.set_text( 'Writing out {0} to {1} ...'.format(shorten_name(infile, 10), shorten_name(oname, 10))) self.logger.debug( 'Writing out {0} to {1} ...'.format(infile, oname)) if os.path.exists(outfile) and not clobber: self.logger.error('{0} already exists'.format(outfile)) continue bnch = res_dict[infile] if bnch.path is None or not os.path.isfile(bnch.path): self._write_mosaic(f_pfx, outfile) else: shutil.copyfile(bnch.path, outfile) self._write_mef(f_pfx, bnch.extlist, outfile) output_images.append(outfile) self.logger.info('{0} written'.format(outfile)) # Save QUIP Log, which stores change history self.logger.info('Saving {0}'.format(self.logfile)) try: self._write_quiplog() except Exception as e: self.w.status.set_text('Cannot write QUIP log!') self.logger.error(str(e)) return # Save QUIP Out, which stores output image list self.logger.info('Saving {0}'.format(self.stafile)) try: output_xml(quip_out_dict(images=output_images), self.stafile) except Exception as e: self.w.status.set_text('Cannot write QUIP out!') self.logger.error(str(e)) return self.w.status.set_text('Done! Quit Ginga to exit QUIP')
def save_imlist(self): """Save selected image filename(s) to QUIP OUT XML only (no Activity Log). If no image selected, no output is generated. """ if QUIP_DIRECTIVE is None: s = 'Invalid QUIP operation file!' self.logger.error(s) self.update_status(s) return imlist = self.get_selected_paths() if len(imlist) == 0: s = 'No image selected!' self.logger.error(s) self.update_status(s) return orig_images = QUIP_DIRECTIVE['IMAGES']['IMAGE_PATH'] outfile = QUIP_DIRECTIVE['OUTPUT']['OUT_FILE_PATH'] keep_list = [] ignored_list = [] # Get full path of images to keep and ensure uniqueness for images # with multiple mosaicked extensions. for im in imlist: basefname = os.path.basename(im) realpath = '' is_found = False # Get the path to actual input, not the shrunken version for orig_im in orig_images: if basefname in orig_im: realpath = orig_im is_found = True break if is_found: keep_list.append(realpath) else: self.logger.error('{0} not found in operation file'.format(im)) ignored_list.append(im) images = sorted(set(keep_list)) self.logger.info('Saving {0}'.format(outfile)) # Save QUIP out file XML try: output_xml(quip_out_dict(images=images), outfile) except OSError as e: s = str(e) self.logger.error(s) self.update_status('ERROR: ' + s) return if len(ignored_list) > 0: self.logger.info('Ignored {0}'.format(','.join(ignored_list))) extra_msg = ', ignored file(s)' else: extra_msg = '' self.update_status('Image list saved' + extra_msg)
def save_imlist(self): """Save selected image filename(s) to QUIP OUT XML only (no Activity Log). If no image selected, no output is generated. """ if QUIP_DIRECTIVE is None: s = 'Invalid QUIP operation file!' self.logger.error(s) self.update_status(s) return imlist = self.get_selected_paths() if len(imlist) == 0: s = 'No image selected!' self.logger.error(s) self.update_status(s) return orig_images = QUIP_DIRECTIVE['IMAGES']['IMAGE_PATH'] outfile = QUIP_DIRECTIVE['OUTPUT']['OUT_FILE_PATH'] keep_list = [] ignored_list = [] # Get full path of images to keep and ensure uniqueness for images # with multiple mosaicked extensions. for im in imlist: basefname = os.path.basename(im) realpath = '' is_found = False # Get the path to actual input, not the shrunken version for orig_im in orig_images: if basefname in orig_im: realpath = orig_im is_found = True break if is_found: keep_list.append(realpath) else: self.logger.error(f'{im} not found in operation file') ignored_list.append(im) images = sorted(set(keep_list)) self.logger.info(f'Saving {outfile}') # Save QUIP out file XML try: output_xml(quip_out_dict(images=images), outfile) except OSError as e: s = str(e) self.logger.error(s) self.update_status('ERROR: ' + s) return if len(ignored_list) > 0: self.logger.info(f"Ignored {','.join(ignored_list)}") extra_msg = ', ignored file(s)' else: extra_msg = '' self.update_status('Image list saved' + extra_msg)
def save_images(self): """Save selected images and output XML files. """ output_images = [] res_dict = self.treeview.get_selected() clobber = self.settings.get('clobber', False) self.treeview.clear_selection() # Automatically disables Save button # If user gives empty string, no suffix. if self.suffix: sfx = '_' + self.suffix else: sfx = '' # Also include channel name in suffix. This is useful if user likes to # open the same image in multiple channels. if self.settings.get('include_chname', True): sfx += '_' + self.chname # Process each selected file. Each can have multiple edited extensions. for infile in res_dict: f_pfx = os.path.splitext(infile)[0] # prefix f_ext = '.fits' # Only FITS supported oname = f_pfx + sfx + f_ext outfile = os.path.join(self.outdir, oname) self.w.status.set_text( f'Writing out {shorten_name(infile, 10)} to ' f'{shorten_name(oname, 10)} ...') self.logger.debug( f'Writing out {infile} to {oname} ...') if os.path.exists(outfile) and not clobber: self.logger.error(f'{outfile} already exists') continue bnch = res_dict[infile] if bnch.path is None or not os.path.isfile(bnch.path): self._write_mosaic(f_pfx, outfile) else: shutil.copyfile(bnch.path, outfile) self._write_mef(f_pfx, bnch.extlist, outfile) output_images.append(outfile) self.logger.info(f'{outfile} written') # Save QUIP Log, which stores change history self.logger.info(f'Saving {self.logfile}') try: self._write_quiplog() except Exception as e: self.w.status.set_text('Cannot write QUIP log!') self.logger.error(str(e)) return # Save QUIP Out, which stores output image list self.logger.info(f'Saving {self.stafile}') try: output_xml(quip_out_dict(images=output_images), self.stafile) except Exception as e: self.w.status.set_text('Cannot write QUIP out!') self.logger.error(str(e)) return self.w.status.set_text('Done! Quit Ginga to exit QUIP')