From 654cc6f56a235d2ad25544ef549b6f925c830214 Mon Sep 17 00:00:00 2001 From: karimpwnz Date: Mon, 4 Jan 2021 02:37:54 +0200 Subject: [PATCH] Create datecount plugin --- jrnl/plugins/__init__.py | 3 +++ jrnl/plugins/datecount_exporter.py | 27 +++++++++++++++++++++++++++ jrnl/plugins/util.py | 13 +++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 jrnl/plugins/datecount_exporter.py diff --git a/jrnl/plugins/__init__.py b/jrnl/plugins/__init__.py index 0d2b39b4..089cc62e 100644 --- a/jrnl/plugins/__init__.py +++ b/jrnl/plugins/__init__.py @@ -3,11 +3,13 @@ # Copyright (C) 2012-2021 jrnl contributors # License: https://www.gnu.org/licenses/gpl-3.0.html +from jrnl.plugins.datecount_exporter import DatecountExporter from .fancy_exporter import FancyExporter from .jrnl_importer import JRNLImporter from .json_exporter import JSONExporter from .markdown_exporter import MarkdownExporter from .tag_exporter import TagExporter +from .datecount_exporter import DatecountExporter from .template_exporter import __all__ as template_exporters from .text_exporter import TextExporter from .xml_exporter import XMLExporter @@ -17,6 +19,7 @@ __exporters = [ JSONExporter, MarkdownExporter, TagExporter, + DatecountExporter, TextExporter, XMLExporter, YAMLExporter, diff --git a/jrnl/plugins/datecount_exporter.py b/jrnl/plugins/datecount_exporter.py new file mode 100644 index 00000000..c21df260 --- /dev/null +++ b/jrnl/plugins/datecount_exporter.py @@ -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 diff --git a/jrnl/plugins/util.py b/jrnl/plugins/util.py index 04159ca4..c703d621 100644 --- a/jrnl/plugins/util.py +++ b/jrnl/plugins/util.py @@ -3,6 +3,19 @@ # Copyright (C) 2012-2021 jrnl contributors # 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): """Returns a set of tuples (count, tag) for all tags present in the journal."""