diff --git a/qgis_plugin_repo/dispatcher.py b/qgis_plugin_repo/dispatcher.py index 85a44fe..c2edb03 100644 --- a/qgis_plugin_repo/dispatcher.py +++ b/qgis_plugin_repo/dispatcher.py @@ -8,16 +8,20 @@ from pathlib import Path from typing import List, Tuple, Union +import requests + +from qgis_plugin_repo.tools import is_url + class Dispatcher: def __init__(self, input_uri: Union[Path, str], outputs_uri: List[str]): """ Constructor. """ - if isinstance(input_uri, str): - input_uri = Path(input_uri) - self.input_uri = input_uri - self.input_parser = ET.parse(self.input_uri.absolute()).getroot() + if is_url: + self.input_parser = ET.fromstring(requests.get(self.input_uri).content) + else: + self.input_parser = ET.parse(self.input_uri.absolute()).getroot() self.outputs_uri = [Path(f) for f in outputs_uri] diff --git a/qgis_plugin_repo/merger.py b/qgis_plugin_repo/merger.py index 8b196c9..fafe833 100644 --- a/qgis_plugin_repo/merger.py +++ b/qgis_plugin_repo/merger.py @@ -8,6 +8,8 @@ import requests +from qgis_plugin_repo.tools import is_url + __copyright__ = 'Copyright 2021, 3Liz' __license__ = 'GPL version 3' __email__ = 'info@3liz.org' @@ -57,7 +59,7 @@ def init(self) -> None: def xml_input_parser(self) -> ET.Element: """ Returns the XML parser for the input file. """ - if self.input_is_url(): + if is_url(self.input_uri): self.input_parser = ET.fromstring(requests.get(self.input_uri).content) else: if not isinstance(self.input_uri, Path): @@ -75,7 +77,12 @@ def xml_output_parser(self) -> ET.Element: if not self.exists(): self.init() - self.output_tree = ET.parse(self.destination.absolute()) + try: + self.output_tree = ET.parse(self.destination.absolute()) + except ET.ParseError: + self.init() + self.output_tree = ET.parse(self.destination.absolute()) + self.output_parser = self.output_tree.getroot() return self.output_parser diff --git a/qgis_plugin_repo/tools.py b/qgis_plugin_repo/tools.py new file mode 100644 index 0000000..a9cea59 --- /dev/null +++ b/qgis_plugin_repo/tools.py @@ -0,0 +1,24 @@ +from pathlib import Path +from typing import Union +from urllib.parse import urlparse + +__copyright__ = 'Copyright 2022, 3Liz' +__license__ = 'GPL version 3' +__email__ = 'info@3liz.org' + + +def is_url(uri: Union[Path, str]) -> bool: + """ Check if the URI can be a URL. """ + if isinstance(uri, Path): + return False + + path = Path(uri) + if path.exists(): + return False + + # noinspection PyBroadException + try: + urlparse(uri) + return True + except Exception: + return False