Example #1
0
    def delete_profile(self, profile):
        """
        Removes the folder and all config files for the specified profile.

        :param profile:
            either a Profile object or the name of an existing profile

        """

        # if the profile name was passed, get its Profile object
        if isinstance(profile, str):
            profile = Profile(self._profiles_dir, profile)

        # now make sure we have a Profile instance
        assert isinstance(profile, Profile)

        # don't allow deletion of the fallback; .lower() is redundant on
        # FALLBACK_PROFILE as it is, but just to future-proof it...
        if profile.name.lower() == FALLBACK_PROFILE.lower():
            raise exceptions.DeleteDefaultProfileError()

        # remove from available_profiles list
        self._profile_names.remove(profile.name)
        # and from cache
        if profile.name in self.__cache:
            self.__cache.remove(profile.name)

        try:
            # delete files in folder
            profile.delete_files()
            # remove folder
            profile.folder.rmdir()
        except OSError as e:
            self.LOGGER.error(e)
            raise exceptions.ProfileDeletionError(profile.name) from e
Example #2
0
    def rename(self, new_name):
        """

        :param str new_name:
        :return:
        """
        if self.name.lower() == FALLBACK_PROFILE.lower():
            raise exceptions.DeleteDefaultProfileError()

        new_dir = self.folder.with_name(new_name)  #type: Path

        ## if the folder exists or if it matches (case-insensitively)
        ##  one of the other profile dirs, raise exception
        if new_dir.exists() or \
            new_name.lower() in [f.name.lower() for f in self.folder.parent.iterdir()]:
            raise exceptions.ProfileExistsError(new_name)

        ## rename the directory (doesn't affect path obj)
        self.folder.rename(new_dir)

        ## verify that rename happened successfully
        if not new_dir.exists() or self.folder.exists():
            raise exceptions.ProfileError(
                self.name,
                f"Error while renaming profile '{self.name}' to '{new_name}'")

        ## update reference
        self.folder = new_dir
        self.name = new_name
Example #3
0
    def rename(self, new_name):
        """

        :param str new_name:
        :return:
        """
        if self.name.lower() == FALLBACK_PROFILE.lower():
            raise exceptions.DeleteDefaultProfileError()

        new_dir = self.folder.with_name(new_name) #type: Path

        ## if the folder exists or if it matches (case-insensitively)
        ##  one of the other profile dirs, raise exception
        if new_dir.exists() or \
            new_name.lower() in [f.name.lower() for f in self.folder.parent.iterdir()]:
            raise exceptions.ProfileExistsError(new_name)

        ## rename the directory (doesn't affect path obj)
        self.folder.rename(new_dir)

        ## verify that rename happened successfully
        if not new_dir.exists() or self.folder.exists():
            raise exceptions.ProfileError(
                self.name,
                "Error while renaming profile '{name}' to '{new_name}'"
                    .format(name=self.name, new_name=new_name))

        ## update reference
        self.folder = new_dir
        self.name = new_name
Example #4
0
    def delete_profile(self, profile):
        """
        Removes the folder and all config files for the specified profile.

        :param profile:
            either a Profile object or the name of an existing profile

        """

        # if the profile name was passed, get its Profile object
        if isinstance(profile, str):
            profile = Profile(self._profiles_dir, profile)

        # now make sure we have a Profile instance
        assert isinstance(profile, Profile)

        # don't allow deletion of the fallback; .lower() is redundant on
        # FALLBACK_PROFILE as it is, but just to future-proof it...
        if profile.name.lower() == FALLBACK_PROFILE.lower():
            raise exceptions.DeleteDefaultProfileError()

        # remove from available_profiles list
        self._profile_names.remove(profile.name)
        # and from cache
        if profile.name in self.__cache:
            self.__cache.remove(profile.name)

        try:
            # delete files in folder
            profile.delete_files()
            # remove folder
            profile.folder.rmdir()
        except OSError as e:
            self.LOGGER.error(e)
            raise exceptions.ProfileDeletionError(profile.name) from e