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

Method get_resource

src/commoncode/resource.py:860–895  ·  view source on GitHub ↗

Return the Resource with `path` or None if it does not exists. The ``path`` must be relative to the root (and including the root name as its first segment).

(self, path)

Source from the content-addressed store, hash-verified

858
859 # FIXME: the PATH SHOULD NOT INCLUDE THE ROOT NAME
860 def get_resource(self, path):
861 """
862 Return the Resource with `path` or None if it does not exists.
863 The ``path`` must be relative to the root (and including the root
864 name as its first segment).
865 """
866 assert isinstance(path, str), f"Invalid path: {path!r} is not a string."
867 path = clean_path(path)
868 if TRACE:
869 msg = [" Codebase.get_resource:", "path:", path]
870 if not path or path not in self.resources_by_path:
871 msg.append("not in resources!")
872 else:
873 msg.extend(["exists on disk:", self._exists_on_disk(path)])
874 msg.extend(["exists in memo:", self._exists_in_memory(path)])
875 logger_debug(*msg)
876
877 # we use Codebase.CACHED_RESOURCE as a semaphore for existing but only
878 # on-disk, non-in-memory resource that we need to load from the disk
879 # cache to differentiate from None which means missing
880 res = self.resources_by_path.get(path)
881 if res is Codebase.CACHED_RESOURCE:
882 res = self._load_resource(path)
883
884 elif isinstance(res, Resource):
885 res = attr.evolve(res)
886
887 elif res is None:
888 pass
889 else:
890 # this should never happen
891 raise Exception(f"get_resource: Internal error when getting {path!r}")
892
893 if TRACE:
894 logger_debug(" Resource:", res)
895 return res
896
897 def save_resource(self, resource):
898 """

Calls 8

_exists_on_diskMethod · 0.95
_exists_in_memoryMethod · 0.95
_load_resourceMethod · 0.95
clean_pathFunction · 0.85
evolveMethod · 0.80
logger_debugFunction · 0.70
getMethod · 0.65
appendMethod · 0.45