[Dayone] Add Creator section to dayone import and YAML export

This commit is contained in:
MinchinWeb 2015-05-04 00:21:33 -06:00
parent 91368c7b63
commit 0fcf078c1c
3 changed files with 74 additions and 11 deletions

View file

@ -4,6 +4,8 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from . import Entry from . import Entry
from . import Journal from . import Journal
from . import __title__ # 'jrnl'
from . import __version__
import os import os
import re import re
from datetime import datetime from datetime import datetime
@ -14,6 +16,8 @@ import pytz
import uuid import uuid
import tzlocal import tzlocal
from xml.parsers.expat import ExpatError from xml.parsers.expat import ExpatError
import socket
import platform
class DayOne(Journal.Journal): class DayOne(Journal.Journal):
@ -53,6 +57,27 @@ class DayOne(Journal.Journal):
entry = Entry.Entry(self, date, title, body, starred=dict_entry["Starred"]) entry = Entry.Entry(self, date, title, body, starred=dict_entry["Starred"])
entry.uuid = dict_entry["UUID"] entry.uuid = dict_entry["UUID"]
entry.tags = [self.config['tagsymbols'][0] + tag for tag in dict_entry.get("Tags", [])] entry.tags = [self.config['tagsymbols'][0] + tag for tag in dict_entry.get("Tags", [])]
"""Extended DayOne attributes"""
try:
entry.creator_device_agent = dict_entry['Creator']['Device Agent']
except:
pass
try:
entry.creator_generation_date = dict_entry['Creator']['Generation Date']
except:
pass
try:
entry.creator_host_name = dict_entry['Creator']['Host Name']
except:
pass
try:
entry.creator_os_agent = dict_entry['Creator']['OS Agent']
except:
pass
try:
entry.creator_software_agent = dict_entry['Creator']['Software Agent']
except:
pass
self.entries.append(entry) self.entries.append(entry)
self.sort() self.sort()
return self return self
@ -61,17 +86,34 @@ class DayOne(Journal.Journal):
"""Writes only the entries that have been modified into plist files.""" """Writes only the entries that have been modified into plist files."""
for entry in self.entries: for entry in self.entries:
if entry.modified: if entry.modified:
utc_time = datetime.utcfromtimestamp(time.mktime(entry.date.timetuple()))
filename = os.path.join(self.config['journal'], "entries", entry.uuid.upper() + ".doentry")
if not hasattr(entry, "uuid"): if not hasattr(entry, "uuid"):
entry.uuid = uuid.uuid1().hex entry.uuid = uuid.uuid1().hex
utc_time = datetime.utcfromtimestamp(time.mktime(entry.date.timetuple())) if not hasattr(entry, "creator_device_agent"):
filename = os.path.join(self.config['journal'], "entries", entry.uuid + ".doentry") entry.creator_device_agent = '' # iPhone/iPhone5,3
if not hasattr(entry, "creator_generation_date"):
entry.creator_generation_date = utc_time
if not hasattr(entry, "creator_host_name"):
entry.creator_host_name = socket.gethostname()
if not hasattr(entry, "creator_os_agent"):
entry.creator_os_agent = '{} {}'.format(platform.system(), platform.release())
if not hasattr(entry, "creator_software_agent"):
entry.creator_software_agent = '{} {}'.format(__title__, __version__)
entry_plist = { entry_plist = {
'Creation Date': utc_time, 'Creation Date': utc_time,
'Starred': entry.starred if hasattr(entry, 'starred') else False, 'Starred': entry.starred if hasattr(entry, 'starred') else False,
'Entry Text': entry.title + "\n" + entry.body, 'Entry Text': entry.title + "\n" + entry.body,
'Time Zone': str(tzlocal.get_localzone()), 'Time Zone': str(tzlocal.get_localzone()),
'UUID': entry.uuid, 'UUID': entry.uuid.upper(),
'Tags': [tag.strip(self.config['tagsymbols']).replace("_", " ") for tag in entry.tags] 'Tags': [tag.strip(self.config['tagsymbols']).replace("_", " ") for tag in entry.tags],
'Creator': {'Device Agent': entry.creator_device_agent,
'Generation Date': entry.creator_generation_date,
'Host Name': entry.creator_host_name,
'OS Agent': entry.creator_os_agent,
'Sofware Agent': entry.creator_software_agent}
} }
plistlib.writePlist(entry_plist, filename) plistlib.writePlist(entry_plist, filename)
for entry in self._deleted_entries: for entry in self._deleted_entries:

View file

@ -11,4 +11,4 @@ __title__ = 'jrnl'
__version__ = '2.0.0-rc1' __version__ = '2.0.0-rc1'
__author__ = 'Manuel Ebert' __author__ = 'Manuel Ebert'
__license__ = 'MIT License' __license__ = 'MIT License'
__copyright__ = 'Copyright 2013 - 2014 Manuel Ebert' __copyright__ = 'Copyright 2013 - 2015 Manuel Ebert'

View file

@ -58,12 +58,33 @@ class YAMLExporter(TextExporter):
if warn_on_heading_level is True: if warn_on_heading_level is True:
print("{}WARNING{}: Headings increased past H6 on export - {} {}".format("\033[33m", "\033[0m", date_str, entry.title), file=sys.stderr) print("{}WARNING{}: Headings increased past H6 on export - {} {}".format("\033[33m", "\033[0m", date_str, entry.title), file=sys.stderr)
return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{body} {space}".format( dayone_attributes = ''
date=date_str, if hasattr(entry, "uuid"):
title=entry.title, dayone_attributes += 'uuid: ' + entry.uuid + '\n'
stared=entry.starred, if hasattr(entry, 'creator_device_agent') or \
tags=', '.join([tag[1:] for tag in entry.tags]), hasattr(entry, 'creator_generation_date') or \
body=newbody, hasattr(entry, 'creator_host_name') or \
hasattr(entry, 'creator_os_agent') or \
hasattr(entry, 'creator_software_agent'):
dayone_attributes += 'creator:\n'
if hasattr(entry, 'creator_device_agent'):
dayone_attributes += ' device agent: ' + entry.creator_device_agent + '\n'
if hasattr(entry, 'creator_generation_date'):
dayone_attributes += ' generation date: ' + str(entry.creator_generation_date) + '\n'
if hasattr(entry, 'creator_host_name'):
dayone_attributes += ' host name: ' + entry.creator_host_name + '\n'
if hasattr(entry, 'creator_os_agent'):
dayone_attributes += ' os agent: ' + entry.creator_os_agent + '\n'
if hasattr(entry, 'creator_software_agent'):
dayone_attributes += ' software agent: ' + entry.creator_software_agent + '\n'
return "title: {title}\ndate: {date}\nstared: {stared}\ntags: {tags}\n{dayone} {body} {space}".format(
date = date_str,
title = entry.title,
stared = entry.starred,
tags = ', '.join([tag[1:] for tag in entry.tags]),
dayone = dayone_attributes,
body = newbody,
space="" space=""
) )