style: Run linter and fix code formatting in webserver.py
This commit is contained in:
parent
b83f70c354
commit
0763e07039
|
@ -1,14 +1,16 @@
|
||||||
import json
|
|
||||||
import datetime
|
import datetime
|
||||||
|
import json
|
||||||
import logging
|
import logging
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from bottle import route, run, request, response
|
from bottle import request, response, route, run
|
||||||
|
|
||||||
from personal_data.csv_import import load_csv_file
|
from personal_data.csv_import import load_csv_file
|
||||||
|
|
||||||
logger = logging.getLogger("webserver")
|
logger = logging.getLogger('webserver')
|
||||||
logger.setLevel(logging.INFO)
|
logger.setLevel(logging.INFO)
|
||||||
|
|
||||||
|
|
||||||
def parse_time(time_str: str) -> datetime.datetime:
|
def parse_time(time_str: str) -> datetime.datetime:
|
||||||
"""
|
"""
|
||||||
Parse a time string assuming ISO-8601 format.
|
Parse a time string assuming ISO-8601 format.
|
||||||
|
@ -16,35 +18,37 @@ def parse_time(time_str: str) -> datetime.datetime:
|
||||||
"""
|
"""
|
||||||
return datetime.datetime.fromisoformat(time_str)
|
return datetime.datetime.fromisoformat(time_str)
|
||||||
|
|
||||||
|
|
||||||
@route('/newest')
|
@route('/newest')
|
||||||
def newest_entry():
|
def newest_entry():
|
||||||
"""
|
"""
|
||||||
Loads a CSV file (default: data.csv, overridable by query param 'file'),
|
Loads a CSV file (default: data.csv, overridable by query param 'file'),
|
||||||
finds the newest entry based on the 'time.current' column, and returns it as JSON.
|
finds the newest entry based on the 'time.current' column, and returns it as JSON.
|
||||||
"""
|
"""
|
||||||
file_param = request.query.get("file", "data.csv")
|
file_param = request.query.get('file', 'data.csv')
|
||||||
csv_path = Path(file_param)
|
csv_path = Path(file_param)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data = load_csv_file(csv_path)
|
data = load_csv_file(csv_path)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error loading CSV file at {csv_path}: {e}")
|
logger.error(f'Error loading CSV file at {csv_path}: {e}')
|
||||||
response.status = 500
|
response.status = 500
|
||||||
return {"error": f"Failed to load CSV: {str(e)}"}
|
return {'error': f'Failed to load CSV: {str(e)}'}
|
||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
response.status = 404
|
response.status = 404
|
||||||
return {"error": "CSV file is empty or no data found"}
|
return {'error': 'CSV file is empty or no data found'}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
newest = max(data, key=lambda r: parse_time(r['time.current']))
|
newest = max(data, key=lambda r: parse_time(r['time.current']))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error processing CSV data: {e}")
|
logger.error(f'Error processing CSV data: {e}')
|
||||||
response.status = 500
|
response.status = 500
|
||||||
return {"error": f"Failed to process CSV data: {str(e)}"}
|
return {'error': f'Failed to process CSV data: {str(e)}'}
|
||||||
|
|
||||||
response.content_type = "application/json"
|
response.content_type = 'application/json'
|
||||||
return json.dumps(newest)
|
return json.dumps(newest)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run(host='localhost', port=8080, debug=True)
|
run(host='localhost', port=8080, debug=True)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user