JSON exports tags

Closes #78. This changes the export format to

{
tags: {tag: count},
entries: [{…}, {…}, …]
}
This commit is contained in:
Manuel Ebert 2013-06-09 14:48:56 -07:00
parent 992beb393e
commit ac7a882142
4 changed files with 34 additions and 19 deletions

View file

@ -1,9 +1,12 @@
Changelog Changelog
========= =========
#### 1.1.0
* [New] JSON export exports tags as well.
#### 1.0.5 #### 1.0.5
* Backwards compatibility with `parsedatetime` 0.8.7 * [Improved] Backwards compatibility with `parsedatetime` 0.8.7
#### 1.0.4 #### 1.0.4

View file

@ -7,7 +7,7 @@ jrnl is a simple journal application for your command line.
""" """
__title__ = 'jrnl' __title__ = 'jrnl'
__version__ = '1.0.5' __version__ = '1.1.0'
__author__ = 'Manuel Ebert' __author__ = 'Manuel Ebert'
__license__ = 'MIT License' __license__ = 'MIT License'
__copyright__ = 'Copyright 2013 Manuel Ebert' __copyright__ = 'Copyright 2013 Manuel Ebert'

View file

@ -3,10 +3,16 @@
try: import simplejson as json try: import simplejson as json
except ImportError: import json except ImportError: import json
from jrnl import get_tags_count
def to_json(journal): def to_json(journal):
"""Returns a JSON representation of the Journal.""" """Returns a JSON representation of the Journal."""
return json.dumps([e.to_dict() for e in journal.entries], indent=2) tags = get_tags_count(journal)
result = {
"tags": dict((tag, count) for count, tag in tags),
"entries": [e.to_dict() for e in journal.entries]
}
return json.dumps(result, indent=2)
def to_md(journal): def to_md(journal):
"""Returns a markdown representation of the Journal""" """Returns a markdown representation of the Journal"""

View file

@ -104,23 +104,29 @@ def decrypt(journal, filename=None):
journal.write(filename) journal.write(filename)
print("Journal decrypted to {0}.".format(filename or journal.config['journal'])) print("Journal decrypted to {0}.".format(filename or journal.config['journal']))
def get_tags_count(journal):
"""Returns a set of tuples (count, tag) for all tags present in the journal."""
# Astute reader: should the following line leave you as puzzled as me the first time
# I came across this construction, worry not and embrace the ensuing moment of enlightment.
tags = [tag
for entry in journal.entries
for tag in set(entry.tags)
]
# To be read: [for entry in journal.entries: for tag in set(entry.tags): tag]
tag_counts = set([(tags.count(tag), tag) for tag in tags])
return tag_counts
def print_tags(journal): def print_tags(journal):
"""Prints a list of all tags and the number of occurances.""" """Prints a list of all tags and the number of occurances."""
# Astute reader: should the following line leave you as puzzled as me the first time tag_counts = get_tags_count(journal)
# I came across this construction, worry not and embrace the ensuing moment of enlightment. if not tag_counts:
tags = [tag print('[No tags found in journal.]')
for entry in journal.entries elif min(tag_counts)[0] == 0:
for tag in set(entry.tags) tag_counts = filter(lambda x: x[0] > 1, tag_counts)
] print('[Removed tags that appear only once.]')
# To be read: [for entry in journal.entries: for tag in set(entry.tags): tag] for n, tag in sorted(tag_counts, reverse=False):
tag_counts = set([(tags.count(tag), tag) for tag in tags]) print("{0:20} : {1}".format(tag, n))
if not tag_counts:
print('[No tags found in journal.]')
elif min(tag_counts)[0] == 0:
tag_counts = filter(lambda x: x[0] > 1, tag_counts)
print('[Removed tags that appear only once.]')
for n, tag in sorted(tag_counts, reverse=False):
print("{0:20} : {1}".format(tag, n))
def touch_journal(filename): def touch_journal(filename):