MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / _load_resource

Method _load_resource

src/commoncode/resource.py:936–968  ·  view source on GitHub ↗

Return a Resource with ``path`` loaded from the disk cache.

(self, path)

Source from the content-addressed store, hash-verified

934
935 # TODO: consider adding a small LRU cache in front of this for perf?
936 def _load_resource(self, path):
937 """
938 Return a Resource with ``path`` loaded from the disk cache.
939 """
940 path = clean_path(path)
941 cache_location = self._get_resource_cache_location(path, create_dirs=False)
942
943 if TRACE:
944 logger_debug(
945 " Codebase._load_resource: exists:",
946 exists(cache_location),
947 "cache_location:",
948 cache_location,
949 )
950
951 if not exists(cache_location):
952 raise ResourceNotInCache(f"Failed to load Resource: {path} from {cache_location!r}")
953
954 # TODO: consider messagepack or protobuf for compact/faster processing
955 try:
956 with open(cache_location, "rb") as cached:
957 # TODO: Use custom json encoder to encode JSON list as a tuple
958 # TODO: Consider using simplejson
959 data = json.load(cached)
960 return self.resource_class(**data)
961 except Exception as e:
962 with open(cache_location, "rb") as cached:
963 cached_data = cached.read()
964 msg = (
965 f"ERROR: failed to load resource from cached location: {cache_location} "
966 "with content:\n\n" + repr(cached_data) + "\n\n" + traceback.format_exc()
967 )
968 raise Exception(msg) from e
969
970 def _remove_resource(self, resource):
971 """

Callers 1

get_resourceMethod · 0.95

Calls 6

clean_pathFunction · 0.85
ResourceNotInCacheClass · 0.85
logger_debugFunction · 0.70
loadMethod · 0.45
readMethod · 0.45

Tested by

no test coverage detected