Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small stages.py refactors #4674

Merged
merged 3 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 6 additions & 17 deletions cloudinit/stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def update_event_enabled(
datasource: sources.DataSource,
cfg: dict,
event_source_type: EventType,
scope: Optional[EventScope] = None,
scope: EventScope,
) -> bool:
"""Determine if a particular EventType is enabled.

Expand Down Expand Up @@ -93,11 +93,7 @@ def update_event_enabled(
)
LOG.debug("Allowed events: %s", allowed)

scopes: Iterable[EventScope]
if not scope:
scopes = allowed.keys()
else:
scopes = [scope]
scopes: Iterable[EventScope] = [scope]
scope_values = [s.value for s in scopes]

for evt_scope in scopes:
Expand All @@ -122,7 +118,7 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None):
else:
self.ds_deps = [sources.DEP_FILESYSTEM, sources.DEP_NETWORK]
# Created on first use
self._cfg: Optional[dict] = None
self._cfg: Dict = {}
self._paths: Optional[helpers.Paths] = None
self._distro: Optional[distros.Distro] = None
# Changed only when a fetch occurs
Expand All @@ -138,14 +134,11 @@ def __init__(self, ds_deps: Optional[List[str]] = None, reporter=None):
)
self.reporter = reporter

def _reset(self, reset_ds=False):
def _reset(self):
# Recreated on access
self._cfg = None
self._cfg = {}
self._paths = None
self._distro = None
if reset_ds:
self.datasource = None
self.ds_restored = False

@property
def distro(self):
Expand Down Expand Up @@ -179,8 +172,6 @@ def _extract_cfg(self, restriction):
ocfg = util.get_cfg_by_path(ocfg, ("system_info",), {})
elif restriction == "paths":
ocfg = util.get_cfg_by_path(ocfg, ("system_info", "paths"), {})
if not isinstance(ocfg, (dict)):
ocfg = {}
return ocfg

@property
Expand Down Expand Up @@ -262,10 +253,8 @@ def _initialize_filesystem(self):
)

def read_cfg(self, extra_fns=None):
# None check so that we don't keep on re-loading if empty
if self._cfg is None:
if not self._cfg:
self._cfg = self._read_cfg(extra_fns)
# LOG.debug("Loaded 'init' config %s", self._cfg)

def _read_cfg(self, extra_fns):
no_cfg_paths = helpers.Paths({}, self.datasource)
Expand Down
3 changes: 2 additions & 1 deletion tests/unittests/test_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,9 @@ def init(self, paths):
yield init

@mock.patch(M_PATH + "util.ensure_file")
@mock.patch(f"{M_PATH}Init._read_cfg")
def test_ensure_file_not_called_if_no_log_file_configured(
self, m_ensure_file, init
self, m_read_cfg, m_ensure_file, init
):
"""If no log file is configured, we should not ensure its existence."""
init._cfg = {}
Expand Down
Loading