Overhaul logging and do database creation

* print_log() now accepts a True/False to decide if a message should be printed
* Pass the config['DEBUG'] boolean to some of the print_log statements
* Add timestamps to log messages
* Change the source levels on some log messages to something more fitting
* Add a time_only option to the date/time string generator (Not sure why I even kept the old option but meh)
* Implement a "better than nothing" database-creation sequence
This commit is contained in:
Dominik V. Salonen 2018-03-20 19:19:26 +01:00
parent 09e5769373
commit e798886b7d
2 changed files with 44 additions and 22 deletions

View File

@ -1,18 +1,30 @@
from datetime import datetime
import time
# TODO: I dunno, but there's gotta be a better way to do this crap
def print_log(source, message):
if source == "Main":
print('\033[92m' + source + ': \033[0m' + message)
elif source == "Notice":
print('\033[93m' + source + ': \033[0m' + message)
elif source == "Warning":
print('\033[91m' + source + ': \033[0m' + message)
elif source == "Web":
print('\033[95m' + source + ': \033[0m' + message)
else:
print('\033[94m' + source + ': \033[0m' + message)
# TODO: I dunno, but there's gotta be a better way to do this crap. This is way too messy
def time_to_string(unixtime):
return datetime.fromtimestamp(unixtime).strftime('%B %d, %Y (%H:%M - ' + time.tzname[time.daylight] + ')')
# Some messages pass a True or False as should_print depending on config variables
# Default to true, but return doing nothing if they pass a False.
def print_log(source, message, should_print=True):
if should_print == False:
return
if source == "Main":
print('\033[92m', current_time(), source.center(8, '-'), ': \033[0m', message)
elif source == "Notice":
print('\033[93m', current_time(), source.center(8, '-'), ': \033[0m', message)
elif source == "Warning":
print('\033[91m', current_time(), source.center(8, '-'), ': \033[0m', message)
elif source == "Web":
print('\033[95m', current_time(), source.center(8, '-'), ': \033[0m', message)
else:
print('\033[94m', current_time(), source.center(8, '-'), ': \033[0m', message)
def current_time():
return time_to_string(time.time(), True)
def time_to_string(unixtime, time_only = False):
if time_only:
return datetime.fromtimestamp(unixtime).strftime('[%H:%M:%S - ' + time.tzname[time.daylight] + ']')
else:
return datetime.fromtimestamp(unixtime).strftime('%B %d, %Y (%H:%M:%S - ' + time.tzname[time.daylight] + ')')

26
run.py
View File

@ -24,12 +24,21 @@ app = Flask(__name__)
print_log('Main', 'Running in "' + os.getcwd() + '"')
print_log('Main', 'Checking for data folder')
if not os.path.exists(config['UPLOAD_FOLDER']):
print_log('Main', 'Data folder not found, creating')
print_log('Warning', 'Data folder not found, creating')
os.makedirs(config['UPLOAD_FOLDER'])
if not os.path.exists('files.db'):
print_log('Warning', 'Database not found, attempting to create')
os.system('sqlite3 files.db < schema.sql')
if not os.path.exists('files.db'):
print_log('Warning', 'Could not create database. Is sqlite3 available?')
quit()
else:
print_log('Notice', 'Database created')
if config["EXTENDED_DEBUG"] == False:
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
print_debug = config["DEBUG"]
def cleaner_thread():
# Call itself again after the interval
@ -42,11 +51,11 @@ def cleaner_thread():
def delete_old():
print_log('Notice', 'Cleaner running')
print_log('Thread', 'Cleaner running', print_debug)
targetTime = time.time() - config["TIME"]
old = db.get_old_files(targetTime)
for file in old:
print_log('Notice', 'Removing old file "' + file["file"] + '"')
print_log('Thread', 'Removing old file "' + file["file"] + '"')
try:
os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"]))
except Exception:
@ -63,7 +72,7 @@ def allowed_file(filename):
return True
else:
if config["BLACKLIST"]:
return '.' in filename and filename.rsplit('.', 1)[1] not in config["BANNED_EXTENSIONS"]
return '.' in filename and filename.rsplit('.', 1)[1] not in config["BANNED_EXTENSIONS"]
else:
return '.' in filename and filename.rsplit('.', 1)[1] in config["ALLOWED_EXTENSIONS"]
@ -71,7 +80,7 @@ def allowed_file(filename):
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
print_log('Web', 'New file received')
print_log('Main', 'New file received', print_debug)
if not application.basicauth(request.headers.get('X-Hyozan-Auth'), config["KEY"]):
abort(403)
data = dict()
@ -85,7 +94,7 @@ def upload_file():
thread1 = Thread(target = db.add_file, args = (filename,))
thread1.start()
print_log('Thread', 'Adding to DB')
print_log('Thread', 'Adding file to DB', print_debug)
file.save(os.path.join(config['UPLOAD_FOLDER'], filename))
thread1.join()
@ -104,6 +113,7 @@ def upload_file():
# Return Web UI if we have a GET request
elif request.method == 'GET':
print_log('Web', 'Hit upload page')
return render_template('upload.html', page=config["SITE_DATA"])
# Def all the static pages
@ -149,7 +159,7 @@ def no_permission(e):
@app.route('/<filename>', methods=['GET'])
def get_file(filename):
print_log('Web', 'Hit "' + filename + '" - ' + time_to_string(time.time()))
print_log('Web', 'Hit "' + filename + '"')
try:
db.update_file(filename)
except Exception:
@ -170,7 +180,7 @@ def nginx_error(error):
if config["DELETE_FILES"]:
cleaner_thread()
if __name__ == '__main__':
app.run(
port=config["PORT"],