def main(): """ NAME upload_magic.py DESCRIPTION This program will prepare your PMAG text files created by the programs nfo_magic.py, zeq_magic.py, thellier_magic.py, mag_magic, specimens_results_magic.py and so on. it will check for all the MagIC text files and skip the missing ones SYNTAX upload_magic.py INPUT MagIC txt files OPTIONS -h prints help message and quits -all include all the measurement data, default is only those used in interpretations OUTPUT upload.txt: file for uploading to MagIC database """ if '-h' in sys.argv: print main.__doc__ sys.exit() else: dataframe = extractor.command_line_dataframe([['cat', False, 0], ['F', False, ''], ['f', False, '']]) checked_args = extractor.extract_and_check_args(sys.argv, dataframe) dir_path, concat = extractor.get_vars(['WD', 'cat'], checked_args) ipmag.upload_magic(concat, dir_path)
def on_upload_file(self, event): """ Write all data to appropriate er_* and pmag_* files. Then use those files to create a MagIC upload format file. Validate the upload file. """ # coherence validations wait = wx.BusyInfo('Validating data, please wait...') spec_warnings, samp_warnings, site_warnings, loc_warnings = self.er_magic.validate_data() result_warnings = self.er_magic.validate_results(self.er_magic.results) meas_warnings = self.er_magic.validate_measurements(self.er_magic.measurements) self.warn_dict = {'specimen': spec_warnings, 'sample': samp_warnings, 'site': site_warnings, 'location': loc_warnings, 'result': result_warnings, 'age': {}, 'measurement': meas_warnings} # done coherence validations del wait # write upload file and perform data validations wait = wx.BusyInfo('Making upload file, please wait...') self.er_magic.write_files() upfile, error_message, errors = ipmag.upload_magic(dir_path=self.WD) del wait if upfile: text = "You are ready to upload.\nYour file:\n{}\nwas generated in directory: \n{}\nDrag and drop this file in the MagIC database.".format(os.path.split(upfile)[1], self.WD) dlg = wx.MessageDialog(self, caption="Saved", message=text, style=wx.OK) else: text = "There were some problems with the creation of your upload file.\nError message: {}\nSee Terminal/Command Prompt for details".format(error_message) dlg = wx.MessageDialog(self, caption="Error", message=text, style=wx.OK) result = dlg.ShowModal() if result == wx.ID_OK: dlg.Destroy() self.edited = False ## add together data & coherence errors into one dictionary if errors: for item_type in errors: for item_name in errors[item_type]: if item_name in self.warn_dict[item_type]: self.warn_dict[item_type][item_name].update(errors[item_type][item_name]) else: self.warn_dict[item_type][item_name] = errors[item_type][item_name] has_problems = [] for item_type, warnings in self.warn_dict.items(): if warnings: has_problems.append(item_type) # for any dtypes with validation problems (data or coherence), # highlight the button to the corresponding grid for dtype in self.warn_dict: wind = self.FindWindowByName(dtype + '_btn') if wind: if dtype in has_problems: wind.Bind(wx.EVT_PAINT, self.highlight_button) else: wind.Unbind(wx.EVT_PAINT, handler=self.highlight_button)# this sucks (makes buttons disappear) self.Refresh() if has_problems: self.validation_mode = set(has_problems) self.message.SetLabel('Highlighted grids have incorrect or incomplete data') self.bSizer_msg.ShowItems(True) self.hbox.Fit(self)
def test_with_valid_files(self): #print os.path.join(self.dir_path, 'my_project') outfile, error_message, errors = ipmag.upload_magic(dir_path=os.path.join(self.dir_path, 'my_project')) self.assertTrue(outfile) self.assertEqual(error_message, '') self.assertFalse(errors) assert os.path.isfile(outfile) directory = os.path.join(self.dir_path, 'my_project_with_errors') os.remove(os.path.join(directory, outfile))
def on_btn_upload(self, event): outstring="upload_magic.py" print "-I- running python script:\n %s"%(outstring) wait = wx.BusyInfo("Please wait, working...") upfile, error_message, errors = ipmag.upload_magic(dir_path=self.WD) del wait if upfile: text = "You are ready to upload.\n Your file: {} was generated in MagIC Project Directory.\nDrag and drop this file in the MagIC database.".format(os.path.split(upfile)[1]) dlg = wx.MessageDialog(self, caption="Saved", message=text, style=wx.OK) else: text = "There were some problems with the creation of your upload file.\nError message: {}\nSee Terminal/Command Prompt for details".format(error_message) dlg = wx.MessageDialog(self, caption="Error", message=text, style=wx.OK) result = dlg.ShowModal() if result == wx.ID_OK: dlg.Destroy()
def test_with_invalid_files(self): outfile, error_message, errors = ipmag.upload_magic(dir_path=os.path.join(self.dir_path, 'my_project_with_errors')) self.assertTrue(errors) self.assertFalse(outfile) self.assertEqual(error_message, "file validation has failed. You may run into problems if you try to upload this file.") directory = os.path.join(self.dir_path, 'my_project_with_errors') # delete any upload file that was partially created import re pattern = re.compile('\w*[.]\w*[.]\w*[20]\d{2}\w*.txt$') possible_files = os.listdir(directory) files = [] for f in possible_files: if pattern.match(f): files.append(f) pmag.remove_files(files, directory)
def test_empty_dir(self): outfile, error_message, errors = ipmag.upload_magic(dir_path=os.path.join(self.dir_path, 'empty_dir')) self.assertFalse(errors) self.assertFalse(outfile) self.assertEqual(error_message, "no data found, upload file not created")