Skip to content

Commit

Permalink
Some cleanup of path splitting and null checks in BoundaryTrie methods
Browse files Browse the repository at this point in the history
  • Loading branch information
emdoyle committed Feb 9, 2024
1 parent 6645302 commit e786bb7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions modguard/core/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ class BoundaryTrie:
def __iter__(self):
return boundary_trie_iterator(self)

@staticmethod
def _split_mod_path(path: str) -> list[str]:
# By default "".split(".") -> ['']
# so we want to remove any whitespace path components
return [part for part in path.split(".") if part]

def get(self, path: str) -> Optional[BoundaryNode]:
node = self.root
parts = path.split(".")
parts = [part for part in parts if part]
parts = self._split_mod_path(path)

for part in parts:
if part not in node.children:
Expand All @@ -38,8 +43,7 @@ def get(self, path: str) -> Optional[BoundaryNode]:

def insert(self, path: str, public_members: Optional[list[PublicMember]] = None):
node = self.root
parts = path.split(".")
parts = [part for part in parts if part]
parts = self._split_mod_path(path)

for part in parts:
if part not in node.children:
Expand Down Expand Up @@ -67,9 +71,8 @@ def register_public_member(self, path: str, member: PublicMember):

def find_nearest(self, path: str) -> Optional[BoundaryNode]:
node = self.root
parts = path.split(".")
parts = [part for part in parts if part]
nearest_parent = node if node.is_end_of_path else None
parts = self._split_mod_path(path)
nearest_parent = node

for part in parts:
if part in node.children:
Expand All @@ -79,7 +82,7 @@ def find_nearest(self, path: str) -> Optional[BoundaryNode]:
else:
break

return nearest_parent
return nearest_parent if nearest_parent.is_end_of_path else None


def boundary_trie_iterator(trie: BoundaryTrie) -> Generator[BoundaryNode, None, None]:
Expand Down

0 comments on commit e786bb7

Please sign in to comment.