Skip to content

Commit

Permalink
WIP: Merge _load_raw into existing _load method
Browse files Browse the repository at this point in the history
  • Loading branch information
MHendricks committed Jan 5, 2024
1 parent 603ba1a commit 6a9da11
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
4 changes: 3 additions & 1 deletion hab/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ def generate_cache(self, resolver, site_file, version=1):
for path in sorted(glob.glob(str(dirname / glob_str))):
path = Path(path)
try:
data = cls(forest={}, resolver=resolver)._load_raw(path)
data = cls(forest={}, resolver=resolver)._load(
path, cached=False
)
except (
InvalidVersion,
InvalidVersionError,
Expand Down
20 changes: 8 additions & 12 deletions hab/parsers/distro_version.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from pathlib import Path

from packaging.version import InvalidVersion, Version

from .. import NotSet
Expand Down Expand Up @@ -100,15 +98,16 @@ def alias_mods(self):
"""
return self._alias_mods

def _load_raw(self, filename):
"""Load the raw cache data and resolve any extra cached data."""
ret = super()._load_raw(filename)
def _load(self, filename, cached=True):
"""Sets self.filename and parses the json file returning the data."""
ret = super()._load(filename, cached=cached)

# Ensure the version is added to the return and is valid
self.filename = Path(filename)
self.version = self._resolve_version(ret, filename)
ret["version"] = str(self.version)
# Resolve the version from the various supported ways its stored.
self._resolve_version(ret, filename)

if not cached:
# Ensure the version is stored on the returned dictionary
ret["version"] = str(self.version)
return ret

def load(self, filename):
Expand All @@ -119,9 +118,6 @@ def load(self, filename):
# Store any alias_mods, they will be processed later when flattening
self._alias_mods = data.get("alias_mods", NotSet)

# Resolve the version from the various supported ways its stored.
self.version = self._resolve_version(data, filename)

# The name should be the version == specifier.
self.distro_name = data.get("name")
self.name = "{}=={}".format(self.distro_name, self.version)
Expand Down
24 changes: 13 additions & 11 deletions hab/parsers/hab_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,23 +520,25 @@ def inherits(self):
# Note: Sub-classes need to override this method to enable inheritance.
return False

def _load(self, filename):
"""Sets self.filename and parses the json file returning the data."""
def _load(self, filename, cached=True):
"""Sets self.filename and parses the json file returning the data dict.
Args:
filename (pathlib.Path): The file to load.
cached (bool, optional): Enables loading of cached data instead of
loading the data from disk.
"""
self.filename = Path(filename)

ret = self._cache().get(Path(filename).as_posix())
if ret:
logger.debug(f'Cached: "{filename}"')
return ret
if cached:
ret = self._cache().get(Path(filename).as_posix())
if ret:
logger.debug(f'Cached: "{filename}"')
return ret

logger.debug(f'Loading "{filename}"')
return utils.load_json_file(self.filename)

def _load_raw(self, filename):
"""Load the raw cache data and resolve any extra cached data."""
logger.debug(f'Loading "{filename}"')
return utils.load_json_file(filename)

def load(self, filename, data=None):
"""Load this objects configuration from the given json filename.
Expand Down

0 comments on commit 6a9da11

Please sign in to comment.