def import_from_string(self, text, title=None): """Import data from string""" data = self.model.get_data() editor = ImportWizard(self, text, title=title, contents_title=translate("DictEditor", "Clipboard contents"), varname=fix_reference_name("data", blacklist=data.keys())) if editor.exec_(): var_name, clip_data = editor.get_data() self.new_value(var_name, clip_data)
def loadglobals(self, glbs): """Load globals() from filename""" filename = pickle.loads( read_packet(self.request) ) data, error_message = iofunctions.load(filename) if error_message: return error_message for key in data.keys(): new_key = fix_reference_name(key, blacklist=glbs.keys()) if new_key != key: data[new_key] = data.pop(key) try: glbs.update(data) except Exception, error: return str(error)
def import_data(self): sock = self.shellwidget.monitor_socket title = self.tr("Import data") if self.filename is None: basedir = os.getcwdu() else: basedir = osp.dirname(self.filename) filename = iofunctions.get_open_filename(self, basedir, title) if filename: filename = unicode(filename) else: return self.filename = filename ext = osp.splitext(self.filename)[1].lower() if ext not in iofunctions.load_funcs: buttons = QMessageBox.Yes | QMessageBox.Cancel answer = QMessageBox.question(self, title, self.tr("<b>Unsupported file type '%1'</b><br><br>" "Would you like to import it as a text file?") \ .arg(ext), buttons) if answer == QMessageBox.Cancel: return else: load_func = 'import_wizard' else: load_func = iofunctions.load_funcs[ext] if isinstance(load_func, basestring): # 'import_wizard' (self.setup_io) # Import data with import wizard error_message = None try: text, _encoding = encoding.read(self.filename) base_name = osp.basename(self.filename) editor = ImportWizard(self, text, title=base_name, varname=fix_reference_name(base_name)) if editor.exec_(): var_name, clip_data = editor.get_data() monitor_set_global(sock, var_name, clip_data) except Exception, error: error_message = str(error)