def set_explain_results(self, query): self.explain_results.reset() if query.failed: dialogs.error(_(u'Failed'), '\n'.join(query.errors), parent=self.instance) else: self.explain_results.set_result(query.rows, query.description)
def explain(self): self.results.assure_visible() buf = self.textview.get_buffer() bounds = buf.get_selection_bounds() if not bounds: bounds = buf.get_bounds() statement = buf.get_text(*bounds) if len(sqlparse.split(statement)) > 1: dialogs.error(_(u"Select a single statement to explain.")) return if not self.connection: return queries = [Query(stmt, self.connection) for stmt in self.connection.explain_statements(statement)] def _execute_next(last, queries): if last is not None and last.failed: self.results.set_explain_results(last) return q = queries.pop(0) if len(queries) == 0: q.connect('finished', lambda x: self.results.set_explain_results(x)) else: q.connect('finished', lambda x: _execute_next(x, queries)) q.execute() _execute_next(None, queries)
def set_filename(self, filename): """Opens filename. Returns ``True`` if the file was successfully opened. Otherwise ``False``. """ msg = None if not os.path.isfile(filename): msg = _(u'No such file: %(name)s') elif not os.access(filename, os.R_OK): msg = _(u'File is not readable: %(name)s') if msg is not None: dialogs.error(_(u"Failed to open file"), msg % {'name': filename}) return False self._filename = filename if filename: f = open(self._filename) a = f.read() f.close() else: a = "" self._filecontent_read = a self.set_text(a) self.app.recent_manager.add_item(to_uri(filename)) return True
def on_child_exited(self, term, cmd, args): exit_status = term.get_child_exit_status() if exit_status != 0: msg = _((u'The command "%(command)s" failed ' u'(Exit status: %(status)d).')) msg = msg % {'command': cmd, 'status': exit_status} gtk.gdk.threads_enter() dialogs.error(_(u'Failed'), msg) gtk.gdk.threads_leave() self.destroy()
def set_query(self, query): self.query = query self.grid.reset() if self.query.description: try: self.grid.set_result(self.query.rows, self.query.description, self.query.coding_hint) except Exception, err: logging.exception('Failed to display query results') dialogs.error(_(u'Failed to display results'), str(err))
def test_connection(self): """Test the current settings.""" if not self.validate_data(): return ds = self.make_datasource(create_new=True) try: conn = ds._open_connection() conn.close() dialogs.info(_(u'Succeeded'), parent=self.dlg) except: dialogs.error(_(u'Failed'), str(sys.exc_info()[1]))
def __init_userdb(self): """Initializes the connection and cursor. This method creates the database if necessary. """ logging.debug("Initializing user database: %s", self._user_db) create = not os.path.isfile(self._user_db) try: self.conn = sqlite3.connect(self._user_db) except sqlite3.OperationalError, err: logging.exception('Unable to open user database:') dialogs.error(_(u'Unable to open user database'), '%s (%s)' % (str(err), self._user_db)) sys.exit(1)
def show_help(self, topic="index"): """Opens a webbrowser with a help page. Arguments: topic: If given, the URL points to the selected topic. """ url = os.path.join(MANUAL_URL, "%s.html" % topic) parsed = urlparse.urlparse(url) if parsed[0] in ("file", ""): local_path = parsed[2] else: local_path = url if not os.path.exists(local_path): dialogs.error(_(u"Documentation not found"), _(u"Could not find documentation at %s.") % url) else: webbrowser.open(url)
def validate_data(self): """Validates data and displays dialog on errors.""" result = self.clean_data() msg = '' if result['required']: msg += _(u'Missing fields:\n%(fields)s') msg = msg % {'fields': ', '.join( [opt.label for opt in result['required']])} if result['errors']: msg += _(u'Validation errors:\n') msg += '\n'.join('%s: %s' % (opt.label, err) for opt, err in result['errors']) if msg: dialogs.error(_(u'Failed'), msg, parent=self.dlg) return False return True
def validate_data(self): """Validates data and displays dialog on errors.""" result = self.clean_data() msg = '' if result['required']: msg += _(u'Missing fields:\n%(fields)s') msg = msg % { 'fields': ', '.join([opt.label for opt in result['required']]) } if result['errors']: msg += _(u'Validation errors:\n') msg += '\n'.join('%s: %s' % (opt.label, err) for opt, err in result['errors']) if msg: dialogs.error(_(u'Failed'), msg, parent=self.dlg) return False return True
def show_help(self, topic='index'): """Opens a webbrowser with a help page. Arguments: topic: If given, the URL points to the selected topic. """ url = os.path.join(MANUAL_URL, '%s.html' % topic) parsed = urlparse.urlparse(url) if parsed[0] in ('file', ''): local_path = parsed[2] else: local_path = url if not os.path.exists(local_path): dialogs.error(_(u"Documentation not found"), _(u"Could not find documentation at %s.") % url) else: webbrowser.open(url)
def dbconnect(self): """Returns a database connection.""" try: conn = self._open_connection() self._conn_count += 1 conn.num = self._conn_count self.connections.add(conn) conn.connect('closed', self.on_connection_closed) if self.internal_connection is None: self.internal_connection = conn if self.manager is not None: self.manager.emit('datasource-changed', self) return conn except: logging.exception('Datasource.dbconnect failed:') msg = _(u'Could not connect to %(name)s: %(error)s') msg = msg % {'name': self.get_label(), 'error': str(sys.exc_info()[1])} dialogs.error(_(u'Failed'), msg) return None