diff --git a/qgis_resource_sharing/collection_manager.py b/qgis_resource_sharing/collection_manager.py index 62a1a4db..01a17547 100644 --- a/qgis_resource_sharing/collection_manager.py +++ b/qgis_resource_sharing/collection_manager.py @@ -7,10 +7,7 @@ from qgis.PyQt.QtCore import QObject, pyqtSignal from qgis_resource_sharing import config -from qgis_resource_sharing.config import ( - COLLECTION_INSTALLED_STATUS, - COLLECTION_NOT_INSTALLED_STATUS, -) +from qgis_resource_sharing.config import CollectionStatus from qgis_resource_sharing.repository_handler import BaseRepositoryHandler from qgis_resource_sharing.resource_handler import BaseResourceHandler from qgis_resource_sharing.utilities import ( @@ -105,7 +102,7 @@ def get_collection(self, collection_id: str) -> Dict[str, str]: html = html + ".
Reinstall to update" if resource_types == 0: html = "No standard resources found." - if config.COLLECTIONS[collection_id]["status"] != COLLECTION_INSTALLED_STATUS: + if config.COLLECTIONS[collection_id]["status"] != CollectionStatus.INSTALLED: html = "Unknown before installation" config.COLLECTIONS[collection_id]["resources_html"] = html @@ -125,7 +122,7 @@ def get_installed_collections(self, repo_url=None): """ installed_collections = {} for collection_id, collection in config.COLLECTIONS.items(): - if collection["status"] != COLLECTION_INSTALLED_STATUS: + if collection["status"] != CollectionStatus.INSTALLED: continue if repo_url: @@ -166,7 +163,7 @@ def install(self, collection_id): resource_handler_instance = resource_handler(collection_id) resource_handler_instance.install() - config.COLLECTIONS[collection_id]["status"] = COLLECTION_INSTALLED_STATUS + config.COLLECTIONS[collection_id]["status"] = CollectionStatus.INSTALLED def uninstall(self, collection_id): """Uninstall the collection. @@ -184,7 +181,7 @@ def uninstall(self, collection_id): if collection_dir.exists(): shutil.rmtree(str(collection_dir)) - config.COLLECTIONS[collection_id]["status"] = COLLECTION_NOT_INSTALLED_STATUS + config.COLLECTIONS[collection_id]["status"] = CollectionStatus.NOT_INSTALLED # Should items from other installed collections be reinstalled # "automatically"? diff --git a/qgis_resource_sharing/config.py b/qgis_resource_sharing/config.py index 601d6916..ce804cb6 100644 --- a/qgis_resource_sharing/config.py +++ b/qgis_resource_sharing/config.py @@ -1,3 +1,5 @@ +from enum import IntEnum + """ # Put the COLLECTIONS object (dict) in the module namespace # (http://effbot.org/pyfaq/how-do-i-share-global-variables-across-modules.htm) @@ -15,7 +17,7 @@ 'author_email': email, 'repository_url': self.url, 'repository_name': , - 'status': COLLECTION_NOT_INSTALLED_STATUS, + 'status': CollectionStatus.NOT_INSTALLED, 'name': parser.get(collection, 'name'), 'tags': parser.get(collection, 'tags'), 'description': parser.get(collection, 'description'), @@ -35,8 +37,13 @@ } """ -COLLECTION_NOT_INSTALLED_STATUS = 0 -COLLECTION_INSTALLED_STATUS = 1 -COLLECTION_ALL_STATUS = 2 + +class CollectionStatus(IntEnum): + """Describe the status of a collection""" + + NOT_INSTALLED = 0 + INSTALLED = 1 + ALL = 2 + COLLECTIONS = {} diff --git a/qgis_resource_sharing/gui/custom_sort_filter_proxy.py b/qgis_resource_sharing/gui/custom_sort_filter_proxy.py index 467cbbf8..ffbb5537 100644 --- a/qgis_resource_sharing/gui/custom_sort_filter_proxy.py +++ b/qgis_resource_sharing/gui/custom_sort_filter_proxy.py @@ -1,6 +1,6 @@ from qgis.PyQt.QtCore import QSortFilterProxyModel, Qt -from qgis_resource_sharing.config import COLLECTION_INSTALLED_STATUS +from qgis_resource_sharing.config import CollectionStatus COLLECTION_NAME_ROLE = Qt.UserRole + 1 COLLECTION_DESCRIPTION_ROLE = Qt.UserRole + 2 @@ -54,10 +54,10 @@ def filterAcceptsRow(self, row_num, source_parent): >= 0 ) - if self.accepted_status == COLLECTION_INSTALLED_STATUS: + if self.accepted_status == CollectionStatus.INSTALLED: # For installed collection status collection_status = self.sourceModel().data(index, COLLECTION_STATUS_ROLE) - status = collection_status == COLLECTION_INSTALLED_STATUS + status = collection_status == CollectionStatus.INSTALLED else: status = True diff --git a/qgis_resource_sharing/gui/resource_sharing_details.py b/qgis_resource_sharing/gui/resource_sharing_details.py index a733460a..23d92804 100644 --- a/qgis_resource_sharing/gui/resource_sharing_details.py +++ b/qgis_resource_sharing/gui/resource_sharing_details.py @@ -29,7 +29,7 @@ from qgis.PyQt.QtWidgets import QLabel, QWidget from qgis_resource_sharing.collection_manager import CollectionManager -from qgis_resource_sharing.config import COLLECTION_INSTALLED_STATUS +from qgis_resource_sharing.config import CollectionStatus from qgis_resource_sharing.utilities import ui_path # -- GLOBALS @@ -79,7 +79,7 @@ def set_content(self, collection_id: str) -> None: self.tagsContent.setText(collection["tags"]) # resources - show_resources_html = collection["status"] == COLLECTION_INSTALLED_STATUS + show_resources_html = collection["status"] == CollectionStatus.INSTALLED self.resourcesLabel.setVisible(show_resources_html) self.resourcesContent.setVisible(show_resources_html) self.resourcesContent.setText(collection["resources_html"]) diff --git a/qgis_resource_sharing/gui/resource_sharing_dialog.py b/qgis_resource_sharing/gui/resource_sharing_dialog.py index f5840347..db443fa2 100644 --- a/qgis_resource_sharing/gui/resource_sharing_dialog.py +++ b/qgis_resource_sharing/gui/resource_sharing_dialog.py @@ -44,10 +44,7 @@ CollectionInstaller, CollectionManager, ) -from qgis_resource_sharing.config import ( - COLLECTION_ALL_STATUS, - COLLECTION_INSTALLED_STATUS, -) +from qgis_resource_sharing.config import CollectionStatus from qgis_resource_sharing.gui.custom_sort_filter_proxy import ( COLLECTION_AUTHOR_ROLE, COLLECTION_DESCRIPTION_ROLE, @@ -184,7 +181,7 @@ def set_current_tab(self, index): # Not settings, must be Collections (all or installed) if index == 1: # Installed collections - self.collection_proxy.accepted_status = COLLECTION_INSTALLED_STATUS + self.collection_proxy.accepted_status = CollectionStatus.INSTALLED # Set the web view title = self.tr("Installed Collections") description = self.tr( @@ -192,7 +189,7 @@ def set_current_tab(self, index): ) else: # All collections (0) - self.collection_proxy.accepted_status = COLLECTION_ALL_STATUS + self.collection_proxy.accepted_status = CollectionStatus.ALL # Set the web view title = self.tr("All Collections") description = self.tr( @@ -544,7 +541,7 @@ def uninstall_collection(self): self._sel_coll_id = current_coll_id # Update buttons status = config.COLLECTIONS[current_coll_id]["status"] - if status == COLLECTION_INSTALLED_STATUS: + if status == CollectionStatus.INSTALLED: self.button_install.setEnabled(True) self.button_install.setText("Reinstall") self.button_open.setEnabled(True) @@ -698,7 +695,7 @@ def on_list_view_collections_clicked(self, index): self._sel_coll_id = collection_id # Enable / disable buttons status = config.COLLECTIONS[self._sel_coll_id]["status"] - is_installed = status == COLLECTION_INSTALLED_STATUS + is_installed = status == CollectionStatus.INSTALLED if is_installed: self.button_install.setEnabled(True) self.button_install.setText("Reinstall") diff --git a/qgis_resource_sharing/repository_handler/base.py b/qgis_resource_sharing/repository_handler/base.py index e07c43c2..77aad7a6 100644 --- a/qgis_resource_sharing/repository_handler/base.py +++ b/qgis_resource_sharing/repository_handler/base.py @@ -10,7 +10,7 @@ from qgis.core import Qgis from six import add_metaclass -from qgis_resource_sharing.config import COLLECTION_NOT_INSTALLED_STATUS +from qgis_resource_sharing.config import CollectionStatus from qgis_resource_sharing.exception import MetadataError from qgis_resource_sharing.network_manager import NetworkManager from qgis_resource_sharing.version_compare import isCompatible @@ -240,7 +240,7 @@ def parse_metadata(self) -> List[Dict]: "register_name": collection, "repository_name": "", "repository_url": self.url, - "status": COLLECTION_NOT_INSTALLED_STATUS, + "status": CollectionStatus.NOT_INSTALLED, "tags": tags, } collections.append(collection_dict) diff --git a/qgis_resource_sharing/repository_manager.py b/qgis_resource_sharing/repository_manager.py index 02eac4c4..607f945f 100644 --- a/qgis_resource_sharing/repository_manager.py +++ b/qgis_resource_sharing/repository_manager.py @@ -7,7 +7,7 @@ from qgis_resource_sharing import config from qgis_resource_sharing.collection_manager import CollectionManager -from qgis_resource_sharing.config import COLLECTION_INSTALLED_STATUS +from qgis_resource_sharing.config import CollectionStatus from qgis_resource_sharing.exception import MetadataError from qgis_resource_sharing.network_manager import NetworkManager from qgis_resource_sharing.repository_handler import BaseRepositoryHandler @@ -48,7 +48,7 @@ def __init__(self): 'author': author, 'author_email': email, 'repository_url': self.url, - 'status': COLLECTION_NOT_INSTALLED_STATUS, + 'status': CollectionStatus.NOT_INSTALLED, 'name': parser.get(collection, 'name'), 'tags': parser.get(collection, 'tags'), 'description': parser.get(collection, 'description'), @@ -276,7 +276,7 @@ def edit_directory( # 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: + if old_collection["status"] == CollectionStatus.INSTALLED: installed_old_collections.append(old_collection) # Handling installed collections # An old collection that is present in the new location @@ -301,7 +301,7 @@ def edit_directory( is_present = True if old_url == new_url: # Set the status to installed - n_coll["status"] = COLLECTION_INSTALLED_STATUS + n_coll["status"] = CollectionStatus.INSTALLED # Keep the collection statistics for key in installed_collection.keys(): if key in [ @@ -382,7 +382,7 @@ def rebuild_collections(self): # Check the file system to see if the collection exists. # If not, also uninstall its resources current_status = config.COLLECTIONS[collection_id]["status"] - if current_status == COLLECTION_INSTALLED_STATUS: + if current_status == CollectionStatus.INSTALLED: if not collection_path.exists(): # Uninstall the collection self._collections_manager.uninstall(collection_id)