Skip to content

Commit

Permalink
feat: Impl PathIndexer
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Sep 8, 2024
1 parent 7d18d2c commit 6262eac
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/sphinxnotes/any/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ def classify(self, objref: Value) -> list[Category]:

_T = TypeVar('_T')

@abstractmethod
def sort(self, data: Iterable[_T], key: Callable[[_T], Category]) -> list[_T]:
raise NotImplementedError
return sorted(data, key=lambda x: key(x)._sort_key)

@abstractmethod
def anchor(self, refval: str) -> str:
Expand All @@ -178,11 +177,6 @@ def classify(self, objref: Value) -> list[Category]:
entries.append(Category(main=v))
return entries

def sort(
self, data: Iterable[Indexer._T], key: Callable[[Indexer._T], Category]
) -> list[Indexer._T]:
return sorted(data, key=lambda x: key(x)._sort_key)

def anchor(self, refval: str) -> str:
return refval

Expand Down Expand Up @@ -303,6 +297,27 @@ def anchor(self, refval: str) -> str:
return ''


class PathIndexer(Indexer):
name = 'path'

def __init__(self, sep: str, maxsplit: Literal[1, 2]):
self.sep = sep
self.maxsplit = maxsplit

def classify(self, objref: Value) -> list[Category]:
entries = []
for v in objref.as_list():
comps = v.split(self.sep, maxsplit=self.maxsplit)
category = Category(main=comps[0], extra=v)
if self.maxsplit == 2:
category.sub = v[1] if len(comps) > 1 else None
entries.append(category)
return entries

def anchor(self, refval: str) -> str:
return refval.split(self.sep, maxsplit=self.maxsplit)[0]


@dataclasses.dataclass(frozen=True)
class Object(object):
objtype: str
Expand Down

0 comments on commit 6262eac

Please sign in to comment.