def __init__(self, parent=None):
     QtGui.QListWidget.__init__(self, parent)
     self.__list = ExtConnectionList(default_connections_file())
     self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
     self.setIconSize(QtCore.QSize(32, 32))
     self.loadConnections()
     self.editAct = QtGui.QAction("Edit", self)
     self.editAct.setStatusTip("Edit the selected connection")
     self.connect(self.editAct, QtCore.SIGNAL("triggered()"), self.editConnection)
Example #2
0
 def __init__(self, parent=None):
     QtGui.QListWidget.__init__(self,parent)
     self.__list = ExtConnectionList.getInstance(default_connections_file())
     self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
     self.setIconSize(QtCore.QSize(32,32))
     self.loadConnections()
     self.editAct = QtGui.QAction("Edit", self)
     self.editAct.setStatusTip("Edit the selected connection")
     self.connect(self.editAct,
                  QtCore.SIGNAL("triggered()"),
                  self.editConnection)
Example #3
0
class DBLocator(_DBLocator, CoreLocator):

    __list = ExtConnectionList.getInstance(default_connections_file())

    class getKeyChain(object):
        def set_key(self, key, passwd):
            get_vistrails_application().keyChain.set_key(key, passwd)

        def get_key(self, key):
            return get_vistrails_application().keyChain.get_key(key)

    keyChain = getKeyChain()

    def __init__(self,
                 host,
                 port,
                 database,
                 user,
                 passwd,
                 name=None,
                 **kwargs):

        _DBLocator.__init__(self, host, port, database, user, passwd, name,
                            **kwargs)
        self.ext_connection_id = -1

    def load(self, klass=None):
        from core.vistrail.vistrail import Vistrail
        if klass is None:
            klass = Vistrail
        save_bundle = _DBLocator.load(
            self, klass.vtType,
            ThumbnailCache.getInstance().get_directory())
        if klass.vtType == DBWorkflow.vtType:
            wf = save_bundle
            klass = self.get_convert_klass(wf.vtType)
            klass.convert(wf)
            wf.locator = self
            return wf
        for obj in save_bundle.get_db_objs():
            klass = self.get_convert_klass(obj.vtType)
            klass.convert(obj)
            obj.locator = self
        return save_bundle

    def save(self, save_bundle):
        save_bundle = _DBLocator.save(self, save_bundle, False)
        for obj in save_bundle.get_db_objs():
            klass = self.get_convert_klass(obj.vtType)
            klass.convert(obj)
            obj.locator = self
        return save_bundle

    def save_as(self, save_bundle, version=None):
        save_bundle = _DBLocator.save(self, save_bundle, True, version)
        for obj in save_bundle.get_db_objs():
            klass = self.get_convert_klass(obj.vtType)
            klass.convert(obj)
            obj.locator = self
        # Need to copy images into thumbnail cache directory so references
        # won't become invalid if they are in a temp dir that gets destroyed
        # when the previous locator is closed
        import shutil
        thumb_cache = ThumbnailCache.getInstance()
        thumb_cache_dir = thumb_cache.get_directory()
        new_thumbnails = []
        for thumbnail in save_bundle.thumbnails:
            if os.path.dirname(thumbnail) == thumb_cache_dir:
                new_thumbnails.append(thumbnail)
            else:
                cachedir_thumbnail = os.path.join(thumb_cache_dir,
                                                  os.path.basename(thumbnail))
                try:
                    shutil.copyfile(thumbnail, cachedir_thumbnail)
                    new_thumbnails.append(cachedir_thumbnail)
                except Exception, e:
                    debug.critical('copying %s -> %s failed: %s' % \
                                       (thumbnail, cachedir_thumbnail, str(e)))
        save_bundle.thumbnails = new_thumbnails
        # Need to update thumbnail cache in case some references have changed
        thumb_cache.add_entries_from_files(save_bundle.thumbnails)
        return save_bundle
class QDBConnectionList(QtGui.QListWidget):
    """
    QDBConnection list is a widget to show the available databases

    """

    def __init__(self, parent=None):
        QtGui.QListWidget.__init__(self, parent)
        self.__list = ExtConnectionList(default_connections_file())
        self.setSelectionMode(QtGui.QAbstractItemView.SingleSelection)
        self.setIconSize(QtCore.QSize(32, 32))
        self.loadConnections()
        self.editAct = QtGui.QAction("Edit", self)
        self.editAct.setStatusTip("Edit the selected connection")
        self.connect(self.editAct, QtCore.SIGNAL("triggered()"), self.editConnection)

    def getCurrentItemId(self):
        """getCurrentItemId() -> int
        Returns the id of the selected item. If there is no item selected,
        it will return -1.

        """
        item = None
        if len(self.selectedItems()) > 0:
            item = self.selectedItems()[0]
        if item != None:
            return int(item.id)
        else:
            return -1

    def contextMenuEvent(self, e):
        """contextMenuEvent(e: QContextMenuEvent) -> None
        Shows a popup menu for the connection

        """
        item = self.currentItem()
        if item:
            menu = QtGui.QMenu()
            menu.addAction(self.editAct)
            menu.exec_(e.globalPos())

    def editConnection(self):
        """editConnection() -> None
        Method called to edit a connection. It will get the information
        from the selected connection and show the dialog so the user can
        update the fields
        
        """
        conn_id = self.getCurrentItemId()
        config = self.getConnectionInfo(conn_id)
        if config != None:
            config["create"] = False
            self.parent().showConnConfig(**config)

    def updateGUI(self):
        """updateGUI() -> None
        Update GUI list to be consistent with the the list of connections

        """
        self.clear()
        for (id, c) in self.__list.items():
            cItem = QDBConnectionListItem(CurrentTheme.DB_ICON, int(id), str(c.name))
            self.addItem(cItem)
        self.emit(QtCore.SIGNAL("reloadConnections"))

    def loadConnections(self):
        """loadConnections() -> None
        Loads the internal connections and updates the GUI

        """
        self.__list.clear()
        self.__list.load_connections()
        self.updateGUI()

    def getConnectionInfo(self, id):
        """getConnectionInfo(id: int) -> dict
        Returns info of ExtConnection """
        conn = self.__list.get_connection(id)
        key = str(conn.id) + "." + conn.name + "." + conn.host
        passwd = gui.application.VistrailsApplication.keyChain.get_key(key)
        if conn != None:
            config = {
                "id": conn.id,
                "name": conn.name,
                "host": conn.host,
                "port": conn.port,
                "user": conn.user,
                "passwd": passwd,
                "db": conn.database,
            }
        else:
            config = None
        return config

    def findConnectionInfo(self, host, port, db):
        """findConnection(host:str, port: int, db: str) -> dict
        Returns complete info of a connection with the given parameters

        """
        id = self.__list.find_db_connection(host, port, db)
        if id != -1:
            return self.getConnectionInfo(id)
        else:
            return None

    def removeConnection(self):
        """removeConnection() -> None
        Removes the selected connection

        """
        id = self.getCurrentItemId()
        self.takeItem(self.currentRow())
        self.__list.remove_connection(id)

    def setConnectionInfo(self, *args, **kwargs):
        """setConnectionInfo(id: int, name: str, host: str, port:int,
                     user:str, passwd:str, db:str) -> None
        If the connection exists it will update it, else it will add it

        """
        if kwargs.has_key("id"):
            id = kwargs["id"]
        if kwargs.has_key("name"):
            name = kwargs["name"]
        if kwargs.has_key("host"):
            host = kwargs["host"]
        if kwargs.has_key("port"):
            port = kwargs["port"]
        if kwargs.has_key("user"):
            user = kwargs["user"]
        if kwargs.has_key("passwd"):
            passwd = kwargs["passwd"]
        if kwargs.has_key("db"):
            db = kwargs["db"]

        conn = DBConnection(id=id, name=name, host=host, port=port, user=user, passwd="", database=db, dbtype="MySQL")

        if self.__list.has_connection(id):
            self.__list.set_connection(id, conn)
        else:
            if conn.id == -1:
                conn.id = self.__list.get_fresh_id()
            self.__list.add_connection(conn)
        self.updateGUI()
        key = str(conn.id) + "." + conn.name + "." + conn.host
        gui.application.VistrailsApplication.keyChain.set_key(key, passwd)
        return conn.id

    def setCurrentId(self, id):
        """setCurrentId(id: int) -> None
        set the connection with id 'id' to be the current selected connection

        """
        conn = self.__list.get_connection(id)

        for i in self.findItems(conn.name, QtCore.Qt.MatchFixedString):
            if i.id == id:
                self.setCurrentItem(i)
                break
        self.emit(QtCore.SIGNAL("reloadConnections"), id)

    def getCurrentConnConfig(self):
        """getCurrentConnConfig() -> dict
        Return dictionary of parameters of the current connection to pass
        to MySQLdb

        """
        conn_id = self.currentItem().id
        conn = self.__list.get_connection(conn_id)
        config = self.getConnectionInfo(conn_id)
        if conn.dbtype == "MySQL":
            # removing extra keyword arguments for MySQldb
            del config["name"]
        return config

    def getDBObjectList(self, conn_id, obj_type):
        """getDBObjectList(conn_id: int, obj_type : str) -> list
        Returns list of vistrails objects

        """
        conn = self.__list.get_connection(conn_id)
        config = self.getConnectionInfo(conn_id)
        if conn.dbtype == "MySQL":
            # removing extra keyword arguments for MySQldb
            config_name = config["name"]
            del config["name"]
            config_id = config["id"]
            del config["id"]
        vt_list = db.services.io.get_db_object_list(config, obj_type)
        if conn.dbtype == "MySQL":
            config["name"] = config_name
            config["id"] = config_id
        return vt_list