def do_sample_import(self, event): cname = self.corename core = datastore.cores.get(cname) if core is None: core = Core(cname) datastore.cores[cname] = core for item in self.rows: # TODO -- need to update existing samples if they exist, not # add new ones! s = Sample('input', item) core.add(s) all = Sample('input', {'depth': 'all'}) source = self.corepage.source_name # TODO: should only put metada in mdata if it will not be used for # calculations anywhere if source: all['input']['Provenance'] = source core.mdata.atts['Provenance'] = (mData.CorePubAtt('input', 'Provenance', source)) latlng = self.corepage.latlng all['input']['Latitude'] = latlng[0] all['input']['Longitude'] = latlng[1] guid = self.corepage.core_guid if guid: all['input']['Core GUID'] = guid core.mdata.atts['Core GUID'] = (mData.CoreAttribute('input', 'Core GUID', guid, 'guid')) core.mdata.atts['Geography'] = (mData.CoreGeoAtt('input', 'Geography', latlng, "")) core.add(all) core.loaded = True
def do_sample_import(self, event): cname = self.corename core = datastore.cores.get(cname) if core is None: core = Core(cname) datastore.cores[cname] = core for item in self.rows: # TODO -- need to update existing samples if they exist, not # add new ones! s = Sample("input", item) core.add(s) all = Sample("input", {"depth": "all"}) source = self.corepage.source_name # TODO: should only put metada in mdata if it will not be used for # calculations anywhere if source: all["input"]["Provenance"] = source core.mdata.atts["Provenance"] = mData.CorePubAtt("input", "Provenance", source) latlng = self.corepage.latlng all["input"]["Latitude"] = latlng[0] all["input"]["Longitude"] = latlng[1] guid = self.corepage.core_guid if guid: all["input"]["Core GUID"] = guid core.mdata.atts["Core GUID"] = mData.CoreAttribute("input", "Core GUID", guid, "guid") core.mdata.atts["Geography"] = mData.CoreGeoAtt("input", "Geography", latlng, "") core.add(all) core.loaded = True
def import_samples(self, event): dialog = wx.FileDialog(None, "Please select a CSV File containing Samples to be Imported or Updated:", defaultDir=os.getcwd(), wildcard="CSV Files (*.csv)|*.csv|All Files|*.*", style=wx.OPEN | wx.DD_CHANGE_DIR) result = dialog.ShowModal() path = dialog.GetPath() #destroy the dialog now so no problems happen on early return dialog.Destroy() if result == wx.ID_OK: with open(path, 'rU') as input_file: #allow whatever sane csv formats we can manage, here sniffer = csv.Sniffer() dialect = sniffer.sniff(input_file.read(1024)) dialect.skipinitialspace = True input_file.seek(0) reader = csv.DictReader(input_file, dialect=dialect) if not reader.fieldnames: wx.MessageBox("Selected file is empty.", "Operation Cancelled", wx.OK | wx.ICON_INFORMATION) return #strip extra spaces, since that was apparently a problem before? reader.fieldnames = [name.strip() for name in reader.fieldnames] if 'depth' not in reader.fieldnames: wx.MessageBox("Selected file is missing the required attribute 'depth'.", "Operation Cancelled", wx.OK | wx.ICON_INFORMATION) return rows = [] for index, line in enumerate(reader, 1): #do appropriate type conversions... for key, value in line.iteritems(): try: line[key] = datastore.sample_attributes.convert_value(key, value) except ValueError: wx.MessageBox("%s on row %i has an incorrect type." "Please update the csv file and try again." % (key, index), "Operation Cancelled", wx.OK | wx.ICON_INFORMATION) return rows.append(line) if not rows: wx.MessageBox("Selected file appears to contain no data.", "Operation Cancelled", wx.OK | wx.ICON_INFORMATION) return dialog = DisplayImportedSamples(self, os.path.basename(path), reader.fieldnames, rows) if dialog.ShowModal() == wx.ID_OK: if dialog.source_name: for item in rows: item['source'] = dialog.source_name cname = dialog.core_name core = datastore.cores.get(cname, None) if core is None: core = Core(cname) datastore.cores[cname] = core for item in rows: s = Sample('input', item) core.add(s) wx.MessageBox('Core %s imported/updated' % cname, "Import Results", wx.OK | wx.CENTRE) events.post_change(self, 'samples') dialog.Destroy()