-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1667 from elementary-data/ele-3527-add-new-groups…
…-to-cli Added new groups
- Loading branch information
Showing
4 changed files
with
77 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Dict, Generic, List, Optional, TypeVar, Union | ||
|
||
T = TypeVar("T") | ||
TreeT = Dict[str, Union[List[T], "TreeT"]] | ||
|
||
|
||
class TreeBuilder(Generic[T]): | ||
def __init__(self, separator: str, files_keyword: str = "__files__") -> None: | ||
self.separator = separator | ||
self.files_keyword = files_keyword | ||
self._tree: TreeT = {} | ||
|
||
def add(self, path: Optional[str], data: Optional[T]) -> None: | ||
if path is None or data is None: | ||
return | ||
parts = path.split(self.separator) | ||
current: dict = self._tree | ||
for part in parts[:-1]: | ||
if isinstance(current, list): | ||
raise ValueError( | ||
f"Path parts cannot contain files keyword: {self.files_keyword}" | ||
) | ||
current = current.setdefault(part, {}) | ||
if self.files_keyword in current: | ||
if id not in current[self.files_keyword]: | ||
current[self.files_keyword].append(data) | ||
else: | ||
current[self.files_keyword] = [data] | ||
|
||
def get_tree(self) -> TreeT: | ||
return self._tree |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters