From 2dd53dd28f8bf5a936289f63036a5039cc5d7fa2 Mon Sep 17 00:00:00 2001 From: Timur Demin Date: Tue, 9 Apr 2019 11:12:51 +0500 Subject: [PATCH] Make the note model more portable Signed-off-by: Timur Demin --- scarlet_export/__init__.py | 9 +++++++-- scarlet_export/notable.py | 31 ++++++++++++++++--------------- scarlet_export/note.py | 17 +++++++++++++++++ 3 files changed, 40 insertions(+), 17 deletions(-) create mode 100644 scarlet_export/note.py diff --git a/scarlet_export/__init__.py b/scarlet_export/__init__.py index 246ef87..d22d37f 100644 --- a/scarlet_export/__init__.py +++ b/scarlet_export/__init__.py @@ -2,6 +2,7 @@ from pathlib import Path import json +from note import Note from notable import exportNotes as exportToNotable if __name__ == '__main__': @@ -48,11 +49,15 @@ except json.JSONDecodeError: print('Could not parse the input file.') exit(1) - inputFile.close() + # do parsing + notes = [] + for note in data['notes']: + parsedNote = Note(note) + notes.append(parsedNote) # currently the script only supports a single program to export the # data to if args['program'] == 'notable': - exportToNotable(data, args['outputDirName']) + exportToNotable(notes, args['outputDirName']) else: print('The program {0} is currently not supported.'.format( args['program'] diff --git a/scarlet_export/notable.py b/scarlet_export/notable.py index 88098d3..56af0ab 100644 --- a/scarlet_export/notable.py +++ b/scarlet_export/notable.py @@ -1,36 +1,37 @@ from datetime import datetime from json import loads -def exportNotes(inputJson, outputDir): +def exportNotes(notesList, outputDir): """ Exports the notes from Scarlet Notes to Notable. - @param inputJson: an object generated with `json.load()` + @param notesList: a list of `Note` @param outputDir: output directory name """ - for note in inputJson['notes']: + for note in notesList: # color is not available in Notable, so it's not preserved # the metadata Notable uses is just YAML front matter - if note['folder'] != '': - outputFolder = outputDir + '/' + note['folder'] + if note.folder != '': + outputFolder = outputDir + '/' + note.folder else: outputFolder = outputDir - outputFileName = outputFolder + '/' + note['uuid'] + '.md' - tags = '[{0}]'.format(note['tags']) - timestamp = int(note['timestamp']) - updateTimestamp = int(note['updateTimestamp']) - created = datetime.utcfromtimestamp(timestamp) - modified = datetime.utcfromtimestamp(updateTimestamp) + outputFileName = outputFolder + '/' + note.uuid + '.md' + # make up a string containing all of the tags + tags = '' + for tag in note.tags: + tags += tag + ',' + tags.rstrip(',') + created = datetime.utcfromtimestamp(note.timestamp) + modified = datetime.utcfromtimestamp(note.updateTimestamp) with open(outputFileName) as output: - # print YAML front matter first, then dump the note content + # print YAML front matter first output.write('---') output.write('created: {0}'.format(created)) output.write('modified: {0}'.format(modified)) - output.write('tags: {0}'.format(tags)) + output.write('tags: [{0}]'.format(tags)) output.write('---') output.write('') # an empty string # dump the note text as well, contained in another # JSON-formatted string - noteText = loads(note['description'])['note']['text'] - output.write(noteText) + output.write(note.content) return diff --git a/scarlet_export/note.py b/scarlet_export/note.py new file mode 100644 index 0000000..d189d2b --- /dev/null +++ b/scarlet_export/note.py @@ -0,0 +1,17 @@ +from json import loads + +class Note: + """ + A Scarlet Notes note model. Has six properties: `uuid`, `folder`, + `updateTimestamp`, `timestamp`, `tags`, and `content`. + + The timestamps are both ints in Unix time. UUID and folder are both + strings. `content` is a string containing all of the note content. + """ + def __init__(self, note): + self.uuid = note['uuid'] + self.folder = note['folder'] + self.updateTimestamp = int(note['updateTimestamp']) + self.timestamp = int(note['timestamp']) + self.tags = note['tags'].split(',') + self.content = loads(note['description'])['note']['text']