1
0
personal-data/html_data_format/__main__.py
Jon Michael Aanes acaedcbd3a
Some checks failed
Run Python tests (through Pytest) / Test (push) Failing after 29s
Verify Python project can be installed, loaded and have version checked / Test (push) Failing after 26s
Fix code style and optimization issues
- Fix variable naming: TIME_COLUMN -> time_column, l -> components, COLUMNS -> columns
- Extract exception string literals to variables (EM101)
- Replace assert statements with proper error handling in obsidian_import
- Use dict.pop() instead of del for key removal (RUF051)
- Use elif instead of else-if to reduce indentation (PLR5501)
- Replace magic number 10 with MIN_COOKIES_THRESHOLD constant (PLR2004)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-06-25 00:27:33 +02:00

47 lines
1.2 KiB
Python

import logging
from pathlib import Path
import bottle
from personal_data import csv_import
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ROOT_DIRECTORY = Path('output')
@bottle.route('/<csv_type>/newest')
def newest_entry(csv_type: str):
"""Loads a CSV file and finds the newest entry based on 'time.current' column, returns as JSON."""
path = ROOT_DIRECTORY / f'{csv_type}.csv'
bottle.response.content_type = 'application/json'
try:
data = csv_import.load_csv_file(path)
except Exception:
logger.exception('Error loading CSV file at %s', path)
bottle.response.status = 500
return {'error': 'Failed to load CSV'}
if not data:
bottle.response.status = 404
return {'error': 'CSV file is empty or no data found'}
time_column = 'time.current'
if time_column in data[0]:
newest = max(data, key=lambda r: r.get(time_column))
else:
newest = data[-1]
return {
csv_import.csv_safe_value(k): csv_import.csv_safe_value(v)
for k, v in newest.items()
}
if __name__ == '__main__':
bottle.run(host='localhost', port=8080, debug=True)