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

Commit 400c24d

Browse files
author
Patrick Kuehn
committed
Fix imports
- Remove imports from __init__ - Fix circle import from File and app
1 parent 191a6da commit 400c24d

File tree

8 files changed

+27
-25
lines changed

8 files changed

+27
-25
lines changed

autofocus/predict/app/app.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
from flask import Flask, jsonify, request
44

5-
from .models import File, Predictor, ZipArchive
6-
from .requests import PredictRequestValidator, PredictZipRequestValidator
5+
from .models.File import File
6+
from .models.Predictor import Predictor
7+
from .models.ZipArchive import ZipArchive
8+
from .requests.PredictRequestValidator import PredictRequestValidator
9+
from .requests.PredictZipRequestValidator import PredictZipRequestValidator
710

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

2427
# Get File object
25-
file = File(request.files["file"])
28+
file = File(request.files["file"], app.config["UPLOAD_FOLDER"])
2629

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

47-
file = ZipArchive(request.files["file"])
50+
file = ZipArchive(request.files["file"], app.config["UPLOAD_FOLDER"])
4851
if not file.hasImages():
4952
validator.error['file'] = "No image files detected in the zip file."
5053
validator.abort()

autofocus/predict/app/models/File.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
from werkzeug import secure_filename
3-
from ..app import app
43

54

65
class File:
@@ -12,17 +11,18 @@ class File:
1211
name: Secured filename (Can be empty)
1312
"""
1413

15-
def __init__(self, file=None):
14+
def __init__(self, file=None, upload_path=None):
1615
"""
1716
Constructor of File
1817
1918
Save the file on the server if a file is given.
2019
2120
Parameters:
2221
file: Uploaded file object from flask
22+
upload_path: The path to upload the file
2323
"""
2424
if file:
25-
self.setFromUploadedFile(file)
25+
self.setFromUploadedFile(file, upload_path)
2626

2727
def __del__(self):
2828
"""
@@ -32,15 +32,18 @@ def __del__(self):
3232
"""
3333
os.remove(self.path)
3434

35-
def setFromUploadedFile(self, file):
35+
def setFromUploadedFile(self, file, upload_path=None):
3636
"""
3737
Save file from uploaded file
3838
3939
Parameters:
4040
file: Uploaded file object from flask
41+
upload_path: The path to upload the file
4142
"""
4243
self.name = secure_filename(file.filename)
43-
self.path = os.path.join(app.config["UPLOAD_FOLDER"], self.name)
44+
self.path = self.name
45+
if upload_path:
46+
self.path = os.path.join(upload_path, self.path)
4447
file.save(self.path)
4548

4649
def setPath(self, path):

autofocus/predict/app/models/ZipArchive.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import os
22
from zipfile import ZipFile
3-
from . import File
4-
from ..requests import ALLOWED_IMAGE_FILES
3+
from .File import File
4+
from ..requests.Validator import ALLOWED_IMAGE_FILES
55
from ..utils import allowed_file
66

77

@@ -16,16 +16,17 @@ class ZipArchive:
1616
zip: Opened zip file
1717
"""
1818

19-
def __init__(self, file):
19+
def __init__(self, file, upload_folder=None):
2020
"""
2121
Constructor of ZipFile
2222
2323
Store the given file and open the zip file.
2424
2525
Parameters:
2626
file: Uploaded file from flask
27+
upload_folder: The folder to save the zip file
2728
"""
28-
self.file = File.File(file)
29+
self.file = File(file, upload_folder)
2930
self.zip = ZipFile(self.file.getPath())
3031

3132
def listFiles(self):
@@ -80,7 +81,7 @@ def extractAll(self, path=None, members=None):
8081
self.zip.extractall(path, members)
8182
extractedFiles = {}
8283
for member in members:
83-
file = File.File()
84+
file = File()
8485
file.setPath(os.path.join(path, member))
8586
extractedFiles[member] = file
8687
return extractedFiles
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from .File import File
2-
from .Predictor import Predictor
3-
from .ZipArchive import ZipArchive

autofocus/predict/app/requests/PredictRequestValidator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from . import ALLOWED_IMAGE_FILES, Validator
1+
from .Validator import ALLOWED_IMAGE_FILES, Validator
22
from ..utils import allowed_file
33

44

autofocus/predict/app/requests/PredictZipRequestValidator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from . import ALLOWED_ZIP_FILES, Validator
1+
from .Validator import ALLOWED_ZIP_FILES, Validator
22
from ..utils import allowed_file
33

44

autofocus/predict/app/requests/Validator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from flask_api import status
44

55

6+
ALLOWED_IMAGE_FILES = set(["png", "jpg", "jpeg", "gif", "bmp"])
7+
ALLOWED_ZIP_FILES = {"zip"}
8+
9+
610
class Validator(ABC):
711
"""
812
Validate given request
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
from .PredictRequestValidator import PredictRequestValidator
2-
from .Validator import Validator
3-
from .PredictZipRequestValidator import PredictZipRequestValidator
4-
5-
ALLOWED_IMAGE_FILES = set(["png", "jpg", "jpeg", "gif", "bmp"])
6-
ALLOWED_ZIP_FILES = {"zip"}

0 commit comments

Comments
 (0)