Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Commit

Permalink
Fix imports
Browse files Browse the repository at this point in the history
- Remove imports from __init__
- Fix circle import from File and app
  • Loading branch information
Patrick Kuehn committed Oct 16, 2019
1 parent 191a6da commit 400c24d
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 25 deletions.
11 changes: 7 additions & 4 deletions autofocus/predict/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

from flask import Flask, jsonify, request

from .models import File, Predictor, ZipArchive
from .requests import PredictRequestValidator, PredictZipRequestValidator
from .models.File import File
from .models.Predictor import Predictor
from .models.ZipArchive import ZipArchive
from .requests.PredictRequestValidator import PredictRequestValidator
from .requests.PredictZipRequestValidator import PredictZipRequestValidator

# We are going to upload the files to the server as part of the request, so set tmp folder here.
UPLOAD_FOLDER = "/tmp/"
Expand All @@ -22,7 +25,7 @@ def classify_single():
validator.abort()

# Get File object
file = File(request.files["file"])
file = File(request.files["file"], app.config["UPLOAD_FOLDER"])

# Predict probabilities
app.logger.info("Classifying image %s" % (file.getPath()))
Expand All @@ -44,7 +47,7 @@ def classify_zip():
if not validator.validate():
validator.abort()

file = ZipArchive(request.files["file"])
file = ZipArchive(request.files["file"], app.config["UPLOAD_FOLDER"])
if not file.hasImages():
validator.error['file'] = "No image files detected in the zip file."
validator.abort()
Expand Down
13 changes: 8 additions & 5 deletions autofocus/predict/app/models/File.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
from werkzeug import secure_filename
from ..app import app


class File:
Expand All @@ -12,17 +11,18 @@ class File:
name: Secured filename (Can be empty)
"""

def __init__(self, file=None):
def __init__(self, file=None, upload_path=None):
"""
Constructor of File
Save the file on the server if a file is given.
Parameters:
file: Uploaded file object from flask
upload_path: The path to upload the file
"""
if file:
self.setFromUploadedFile(file)
self.setFromUploadedFile(file, upload_path)

def __del__(self):
"""
Expand All @@ -32,15 +32,18 @@ def __del__(self):
"""
os.remove(self.path)

def setFromUploadedFile(self, file):
def setFromUploadedFile(self, file, upload_path=None):
"""
Save file from uploaded file
Parameters:
file: Uploaded file object from flask
upload_path: The path to upload the file
"""
self.name = secure_filename(file.filename)
self.path = os.path.join(app.config["UPLOAD_FOLDER"], self.name)
self.path = self.name
if upload_path:
self.path = os.path.join(upload_path, self.path)
file.save(self.path)

def setPath(self, path):
Expand Down
11 changes: 6 additions & 5 deletions autofocus/predict/app/models/ZipArchive.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
from zipfile import ZipFile
from . import File
from ..requests import ALLOWED_IMAGE_FILES
from .File import File
from ..requests.Validator import ALLOWED_IMAGE_FILES
from ..utils import allowed_file


Expand All @@ -16,16 +16,17 @@ class ZipArchive:
zip: Opened zip file
"""

def __init__(self, file):
def __init__(self, file, upload_folder=None):
"""
Constructor of ZipFile
Store the given file and open the zip file.
Parameters:
file: Uploaded file from flask
upload_folder: The folder to save the zip file
"""
self.file = File.File(file)
self.file = File(file, upload_folder)
self.zip = ZipFile(self.file.getPath())

def listFiles(self):
Expand Down Expand Up @@ -80,7 +81,7 @@ def extractAll(self, path=None, members=None):
self.zip.extractall(path, members)
extractedFiles = {}
for member in members:
file = File.File()
file = File()
file.setPath(os.path.join(path, member))
extractedFiles[member] = file
return extractedFiles
3 changes: 0 additions & 3 deletions autofocus/predict/app/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +0,0 @@
from .File import File
from .Predictor import Predictor
from .ZipArchive import ZipArchive
2 changes: 1 addition & 1 deletion autofocus/predict/app/requests/PredictRequestValidator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import ALLOWED_IMAGE_FILES, Validator
from .Validator import ALLOWED_IMAGE_FILES, Validator
from ..utils import allowed_file


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from . import ALLOWED_ZIP_FILES, Validator
from .Validator import ALLOWED_ZIP_FILES, Validator
from ..utils import allowed_file


Expand Down
4 changes: 4 additions & 0 deletions autofocus/predict/app/requests/Validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
from flask_api import status


ALLOWED_IMAGE_FILES = set(["png", "jpg", "jpeg", "gif", "bmp"])
ALLOWED_ZIP_FILES = {"zip"}


class Validator(ABC):
"""
Validate given request
Expand Down
6 changes: 0 additions & 6 deletions autofocus/predict/app/requests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
from .PredictRequestValidator import PredictRequestValidator
from .Validator import Validator
from .PredictZipRequestValidator import PredictZipRequestValidator

ALLOWED_IMAGE_FILES = set(["png", "jpg", "jpeg", "gif", "bmp"])
ALLOWED_ZIP_FILES = {"zip"}

0 comments on commit 400c24d

Please sign in to comment.