Allow 'text' and 'markdown' aliases in export to dir

Fixes #201
This commit is contained in:
Manuel Ebert 2014-06-26 15:00:38 +02:00
parent 3eb911f11b
commit d8ccb928e0

View file

@ -13,13 +13,13 @@ def get_tags_count(journal):
# Astute reader: should the following line leave you as puzzled as me the first time # 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. # I came across this construction, worry not and embrace the ensuing moment of enlightment.
tags = [tag tags = [tag
for entry in journal.entries for entry in journal.entries
for tag in set(entry.tags) for tag in set(entry.tags)]
]
# To be read: [for entry in journal.entries: for tag in set(entry.tags): tag] # 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]) tag_counts = set([(tags.count(tag), tag) for tag in tags])
return tag_counts return tag_counts
def to_tag_list(journal): def to_tag_list(journal):
"""Prints a list of all tags and the number of occurrences.""" """Prints a list of all tags and the number of occurrences."""
tag_counts = get_tags_count(journal) tag_counts = get_tags_count(journal)
@ -32,6 +32,7 @@ def to_tag_list(journal):
result += "\n".join(u"{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True)) result += "\n".join(u"{0:20} : {1}".format(tag, n) for n, tag in sorted(tag_counts, reverse=True))
return result return result
def to_json(journal): def to_json(journal):
"""Returns a JSON representation of the Journal.""" """Returns a JSON representation of the Journal."""
tags = get_tags_count(journal) tags = get_tags_count(journal)
@ -41,6 +42,7 @@ def to_json(journal):
} }
return json.dumps(result, indent=2) 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"""
out = [] out = []
@ -58,10 +60,12 @@ def to_md(journal):
result = "\n".join(out) result = "\n".join(out)
return result return result
def to_txt(journal): def to_txt(journal):
"""Returns the complete text of the Journal.""" """Returns the complete text of the Journal."""
return journal.pprint() return journal.pprint()
def export(journal, format, output=None): def export(journal, format, output=None):
"""Exports the journal to various formats. """Exports the journal to various formats.
format should be one of json, txt, text, md, markdown. format should be one of json, txt, text, md, markdown.
@ -76,7 +80,9 @@ def export(journal, format, output=None):
"md": to_md, "md": to_md,
"markdown": to_md "markdown": to_md
} }
if output and os.path.isdir(output): # multiple files if format not in maps:
return u"[ERROR: can't export to {0}. Valid options are 'md', 'txt', and 'json']".format(format)
if output and os.path.isdir(output): # multiple files
return write_files(journal, output, format) return write_files(journal, output, format)
else: else:
content = maps[format](journal) content = maps[format](journal)
@ -84,12 +90,13 @@ def export(journal, format, output=None):
try: try:
with codecs.open(output, "w", "utf-8") as f: with codecs.open(output, "w", "utf-8") as f:
f.write(content) f.write(content)
return "[Journal exported to {0}]".format(output) return u"[Journal exported to {0}]".format(output)
except IOError as e: except IOError as e:
return "[ERROR: {0} {1}]".format(e.filename, e.strerror) return u"[ERROR: {0} {1}]".format(e.filename, e.strerror)
else: else:
return content return content
def write_files(journal, path, format): def write_files(journal, path, format):
"""Turns your journal into separate files for each entry. """Turns your journal into separate files for each entry.
Format should be either json, md or txt.""" Format should be either json, md or txt."""
@ -98,10 +105,10 @@ def write_files(journal, path, format):
full_path = os.path.join(path, make_filename(e)) full_path = os.path.join(path, make_filename(e))
if format == 'json': if format == 'json':
content = json.dumps(e.to_dict(), indent=2) + "\n" content = json.dumps(e.to_dict(), indent=2) + "\n"
elif format == 'md': elif format in ('md', 'markdown'):
content = e.to_md() content = e.to_md()
elif format == 'txt': elif format in ('txt', 'text'):
content = u(e) content = u(e)
with codecs.open(full_path, "w", "utf-8") as f: with codecs.open(full_path, "w", "utf-8") as f:
f.write(content) f.write(content)
return "[Journal exported individual files in {0}]".format(path) return u"[Journal exported individual files in {0}]".format(path)