Skip to content

Commit

Permalink
Fix uninstall (#133)
Browse files Browse the repository at this point in the history
* Debugging

* Fixing path related issues

* Update message
  • Loading branch information
havatv authored May 15, 2020
1 parent 61414c4 commit 3b6f72e
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 129 deletions.
2 changes: 1 addition & 1 deletion resource_sharing/collection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def get_html(self, collection_id):
resource_types = resource_types + 1
html = html + '.<br><i>Reinstall</i> to update'
if resource_types == 0:
html = '<i>Reinstall</i> the collection to get statistics'
html = '<i>No standard resources found</i>.'
if config.COLLECTIONS[collection_id]['status'] != COLLECTION_INSTALLED_STATUS:
html = '<i>Unknown before installation</i>'

Expand Down
10 changes: 5 additions & 5 deletions resource_sharing/gui/resource_sharing_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,27 @@ def install_finished(self):
number = 0
if 'style' in config.COLLECTIONS[self._selected_collection_id].keys():
number = config.COLLECTIONS[self._selected_collection_id]['style']
message = message + '\n<li> ' + str(config.COLLECTIONS[self._selected_collection_id]['style']) + ' Layer style (QML) file'
message = message + '\n<li> ' + str(number) + ' Layer style (QML) file'
if number > 1:
message = message + 's'
if 'symbol' in config.COLLECTIONS[self._selected_collection_id].keys():
number = config.COLLECTIONS[self._selected_collection_id]['symbol']
message = message + '\n<li> ' + str(config.COLLECTIONS[self._selected_collection_id]['symbol']) + ' XML symbol file'
message = message + '\n<li> ' + str(number) + ' XML symbol file'
if number > 1:
message = message + 's'
if 'svg' in config.COLLECTIONS[self._selected_collection_id].keys():
number = config.COLLECTIONS[self._selected_collection_id]['svg']
message = message + '\n<li> ' + str(config.COLLECTIONS[self._selected_collection_id]['svg']) + ' SVG file'
message = message + '\n<li> ' + str(number) + ' SVG file'
if number > 1:
message = message + 's'
if 'models' in config.COLLECTIONS[self._selected_collection_id].keys():
number = config.COLLECTIONS[self._selected_collection_id]['models']
message = message + '\n<li> ' + str(config.COLLECTIONS[self._selected_collection_id]['models']) + ' model'
message = message + '\n<li> ' + str(number) + ' model'
if number > 1:
message = message + 's'
if 'processing' in config.COLLECTIONS[self._selected_collection_id].keys():
number = config.COLLECTIONS[self._selected_collection_id]['processing']
message = message + '\n<li> ' + str(config.COLLECTIONS[self._selected_collection_id]['processing']) + ' processing script'
message = message + '\n<li> ' + str(number) + ' processing script'
if number > 1:
message = message + 's'
if 'rscripts' in config.COLLECTIONS[self._selected_collection_id].keys():
Expand Down
39 changes: 13 additions & 26 deletions resource_sharing/resource_handler/model_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding=utf-8
#import os
# Use pathlib instead of os.path?
# Use pathlib instead of os.path
from pathlib import Path
import fnmatch
import shutil
import logging
from processing.tools.system import userFolder, mkdir
Expand Down Expand Up @@ -36,19 +34,14 @@ def install(self):
model directory and refresh the provider.
"""
# Return silently if the directory does not exist
#if not os.path.exists(self.resource_dir):
if not Path(self.resource_dir).exists():
return

# Get all the model files under self.resource_dir
# Handle the model files located in self.resource_dir
model_files = []
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*.model3'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
model_files.append(file_path)
#if fnmatch.fnmatch(file_path, '*.model3'):
#model_files.append(file_path)
valid = 0
for model_file in model_files:
# Install the model file silently
Expand All @@ -63,40 +56,34 @@ def install(self):
self.collection[MODELS] = valid

def uninstall(self):
"""Uninstall the models from processing toolbox."""
#if not os.path.exists(self.resource_dir):
"""Uninstall the collection's models from the processing toolbox."""
if not Path(self.resource_dir).exists():
return
# Remove the model files that are present in this collection
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
if fnmatch.fnmatch(file_path, '*%s*' % self.collection_id):
#model_path = os.path.join(self.Models_folder(), item)
model_path = Path(self.Models_folder(), item)
#if os.path.exists(model_path):
if model_path.exists():
#os.remove(model_path)
model_path.unlink()

for item in Path(self.resource_dir).glob('*.model3'):
model_path = Path(self.Models_folder(), item.name)
if model_path.exists():
model_path.unlink()
self.refresh_Model_provider()

def refresh_Model_provider(self):
"""Refresh the processing model provider."""
mod_prov = QgsApplication.processingRegistry().providerById("model")
if (mod_prov is not None):
mod_prov.refreshAlgorithms()
try:
mod_prov.refreshAlgorithms()
except Error as e:
LOGGER.error("Exception refreshing models:\n" +
str(e))

def default_models_folder(self):
"""Return the default location of the processing models folder."""
#folder = str(os.path.join(userFolder(), MODELS_PROCESSING_FOLDER))
folder = Path(userFolder(), MODELS_PROCESSING_FOLDER)
mkdir(str(folder))
#return os.path.abspath(folder)
return str(folder)

def Models_folder(self):
"""Return the folder where processing expects to find models."""
# Use the default location
return self.default_models_folder()

52 changes: 19 additions & 33 deletions resource_sharing/resource_handler/processing_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding=utf-8
#import os
# Use pathlib instead of os.path?
# Use pathlib instead of os.path
from pathlib import Path
import fnmatch
import shutil
import logging

Expand All @@ -27,32 +25,25 @@ def dir_name(cls):
return PROCESSING

def install(self):
"""Install the processing scripts of the collection.
"""Install the collection's processing scripts.
We copy the processing scripts to the user's processing
scripts directory, and refresh the provider.
"""
# Pass silently if the directory does not exist
#if not os.path.exists(self.resource_dir):
if not Path(self.resource_dir).exists():
return

# Get all the script files under self.resource_dir
# Handle the script files located in self.resource_dir
processing_files = []
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*.py'):
file_path = Path(self.resource_dir, item)
processing_files.append(file_path)
#file_path = os.path.join(self.resource_dir, item)
#if fnmatch.fnmatch(file_path, '*.py'):
#processing_files.append(file_path)

processing_files.append(item)
valid = 0
for processing_file in processing_files:
# Install the processing file silently
try:
shutil.copy(processing_file, self.scripts_folder())
valid += 1
if processing_file.suffix.lower().endswith('py'):
valid += 1
except OSError as e:
LOGGER.error("Could not copy script '" +
str(processing_file) + "'\n" + str(e))
Expand All @@ -61,32 +52,27 @@ def install(self):
self.collection[PROCESSING] = valid

def uninstall(self):
"""Uninstall the processing scripts from processing toolbox."""
# if not Path(self.resource_dir).exists():
#if not os.path.exists(self.resource_dir):
"""Uninstall the processing scripts from the processing toolbox."""
if not Path(self.resource_dir).exists():
return
# Remove the processing script files that are present in this
# collection
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
if fnmatch.fnmatch(str(file_path), '*%s*' % self.collection_id):
#script_path = os.path.join(self.scripts_folder(), item)
script_path = Path(self.scripts_folder(), item)
#if os.path.exists(script_path):
if script_path.exists():
#os.remove(script_path)
script_path.unlink()
# Remove the collection's processing script files
for item in Path(self.resource_dir).glob('*.py'):
script_path = Path(self.scripts_folder(), item.name)
if script_path.exists():
script_path.unlink()
self.refresh_script_provider()

def refresh_script_provider(self):
"""Refresh the processing script provider."""
script_pr = QgsApplication.processingRegistry().providerById("script")
if script_pr is not None:
script_pr.refreshAlgorithms()

try:
script_pr.refreshAlgorithms()
except Error as e:
LOGGER.error("Exception refreshing algorithms:\n" +
str(e))

def scripts_folder(self):
"""Return the default processing scripts folder."""
return ScriptUtils.defaultScriptsFolder()

53 changes: 20 additions & 33 deletions resource_sharing/resource_handler/r_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding=utf-8
#import os
# Use pathlib instead of os.path?
# Use pathlib instead of os.path
from pathlib import Path
import fnmatch
import shutil
import logging

Expand Down Expand Up @@ -33,34 +31,29 @@ def dir_name(cls):
def install(self):
"""Install the R scripts of the collection.
We copy the R scripts (*.rsx and *.rsx.help) that exist in
the rscripts dir to the user's R script directory and refresh
the provider.
Copy the collection's R scripts (*.rsx and *.rsx.help) from
its rscripts directory to the user's R script directory and
refresh the provider.
"""
# Check if the dir exists, return silently if it doesn't
#if not os.path.exists(self.resource_dir):
if not Path(self.resource_dir).exists():
return

# Get all the R script files under self.resource_dir
# Handle the R script files located in self.resource_dir
R_files = []
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
#if fnmatch.fnmatch(file_path, '*.rsx'):
if file_path.suffix.lower() == 'rsx':
if file_path.suffix.lower().endswith('rsx'):
R_files.append(file_path)
#if fnmatch.fnmatch(file_path, '*.rsx.help'):
if ''.join(file_path.suffixes).lower().endswith('rsx.help'):
R_files.append(file_path)

valid = 0
for R_file in R_files:
# Install the R script file silently
try:
shutil.copy(str(R_file), self.RScripts_folder())
valid += 1
if R_file.suffix.lower().endswith('rsx'):
valid += 1
except OSError as e:
LOGGER.error("Could not copy script '" + str(R_file) +
"'\n" + str(e))
Expand All @@ -69,41 +62,35 @@ def install(self):
self.collection[RSCRIPTS_FOLDER] = valid

def uninstall(self):
"""Uninstall the R scripts from processing toolbox."""
#if not os.path.exists(self.resource_dir):
"""Uninstall the collection's R scripts from the processing toolbox."""
if not Path(self.resource_dir).exists():
return
# Remove the R script files that are present in this collection
#for item in os.listdir(self.resource_dir):
# Remove the collection's R script files
for item in Path(self.resource_dir).glob('*'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
if fnmatch.fnmatch(str(file_path), '*%s*' % self.collection_id):
#script_path = os.path.join(self.RScripts_folder(), item)
script_path = Path(self.RScripts_folder(), item)
#if os.path.exists(script_path):
if script_path.exists():
#os.remove(script_path)
script_path.unlink()
rscript_path = Path(self.RScripts_folder(), item.name)
if rscript_path.exists():
rscript_path.unlink()
self.refresh_Rscript_provider()

def refresh_Rscript_provider(self):
"""Refresh the R script provider."""
r_provider = QgsApplication.processingRegistry().providerById("r")
if r_provider is not None:
r_provider.refreshAlgorithms()
try:
r_provider.refreshAlgorithms()
except Error as e:
LOGGER.error("Exception when refreshing after adding R scripts:\n" +
str(e))

def default_rscripts_folder(self):
"""Return the default R scripts folder."""
"""Return the default user R scripts folder."""
# Perphaps better to use RUtils.default_scripts_folder()?
# return RUtils.default_scripts_folder()
# folder = userFolder() / RSCRIPTS_PROCESSING_FOLDER
#folder = str(os.path.join(userFolder(), RSCRIPTS_PROCESSING_FOLDER))
folder = Path(userFolder(), RSCRIPTS_PROCESSING_FOLDER)
mkdir(str(folder))
#return os.path.abspath(folder)
return str(folder)

def RScripts_folder(self):
"""Return the default R scripts folder."""
return self.default_rscripts_folder()

22 changes: 6 additions & 16 deletions resource_sharing/resource_handler/style_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# coding=utf-8
#import os
# Use pathlib instead of os.path?
# Use pathlib instead of os.path
from pathlib import Path
#import fnmatch
import logging

from resource_sharing.resource_handler.base import BaseResourceHandler
Expand All @@ -26,28 +24,19 @@ def dir_name(cls):

def install(self):
"""Install the style.
Resolve the symbol SVG/image paths in the QML file
Resolve the symbol SVG/image paths in the QML files
"""
# Check if the dir exists, pass silently if it doesn't
#if not os.path.exists(self.resource_dir):
if not Path(self.resource_dir).exists():
return

# Get all the style XML files under resource dirs
# Get all the collection's layer style QML files located in
# self.resource_dir
style_files = []
#for item in os.listdir(self.resource_dir):
for item in Path(self.resource_dir).glob('*.qml'):
#file_path = os.path.join(self.resource_dir, item)
file_path = Path(self.resource_dir, item)
style_files.append(file_path)
#if fnmatch.fnmatch(file_path, '*.qml'):
#style_files.append(file_path)

style_files.append(item)
# Nothing to do if there are no symbol files
if len(style_files) == 0:
return

valid = 0
for style_file in style_files:
# Try to fix image and SVG paths in the QML file
Expand All @@ -60,3 +49,4 @@ def uninstall(self):
"""Uninstall the style."""
# Styles are not installed, so do nothing.
pass

Loading

0 comments on commit 3b6f72e

Please sign in to comment.