Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/model.py
Show First 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | class Tree: | ||||
@property | @property | ||||
def attributes(self): | def attributes(self): | ||||
""" | """ | ||||
Get the attributes of the current node grouped by the relative path. | Get the attributes of the current node grouped by the relative path. | ||||
Returns: | Returns: | ||||
a dictionary containing a path as key and its known/unknown status and the | a dictionary containing a path as key and its known/unknown status and the | ||||
Software Heritage persistent identifier as values. | SWHID as values. | ||||
""" | """ | ||||
return {str(self.path): {"swhid": self.swhid, "known": self.known,}} | return {str(self.path): {"swhid": self.swhid, "known": self.known,}} | ||||
def toDict(self, dict_nodes={}) -> Dict[str, Dict[str, Dict]]: | def toDict(self, dict_nodes={}) -> Dict[str, Dict[str, Dict]]: | ||||
""" | """ | ||||
Recursively groups the current child nodes inside a dictionary. | Recursively groups the current child nodes inside a dictionary. | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | class Tree: | ||||
def __iterNodesAttr(self) -> Iterable[Dict[str, Dict]]: | def __iterNodesAttr(self) -> Iterable[Dict[str, Dict]]: | ||||
""" | """ | ||||
Recursively iterate through the children of the current node returning | Recursively iterate through the children of the current node returning | ||||
an iterable of the children nodes attributes | an iterable of the children nodes attributes | ||||
Yields: | Yields: | ||||
a dictionary containing a path with its known/unknown status and the | a dictionary containing a path with its known/unknown status and the | ||||
Software Heritage persistent identifier | SWHID | ||||
""" | """ | ||||
for child_node in self.iterate(): | for child_node in self.iterate(): | ||||
yield child_node.attributes | yield child_node.attributes | ||||
if child_node.otype == DIRECTORY: | if child_node.otype == DIRECTORY: | ||||
yield from child_node.__iterNodesAttr() | yield from child_node.__iterNodesAttr() | ||||
def getFilesFromDir(self, dir_path: PosixPath) -> List: | def getFilesFromDir(self, dir_path: PosixPath) -> List: | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | def getDirectoriesInfo(self, root: PosixPath) -> Dict[PosixPath, Tuple[int, int]]: | ||||
""" | """ | ||||
directories = {root: self.count_contents()} | directories = {root: self.count_contents()} | ||||
self.__getSubDirsInfo(root, directories) | self.__getSubDirsInfo(root, directories) | ||||
return directories | return directories | ||||
def count_contents(self) -> Tuple[int, int]: | def count_contents(self) -> Tuple[int, int]: | ||||
"""Count how many contents are present inside a directory. | """Count how many contents are present inside a directory. | ||||
If a directory has a pid returns as it has all the contents. | If a directory has a SWHID returns as it has all the contents. | ||||
Returns: | Returns: | ||||
A tuple with the total number of the contents and the number | A tuple with the total number of the contents and the number | ||||
of contents known (the ones that have a persistent identifier). | of contents known (the ones that have a persistent identifier). | ||||
""" | """ | ||||
contents = 0 | contents = 0 | ||||
discovered = 0 | discovered = 0 | ||||
Show All 25 Lines |