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'
This commit is contained in:
Karim Rahal 2021-01-17 00:50:47 +02:00 committed by GitHub
parent 5c0a2d4c4e
commit a6b828e892
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 0 deletions

View file

@ -558,3 +558,22 @@ Feature: Custom formats
| basic_encrypted |
| basic_folder |
| basic_dayone |
Scenario Outline: Export date counts
Given we use the config "<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 |

View file

@ -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,

View file

@ -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