-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor and add loading latest model from folder
- Loading branch information
Showing
5 changed files
with
84 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,45 @@ | ||
from os import scandir, listdir, environ | ||
from os.path import isfile, isdir, dirname, basename, relpath, join, exists, getsize | ||
from datetime import datetime | ||
from flask import Flask, render_template, send_from_directory | ||
from os import sep | ||
from os.path import isdir, dirname, basename, relpath, join, exists | ||
|
||
from config import models_dir, server_port | ||
from filesystem import Scaner | ||
|
||
class Scaner: | ||
def __init__(self, path): | ||
self.entries = [Entry(entry) for entry in scandir(path.encode())] | ||
|
||
class Entry: | ||
def __init__(self, entry): | ||
self.name = entry.name.decode() | ||
self.path = entry.path.decode() | ||
self.rel_path = relpath(self.path, models_dir) | ||
self.is_dir = entry.is_dir() | ||
self.created_time = datetime.fromtimestamp(entry.stat().st_ctime).ctime() | ||
self.modified_time = datetime.fromtimestamp(entry.stat().st_mtime).ctime() | ||
self.size = self._human_readable_size(self._get_size(entry.path)) | ||
|
||
def _get_size(self, path): | ||
total_size = getsize(path) | ||
if isdir(path): | ||
for item in listdir(path): | ||
item_path = join(path, item) | ||
if isfile(item_path): | ||
total_size += getsize(item_path) | ||
elif isdir(item_path): | ||
total_size += self._get_size(item_path) | ||
return total_size | ||
|
||
def _human_readable_size(self, size): | ||
units = ['B', 'KB', 'MB', 'GB', 'TB'] | ||
human_fmt = '{0:.2f} {1}' | ||
human_radix = 1024. | ||
|
||
for unit in units[:-1]: | ||
if size < human_radix: | ||
return human_fmt.format(size, unit) | ||
size /= humanradix | ||
|
||
return human_fmt.format(size, units[-1]) | ||
|
||
|
||
models_dir = environ.get('MODELS_DIR', 'models') | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/', methods=['GET']) | ||
def index(): | ||
return render_template('index.html', path='/', entries=Scaner(models_dir).entries) | ||
return list_dir(models_dir) | ||
|
||
@app.route('/<path:path>', methods=['GET']) | ||
def serve(path): | ||
real_path = join(models_dir, path) | ||
parent_path = relpath(dirname(real_path), models_dir) | ||
|
||
fetch_latest = '@latest' in path | ||
real_path = join(models_dir, path.replace('@latest', '')) | ||
if not exists(real_path): | ||
return 'Not Found', 404 | ||
|
||
if isdir(real_path): | ||
return render_template('index.html', parent_path=parent_path, path=path, entries=Scaner(real_path).entries) | ||
if fetch_latest: | ||
latest_entry = Scaner(real_path).latest_entry | ||
if not latest_entry: | ||
return 'No Models Found', 404 | ||
return download_file(latest_entry.path) | ||
else: | ||
return list_dir(real_path) | ||
else: | ||
return send_from_directory(dirname(real_path), basename(real_path), as_attachment=True, attachment_filename=basename(real_path)) | ||
return download_file(real_path) | ||
|
||
|
||
def list_dir(path): | ||
rel_path = relpath(path, models_dir) | ||
parent_path = dirname(rel_path) | ||
return render_template('index.html', sep=sep, parent_path=parent_path, path=rel_path, entries=Scaner(path).entries) | ||
|
||
def download_file(path): | ||
return send_from_directory(dirname(path), basename(path), as_attachment=True, attachment_filename=basename(path)) | ||
|
||
|
||
if __name__ == '__main__': | ||
app.run(host='0.0.0.0', port=environ.get('PORT', 8080)) | ||
app.run(host='0.0.0.0', port=server_port) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
from os import environ | ||
|
||
models_dir = environ.get('MODELS_DIR', 'models') | ||
server_port = environ.get('PORT', 8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from datetime import datetime | ||
from os import scandir, listdir | ||
from os.path import isfile, isdir, relpath, join, getsize | ||
|
||
from config import models_dir | ||
|
||
class Scaner: | ||
def __init__(self, path): | ||
self.entries = [Entry(entry) for entry in scandir(path.encode())] | ||
self.latest_entry = next(iter(sorted(self.entries, key=lambda e: e.modified_time, reverse=True)), None) | ||
|
||
class Entry: | ||
def __init__(self, entry): | ||
self.name = entry.name.decode() | ||
self.path = entry.path.decode() | ||
self.rel_path = relpath(self.path, models_dir) | ||
self.is_dir = entry.is_dir() | ||
self.created_time = datetime.fromtimestamp(entry.stat().st_ctime).ctime() | ||
self.modified_time = datetime.fromtimestamp(entry.stat().st_mtime).ctime() | ||
self.size = self._human_readable_size(self._get_size(entry.path)) | ||
|
||
def _get_size(self, path): | ||
total_size = getsize(path) | ||
if isdir(path): | ||
for item in listdir(path): | ||
item_path = join(path, item) | ||
if isfile(item_path): | ||
total_size += getsize(item_path) | ||
elif isdir(item_path): | ||
total_size += self._get_size(item_path) | ||
return total_size | ||
|
||
def _human_readable_size(self, size): | ||
units = ['B', 'KB', 'MB', 'GB', 'TB'] | ||
human_fmt = '{0:.2f} {1}' | ||
human_radix = 1024. | ||
|
||
for unit in units[:-1]: | ||
if size < human_radix: | ||
return human_fmt.format(size, unit) | ||
size /= human_radix | ||
|
||
return human_fmt.format(size, units[-1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters