class CategoryProcessor(SummaryProcessor): """Subclass of SummaryProcessor which counts categories under a root""" def __init__(self, root=''): super(CategoryProcessor, self).__init__(root, '', cfg('YAWT_CATEGORY_COUNT_FILE')) def _init_summary(self): self.summary = HierarchyCount() def on_visit_article(self, article): category = article.info.category if category == self.root or category.startswith(self.root): category = self._slice_base(category, self.root) self.summary.add(category) def unvisit(self, name): category = os.path.dirname(name) if category.startswith(self.root): category = self._slice_base(category, self.root) self.summary.remove(category) @staticmethod def _slice_base(category, countbase): category = re.sub('^'+countbase, '', category) if category.startswith('/'): category = category[1:] return category
def test_fetches_named_child(self): hc = HierarchyCount() hc.add("cooking/indian") hc.add("cooking/italian") hc.add("reading/history") hc.add("") self.assertEquals("cooking", hc.child("cooking").category) self.assertEquals(None, hc.child("astronomy"))
def test_sort_children(self): hc = HierarchyCount() hc.add("cooking/indian") hc.add("cooking/italian") hc.add("reading/history") hc.add("") hc.sort(reverse=True) self.assertEquals("reading", hc.children[0].category) self.assertEquals("cooking", hc.children[1].category)
class ArchiveProcessor(SummaryProcessor): """Subclass of SummaryProcessor which counts archives under a root""" def __init__(self, root=''): super(ArchiveProcessor, self).__init__(root, '', cfg('YAWT_ARCHIVE_COUNT_FILE')) def _init_summary(self): self.summary = HierarchyCount() def on_visit_article(self, article): datefield = current_app.config['YAWT_ARCHIVE_DATEFIELD'] date = getattr(article.info, datefield) datestring = _date_hierarchy(date) self.summary.add(datestring) def unvisit(self, name): create_time = _fetch_date_for_name(name) if create_time: datestring = _date_hierarchy(create_time) self.summary.remove(datestring) def on_post_walk(self): self.summary.sort(reverse=True) super(ArchiveProcessor, self).on_post_walk()
def test_removing_adjusts_counting_tree(self): hc = HierarchyCount() hc.add("cooking/indian") hc.add("cooking/italian") hc.add("reading/history") hc.add("") hc.remove("cooking/indian") expected = HierarchyCount( category="", count=3, children=[ HierarchyCount(category="cooking", count=1, children=[HierarchyCount(category="italian", count=1)]), HierarchyCount(category="reading", count=1, children=[HierarchyCount(category="history", count=1)]), ], ) self.assertEquals(expected, hc)