Skip to content

Commit

Permalink
update:
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed Sep 3, 2024
1 parent 1faa810 commit ffc69f1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
4 changes: 4 additions & 0 deletions src/sphinxnotes/any/indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def derive(
typ = f'Any{schema.objtype.title()}Index'
name = schema.objtype
localname = f'{schema.objtype.title()} Reference Index'
if classifier.by is not None:
typ += f'By{classifier.by.title()}'
name += '-by-' + classifier.by
localname += f'By {classifier.by.title()}'
return type(
typ,
(cls,),
Expand Down
28 changes: 19 additions & 9 deletions src/sphinxnotes/any/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __hash__(self):


class Classifier(object):
name = ''
by: str | None = None

@abstractmethod
def classify(self, objref: Value) -> list[Classif]:
Expand All @@ -176,8 +176,6 @@ def sort(self, data: Iterable[_T], key: Callable[[_T], Classif]) -> list[_T]:


class PlainClassifier(Classifier):
name = ''

def classify(self, objref: Value) -> list[Classif]:
entries = []
for v in objref.as_list():
Expand All @@ -199,11 +197,17 @@ def sort(
DISPFMTS_YM = '%Y 年 %m 月'
DISPFMTS_MD = '%m 月 %d 日,%a'


class YearClassifier(Classifier):
name = 'by-year'
by = 'year'

def __init__(self, inputfmts: list[str] = INPUTFMTS, dispfmt_y: str = DISPFMTS_Y,
dispfmt_m: str = DISPFMTS_M, dispfmt_md: str = DISPFMTS_MD):
def __init__(
self,
inputfmts: list[str] = INPUTFMTS,
dispfmt_y: str = DISPFMTS_Y,
dispfmt_m: str = DISPFMTS_M,
dispfmt_md: str = DISPFMTS_MD,
):
"""*xxxfmt* are date format used by time.strptime/strftime.
.. seealso:: https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes"""
Expand Down Expand Up @@ -235,11 +239,16 @@ def sort(
# TODO: Sort by time.
return sorted(data, key=lambda x: key(x)._sort_key, reverse=True)


class MonthClassifier(Classifier):
name = 'by-month'
by = 'month'

def __init__(self, inputfmts: list[str] = INPUTFMTS, dispfmt_ym: str = DISPFMTS_YM,
dispfmt_md: str = DISPFMTS_MD):
def __init__(
self,
inputfmts: list[str] = INPUTFMTS,
dispfmt_ym: str = DISPFMTS_YM,
dispfmt_md: str = DISPFMTS_MD,
):
self.inputfmts = inputfmts
self.dispfmt_ym = dispfmt_ym
self.dispfmt_md = dispfmt_md
Expand All @@ -266,6 +275,7 @@ def sort(
# TODO: Sort by time.
return sorted(data, key=lambda x: key(x)._sort_key, reverse=True)


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

0 comments on commit ffc69f1

Please sign in to comment.