Create datecount plugin

This commit is contained in:
karimpwnz 2021-01-04 02:37:54 +02:00
parent c155bafa84
commit 654cc6f56a
3 changed files with 43 additions and 0 deletions

View file

@ -3,11 +3,13 @@
# Copyright (C) 2012-2021 jrnl contributors # Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
from jrnl.plugins.datecount_exporter import DatecountExporter
from .fancy_exporter import FancyExporter from .fancy_exporter import FancyExporter
from .jrnl_importer import JRNLImporter from .jrnl_importer import JRNLImporter
from .json_exporter import JSONExporter from .json_exporter import JSONExporter
from .markdown_exporter import MarkdownExporter from .markdown_exporter import MarkdownExporter
from .tag_exporter import TagExporter from .tag_exporter import TagExporter
from .datecount_exporter import DatecountExporter
from .template_exporter import __all__ as template_exporters from .template_exporter import __all__ as template_exporters
from .text_exporter import TextExporter from .text_exporter import TextExporter
from .xml_exporter import XMLExporter from .xml_exporter import XMLExporter
@ -17,6 +19,7 @@ __exporters = [
JSONExporter, JSONExporter,
MarkdownExporter, MarkdownExporter,
TagExporter, TagExporter,
DatecountExporter,
TextExporter, TextExporter,
XMLExporter, XMLExporter,
YAMLExporter, YAMLExporter,

View file

@ -0,0 +1,27 @@
#!/usr/bin/env python
# encoding: utf-8
# Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html
from .text_exporter import TextExporter
from .util import get_date_counts
class DatecountExporter(TextExporter):
"""This Exporter can lists the tags for entries and journals, exported as a plain text file."""
names = ["datecount"]
extension = "datecount"
@classmethod
def export_entry(cls, entry):
raise NotImplementedError
@classmethod
def export_journal(cls, journal):
"""Returns dates and their frequencies for an entire journal."""
date_counts = get_date_counts(journal)
if not date_counts:
return "[No dates found in journal.]"
result = "\n".join(f"{date} {count}" for date, count in date_counts.items())
return result

View file

@ -3,6 +3,19 @@
# Copyright (C) 2012-2021 jrnl contributors # Copyright (C) 2012-2021 jrnl contributors
# License: https://www.gnu.org/licenses/gpl-3.0.html # License: https://www.gnu.org/licenses/gpl-3.0.html
from collections import Counter
def get_date_counts(journal):
"""Returns a collections.Counter object containing date counts"""
# Dates are normalized
date_counts = Counter()
for entry in journal.entries:
# entry.date.date() gets date without time
date = str(entry.date.date())
date_counts[date] += 1
return date_counts
def get_tags_count(journal): def get_tags_count(journal):
"""Returns a set of tuples (count, tag) for all tags present in the journal.""" """Returns a set of tuples (count, tag) for all tags present in the journal."""