Files
OpenBB/openbb_terminal/core/session/system_handler.py
Henrique Joaquim 937d033160 Adds BaseModel and SystemModel (#4491)
* adding BaseModel

* initial version of the system model

* creating setup functions and removing constants from config_terminal

* removing log_supress const

* fix test

* merge

* system handler

* fix import

* centralized configs setup

* repr to use the BaseModel

* updating i18n dict location

* ignoring warning on wrong type-var

* ruff

* Update openbb_terminal/core/models/system_model.py

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* Update openbb_terminal/core/models/system_model.py

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>

* import literal

* loading env files before the plot backend is called

* fix tests/openbb_terminal/test_terminal_helper.py

* tests need to have i18n initialized

* fix seeking_alpha tests

---------

Co-authored-by: montezdesousa <79287829+montezdesousa@users.noreply.github.com>
2023-03-22 15:00:58 +00:00

86 lines
2.1 KiB
Python

import atexit
import json
import os
from pathlib import Path
from pydantic.json import pydantic_encoder
from openbb_terminal.core.config.paths import (
SETTINGS_DIRECTORY,
)
from openbb_terminal.core.models import SystemModel
from openbb_terminal.core.session.env_handler import load_env_to_model, reading_env
SYSTEM_FILE_PATH = SETTINGS_DIRECTORY / "system.json"
def handle_system(file_path: Path = SYSTEM_FILE_PATH) -> SystemModel:
"""
Handle the system.
Reads system data from file if it exists, else reads environment variable data.
Returns a system model built from the loaded data.
Parameters
----------
file_path (Path): The file path.
Returns
-------
SystemModel: The system model.
"""
try:
if os.path.isfile(file_path):
system_data = {}
with open(file_path) as f:
system_data = json.load(f)
if system_data:
return load_env_to_model(system_data, SystemModel)
except Exception:
pass
__env_dict = reading_env()
system = load_env_to_model(__env_dict, SystemModel)
save_system(system=system, file_path=file_path)
subscribe_delete_system_on_exit(file_path=file_path)
return system
def save_system(system: SystemModel, file_path: Path = SYSTEM_FILE_PATH):
"""
Write system to file.
Parameters
----------
system (SystemModel): The system model.
file_path (Path): The file path.
Returns
-------
system (SystemModel): The system model.
"""
try:
with open(file_path, "w") as f:
json_content = json.dumps(system, default=pydantic_encoder)
f.write(json_content)
except Exception:
pass
def subscribe_delete_system_on_exit(file_path: Path):
"""
Deletes the system file on exiting the application.
Parameters
----------
file_path (Path): The file path.
"""
def delete_file(file_path: Path):
if os.path.isfile(file_path):
os.remove(file_path)
atexit.register(delete_file, file_path=file_path)