feat: add cleanup support

This commit is contained in:
Alex Wellnitz 2024-09-19 13:49:39 +02:00
parent 541a3c1ae7
commit 4cc3c6efb2
2 changed files with 19 additions and 9 deletions

View File

@ -4,7 +4,10 @@
### Installation ### Installation
```bash ```bash
python -m venv .venv git clone https://github.com/alexohneander/sim_free
cd sim_free
python3 -m venv .venv
source .venv/bin/activate source .venv/bin/activate
pip install -r requirements.txt pip install -r requirements.txt
``` ```

23
main.py
View File

@ -1,13 +1,14 @@
import os import os
import logging import logging
import uuid import uuid
from pathlib import Path
from simcrunner import Simc, JsonExport, Arguments, Profile from simcrunner import Simc, JsonExport, Arguments, Profile
from typing import Union, Annotated from typing import Union, Annotated
from fastapi import FastAPI, Form from fastapi import FastAPI, Form
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel from pydantic import BaseModel
from starlette.responses import FileResponse from starlette.responses import FileResponse, HTMLResponse
from simcrunner.simc import HtmlExport from simcrunner.simc import HtmlExport
# SIMC Settings # SIMC Settings
@ -28,17 +29,26 @@ def read_root():
@app.post("/sim/current_gear") @app.post("/sim/current_gear")
async def simulate_current_gear(simcprofile: Annotated[str, Form()]): async def simulate_current_gear(simcprofile: Annotated[str, Form()]):
sim_args = create_sim_arguments(simcprofile) profile_path = create_sim_arguments(simcprofile)
export_path = create_html_export() export_path = create_html_export()
html_export = HtmlExport(export_path) html_export = HtmlExport(export_path)
profile = Profile(profile_path)
args = Arguments(profile, iterations=1000)
(runner (runner
.add_args(sim_args) .add_args(args)
.add_args('target_error=0.05', threads=4) .add_args('target_error=0.05', threads=4)
.add_args(html_export) .add_args(html_export)
.run()) .run())
return FileResponse(export_path) with open(export_path, 'r') as file:
response = file.read().replace('\n', '')
os.remove(export_path)
os.remove(profile_path)
return HTMLResponse(response)
# HELPER Functions # HELPER Functions
def create_profile(profile_path: str, profile_data: str): def create_profile(profile_path: str, profile_data: str):
@ -58,7 +68,4 @@ def create_sim_arguments(profile_data: str):
create_profile(profile_path, profile_data) create_profile(profile_path, profile_data)
profile = Profile(profile_path) return profile_path
args = Arguments(profile, iterations=1000)
return args