Exemplo n.º 1
0
class TestBaseHandler(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        start_app()

    def setUp(self):
        self.base_handler = BaseRepositoryHandler(test_repository_url())
        self.fs_handler = FileSystemHandler(test_repository_url())

    def test_get_handler(self):
        handler = self.base_handler.get_handler(test_repository_url())
        self.assertEqual(handler.__class__.__name__, "FileSystemHandler")

    def test_is_git_repository(self):
        self.assertEqual(self.fs_handler.is_git_repository, False)

    def test_parse_metadata(self):
        """Testing parsing the metadata."""
        result, _ = self.fs_handler.fetch_metadata()
        self.assertTrue(result)
        collections = self.fs_handler.parse_metadata()
        # There's only 1 collection defined there
        self.assertEqual(len(collections), 1)

        expected_collection = {
            "status":
            0,
            "description":
            u"The collection contains various resources for testing",
            "tags":
            u"test, symbol, svg, processing",
            "register_name":
            u"test_collection",
            "repository_url":
            test_repository_url(),
            "name":
            u"Akbar's Test Collection",
            "author":
            u"Akbar Gumbira",
            "author_email":
            u"*****@*****.**",
            "qgis_min_version":
            u"2.0",
            "qgis_max_version":
            u"3.99",
            "license":
            "GNU GPL",
            "license_url":
            "%s/collections/test_collection/LICENSE.txt" %
            test_repository_url(),
            "preview": [
                "%s/collections/test_collection/preview/prev_1.png" %
                test_repository_url(),
                "%s/collections/test_collection/preview/prev_2.png" %
                test_repository_url(),
            ],
        }
        self.assertDictEqual(collections[0], expected_collection)
class TestBaseHandler(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        start_app()

    def setUp(self):
        self.base_handler = BaseRepositoryHandler(test_repository_url())
        self.fs_handler = FileSystemHandler(test_repository_url())

    def test_get_handler(self):
        handler = self.base_handler.get_handler(test_repository_url())
        self.assertEqual(handler.__class__.__name__, 'FileSystemHandler')

    def test_is_git_repository(self):
        self.assertEqual(self.fs_handler.is_git_repository, False)

    def test_parse_metadata(self):
        """Testing parsing the metadata."""
        result, _ = self.fs_handler.fetch_metadata()
        self.assertTrue(result)
        collections = self.fs_handler.parse_metadata()
        # There's only 1 collection defined there
        self.assertEqual(len(collections), 1)

        expected_collection = {
            'status':
            0,
            'description':
            u'The collection contains various resources for testing',
            'tags':
            u'test, symbol, svg, processing',
            'register_name':
            u'test_collection',
            'repository_url':
            test_repository_url(),
            'name':
            u"Akbar's Test Collection",
            'author':
            u'Akbar Gumbira',
            'author_email':
            u'*****@*****.**',
            'qgis_min_version':
            u'2.0',
            'qgis_max_version':
            u'3.99',
            'license':
            'GNU GPL',
            'license_url':
            '%s/collections/test_collection/LICENSE.txt' %
            test_repository_url(),
            'preview': [
                '%s/collections/test_collection/preview/prev_1.png' %
                test_repository_url(),
                '%s/collections/test_collection/preview/prev_2.png' %
                test_repository_url()
            ]
        }
        self.assertDictEqual(collections[0], expected_collection)
    def download(self, collection_id):
        """Download a collection given the id.

        :param collection_id: The id of the collection about to be downloaded.
        :type collection_id: str
        """
        repo_url = config.COLLECTIONS[collection_id]['repository_url']
        repo_handler = BaseRepositoryHandler.get_handler(repo_url)
        if repo_handler is None:
            message = 'There is no handler available for the given URL!'
            LOGGER.exception(message)
            raise Exception(message)
        register_name = config.COLLECTIONS[collection_id]['register_name']
        status, information = repo_handler.download_collection(
            collection_id, register_name)
        return status, information
Exemplo n.º 4
0
    def download(self, collection_id):
        """Download a collection given the id.

        :param collection_id: The id of the collection about to be downloaded.
        :type collection_id: str
        """
        repo_url = config.COLLECTIONS[collection_id]['repository_url']
        repo_handler = BaseRepositoryHandler.get_handler(repo_url)
        if repo_handler is None:
            message = 'There is no handler available for the given URL!'
            LOGGER.exception(message)
            raise Exception(message)
        register_name = config.COLLECTIONS[collection_id]['register_name']
        status, information = repo_handler.download_collection(
            collection_id, register_name)
        return status, information
    def download(self, collection_id):
        """Download a collection given its ID.

        :param collection_id: The ID of the collection to be downloaded.
        :type collection_id: str
        :return: status (True or False), information from the repo handler
        :rtype: (boolean, string)
        """
        repo_url = config.COLLECTIONS[collection_id]['repository_url']
        repo_handler = BaseRepositoryHandler.get_handler(repo_url)
        if repo_handler is None:
            message = 'There is no handler available for ' + str(repo_url)
            LOGGER.error(message)
            return False, message
        register_name = config.COLLECTIONS[collection_id]['register_name']
        status, information = repo_handler.download_collection(
            collection_id, register_name)
        return status, information
class TestBaseHandler(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        start_app()

    def setUp(self):
        self.base_handler = BaseRepositoryHandler(test_repository_url())
        self.fs_handler = FileSystemHandler(test_repository_url())

    def test_get_handler(self):
        handler = self.base_handler.get_handler(test_repository_url())
        self.assertEqual(handler.__class__.__name__, 'FileSystemHandler')

    def test_is_git_repository(self):
        self.assertEqual(self.fs_handler.is_git_repository, False)

    def test_parse_metadata(self):
        """Testing parsing the metadata."""
        result, _ = self.fs_handler.fetch_metadata()
        self.assertTrue(result)
        collections = self.fs_handler.parse_metadata()
        # There's only 1 collection defined there
        self.assertEqual(len(collections), 1)

        expected_collection = {
            'status': 0,
            'description':
                u'The collection contains various resources for testing',
            'tags': u'test, symbol, svg, processing',
            'register_name': u'test_collection',
            'repository_url': test_repository_url(),
            'name': u"Akbar's Test Collection",
            'author': u'Akbar Gumbira',
            'author_email': u'*****@*****.**',
            'qgis_min_version': u'2.0',
            'qgis_max_version': u'3.99',
            'license': 'GNU GPL',
            'license_url': '%s/collections/test_collection/LICENSE.txt' %test_repository_url(),
            'preview': [
                '%s/collections/test_collection/preview/prev_1.png' % test_repository_url(),
                '%s/collections/test_collection/preview/prev_2.png' % test_repository_url()
            ]
        }
        self.assertDictEqual(collections[0], expected_collection)
    def add_directory(self, repo_name, url, auth_cfg=None):
        """Add a directory to settings and add the collections from that repo.

        :param repo_name: The name of the repository
        :type repo_name: str

        :param url: The URL of the repository
        :type url: str
        """
        repo_handler = BaseRepositoryHandler.get_handler(url)
        if repo_handler is None:
            LOGGER.warning("There is no handler available for URL '" +
                           str(url) + "'!")
        if auth_cfg:
            repo_handler.auth_cfg = auth_cfg

        # Fetch metadata
        status, fetcherror = repo_handler.fetch_metadata()
        if status:
            # Parse metadata
            try:
                collections = repo_handler.parse_metadata()
            except MetadataError as me:
                metadata_warning = ("Error parsing metadata for " +
                                    str(repo_name) + ":\n" + str(me))
                LOGGER.warning(metadata_warning)
                return False, metadata_warning

            # Add the repository and its collections
            self._repositories[repo_name] = collections
            self.rebuild_collections()
            # Add to QgsSettings
            settings = QgsSettings()
            settings.beginGroup(repo_settings_group())
            settings.setValue(repo_name + '/url', url)
            if auth_cfg:
                settings.setValue(repo_name + '/auth_cfg', auth_cfg)
            settings.endGroup()
            # Serialize repositories every time we successfully added a repo
            self.serialize_repositories()

        return status, fetcherror
    def add_directory(self, repo_name, url, auth_cfg=None):
        """Add a directory to settings and add the collections from that repo.

        :param repo_name: The name of the repository
        :type repo_name: str

        :param url: The URL of the repository
        :type url: str
        """
        repo_handler = BaseRepositoryHandler.get_handler(url)
        if repo_handler is None:
            raise Exception('There is no handler available for the given URL!')

        if auth_cfg:
            repo_handler.auth_cfg = auth_cfg

        # Fetch metadata
        status, description = repo_handler.fetch_metadata()
        if status:
            # Parse metadata
            try:
                collections = repo_handler.parse_metadata()
            except MetadataError:
                raise
            # Add the repo and the collections
            self._repositories[repo_name] = collections
            self.rebuild_collections()
            # Add to QSettings
            settings = QSettings()
            settings.beginGroup(repo_settings_group())
            settings.setValue(repo_name + '/url', url)
            if auth_cfg:
                settings.setValue(repo_name + '/auth_cfg', auth_cfg)
            settings.endGroup()
            # Serialize repositories every time we successfully added a repo
            self.serialize_repositories()

        return status, description
Exemplo n.º 9
0
    def add_directory(self, repo_name, url, auth_cfg=None):
        """Add a directory to settings and add the collections from that repo.

        :param repo_name: The name of the repository
        :type repo_name: str

        :param url: The URL of the repository
        :type url: str
        """
        repo_handler = BaseRepositoryHandler.get_handler(url)
        if repo_handler is None:
            raise Exception('There is no handler available for the given URL!')

        if auth_cfg:
            repo_handler.auth_cfg = auth_cfg

        # Fetch metadata
        status, description = repo_handler.fetch_metadata()
        if status:
            # Parse metadata
            try:
                collections = repo_handler.parse_metadata()
            except MetadataError:
                raise
            # Add the repo and the collections
            self._repositories[repo_name] = collections
            self.rebuild_collections()
            # Add to QSettings
            settings = QSettings()
            settings.beginGroup(repo_settings_group())
            settings.setValue(repo_name + '/url', url)
            if auth_cfg:
                settings.setValue(repo_name + '/auth_cfg', auth_cfg)
            settings.endGroup()
            # Serialize repositories every time we successfully added a repo
            self.serialize_repositories()

        return status, description
Exemplo n.º 10
0
 def setUp(self):
     self.base_handler = BaseRepositoryHandler(test_repository_url())
     self.fs_handler = FileSystemHandler(test_repository_url())
    def edit_directory(
        self, old_repo_name, new_repo_name, old_url, new_url, new_auth_cfg
    ):
        """Edit the directory of repositories and update the
        collections.
        Also used to reload repositories (old == new for url and repo_name)

        :param old_repo_name: The old name of the repository
        :type old_repo_name: str
        :param new_repo_name: The new name of the repository
        :type new_repo_name: str
        :param old_url: The old URL of the repository
        :type old_url: str
        :param new_url: The new URL of the repository
        :type new_url: str
        :param new_auth_cfg: The auth config id.
        :type new_auth_cfg: str
        :return: (status, error)
        :rtype: (boolean, string)
        """
        old_collections = self._repositories.get(old_repo_name, [])
        if (old_repo_name != new_repo_name) and (old_url == new_url):
            # Renaming a repository (same URL)
            for old_collection in old_collections:
                coll_id = self._collections_manager.get_collection_id(
                    old_collection["register_name"], old_collection["repository_url"]
                )
                old_path = local_collection_path(coll_id)
                # Update the repository name for this collection
                config.COLLECTIONS[coll_id]["repository_name"] = new_repo_name
                new_path = local_collection_path(coll_id)
                # If the repository is renamed (same URL), the directories
                # of its collections should be renamed accordingly (so that
                # they remain accessible)
                if old_path.exists():
                    old_path.rename(new_path)
            new_collections = old_collections
            status = True
            fetcherror = ""
        else:
            # old_repo_name == new_repo_name and old_url == new_url
            # or new_url != old_url
            # Fetch the metadata (metadata.ini) from the new url
            repo_handler = BaseRepositoryHandler.get_handler(new_url)
            if repo_handler is None:
                repo_warning = "No handler for URL '" + str(new_url) + "'!"
                LOGGER.warning(repo_warning)
                return (False, repo_warning)
            if new_auth_cfg:
                repo_handler.auth_cfg = new_auth_cfg
            status, fetcherror = repo_handler.fetch_metadata()
            if status:
                # Parse metadata
                try:
                    new_collections = repo_handler.parse_metadata()
                except MetadataError as me:
                    metadata_warning = (
                        "Error parsing metadata for "
                        + str(new_repo_name)
                        + ":\n"
                        + str(me)
                    )
                    LOGGER.warning(metadata_warning)
                    return (False, metadata_warning)
                    # raise MetadataError(metadata_warning)
                # Get all the installed collections from the old repository
                installed_old_collections = []
                for old_collection in old_collections:
                    if old_collection["status"] == COLLECTION_INSTALLED_STATUS:
                        installed_old_collections.append(old_collection)
                # Handling installed collections
                # An old collection that is present in the new location
                # (URL) is identified by its register name.
                # Cases for installed collections:
                # 1. Old collection exists in the new, same URL: use the new
                # one, else: update the status to INSTALLED
                # 2. Old collection exists in the new, different URL: keep them
                # both (add the old one). Because they should be treated as
                # different collections
                # 3. Old collection doesn't exist in the new, same URL: keep
                # the old collection
                # 4. Old collection doesn't exist in the new, different URL:
                # same as 3
                for installed_collection in installed_old_collections:
                    reg_name = installed_collection["register_name"]
                    is_present = False
                    for n_coll in new_collections:
                        # Look for collections that are already present
                        if n_coll["register_name"] == reg_name:
                            # Already present
                            is_present = True
                            if old_url == new_url:
                                # Set the status to installed
                                n_coll["status"] = COLLECTION_INSTALLED_STATUS
                                # Keep the collection statistics
                                for key in installed_collection.keys():
                                    if key in [
                                        "models",
                                        "processing",
                                        "rscripts",
                                        "style",
                                        "svg",
                                        "symbol",
                                        "expressions",
                                    ]:
                                        n_coll[key] = installed_collection[key]
                            else:
                                # Different repository URLs, so append
                                new_collections.append(installed_collection)
                            break
                    if not is_present:
                        new_collections.append(installed_collection)
        # Remove the old repository and add the new one
        self._repositories.pop(old_repo_name, None)
        self._repositories[new_repo_name] = new_collections
        self.rebuild_collections()
        # Update QgsSettings
        settings = QgsSettings()
        settings.beginGroup(repo_settings_group())
        settings.remove(old_repo_name)
        settings.setValue(new_repo_name + "/url", new_url)
        settings.setValue(new_repo_name + "/auth_cfg", new_auth_cfg)
        settings.endGroup()
        # Serialize repositories every time we successfully edited repo
        self.serialize_repositories()
        return status, fetcherror
 def setUp(self):
     self.base_handler = BaseRepositoryHandler(test_repository_url())
     self.fs_handler = FileSystemHandler(test_repository_url())
    def edit_directory(
            self,
            old_repo_name,
            new_repo_name,
            old_url,
            new_url,
            new_auth_cfg):
        """Edit a directory and update the collections.

        :param old_repo_name: The old name of the repository
        :type old_repo_name: str

        :param new_repo_name: The new name of the repository
        :type new_repo_name: str

        :param old_url: The old URL of the repository
        :type old_url: str

        :param new_url: The new URL of the repository
        :type new_url: str

        :param new_auth_cfg: The auth config id.
        :type new_auth_cfg: str
        """
        # Fetch the metadata from the new url
        repo_handler = BaseRepositoryHandler.get_handler(new_url)
        if repo_handler is None:
            raise Exception('There is no handler available for the given URL!')

        if new_auth_cfg:
            repo_handler.auth_cfg = new_auth_cfg

        status, description = repo_handler.fetch_metadata()

        if status:
            # Parse metadata
            try:
                new_collections = repo_handler.parse_metadata()
            except MetadataError:
                raise

            old_collections = self._repositories.get(old_repo_name, [])
            # Get all the installed collections from the old repository
            installed_old_collections = []
            for old_collection in old_collections:
                if old_collection['status'] == COLLECTION_INSTALLED_STATUS:
                    installed_old_collections.append(old_collection)

            # Beware of the installed collections
            # Old collection exists in the new URL are identified by its
            # register name. Cases for installed collections:
            # 1. Old collection exists in the new URL, same URL: use the new
            # one, update the status to INSTALLED
            # 2. Old collection exists in the new URL, different URL: keep them
            # both (add the old one). Because they should be treated as
            # different collection
            # 3. Old collection doesn't exist in the new URL, same URL: keep
            # the old collection
            # 4. Old collection doesn't exist in the new URL, different URL:
            # same with 3
            for installed_collection in installed_old_collections:
                reg_name = installed_collection['register_name']
                is_present = False

                for collection in new_collections:
                    if collection['register_name'] == reg_name:
                        is_present = True
                        if old_url == new_url:
                            collection['status'] = COLLECTION_INSTALLED_STATUS
                        else:
                            new_collections.append(installed_collection)
                        break

                # Get to this point could be because it's present or the old
                # installed collection doesn't exist in the new URL
                if not is_present:
                    new_collections.append(installed_collection)

            # Remove old repository and add new one
            self._repositories.pop(old_repo_name, None)
            self._repositories[new_repo_name] = new_collections
            self.rebuild_collections()

            # Update QSettings
            settings = QSettings()
            settings.beginGroup(repo_settings_group())
            settings.remove(old_repo_name)
            settings.setValue(new_repo_name + '/url', new_url)
            settings.setValue(new_repo_name + '/auth_cfg', new_auth_cfg)
            settings.endGroup()
            # Serialize repositories every time we successfully edited repo
            self.serialize_repositories()
        return status, description
Exemplo n.º 14
0
    def edit_directory(self, old_repo_name, new_repo_name, old_url, new_url,
                       new_auth_cfg):
        """Edit a directory and update the collections.

        :param old_repo_name: The old name of the repository
        :type old_repo_name: str

        :param new_repo_name: The new name of the repository
        :type new_repo_name: str

        :param old_url: The old URL of the repository
        :type old_url: str

        :param new_url: The new URL of the repository
        :type new_url: str

        :param new_auth_cfg: The auth config id.
        :type new_auth_cfg: str
        """
        # Fetch the metadata from the new url
        repo_handler = BaseRepositoryHandler.get_handler(new_url)
        if repo_handler is None:
            raise Exception('There is no handler available for the given URL!')

        if new_auth_cfg:
            repo_handler.auth_cfg = new_auth_cfg

        status, description = repo_handler.fetch_metadata()

        if status:
            # Parse metadata
            try:
                new_collections = repo_handler.parse_metadata()
            except MetadataError:
                raise

            old_collections = self._repositories.get(old_repo_name, [])
            # Get all the installed collections from the old repository
            installed_old_collections = []
            for old_collection in old_collections:
                if old_collection['status'] == COLLECTION_INSTALLED_STATUS:
                    installed_old_collections.append(old_collection)

            # Beware of the installed collections
            # Old collection exists in the new URL are identified by its
            # register name. Cases for installed collections:
            # 1. Old collection exists in the new URL, same URL: use the new
            # one, update the status to INSTALLED
            # 2. Old collection exists in the new URL, different URL: keep them
            # both (add the old one). Because they should be treated as
            # different collection
            # 3. Old collection doesn't exist in the new URL, same URL: keep
            # the old collection
            # 4. Old collection doesn't exist in the new URL, different URL:
            # same with 3
            for installed_collection in installed_old_collections:
                reg_name = installed_collection['register_name']
                is_present = False

                for collection in new_collections:
                    if collection['register_name'] == reg_name:
                        is_present = True
                        if old_url == new_url:
                            collection['status'] = COLLECTION_INSTALLED_STATUS
                        else:
                            new_collections.append(installed_collection)
                        break

                # Get to this point could be because it's present or the old
                # installed collection doesn't exist in the new URL
                if not is_present:
                    new_collections.append(installed_collection)

            # Remove old repository and add new one
            self._repositories.pop(old_repo_name, None)
            self._repositories[new_repo_name] = new_collections
            self.rebuild_collections()

            # Update QSettings
            settings = QSettings()
            settings.beginGroup(repo_settings_group())
            settings.remove(old_repo_name)
            settings.setValue(new_repo_name + '/url', new_url)
            settings.setValue(new_repo_name + '/auth_cfg', new_auth_cfg)
            settings.endGroup()
            # Serialize repositories every time we successfully edited repo
            self.serialize_repositories()
        return status, description