Skip to content

Commit

Permalink
Remove some prints and fix if the destination is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Sep 26, 2022
1 parent bded40b commit a2084e4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
12 changes: 8 additions & 4 deletions qgis_plugin_repo/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
11 changes: 9 additions & 2 deletions qgis_plugin_repo/merger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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):
Expand All @@ -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

Expand Down
24 changes: 24 additions & 0 deletions qgis_plugin_repo/tools.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a2084e4

Please sign in to comment.