feat: add support for alternatives

This commit is contained in:
relikd
2022-07-23 13:58:46 +02:00
parent eb0a60ab33
commit fb8321744e
2 changed files with 14 additions and 9 deletions

View File

@@ -84,7 +84,7 @@ class GroupBySource(VirtualSourceObject):
@property @property
def url_path(self) -> str: def url_path(self) -> str:
# Actual path to resource as seen by the browser # Actual path to resource as seen by the browser
return build_url([self.record.path, self.slug]) # slug can be None! return build_url([self.record.url_path, self.slug]) # slug can be None
def iter_source_filenames(self) -> Iterator[str]: def iter_source_filenames(self) -> Iterator[str]:
''' Enumerate all dependencies ''' ''' Enumerate all dependencies '''

View File

@@ -48,8 +48,11 @@ class Watcher:
''' Reset internal state. You must initialize before each build! ''' ''' Reset internal state. You must initialize before each build! '''
assert callable(self.callback), 'No grouping callback provided.' assert callable(self.callback), 'No grouping callback provided.'
self._model_reader = ModelReader(pad.db, self.config.key, self.flatten) self._model_reader = ModelReader(pad.db, self.config.key, self.flatten)
self._root_record = pad.get(self._root) # type: Record self._root_record = {} # type: Dict[str, Record]
self._state = {} # type: Dict[str, GroupBySource] self._state = {} # type: Dict[str, Dict[str, GroupBySource]]
for alt in pad.config.iter_alternatives():
self._root_record[alt] = pad.get(self._root, alt=alt)
self._state[alt] = {}
def should_process(self, node: 'Record') -> bool: def should_process(self, node: 'Record') -> bool:
''' Check if record path is being watched. ''' ''' Check if record path is being watched. '''
@@ -85,12 +88,13 @@ class Watcher:
else: else:
group, extra = obj group, extra = obj
alt = record.alt
slug = self.config.slugify(group) slug = self.config.slugify(group)
if slug not in self._state: if slug not in self._state[alt]:
src = GroupBySource(self._root_record, slug) src = GroupBySource(self._root_record[alt], slug)
self._state[slug] = src self._state[alt][slug] = src
else: else:
src = self._state[slug] src = self._state[alt][slug]
src.append_child(record, extra, group) src.append_child(record, extra, group)
# reverse reference # reverse reference
@@ -99,7 +103,8 @@ class Watcher:
def iter_sources(self, root: 'Record') -> Iterator[GroupBySource]: def iter_sources(self, root: 'Record') -> Iterator[GroupBySource]:
''' Prepare and yield GroupBySource elements. ''' ''' Prepare and yield GroupBySource elements. '''
for vobj in self._state.values(): for vobj_list in self._state.values():
for vobj in vobj_list.values():
yield vobj.finalize(self.config) yield vobj.finalize(self.config)
# cleanup. remove this code if you'd like to iter twice # cleanup. remove this code if you'd like to iter twice
del self._model_reader del self._model_reader