Skip to content

Commit b07b25c

Browse files
committed
improve(qdt): support plugins installed from unofficial repository
1 parent 7282e75 commit b07b25c

3 files changed

Lines changed: 68 additions & 23 deletions

File tree

profile_manager/profiles/utils.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from configparser import NoSectionError, RawConfigParser
2-
from dataclasses import dataclass
32
from pathlib import Path
43
from sys import platform
54
from typing import Any, Dict, List, Optional
@@ -8,6 +7,8 @@
87
from qgis.core import QgsUserProfileManager
98
from qgis.utils import iface
109

10+
from profile_manager.qdt_export.models import QdtPluginInformation
11+
1112

1213
def qgis_profiles_path() -> Path:
1314
"""Get QGIS profiles paths from current QGIS application
@@ -94,9 +95,11 @@ def get_installed_plugin_metadata(
9495
"""
9596
ini_parser = RawConfigParser()
9697
ini_parser.optionxform = str # str = case-sensitive option names
97-
ini_parser.read(get_profile_plugin_metadata_path(profile_name, plugin_slug_name))
98+
plg_metadata_path = get_profile_plugin_metadata_path(profile_name, plugin_slug_name)
99+
ini_parser.read(plg_metadata_path)
98100
try:
99101
metadata = dict(ini_parser.items("general"))
102+
metadata["folder_name"] = plg_metadata_path.parent.name
100103
except NoSectionError:
101104
metadata = {}
102105
return metadata
@@ -128,15 +131,6 @@ def get_profile_name_list() -> List[str]:
128131
return QgsUserProfileManager(qgis_profiles_path()).allProfiles()
129132

130133

131-
@dataclass
132-
class PluginInformation:
133-
name: str
134-
folder_name: str
135-
official_repository: bool
136-
plugin_id: Optional[int]
137-
version: str
138-
139-
140134
def define_plugin_version_from_metadata(
141135
manager_metadata: Dict[str, Any], plugin_metadata: Dict[str, Any]
142136
) -> str:
@@ -168,7 +162,7 @@ def define_plugin_version_from_metadata(
168162

169163
def get_profile_plugin_information(
170164
profile_name: str, plugin_slug_name: str
171-
) -> Optional[PluginInformation]:
165+
) -> Optional[QdtPluginInformation]:
172166
"""Get plugin information from profile. Only official plugin are supported.
173167
174168
Args:
@@ -181,18 +175,27 @@ def get_profile_plugin_information(
181175
manager_metadata = get_plugin_info_from_qgis_manager(
182176
plugin_slug_name=plugin_slug_name
183177
)
178+
184179
plugin_metadata = get_installed_plugin_metadata(
185180
profile_name=profile_name, plugin_slug_name=plugin_slug_name
186181
)
187182

188-
# For now we don't support unofficial plugins
189-
if manager_metadata is None:
183+
if not all([manager_metadata, plugin_metadata]):
184+
print(f"Plugin {plugin_slug_name} not found in profile {profile_name}")
190185
return None
191186

192-
return PluginInformation(
193-
name=manager_metadata["name"],
194-
folder_name=plugin_slug_name,
195-
official_repository=True, # For now we only support official repository
187+
if manager_metadata is None:
188+
manager_metadata = {
189+
"name": plugin_metadata.get("name", plugin_slug_name),
190+
"download_url": None,
191+
"plugin_id": None,
192+
"folder_name": plugin_metadata.get("folder_name", plugin_slug_name),
193+
}
194+
195+
return QdtPluginInformation(
196+
name=manager_metadata.get("name", plugin_slug_name),
197+
download_url=manager_metadata.get("download_url"),
198+
folder_name=plugin_metadata.get("folder_name", plugin_slug_name),
196199
plugin_id=(
197200
int(manager_metadata["plugin_id"])
198201
if manager_metadata["plugin_id"]
@@ -207,7 +210,7 @@ def get_profile_plugin_information(
207210

208211
def get_profile_plugin_list_information(
209212
profile_name: str, only_activated: bool = True
210-
) -> List[PluginInformation]:
213+
) -> List[QdtPluginInformation]:
211214
"""Get profile plugin information
212215
213216
Args:
@@ -220,12 +223,14 @@ def get_profile_plugin_list_information(
220223
plugin_list: List[str] = get_installed_plugin_list(
221224
profile_name=profile_name, only_activated=only_activated
222225
)
223-
# Get information about installed plugin
224-
profile_plugin_list: List[PluginInformation] = []
225226

227+
# Get information about installed plugin
228+
profile_plugin_list: List[QdtPluginInformation] = []
226229
for plugin_name in plugin_list:
227230
plugin_info = get_profile_plugin_information(profile_name, plugin_name)
228231
if plugin_info and plugin_info.plugin_id:
229232
profile_plugin_list.append(plugin_info)
233+
elif plugin_info and not plugin_info.plugin_id:
234+
profile_plugin_list.append(plugin_info)
230235

231236
return profile_plugin_list

profile_manager/qdt_export/models.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,45 @@
11
from dataclasses import dataclass
2+
from typing import Any, Dict, Optional
3+
4+
5+
@dataclass
6+
class QdtPluginInformation:
7+
"""QGIS Plugin representation for QDT profile export."""
8+
9+
name: str
10+
folder_name: str
11+
version: str
12+
download_url: Optional[str] = None
13+
plugin_id: Optional[int] = None
14+
15+
def as_dict(self) -> Dict[str, Any]:
16+
"""Custom as_dict method to handle properties and specific vars.
17+
18+
:return: dict of PluginInformation
19+
:rtype: Dict[str, Any]
20+
"""
21+
out_dict = {
22+
"name": self.name,
23+
"folder_name": self.folder_name,
24+
"official_repository": self.official_repository,
25+
"plugin_id": self.plugin_id,
26+
"version": self.version,
27+
}
28+
if not self.official_repository and self.download_url:
29+
out_dict["url"] = self.download_url
30+
31+
return out_dict
32+
33+
@property
34+
def official_repository(self) -> bool:
35+
"""Check if plugin is from official QGIS repository.
36+
37+
:return: True if plugin is from official QGIS repository.
38+
:rtype: bool
39+
"""
40+
if self.download_url is None:
41+
return False
42+
return self.download_url.startswith("https://plugins.qgis.org")
243

344

445
@dataclass

profile_manager/qdt_export/profile_export.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import dataclasses
21
import json
32
from pathlib import Path
43
from shutil import copytree, rmtree
@@ -72,7 +71,7 @@ def qdt_profile_dict(
7271
"qgisMinimumVersion": qdt_profile_infos.qgis_min_version,
7372
"qgisMaximumVersion": qdt_profile_infos.qgis_max_version,
7473
"version": qdt_profile_infos.version,
75-
"plugins": [dataclasses.asdict(plugin) for plugin in profile_plugin_list],
74+
"plugins": [plugin.as_dict() for plugin in profile_plugin_list],
7675
}
7776

7877

0 commit comments

Comments
 (0)