Skip to content

Commit

Permalink
Functions to clear cache (#60)
Browse files Browse the repository at this point in the history
Functions to clear cache
  • Loading branch information
domi4484 authored Jul 12, 2023
1 parent 167cc6b commit 6876553
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions modelbaker/iliwrapper/ilicache.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import logging
import os
import re
import shutil
import urllib.parse
import xml.etree.ElementTree as ET
from enum import Enum

from PyQt5.QtCore import QObject, QSortFilterProxyModel, Qt, pyqtSignal
from PyQt5.QtCore import QDir, QObject, QSortFilterProxyModel, Qt, pyqtSignal
from PyQt5.QtGui import QPalette, QRegion, QStandardItem, QStandardItemModel
from PyQt5.QtWidgets import QGridLayout, QItemDelegate, QLabel, QStyle, QWidget
from qgis.core import Qgis, QgsMessageLog
Expand All @@ -38,9 +39,10 @@ class IliCache(QObject):

new_message = pyqtSignal(int, str)

CACHE_PATH = os.path.expanduser("~/.ilicache")

def __init__(self, configuration, single_ili_file=None):
QObject.__init__(self)
self.cache_path = os.path.expanduser("~/.ilicache")
self.information_file = "ilimodels.xml"
self.repositories = dict()
self.base_configuration = configuration
Expand Down Expand Up @@ -95,7 +97,7 @@ def file_path(self, netloc, url, file):
return os.path.join(url, file)
else:
netloc = "" if netloc is None else netloc
return os.path.join(self.cache_path, netloc, file)
return os.path.join(self.CACHE_PATH, netloc, file)

def download_repository(self, url):
"""
Expand Down Expand Up @@ -123,10 +125,10 @@ def download_repository(self, url):
self.tr("Could not find local file {}").format(ilisite_url)
)
else:
netloc_dir = os.path.join(self.cache_path, netloc)
netloc_dir = os.path.join(self.CACHE_PATH, netloc)
os.makedirs(netloc_dir, exist_ok=True)
information_file_path = os.path.join(netloc_dir, self.information_file)
ilisite_path = os.path.join(self.cache_path, netloc, "ilisite.xml")
ilisite_path = os.path.join(self.CACHE_PATH, netloc, "ilisite.xml")

# download ilimodels.xml
download_file(
Expand Down Expand Up @@ -154,6 +156,13 @@ def download_repository(self, url):
),
)

@classmethod
def clear_cache(cls):
if not QDir().exists(cls.CACHE_PATH):
return

shutil.rmtree(cls.CACHE_PATH, ignore_errors=False, onerror=None)

def _process_ilisite(self, file):
"""
Parses the ilisite.xml provided in ``file`` and recursively downloads any subidiary sites.
Expand Down Expand Up @@ -426,9 +435,10 @@ class IliDataCache(IliCache):
file_download_failed = pyqtSignal(str, str)
model_refreshed = pyqtSignal(int)

CACHE_PATH = os.path.expanduser("~/.ilimetaconfigcache")

def __init__(self, configuration, type="metaconfig", models=None):
IliCache.__init__(self, configuration)
self.cache_path = os.path.expanduser("~/.ilimetaconfigcache")
self.information_file = "ilidata.xml"

self.model = IliDataItemModel()
Expand Down Expand Up @@ -665,7 +675,7 @@ def download_file(self, netloc, url, file, dataset_id=None):
self.tr("Could not find local file {}").format(file_url),
)
else:
file_path = os.path.join(self.cache_path, netloc, file)
file_path = os.path.join(self.CACHE_PATH, netloc, file)
file_dir = os.path.dirname(file_path)
os.makedirs(file_dir, exist_ok=True)

Expand Down Expand Up @@ -814,9 +824,10 @@ class IliToppingFileCache(IliDataCache):
file_ids can contain ilidata: or file: information
"""

CACHE_PATH = os.path.expanduser("~/.ilitoppingfilescache")

def __init__(self, configuration, file_ids=None, tool_dir=None):
IliDataCache.__init__(self, configuration)
self.cache_path = os.path.expanduser("~/.ilitoppingfilescache")
self.model = IliToppingFileItemModel()
self.sorted_model.setSourceModel(self.model)
self.sorted_model.sort(0, Qt.AscendingOrder)
Expand Down

0 comments on commit 6876553

Please sign in to comment.