def _save(self, type_='pic', path=None): """ """ if path is None: path = get_file_path(default_directory=os.path.expanduser('~')) # from pyface.api import FileDialog, OK # dlg = FileDialog(action='save as', default_directory=os.path.expanduser('~')) # if dlg.open() == OK: # path = dlg.path # self.status_text = 'Image Saved: %s' % path if path is not None: if type_ == 'pdf' or path.endswith('.pdf'): self._render_to_pdf(filename=path) else: # auto add an extension to the filename if not present # extension is necessary for PIL compression # set default save type_ DEFAULT_IMAGE_EXT='.png' # see http://infohost.nmt.edu/tcc/help/pubs/pil/formats.html for ei in IMAGE_EXTENSIONS: if path.endswith(ei): self._render_to_pic(path) break else: path = add_extension(path, DEFAULT_IMAGE_EXT) self._render_to_pic(path)
def perform(self, event): hc = HopConfig() info = hc.edit_traits(kind='livemodal') if not info.result: return p = os.path.join(paths.hops_dir, add_extension(self.hop_name, ext='.yaml')) with open(p, 'r') as rfile: hops = yaml.load(rfile) prog = self._open_prog(hc.ncycles, hops) msg_queue = Queue() app = event.task.application self._alive = True producer = Thread(target=self._perform, args=(app, hc.ncycles, hops, msg_queue)) producer.setDaemon(True) producer.start() consumer = Thread(target=self._consumer, args=(prog, msg_queue)) consumer.setDaemon(True) consumer.start()
def _execute_extraction(self, name, root, kind, new_thread=True, delay_start=0, on_completion=None, context=None): from pychron.pyscripts.extraction_line_pyscript import ExtractionPyScript klass = ExtractionPyScript if kind == 'Laser': from pychron.pyscripts.laser_pyscript import LaserPyScript klass = LaserPyScript script = klass(application=self.application, root=root, name=add_extension(name, '.py'), runner=self._runner) if script.bootstrap(): if context: script.setup_context(**context) else: script.set_default_context() try: script.test() except Exception, e: return self._current_script = script script.setup_context(extract_device='fusions_diode') if self.use_trace: self.active_editor.trace_delay = self.trace_delay ret = script.execute(trace=self.use_trace, new_thread=new_thread, delay_start=delay_start, on_completion=on_completion) return not script.is_canceled() and ret
def get_production(self, pname, **kw): root = os.path.join(paths.meta_dir, 'productions') if not os.path.isdir(root): os.mkdir(root) p = os.path.join(root, add_extension(pname)) ip = Production(p) return ip
def _gain_path(self, name): root = os.path.join(paths.meta_dir, 'spectrometers') if not os.path.isdir(root): os.mkdir(root) p = os.path.join(root, add_extension('{}.gain'.format(name), '.json')) return p
def dump(self): p = os.path.join(paths.hidden_dir, add_extension('config', '.p')) with open(p, 'wb') as wfile: obj = {'name': self.active_name} pickle.dump(obj, wfile) self.dump_item(self.active_item)
def _gain_path(self, name): root = os.path.join(paths.meta_root, 'spectrometers') if not os.path.isdir(root): os.mkdir(root) p = os.path.join(root, add_extension('{}.gain'.format(name), '.json')) return p
def get_file_path(root, action='open'): dlg = FileDialog(action=action, wildcard=FileDialog.create_wildcard('YAML', '*.yaml *.yml'), default_directory=root) if dlg.open(): if dlg.path: return add_extension(dlg.path, ext=('.yaml', '.yml'))
def make_gosub(self): selection = self.control.code.textCursor().selectedText() dlg = FileDialog(action='save as', default_directory=os.path.dirname(self.path)) p = None # root = os.path.dirname(self.path) # p = os.path.join(root, 'common', 'test_gosub.py') if dlg.open(): p = dlg.path if p: p = add_extension(p, '.py') # p='/Users/ross/Desktop/foosub.py' with open(p, 'w') as wfile: wfile.write('# Extracted Gosub\n') wfile.write('# Source: from {}\n'.format(self.path)) wfile.write('# Date: {}\n'.format(datetime.now().strftime('%m-%d-%Y %H:%M'))) wfile.write('def main():\n') for li in selection.split(u'\u2029'): wfile.write(u' {}\n'.format(li.lstrip())) p = remove_extension(p) rp = os.path.relpath(p, self.path) rp = rp.replace('/', ':') self.control.code.replace_selection("gosub('{}')".format(rp[3:]))
def dump_item(self, item): name = item.name p = os.path.join(self.root, add_extension(name, '.p')) print('dump itme') with open(p, 'wb') as wfile: pickle.dump(item, wfile) print('sdfasdf')
def _render_to_pdf(self, save=True, canvas=None, filename=None, dest_box=None): """ """ from chaco.pdf_graphics_context import PdfPlotGraphicsContext if filename: # if not filename.endswith('.pdf'): # filename += '.pdf' filename = add_extension(filename, ext='.pdf') gc = PdfPlotGraphicsContext(filename=filename, pdf_canvas=canvas, dest_box=dest_box) pc = self.plotcontainer # pc.do_layout(force=True) # pc.use_backbuffer=False gc.render_component(pc, valign='center') if save: gc.save() # pc.use_backbuffer=True return gc
def _save_(self, type_='pic', path=None): """ """ if path is None: dlg = FileDialog(action='save as', default_directory=os.path.expanduser('~')) if dlg.open() == OK: path = dlg.path self.status_text = 'Image Saved: %s' % path if path is not None: if type_ == 'pdf' or path.endswith('.pdf'): self._render_to_pdf(filename=path) else: # auto add an extension to the filename if not present # extension is necessary for PIL compression # set default save type_ DEFAULT_IMAGE_EXT='.png' # see http://infohost.nmt.edu/tcc/help/pubs/pil/formats.html saved = False for ei in IMAGE_EXTENSIONS: if path.endswith(ei): self._render_to_pic(path) saved = True break if not saved: self._render_to_pic(add_extension(path, DEFAULT_IMAGE_EXT))
def save_csv(record_id, items): path = os.path.join(paths.data_det_ic_dir, add_extension(record_id, '.csv')) with open(path, 'w') as wfile: wrt = csv.writer(wfile, delimiter='\t') wrt.writerow(['#det', 'intensity', 'err'] + DETECTOR_ORDER) for i in items: wrt.writerow(i.to_row())
def _get_save_path(self, path, ext='.pdf'): if path is None: dlg = FileDialog(action='save as', default_directory=paths.processed_dir) if dlg.open(): if dlg.path: path = add_extension(dlg.path, ext) return path
def _backup_editor(self, editor): p = editor.path p = add_extension(p, '.txt') if os.path.isfile(p): # make a backup copy of the original experiment file bp, pp = backup(p, paths.backup_experiment_dir) self.info('{} - saving a backup copy to {}'.format(bp, pp))
def get_file_path(root, action='open'): dlg = FileDialog(action=action, wildcard=FileDialog.create_wildcard( 'YAML', '*.yaml *.yml'), default_directory=root) if dlg.open(): if dlg.path: return add_extension(dlg.path, ext=('.yaml', '.yml'))
def load_spectrometer_parameters(self, spec_sha): if spec_sha: name = add_extension(spec_sha, '.json') p = repository_path(self.repository_identifier, name) sd = get_spec_sha(p) self.source_parameters = sd['spectrometer'] self.gains = sd['gains'] self.deflections = sd['deflections']
def get_production(self, irrad, level, **kw): path = os.path.join(paths.meta_root, irrad, 'productions.json') obj = dvc_load(path) pname = obj[level] p = os.path.join(paths.meta_root, irrad, 'productions', add_extension(pname, ext='.json')) ip = Production(p) return pname, ip
def dump_item(self, item): name = item.name if not name: name = 'Default' p = os.path.join(self.root, add_extension(name, '.p')) with open(p, 'wb') as wfile: pickle.dump(item, wfile)
def experiment_blob(self): path = self.experiment_queue.path path = add_extension(path, '.txt') if os.path.isfile(path): with open(path, 'r') as fp: return '{}\n{}'.format(path, fp.read()) else: self.warning('{} is not a valid file'.format(path))
def script_path(self): if self.name_prefix: name = '{}{}'.format(self.name_prefix, self.name) name = add_extension(name, '.py') p = os.path.join(self._get_root(), name) return p
def save_as(self): if self._validate_sequence(): dialog = FileDialog(action='save as', default_directory=paths.hops_dir) if dialog.open() == OK: p = dialog.path p = add_extension(p, '.txt') self._save_file(p) self.path = p
def _prepare(self, extraction_script): extraction_script = add_extension(extraction_script) task = self.pyscript_task root, name = os.path.split(extraction_script) ctx = {'analysis_type': 'blank' if 'blank' in name else 'unknown'} ret = task.execute_script(name, root, new_thread=False, context=ctx) self.info('Extraction script {} {}'.format(name, 'completed successfully' if ret else 'failed')) return ret
def make_irradiation_load_template(self): path = self.save_file_dialog() if path: # p = '/Users/ross/Sandbox/irrad_load_template.xls' path = add_extension(path, ".xls") self.manager.make_irradiation_load_template(path) self.information_dialog("Template saved to {}".format(path)) self.view_xls(path)
def export_data(self, path=None, plotid=None): """ """ if path is None: path = get_file_path() if path is not None: path = add_extension(path, '.csv') self._export_data(path, plotid)
def make_irradiation_load_template(self): path = self.save_file_dialog() if path: # p = '/Users/ross/Sandbox/irrad_load_template.xls' path = add_extension(path, '.xls') self.manager.make_irradiation_load_template(path) self.information_dialog('Template saved to {}'.format(path)) self.view_xls(path)
def image(name): name = add_extension(name, '.png') for si in paths.image_search_path: if si and os.path.isfile(os.path.join(si, name)): break else: print 'no image for "{}"'.format(name) return ImageResource(name=name, search_path=paths.image_search_path)
def get(self, name): p = os.path.join(paths.peak_center_config_dir, add_extension(name, '.p')) if os.path.isfile(p): with open(p, 'rb') as rfile: obj = pickle.load(rfile) else: obj = self.item_klass() return obj
def export_data(self, path=None, plotid=None): """ """ if path is None: path = self._path_factory() if path is not None: path = add_extension(path, '.csv') self._export_data(path, plotid)
def _prepare(self, extraction_script): extraction_script = add_extension(extraction_script) task = self.pyscript_task root, name = os.path.split(extraction_script) ctx = {'analysis_type': 'blank' if 'blank' in name else 'unknown'} ret = task.execute_script(name, root, new_thread=False, context=ctx) self.info('Extraction script {} {}'.format( name, 'completed successfully' if ret else 'failed')) return ret
def _stage_map_name_changed(self, new): if new: self.debug('setting stage map to {}'.format(new)) sm = self.stage_map_klass(file_path=os.path.join(self.root, add_extension(new, '.txt'))) self.stage_map = sm self.tray_calibration_manager.load_calibration(stage_map=new) self.canvas.set_map(sm) self.canvas.request_redraw()
def add_production_to_irradiation(self, irrad, name, params, add=True, commit=False): self.debug('adding production {} to irradiation={}'.format(name, irrad)) p = os.path.join(paths.meta_root, irrad, 'productions', add_extension(name, '.json')) prod = Production(p, new=not os.path.isfile(p)) prod.update(params) prod.dump() if add: self.add(p, commit=commit)
def _get_gosub_text(self, gosub): root = os.path.dirname(self.path) new = gosub.replace('/', ':') new = add_extension(new, '.py') paths = new.split(':') p = os.path.join(root, *paths) if os.path.isfile(p): with open(p, 'r') as rfile: return rfile.read()
def _script_factory(self, script_name): if os.path.isfile(os.path.join(paths.extraction_dir, add_extension(script_name, '.py'))): runner = self.application.get_service('pychron.extraction_line.ipyscript_runner.IPyScriptRunner') script = ExtractionPyScript(root=paths.extraction_dir, name=script_name, manager=self.extraction_line_manager, allow_lock=True, runner=runner) return script
def image(name): name = add_extension(name, '.png') for si in paths.image_search_path: if si and os.path.isfile(os.path.join(si, name)): break else: print('no image for "{}"'.format(name)) return ImageResource(name=name, search_path=paths.image_search_path)
def path(self): name = self.name if not name or name == 'Untitled': path, _ = unique_path2(paths.table_dir, 'Untitled', extension='.xlsx') else: path = os.path.join(paths.table_dir, add_extension(name, ext='.xlsx')) return path
def save_sensitivities(self, sens): ps = [] for k, v in sens.items(): root = os.path.join(paths.meta_root, 'spectrometers') p = os.path.join(root, add_extension('{}.sens'.format(k), '.json')) dvc_dump(v, p) ps.append(p) if self.add_paths(ps): self.commit('Updated sensitivity')
def save_file_dialog(self, ext=None, **kw): if "default_directory" not in kw: kw["default_directory"] = self.default_directory dialog = FileDialog(parent=self.window.control, action="save as", **kw) if dialog.open() == OK: path = dialog.path if path: if ext is None: ext = self._default_extension return add_extension(path, ext=ext)
def add_irradiation_holder(self, name, blob, commit=False, overwrite=False): p = os.path.join(paths.meta_dir, 'irradiation_holders', add_extension(name)) if not os.path.isfile(p) or overwrite: with open(p, 'w') as wfile: holes = list(iter_geom(blob)) n = len(holes) wfile.write('{},0.0175\n'.format(n)) for idx, (x, y, r) in holes: wfile.write('{:0.4f},{:0.4f},{:0.4f}\n'.format(x, y, r)) self.add(p, commit=commit)
def _save_button_fired(self): from pyface.file_dialog import FileDialog dialog = FileDialog(action='save as', default_directory=os.path.join(os.path.expanduser('~'))) from pyface.constant import OK if dialog.open() == OK: path = dialog.path if path: self.selected_image.save(add_extension(path, '.jpg'))
def _get_experiment(self, tokens): if not tokens: name = "Current Experiment" else: name = " ".join(tokens) p = os.path.join(paths.experiment_dir, add_extension(name)) if not os.path.isfile(p): raise InvalidExperiment(name) return name, p
def save_file_dialog(self, ext=None, **kw): if 'default_directory' not in kw: kw['default_directory'] = self.default_directory dialog = FileDialog(parent=self.window.control, action='save as', **kw) if dialog.open() == OK: path = dialog.path if path: if ext is None: ext = self._default_extension return add_extension(path, ext=ext)
def _selected_pattern_name_changed(self, new): if not new: return p = add_extension(new, '.lp') if self.save_enabled: if self.confirmation_dialog('You have unsaved changes. Are you sure you want to continue?'): self.load_pattern(p) else: self.load_pattern(p)
def _get_experiment(self, tokens): if not tokens: name = 'Current Experiment' else: name = ' '.join(tokens) p = os.path.join(paths.experiment_dir, add_extension(name)) if not os.path.isfile(p): raise InvalidExperiment(name) return name,p