Skip to content

Commit

Permalink
fix: move session_args to init file
Browse files Browse the repository at this point in the history
  • Loading branch information
horw committed Nov 27, 2023
1 parent ce9df7f commit 635a8c9
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 53 deletions.
5 changes: 5 additions & 0 deletions idf_build_apps/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@

__version__ = '2.0.0b3'

from .session_args import (
SessionArgs,
)

LOGGER = logging.getLogger('idf_build_apps')
SESSION_ARGS = SessionArgs()

from .app import (
App,
Expand Down
5 changes: 3 additions & 2 deletions idf_build_apps/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
computed_field,
)

import idf_build_apps

from . import (
LOGGER,
)
Expand All @@ -55,7 +57,6 @@
)
from .session_args import (
SessionArgs,
get_session_args,
)
from .utils import (
BaseModel,
Expand Down Expand Up @@ -194,7 +195,7 @@ def __init__(
# create logger and process sdkconfig files
self._logger = LOGGER.getChild(str(hash(self)))
self._logger.addFilter(_AppBuildStageFilter(app=self))
self.session_args = get_session_args()
self.session_args = idf_build_apps.SESSION_ARGS

self._process_sdkconfig_files()

Expand Down
6 changes: 2 additions & 4 deletions idf_build_apps/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

from . import (
LOGGER,
SESSION_ARGS,
)
from .app import (
App,
Expand Down Expand Up @@ -46,9 +47,6 @@
FolderRule,
Manifest,
)
from .session_args import (
set_session_args,
)
from .utils import (
InvalidCommand,
files_matches_patterns,
Expand Down Expand Up @@ -730,7 +728,7 @@ def main():
apply_config_args(args)
validate_args(parser, args)

set_session_args(args)
SESSION_ARGS.set(args)
# real call starts here
apps = find_apps(
args.paths,
Expand Down
36 changes: 3 additions & 33 deletions idf_build_apps/session_args.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import os
import re
import typing as t
import warnings

from .utils import (
BaseModel,
Expand All @@ -18,16 +17,12 @@ class SessionArgs(BaseModel):
override_sdkconfig_items: t.Dict[str, t.Any] = {}
override_sdkconfig_file_path: t.Optional[str] = None

def __new__(cls, *args, **kwargs):
if not hasattr(cls, '_instance'):
cls._instance = super().__new__(cls)
return cls._instance

def __init__(self, parsed_args, *, workdir=None):
def __init__(self):
super().__init__()

def set(self, parsed_args, *, workdir=None):
if workdir:
self.workdir = workdir

self._setup_override_sdkconfig(parsed_args)

def _setup_override_sdkconfig(self, args):
Expand Down Expand Up @@ -75,28 +70,3 @@ def _create_override_sdkconfig_merged_file(self, override_sdkconfig_merged_items
for key, value in override_sdkconfig_merged_items.items():
f.write(f'{key}={value}\n')
return f_path


_session_args: t.Optional[SessionArgs] = None


def get_session_args() -> t.Optional[SessionArgs]:
global _session_args
if _session_args is None:
warnings.warn('SessionArgs have not been set up yet.')
return _session_args


def set_session_args(*args, **kwargs) -> None:
global _session_args
if _session_args is None:
_session_args = SessionArgs(*args, **kwargs)
else:
warnings.warn('SessionArgs have been set up. Use `get_session_args` to retrieve an instance of this class.')


def reset_singleton() -> None:
global _session_args
if hasattr(SessionArgs, '_instance'):
_session_args = None
del SessionArgs._instance
8 changes: 4 additions & 4 deletions idf_build_apps/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
)
from pydantic import BaseModel as _BaseModel

from . import (
LOGGER,
)

try:
from typing import (
Literal,
Expand Down Expand Up @@ -161,6 +157,10 @@ def subprocess_run(
:param additional_env_dict: additional environment variables
:return: return code
"""
from . import (
LOGGER,
)

LOGGER.debug('==> Running %s', ' '.join(cmd))

subprocess_env = None
Expand Down
7 changes: 3 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@

import pytest

import idf_build_apps
from idf_build_apps import (
App,
session_args,
setup_logging,
)
from idf_build_apps.session_args import (
reset_singleton,
)


@pytest.fixture(autouse=True)
def clean_cls_attr():
App.MANIFEST = None
reset_singleton()
idf_build_apps.SESSION_ARGS = session_args.SessionArgs()


@pytest.fixture(autouse=True)
Expand Down
9 changes: 3 additions & 6 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,10 +402,7 @@ def test_with_sdkconfig_defaults_env_var_expansion(self, tmp_path, monkeypatch):
assert len(apps) == 0

def test_with_sdkconfig_override(self, tmp_path):
from idf_build_apps.session_args import (
get_session_args,
set_session_args,
)
import idf_build_apps

create_project('test1', tmp_path)
(tmp_path / 'test1' / 'sdkconfig.defaults').write_text(
Expand All @@ -426,8 +423,8 @@ def test_with_sdkconfig_override(self, tmp_path):
args = Args(
'CONFIG_IDF_TARGET=esp32c3,CONFIG_FREERTOS_IDLE_TASK_STACKSIZE=1522,CONFIG_A=10', 'sdkconfig.override1'
)
set_session_args(args, workdir=tmp_path)
session_args = get_session_args()
idf_build_apps.SESSION_ARGS.set(args, workdir=tmp_path)
session_args = idf_build_apps.SESSION_ARGS

assert str(tmp_path) in str(session_args.override_sdkconfig_file_path)
assert len(session_args.override_sdkconfig_items) == 4
Expand Down

0 comments on commit 635a8c9

Please sign in to comment.