Repository: OpenBB-finance/OpenBBTerminal
File: openbb_platform/extensions/platform_api/openbb_platform_api/main.py (Python)

Code before:
HOME = os.environ.get("HOME") or os.environ.get("USERPROFILE")

if not HOME:
    raise ValueError("HOME or USERPROFILE environment variable not set.")

CURRENT_USER_SETTINGS = os.path.join(HOME, ".openbb_platform", "user_settings.json")
USER_SETTINGS_COPY = os.path.join(HOME, ".openbb_platform", "user_settings_backup.json")

# Widget filtering is optional and can be used to exclude widgets from the widgets.json file
# You can generate this filter on OpenBB Hub: https://my.openbb.co/app/platform/widgets
# Alternatively, you can supply a JSON-encoded list of API paths to ignore.
WIDGET_SETTINGS = os.path.join(HOME, ".openbb_platform", "widget_settings.json")

kwargs = parse_args()

_app = kwargs.pop("app", None)

if _app:
    app = _app

WIDGETS_PATH = kwargs.pop("widgets-json", None) or kwargs.pop("widgets-path", None)
APPS_PATH = kwargs.pop("apps-json", None) or kwargs.pop("templates-path", None)
EDITABLE = kwargs.pop("editable", None) is True or WIDGETS_PATH is not None
DEFAULT_APPS_PATH = (
    Path(__file__).absolute().parent.joinpath("assets").joinpath("default_apps.json")
)
AGENTS_PATH = kwargs.pop("agents-json", None)
build = kwargs.pop("build", True)
build = False if kwargs.pop("no-build", None) else build
login = kwargs.pop("login", False)
dont_filter = kwargs.pop("no-filter", False)
widget_exclude_filter: list = kwargs.pop("exclude", [])

uvicorn_settings = (
    SystemService().system_settings.python_settings.model_dump().get("uvicorn", {})
)

for key, value in uvicorn_settings.items():
    if key not in kwargs and key != "app" and value is not None:
        kwargs[key] = value

if not dont_filter and os.path.exists(WIDGET_SETTINGS):
    with open(WIDGET_SETTINGS) as widget_settings_file:
        try:
            widget_exclude_filter_json = json.load(widget_settings_file).get(
                "exclude", []
            )
            if isinstance(widget_exclude_filter_json, list):
                widget_exclude_filter.extend(widget_exclude_filter_json)
        except json.JSONDecodeError as e:
            logger.info("Error loading widget filter settings -> %s", e)

Review comment (suggestion):
Let's internally define how long we will be maintaining the backwards compatibility in order not to bloat the code with logic we forget about. A comment saying it should be removed by date X or version X will make out future selves say "thank you"

Code after:
HOME = os.environ.get("HOME") or os.environ.get("USERPROFILE")

if not HOME:
    raise ValueError("HOME or USERPROFILE environment variable not set.")

CURRENT_USER_SETTINGS = os.path.join(HOME, ".openbb_platform", "user_settings.json")
USER_SETTINGS_COPY = os.path.join(HOME, ".openbb_platform", "user_settings_backup.json")

# Widget filtering is optional and can be used to exclude widgets from the widgets.json file
# You can generate this filter on OpenBB Hub: https://my.openbb.co/app/platform/widgets
# Alternatively, you can supply a JSON-encoded list of API paths to ignore.
WIDGET_SETTINGS = os.path.join(HOME, ".openbb_platform", "widget_settings.json")

kwargs = parse_args()

_app = kwargs.pop("app", None)

if _app:
    app = _app


# These are handled for backwards compatibility, and in ./utils/api::parse_args.
# It should be handled by this point in the code execution, but in case the key
# still exists for some reason, we will pop it so it doesn't get passed to uvicorn.run
# It would be reasonable to remove special handling by V1.3
WIDGETS_PATH = kwargs.pop("widgets-json", None) or kwargs.pop("widgets-path", None)
APPS_PATH = kwargs.pop("apps-json", None) or kwargs.pop("templates-path", None)

EDITABLE = kwargs.pop("editable", None) is True or WIDGETS_PATH is not None
DEFAULT_APPS_PATH = (
    Path(__file__).absolute().parent.joinpath("assets").joinpath("default_apps.json")
)
AGENTS_PATH = kwargs.pop("agents-json", None)
build = kwargs.pop("build", True)
build = False if kwargs.pop("no-build", None) else build
login = kwargs.pop("login", False)
dont_filter = kwargs.pop("no-filter", False)
widget_exclude_filter: list = kwargs.pop("exclude", [])

uvicorn_settings = (
    SystemService().system_settings.python_settings.model_dump().get("uvicorn", {})
)

for key, value in uvicorn_settings.items():
    if key not in kwargs and key != "app" and value is not None:
        kwargs[key] = value

if not dont_filter and os.path.exists(WIDGET_SETTINGS):
    with open(WIDGET_SETTINGS) as widget_settings_file:
        try:
            widget_exclude_filter_json = json.load(widget_settings_file).get(