Exemple #1
0
    def renameProfile(self, oldName, newName):
        """Rename a profile.
		@param oldName: The current name of the profile.
		@type oldName: str
		@param newName: The new name for the profile.
		@type newName: str
		@raise LookupError: If the profile doesn't exist.
		@raise ValueError: If a profile with the new name already exists.
		"""
        if globalVars.appArgs.secure:
            return
        if newName == oldName:
            return
        if not newName:
            raise ValueError("Missing newName")
        oldFn = self._getProfileFn(oldName)
        newFn = self._getProfileFn(newName)
        if not os.path.isfile(oldFn):
            raise LookupError("No such profile: %s" % oldName)
        # Windows file names are case insensitive,
        # so only test for file existence if the names don't match case insensitively.
        if oldName.lower() != newName.lower() and os.path.isfile(newFn):
            raise ValueError(
                "A profile with the same name already exists: %s" % newName)

        os.rename(oldFn, newFn)
        # Update any associated triggers.
        allTriggers = self.triggersToProfiles
        saveTrigs = False
        for trigSpec, trigProfile in allTriggers.items():
            if trigProfile == oldName:
                allTriggers[trigSpec] = newName
                saveTrigs = True
        if saveTrigs:
            self.saveProfileTriggers()
        # Rename the script for the profile.
        # Import late to avoid circular import.
        from globalCommands import ConfigProfileActivationCommands
        ConfigProfileActivationCommands.updateScriptForRenamedProfile(
            oldName, newName)
        try:
            profile = self._profileCache.pop(oldName)
        except KeyError:
            # The profile hasn't been loaded, so there's nothing more to do.
            return
        profile.name = newName
        self._profileCache[newName] = profile
        try:
            self._dirtyProfiles.remove(oldName)
        except KeyError:
            # The profile wasn't dirty.
            return
        self._dirtyProfiles.add(newName)
Exemple #2
0
    def deleteProfile(self, name):
        """Delete a profile.
		@param name: The name of the profile to delete.
		@type name: str
		@raise LookupError: If the profile doesn't exist.
		"""
        if globalVars.appArgs.secure:
            return
        fn = self._getProfileFn(name)
        if not os.path.isfile(fn):
            raise LookupError("No such profile: %s" % name)
        os.remove(fn)
        # Remove the script for the deleted profile from the script collector.
        # Import late to avoid circular import.
        from globalCommands import ConfigProfileActivationCommands
        ConfigProfileActivationCommands.removeScriptForProfile(name)
        try:
            del self._profileCache[name]
        except KeyError:
            pass
        # Remove any triggers associated with this profile.
        allTriggers = self.triggersToProfiles
        # You can't delete from a dict while iterating through it.
        delTrigs = [
            trigSpec for trigSpec, trigProfile in allTriggers.items()
            if trigProfile == name
        ]
        if delTrigs:
            for trigSpec in delTrigs:
                del allTriggers[trigSpec]
            self.saveProfileTriggers()
        # Check if this profile was active.
        delProfile = None
        for index in range(len(self.profiles) - 1, -1, -1):
            profile = self.profiles[index]
            if profile.name == name:
                # Deactivate it.
                del self.profiles[index]
                delProfile = profile
        if not delProfile:
            return
        self._handleProfileSwitch()
        if self._suspendedTriggers:
            # Remove any suspended triggers referring to this profile.
            # As the dictionary changes during iteration, wrap this inside a list call.
            for trigger in list(self._suspendedTriggers):
                if trigger._profile == delProfile:
                    del self._suspendedTriggers[trigger]
Exemple #3
0
	def createProfile(self, name):
		"""Create a profile.
		@param name: The name of the profile to create.
		@type name: str
		@raise ValueError: If a profile with this name already exists.
		"""
		if globalVars.appArgs.secure:
			return
		fn = self._getProfileFn(name)
		if os.path.isfile(fn):
			raise ValueError("A profile with the same name already exists: %s" % name)
		# Just create an empty file to make sure we can.
		open(fn, "w").close()
		# Register a script for the new profile.
		# Import late to avoid circular import.
		from globalCommands import ConfigProfileActivationCommands
		ConfigProfileActivationCommands.addScriptForProfile(name)