Moves tag export to exporters

This commit is contained in:
Manuel Ebert 2013-06-09 15:07:27 -07:00
parent 90863ecee1
commit 73b09085ee
2 changed files with 25 additions and 33 deletions

View file

@ -3,7 +3,30 @@
try: import simplejson as json
except ImportError: import json
from jrnl import get_tags_count
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 to_tag_list(journal):
"""Prints a list of all tags and the number of occurances."""
tag_counts = get_tags_count(journal)
result = ""
if not tag_counts:
return '[No tags found in journal.]'
elif min(tag_counts)[0] == 0:
tag_counts = filter(lambda x: x[0] > 1, tag_counts)
result += '[Removed tags that appear only once.]\n'
result += "\n".join("{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=False))
return result
def to_json(journal):
"""Returns a JSON representation of the Journal."""

View file

@ -25,12 +25,6 @@ import sys
try: import simplejson as json
except ImportError: import json
__title__ = 'jrnl'
__version__ = '1.0.0-rc1'
__author__ = 'Manuel Ebert, Stephan Gabler'
__license__ = 'MIT'
xdg_config = os.environ.get('XDG_CONFIG_HOME')
CONFIG_PATH = os.path.join(xdg_config, "jrnl") if xdg_config else os.path.expanduser('~/.jrnl_config')
PYCRYPTO = install.module_exists("Crypto")
@ -104,31 +98,6 @@ def decrypt(journal, filename=None):
journal.write(filename)
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):
"""Prints a list of all tags and the number of occurances."""
tag_counts = get_tags_count(journal)
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):
"""If filename does not exist, touch the file"""
if not os.path.exists(filename):
@ -218,7 +187,7 @@ def cli():
# Various export modes
elif args.tags:
print_tags(journal)
print(exporters.to_tag_list(journal))
elif args.json: # export to json
print(exporters.to_json(journal))