Skip to content

Commit

Permalink
config: Use an enum to describe a collection status
Browse files Browse the repository at this point in the history
This makes it easier to understand and use.
  • Loading branch information
ptitjano committed Dec 20, 2024
1 parent 1674a40 commit 97e1fcc
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 32 deletions.
13 changes: 5 additions & 8 deletions qgis_resource_sharing/collection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

from qgis_resource_sharing import config
from qgis_resource_sharing.__about__ import __title__
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 (
Expand Down Expand Up @@ -106,7 +103,7 @@ def get_collection(self, collection_id: str) -> Dict[str, str]:
html = html + ".<br><i>Reinstall</i> to update"
if resource_types == 0:
html = "<i>No standard resources found</i>."
if config.COLLECTIONS[collection_id]["status"] != COLLECTION_INSTALLED_STATUS:
if config.COLLECTIONS[collection_id]["status"] != CollectionStatus.INSTALLED:
html = "<i>Unknown before installation</i>"

config.COLLECTIONS[collection_id]["resources_html"] = html
Expand All @@ -126,7 +123,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:
Expand Down Expand Up @@ -167,7 +164,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.
Expand All @@ -185,7 +182,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"?
Expand Down
15 changes: 11 additions & 4 deletions qgis_resource_sharing/config.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -15,7 +17,7 @@
'author_email': email,
'repository_url': self.url,
'repository_name': <the name of the repository>,
'status': COLLECTION_NOT_INSTALLED_STATUS,
'status': CollectionStatus.NOT_INSTALLED,
'name': parser.get(collection, 'name'),
'tags': parser.get(collection, 'tags'),
'description': parser.get(collection, 'description'),
Expand All @@ -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 = {}
6 changes: 3 additions & 3 deletions qgis_resource_sharing/gui/custom_sort_filter_proxy.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions qgis_resource_sharing/gui/resource_sharing_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"])
Expand Down
13 changes: 5 additions & 8 deletions qgis_resource_sharing/gui/resource_sharing_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -202,15 +199,15 @@ 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(
"On the left you see the list of all the " "installed collections."
)
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(
Expand Down Expand Up @@ -578,7 +575,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.setIcon(
QgsApplication.getThemeIcon("mActionRefresh.svg")
Expand Down Expand Up @@ -741,7 +738,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.setIcon(
Expand Down
4 changes: 2 additions & 2 deletions qgis_resource_sharing/repository_handler/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from six import add_metaclass

from qgis_resource_sharing.__about__ import __title__
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
Expand Down Expand Up @@ -241,7 +241,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)
Expand Down
10 changes: 5 additions & 5 deletions qgis_resource_sharing/repository_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from qgis_resource_sharing import config
from qgis_resource_sharing.__about__ import __title__
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
Expand Down Expand Up @@ -49,7 +49,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'),
Expand Down Expand Up @@ -277,7 +277,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
Expand All @@ -302,7 +302,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 [
Expand Down Expand Up @@ -383,7 +383,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)
Expand Down

0 comments on commit 97e1fcc

Please sign in to comment.