Skip to content

Commit 132057f

Browse files
committed
Big refactoring number 2
- Clean separation between GUI, core functions and manager in-between - Data source discovery rules in dataclasses with proper regexes - Transfer of data sources via stored data - Remove unnecessary path twiddling based on OS - Use pathlib everywhere possible - Less classes more functions #42 - Lots of renamings for clarity - Qt6 compatible enums - ...
1 parent 441bc48 commit 132057f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+1772
-2194
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ To install the plugin manually just copy the folder into your QGIS profile direc
3333
- Importing (spatial) bookmarks
3434
- Importing (data source) favourites
3535
- Importing plugins
36-
- Importing expression functions
37-
- Importing models & scripts
36+
- Importing expressions
37+
- Importing models
38+
- Importing scripts
3839
- Importing some symbology types & label settings
3940
- Importing QGIS UI settings (e.g. hidden toolbar items)
4041

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
from pathlib import Path
2+
3+
from lxml import etree as et
4+
5+
6+
def import_bookmarks(source_bookmark_file: Path, target_bookmark_file: Path):
7+
"""Imports spatial bookmarks from source to target file.
8+
9+
Spatial bookmarks are stored in bookmarks.xml, e.g.:
10+
<Bookmarks>
11+
<Bookmark id="..." group="" extent="POLYGON((...))" name="Test Bookmark">
12+
<spatialrefsys nativeFormat="Wkt">
13+
...
14+
</spatialrefsys>
15+
</Bookmark>
16+
...
17+
</Bookmarks>
18+
19+
TODO The deduplication seems garbage
20+
21+
Args:
22+
source_bookmark_file: Path of bookmarks file to import from
23+
target_bookmark_file: Path of bookmarks file to import to
24+
"""
25+
# get the element tree of the source file
26+
source_tree = et.parse(source_bookmark_file, et.XMLParser(remove_blank_text=True))
27+
28+
# check if target file exists
29+
if not target_bookmark_file.is_file():
30+
with open(target_bookmark_file, "w") as new_file:
31+
new_file.write("<Bookmarks></Bookmarks>")
32+
33+
# get the element tree of the target file
34+
# fill if empty
35+
target_tree = et.parse(target_bookmark_file, et.XMLParser(remove_blank_text=True))
36+
37+
# find all bookmark elements
38+
source_root_tag = source_tree.findall("Bookmark")
39+
40+
# get the root element "Bookmarks" # TODO comment does not seem to fit the code?
41+
target_tree_root = target_tree.getroot()
42+
43+
# Remove duplicate entries to prevent piling data
44+
target_tree_root = remove_duplicates(source_root_tag, target_tree, target_tree_root)
45+
46+
# append the elements
47+
for element in source_root_tag:
48+
target_tree_root.append(element)
49+
50+
# overwrite the xml file
51+
et.ElementTree(target_tree_root).write(
52+
target_bookmark_file,
53+
pretty_print=True,
54+
encoding="utf-8",
55+
xml_declaration=True,
56+
)
57+
58+
59+
def remove_duplicates(
60+
source_root_tag: list[et.Element],
61+
target_tree: et.ElementTree,
62+
target_tree_root: et.Element,
63+
):
64+
"""Removes bookmarks from target that exist in the (to be imported) source too."""
65+
# TODO FIXME this only checks the name of the bookmarks which will lead to false positives
66+
# it is ok and supported by QGIS to have the same name for multiple bookmarks
67+
# TODO compare the complete content of the xml node!
68+
target_root_tag = target_tree.findall("Bookmark")
69+
for source_element in source_root_tag:
70+
for target_element in target_root_tag:
71+
if source_element.attrib["name"] == target_element.attrib["name"]:
72+
target_tree_root.remove(target_element)
73+
74+
return target_tree_root

profile_manager/datasources/bookmarks/__init__.py

Whitespace-only changes.

profile_manager/datasources/bookmarks/bookmark_handler.py

Lines changed: 0 additions & 89 deletions
This file was deleted.

profile_manager/datasources/customizations/customization_handler.py renamed to profile_manager/datasources/customization.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
from configparser import RawConfigParser
2-
from os import path
2+
from pathlib import Path
33
from shutil import copy2
44

5-
from profile_manager.utils import adjust_to_operating_system
65

7-
8-
def import_customizations(source_profile_path: str, target_profile_path: str):
6+
def import_customizations(source_profile_path: Path, target_profile_path: Path):
97
"""Imports UI customizations from source to target profile.
108
119
Copies the whole QGISCUSTOMIZATION3.ini file and also transfers the [UI] section from QGIS3.ini if available
@@ -19,25 +17,18 @@ def import_customizations(source_profile_path: str, target_profile_path: str):
1917
...
2018
2119
Args:
22-
TODO
20+
source_profile_path: Path of profile directory to import from
21+
target_profile_path: Path of profile directory to import to
2322
"""
2423
# Copy (overwrite) the QGISCUSTOMIZATION3.ini if exist
25-
source_customini_path = adjust_to_operating_system(
26-
source_profile_path + "QGIS/QGISCUSTOMIZATION3.ini"
27-
)
28-
target_customini_path = adjust_to_operating_system(
29-
target_profile_path + "QGIS/QGISCUSTOMIZATION3.ini"
30-
)
31-
if path.exists(source_customini_path):
24+
source_customini_path = source_profile_path / "QGIS" / "QGISCUSTOMIZATION3.ini"
25+
target_customini_path = target_profile_path / "QGIS" / "QGISCUSTOMIZATION3.ini"
26+
if source_customini_path.exists():
3227
copy2(source_customini_path, target_customini_path)
3328

3429
# Copy [UI] section from QGIS3.ini
35-
source_qgis3ini_path = adjust_to_operating_system(
36-
source_profile_path + "QGIS/QGIS3.ini"
37-
)
38-
target_qgis3ini_path = adjust_to_operating_system(
39-
target_profile_path + "QGIS/QGIS3.ini"
40-
)
30+
source_qgis3ini_path = source_profile_path / "QGIS" / "QGIS3.ini"
31+
target_qgis3ini_path = target_profile_path / "QGIS" / "QGIS3.ini"
4132

4233
source_ini_parser = RawConfigParser()
4334
source_ini_parser.optionxform = str # str = case-sensitive option names

profile_manager/datasources/customizations/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)