Exemplo n.º 1
0
def detectRtiFromFileName(fileName):
    """ Determines the type of RepoTreeItem to use given a file name.
        Uses a DirectoryRti for directories and an UnknownFileRti if the file
        extension doesn't match one of the registered RTI extensions.

        Returns (cls, regItem) tuple. Both the cls ond the regItem can be None.
        If the file is a directory, (DirectoryRti, None) is returned.
        If the file extension is not in the registry, (UnknownFileRti, None) is returned.
        If the cls cannot be imported (None, regItem) returned. regItem.exception will be set.
        Otherwise (cls, regItem) will be returned.
    """
    _, extension = os.path.splitext(fileName)
    if os.path.isdir(fileName):
        rtiRegItem = None
        cls = DirectoryRti
    else:
        try:
            rtiRegItem = globalRtiRegistry().getRtiRegItemByExtension(
                extension)
        except (KeyError):
            logger.debug(
                "No file RTI registered for extension: {}".format(extension))
            rtiRegItem = None
            cls = UnknownFileRti
        else:
            cls = rtiRegItem.getClass(tryImport=True)  # cls can be None

    return cls, rtiRegItem
Exemplo n.º 2
0
def _detectRtiFromFileName(fileName):
    """ Determines the type of RepoTreeItem to use given a file or directory name.
        Uses a DirectoryRti for directories without a registered extension and an UnknownFileRti
        if the file extension doesn't match one of the registered RTI globs.

        Returns (cls, regItem) tuple. Both the cls ond the regItem can be None.
        If the file is a directory without a registered extension, (DirectoryRti, None) is returned.
        If the file extension is not in the registry, (UnknownFileRti, None) is returned.
        If the cls cannot be imported (None, regItem) returned. regItem.exception will be set.
        Otherwise (cls, regItem) will be returned.

         Note that directories can have an extension (e.g. extdir archives). So it is not enough to
         just test if a file is a directory.
    """
    #_, extension = os.path.splitext(os.path.normpath(fileName))
    fullPath = os.path.normpath(os.path.abspath(fileName))
    rtiRegItem = globalRtiRegistry().getRtiRegItemByExtension(fullPath)
    if rtiRegItem is None:
        if os.path.isdir(fileName):
            cls = DirectoryRti
        else:
            logger.debug(
                "No file RTI registered for path: {}".format(fullPath))
            cls = UnknownFileRti
    else:
        cls = rtiRegItem.getClass(tryImport=True)  # cls can be None

    return cls, rtiRegItem
Exemplo n.º 3
0
    def __init__(self, settingsFile=None, setExceptHook=True):
        """ Constructor
            :param settingsFile: Config file from which the persistent settings are loaded.

            :param setExceptHook: Sets the global sys.except hook so that Qt shows a dialog box
                when an exception is raised.

                In debugging mode, the program will just quit in case of an exception. This is
                standard Python behavior but PyQt and PySide swallow exceptions by default (only a
                log message is displayed). The practice of swallowing exceptions fosters bad
                programming IHMO as it is easy to miss errors. I strongly recommend that you set
                the setExceptHook to True.
        """
        super(ArgosApplication, self).__init__()

        if not settingsFile:
            settingsFile = ArgosApplication.defaultSettingsFile()
            logger.debug("No config file specified. Using default: {}".format(
                settingsFile))

        self._settingsFile = ArgosApplication.userConfirmedSettingsFile(
            settingsFile,
            createWithoutConfirm=ArgosApplication.defaultSettingsFile())

        if setExceptHook:
            logger.debug("Setting sys.excepthook to Argos exception handling")
            sys.excepthook = handleException

        QtCore.qInstallMessageHandler(self.handleQtLogMessages)

        if DEBUGGING:
            self.qApplication.focusChanged.connect(
                self.focusChanged)  # for debugging

        self._repo = RepoTreeModel()
        self._rtiRegistry = globalRtiRegistry()
        self._inspectorRegistry = InspectorRegistry()

        self._mainWindows = []
        self._settingsSaved = False  # boolean to prevent saving settings twice
        self._recentFiles = [
        ]  # list of recently opened files ([timeStampe, fileName] per file).
        self._maxRecentFiles = 10  # Maximum size of recent file

        #self.qApplication.lastWindowClosed.connect(self.quit)
        self.qApplication.aboutToQuit.connect(self.aboutToQuitHandler)

        # Activate-actions for all windows
        self.windowActionGroup = QtWidgets.QActionGroup(self)
        self.windowActionGroup.setExclusive(True)

        # Call setup when the event loop starts.
        QtCore.QTimer.singleShot(0, self.setup)
Exemplo n.º 4
0
    def createOpenAsMenu(self, parent=None):
        """ Creates the submenu for the Open As choice
        """
        openAsMenu = QtWidgets.QMenu(parent=parent)
        openAsMenu.setTitle("Open Item As")

        registry = globalRtiRegistry()
        for rtiRegItem in registry.items:
            #rtiRegItem.tryImportClass()
            def createTrigger():
                """Function to create a closure with the regItem"""
                _rtiRegItem = rtiRegItem # keep reference in closure
                return lambda: self.reloadFileOfCurrentItem(_rtiRegItem)

            action = QtWidgets.QAction("{}".format(rtiRegItem.name), self,
                enabled=bool(rtiRegItem.successfullyImported is not False),
                triggered=createTrigger())
            openAsMenu.addAction(action)

        return openAsMenu
Exemplo n.º 5
0
    def __init__(self, setExceptHook=True):
        """ Constructor

            :param setExceptHook: Sets the global sys.except hook so that Qt shows a dialog box
                when an exception is raised.

                In debugging mode, the program will just quit in case of an exception. This is
                standard Python behavior but PyQt and PySide swallow exceptions by default (only a
                log message is displayed). The practice of swallowing exceptions fosters bad
                programming IHMO as it is easy to miss errors. I strongly recommend that you set
                the setExceptHook to True.
        """
        super(ArgosApplication, self).__init__()

        # Call initQtWidgetsApplicationInstance() so that the users can call argos.browse without
        # having to call it themselves.
        self._qApplication = initQApplication()

        if setExceptHook:
            logger.debug("Setting sys.excepthook to Argos exception handling")
            sys.excepthook = handleException

        #self.qApplication.focusChanged.connect(self.focusChanged) # for debugging

        self._repo = RepoTreeModel()
        self._rtiRegistry = globalRtiRegistry()
        self._inspectorRegistry = InspectorRegistry()

        self._profile = ''
        self._mainWindows = []
        self._settingsSaved = False  # boolean to prevent saving settings twice

        self.qApplication.lastWindowClosed.connect(self.quit)

        # Activate-actions for all windows
        self.windowActionGroup = QtWidgets.QActionGroup(self)
        self.windowActionGroup.setExclusive(True)

        # Call setup when the event loop starts.
        QtCore.QTimer.singleShot(0, self.setup)
Exemplo n.º 6
0
    def createOpenAsMenu(self, parent=None):
        """ Creates the submenu for the Open As choice
        """
        openAsMenu = QtWidgets.QMenu(parent=parent)
        openAsMenu.setTitle("Open Item As")

        registry = globalRtiRegistry()
        for rtiRegItem in registry.items:
            #rtiRegItem.tryImportClass()
            def createTrigger():
                """Function to create a closure with the regItem"""
                _rtiRegItem = rtiRegItem  # keep reference in closure
                return lambda: self.reloadFileOfCurrentItem(_rtiRegItem)

            action = QtWidgets.QAction(
                "{}".format(rtiRegItem.name),
                self,
                enabled=bool(rtiRegItem.successfullyImported is not False),
                triggered=createTrigger())
            openAsMenu.addAction(action)

        return openAsMenu
Exemplo n.º 7
0
    def _populateOpenAsMenu(self, openAsMenu):
        """ Repopulates the submenu for the Open Item choice (which is used to reload files).
        """
        registry = globalRtiRegistry()
        for rtiRegItem in (registry.items +
                           registry.extraItemsForOpenAsMenu()):

            if not rtiRegItem.triedImport:
                rtiRegItem.tryImportClass()

            def createTrigger():
                """Function to create a closure with the regItem"""
                _rtiRegItem = rtiRegItem  # keep reference in closure
                return lambda: self.reloadFileOfCurrentItem(_rtiRegItem)

            action = QtWidgets.QAction(
                "{}".format(rtiRegItem.name),
                self,
                enabled=bool(rtiRegItem.successfullyImported is not False),
                triggered=createTrigger(),
                icon=rtiRegItem.decoration)
            openAsMenu.addAction(action)

        return openAsMenu