def test_output_is_written_to_file_if_output_file_is_given(self): with tempfile.TemporaryDirectory() as tmp_dir: output_file = Path(tmp_dir, "import_file.tmp") gf.coinc(output_file=output_file, **self.params) with output_file.open("r") as file: self.assertEqual(self.expected, file.readlines())
def test_shell_injection_fails_for_input_file(self): with tempfile.TemporaryDirectory() as tmp_dir: test_file = Path(tmp_dir, "foo") injection = f"&& touch {test_file}" params = dict(self.params) params["input_file"] = Path(f"{params['input_file']} {injection}") gf.coinc(**params) # injected file should not exist time.sleep(0.1) self.assertFalse(test_file.exists())
def test_shell_injection_fails_for_output_file(self): with tempfile.TemporaryDirectory() as tmp_dir: test_file = Path(tmp_dir, "foo") injection = f"&& touch {test_file}" output_file = Path(tmp_dir, f"import_file.tmp {injection}") gf.coinc(output_file=output_file, **self.params) # injected file should not exist (sleep for a bit to ensure that # subprocess.Popen has finished) time.sleep(0.1) self.assertFalse(test_file.exists())
def test_output_is_not_written_to_file_if_parameters_are_not_ok(self): with tempfile.TemporaryDirectory() as tmp_dir: output_file = Path(tmp_dir, "import_file.tmp") params = dict(self.params) params["columns"] = "" gf.coinc(output_file=output_file, **params) self.assertFalse(output_file.exists()) params = dict(self.params) params["timing"] = {} gf.coinc(output_file=output_file, **params) self.assertFalse(output_file.exists())
def __init__(self, parent, input_file, output_file, adc_timing_spin, icon_manager, skip_lines, trigger, adc_count, timing, coinc_count): """Inits timing graph dialog for measurement import. Args: parent: An ImportMeasurementsDialog class object. input_file: Path to input file. output_file: Path to destination file. adc_timing_spin: A tuple of timing QSpinboxes. icon_manager: An IconManager class object. skip_lines: An integer representing line count to be skipped. trigger: An integer representing ADC number. adc_count: An integer representing ADC count timing: A dictionary of tuples for each ADC. coinc_count: An integer representing number of coincidences to be captured from input_file. """ super().__init__() uic.loadUi(gutils.get_ui_dir() / "ui_import_graph_dialog.ui", self) self.parent = parent self.img_dir = self.parent.request.directory self.timing_low = adc_timing_spin[0] self.timing_high = adc_timing_spin[1] self.button_close.clicked.connect(self.close) data = gf.coinc( input_file, skip_lines=skip_lines, tablesize=10, trigger=trigger, adc_count=adc_count, timing=timing, nevents=coinc_count, columns="$4", timediff=True, output_file=output_file) if not data: QtWidgets.QMessageBox.question( self, "No data", "No coincidence events were found.", QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok) self.close() else: self.matplotlib = MatplotlibImportTimingWidget( self, data, icon_manager, timing) self.exec_()
def test_coinc_returns_expected_output_if_parameters_are_ok(self): with tempfile.TemporaryDirectory(): self.assertEqual(self.expected, gf.coinc(**self.params))
def test_coinc_returns_empty_list_if_no_timings(self): params = dict(self.params) params["timing"] = {} self.assertEqual([], gf.coinc(**params))
def test_coinc_returns_empty_list_if_no_columns(self): with tempfile.TemporaryDirectory(): params = dict(self.params) params["columns"] = "" self.assertEqual([], gf.coinc(**params))
def __import_files(self): """Import listed files with settings defined in the dialog. """ sbh = StatusBarHandler(self.statusbar) string_columns = [] for i in range(self.grid_column.rowCount()): item = self.grid_column.itemAtPosition(i, 0) if not item.isEmpty(): combo_widget = self.grid_column.itemAtPosition(i, 1).widget() # combo_widget = combo_item cur_index = combo_widget.currentIndex() cur_text = combo_widget.currentText() adc = int(re.sub(r"ADC ([0-9]+).*", r"\1", cur_text)) # + 1 since actual column, not index column_index = adc * 2 + cur_index % 2 + 1 string_columns.append("${0}".format(column_index)) string_column = ",".join(string_columns) root = self.treeWidget.invisibleRootItem() root_child_count = root.childCount() timing = dict() for coinc_timing in self.__added_timings.values(): if coinc_timing.is_not_trigger: timing[coinc_timing.adc] = (coinc_timing.low.value(), coinc_timing.high.value()) start_time = timer() sbh.reporter.report(10) filename_list = [] for i in range(root_child_count): item = root.child(i) filename_list.append(item.filename) output_file = df.import_new_measurement(self.request, self.parent, item) gf.coinc(Path(item.file), output_file=output_file, skip_lines=self.spin_skiplines.value(), tablesize=10, trigger=self.spin_adctrigger.value(), adc_count=self.spin_adccount.value(), timing=timing, columns=string_column, nevents=self.spin_eventcount.value()) sbh.reporter.report(10 + (i + 1) / root_child_count * 90) filenames = ", ".join(filename_list) elapsed = timer() - start_time log = "Imported measurements to request: {0}".format(filenames) log_var = "Variables used: {0} {1} {2} {3} {4}".format( "Skip lines: " + str(self.spin_skiplines.value()), "ADC trigger: " + str(self.spin_adctrigger.value()), "ADC count: " + str(self.spin_adccount.value()), "Timing: " + str(timing), "Event count: " + str(self.spin_eventcount.value())) log_elapsed = "Importing finished {0} seconds".format(int(elapsed)) logging.getLogger("request").info(log) logging.getLogger("request").info(log_var) logging.getLogger("request").info(log_elapsed) sbh.reporter.report(100) self.imported = True self.close()