11 changed files with 124 additions and 13 deletions
@ -0,0 +1,30 @@
@@ -0,0 +1,30 @@
|
||||
"""Loader.""" |
||||
|
||||
|
||||
class Loader: |
||||
""" Abstract Loader Class """ |
||||
|
||||
@staticmethod |
||||
def load(uri): |
||||
""" |
||||
Load the uri ressource |
||||
:return: <RGB> image |
||||
""" |
||||
pass |
||||
|
||||
@staticmethod |
||||
def uri_validator(uri): |
||||
""" |
||||
Validate the uri for the loader |
||||
:return: <bool> True is a valid uri |
||||
""" |
||||
return False |
||||
|
||||
@staticmethod |
||||
def get_loader(uri): |
||||
from loader.fs import FSLoader |
||||
from loader.http import HTTPLoader |
||||
for loader in (FSLoader, HTTPLoader): |
||||
if loader.uri_validator(uri): |
||||
return loader |
||||
return None |
@ -0,0 +1,24 @@
@@ -0,0 +1,24 @@
|
||||
""" File Sytem Loading """ |
||||
import os |
||||
|
||||
from loader import Loader |
||||
from utils import read_image |
||||
|
||||
|
||||
class FSLoader(Loader): |
||||
""" File System Loader Class """ |
||||
@staticmethod |
||||
def load(uri): |
||||
""" |
||||
Load the file system ressource |
||||
:return: <RGB> image |
||||
""" |
||||
return read_image(uri) |
||||
|
||||
@staticmethod |
||||
def uri_validator(uri): |
||||
""" |
||||
Validate the uri is a filesystem file |
||||
:return: <bool> True is a valid uri |
||||
""" |
||||
return os.path.exists(uri) |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
import os |
||||
import re |
||||
import tempfile |
||||
|
||||
from utils import dl_file, read_image |
||||
from loader import Loader |
||||
|
||||
|
||||
regex_url = re.compile( |
||||
r'^(?:http)s?://' # http:// or https:// |
||||
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain... |
||||
r'localhost|' # localhost... |
||||
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip |
||||
r'(?::\d+)?' # optional port |
||||
r'(?:/?|[/?]\S+)$', re.IGNORECASE) |
||||
|
||||
|
||||
class HTTPLoader(Loader): |
||||
""" Abstract Loader Class """ |
||||
@staticmethod |
||||
def run(uri): |
||||
""" |
||||
Run the loader ressource |
||||
:return: <RGB> image |
||||
""" |
||||
_, tmp_path = tempfile.mkstemp() |
||||
dl_file(uri, tmp_path) |
||||
img = read_image(tmp_path) |
||||
os.remove(tmp_path) |
||||
return img |
||||
|
||||
@staticmethod |
||||
def uri_validator(uri): |
||||
return regex_url.match(uri) |
Before Width: | Height: | Size: 261 KiB |
Loading…
Reference in new issue