From a6b828e892539fc27271ecf94761bbb9f2a75192 Mon Sep 17 00:00:00 2001 From: Karim Rahal Date: Sun, 17 Jan 2021 00:50:47 +0200 Subject: [PATCH] Add new date format (`--format date`) for heatmapping (#1146) * Create datecount plugin * Fix plugin import * Update datecount format * Create datecount test * Remove outdated comment * Remove unnecessary datecount export condition * Update test config * Move get_date_counts into DateCountExporter; misc changes * Use --format instead of --export in datecount test * Update datecount test to include configs * Better datecount test * Remove old tests * Change 'datecounts' to 'dates' --- features/format.feature | 19 +++++++++++++++++++ jrnl/plugins/__init__.py | 2 ++ jrnl/plugins/dates_exporter.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 jrnl/plugins/dates_exporter.py diff --git a/features/format.feature b/features/format.feature index 610e3a1e..4981f685 100644 --- a/features/format.feature +++ b/features/format.feature @@ -558,3 +558,22 @@ Feature: Custom formats | basic_encrypted | | basic_folder | | basic_dayone | + + Scenario Outline: Export date counts + Given we use the config ".yaml" + And we use the password "test" if prompted + When we run "jrnl 2020-08-31 01:01: Hi." + And we run "jrnl --format dates" + Then the output should be + """ + 2020-08-29, 1 + 2020-08-31, 2 + 2020-09-24, 1 + """ + + Examples: configs + | config | + | basic_onefile | + | basic_encrypted | + | basic_folder | + | basic_dayone | diff --git a/jrnl/plugins/__init__.py b/jrnl/plugins/__init__.py index 0d2b39b4..ad174f0b 100644 --- a/jrnl/plugins/__init__.py +++ b/jrnl/plugins/__init__.py @@ -8,6 +8,7 @@ from .jrnl_importer import JRNLImporter from .json_exporter import JSONExporter from .markdown_exporter import MarkdownExporter from .tag_exporter import TagExporter +from .dates_exporter import DatesExporter from .template_exporter import __all__ as template_exporters from .text_exporter import TextExporter from .xml_exporter import XMLExporter @@ -17,6 +18,7 @@ __exporters = [ JSONExporter, MarkdownExporter, TagExporter, + DatesExporter, TextExporter, XMLExporter, YAMLExporter, diff --git a/jrnl/plugins/dates_exporter.py b/jrnl/plugins/dates_exporter.py new file mode 100644 index 00000000..d11e527c --- /dev/null +++ b/jrnl/plugins/dates_exporter.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python +# encoding: utf-8 +# Copyright (C) 2012-2021 jrnl contributors +# License: https://www.gnu.org/licenses/gpl-3.0.html +from collections import Counter + +from .text_exporter import TextExporter + + +class DatesExporter(TextExporter): + """This Exporter lists dates and their respective counts, for heatingmapping etc.""" + + names = ["dates"] + extension = "dates" + + @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 = Counter() + for entry in journal.entries: + # entry.date.date() gets date without time + date = str(entry.date.date()) + date_counts[date] += 1 + result = "\n".join(f"{date}, {count}" for date, count in date_counts.items()) + return result