diff --git a/.gitignore b/.gitignore index 9992834..f8ca461 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.egg-info **/__pycache__ +build diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..22212b7 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,81 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Python: Current File (Integrated Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "integratedTerminal" + }, + { + "name": "Python: Remote Attach", + "type": "python", + "request": "attach", + "port": 5678, + "host": "localhost", + "pathMappings": [ + { + "localRoot": "${workspaceFolder}", + "remoteRoot": "." + } + ] + }, + { + "name": "Python: Module", + "type": "python", + "request": "launch", + "module": "enter-your-module-name-here", + "console": "integratedTerminal" + }, + { + "name": "Python: Django", + "type": "python", + "request": "launch", + "program": "${workspaceFolder}/manage.py", + "console": "integratedTerminal", + "args": [ + "runserver", + "--noreload", + "--nothreading" + ], + "django": true + }, + { + "name": "Python: Flask", + "type": "python", + "request": "launch", + "module": "flask", + "env": { + "FLASK_APP": "app.py" + }, + "args": [ + "run", + "--no-debugger", + "--no-reload" + ], + "jinja": true + }, + { + "name": "Python: Current File (External Terminal)", + "type": "python", + "request": "launch", + "program": "${file}", + "console": "externalTerminal" + }, + { + "name": "Run scarlet_export", + "type": "python", + "request": "launch", + "cwd": "${workspaceFolder}/build", + "module": "scarlet_export", + "args": [ + "-o", "testing", + "test.txt" + ] + } + ] +} diff --git a/scarlet_export/__init__.py b/scarlet_export/__init__.py index d22d37f..a05be8d 100644 --- a/scarlet_export/__init__.py +++ b/scarlet_export/__init__.py @@ -2,48 +2,55 @@ from pathlib import Path import json -from note import Note -from notable import exportNotes as exportToNotable +from .note import Note +from .notable import exportNotes as exportToNotable -if __name__ == '__main__': - parser = ArgumentParser('Scarlet Notes data export program') +def main(): + """ + Program entry point. + """ + parser = ArgumentParser( + prog='scarlet_export', + description='Scarlet Notes data export program' + ) parser.add_argument( '-t', dest='program', - type='str', + type=str, help='the file format the data will be exported to [notable]', - optional=True + required=False, + default='notable' ) parser.add_argument( '-o', dest='outputDirName', - type='str', + type=str, help='output directory (where the data will be saved to)', - optional=False, + required=True ) parser.add_argument( - 'inputFile', dest='inputFileName', - type='str', - help='input file (the .txt file from Scarlet Notes)', - optional=False + dest='inputFileName', + type=str, + help='input file (the .txt file from Scarlet Notes)' ) args = parser.parse_args() # check if the input file exists - inputPath = Path(args['inputFileName']) + inputPath = Path(args.inputFileName) if not inputPath.is_file(): print('File {0} does not exist. Exiting.'.format( - args['inputFileName'] + args.inputFileName )) exit(1) # create the output dir if it doesn't exist yet - outputPath = Path(args['outputDirName']) - if not outputPath.exists() and not outputPath.is_file(): - outputPath.mkdir() - else: + outputPath = Path(args.outputDirName) + try: + if not outputPath.exists() and not outputPath.is_file(): + outputPath.mkdir() + except IOError: print('Cannot create directory {0}. Exiting.'.format( - args['outputDirName'] + args.outputDirName )) exit(1) # read and parse the file as JSON - with open(args['inputFileName']) as inputFile: + with open(args.inputFileName, encoding='utf-8') as inputFile: try: data = json.load(inputFile) except json.JSONDecodeError: @@ -56,11 +63,11 @@ notes.append(parsedNote) # currently the script only supports a single program to export the # data to - if args['program'] == 'notable': - exportToNotable(notes, args['outputDirName']) + if args.program == 'notable': + exportToNotable(notes, args.outputDirName) else: print('The program {0} is currently not supported.'.format( - args['program'] + args.program )) print('Exiting.') exit(1) diff --git a/scarlet_export/__main__.py b/scarlet_export/__main__.py new file mode 100644 index 0000000..8273c4f --- /dev/null +++ b/scarlet_export/__main__.py @@ -0,0 +1,3 @@ +from . import main + +main() diff --git a/scarlet_export/notable.py b/scarlet_export/notable.py index 56af0ab..ffa6dc0 100644 --- a/scarlet_export/notable.py +++ b/scarlet_export/notable.py @@ -1,5 +1,6 @@ -from datetime import datetime +from time import gmtime, strftime from json import loads +from pathlib import Path def exportNotes(notesList, outputDir): """ @@ -21,16 +22,28 @@ def exportNotes(notesList, outputDir): for tag in note.tags: tags += tag + ',' tags.rstrip(',') - created = datetime.utcfromtimestamp(note.timestamp) - modified = datetime.utcfromtimestamp(note.updateTimestamp) - with open(outputFileName) as output: + createdTime = gmtime(note.timestamp / 1000) + modifiedTime = gmtime(note.updateTimestamp / 1000) + created = strftime( + '%Y-%m-%dT%H:%M:%SZ', + createdTime + ) + modified = strftime( + '%Y-%m-%dT%H:%M:%SZ', + modifiedTime + ) + # check if the folder exists + outputPath = Path(outputFolder) + if not outputPath.is_dir(): + outputPath.mkdir() + with open(outputFileName, encoding='utf-8', mode='w') as output: # 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('---') - output.write('') # an empty string + output.write('---\n') + output.write('created: {0}\n'.format(created)) + output.write('modified: {0}\n'.format(modified)) + output.write('tags: [{0}]\n'.format(tags)) + output.write('---\n') + output.write('\n') # an empty string # dump the note text as well, contained in another # JSON-formatted string output.write(note.content) diff --git a/scarlet_export/note.py b/scarlet_export/note.py index d189d2b..3dd0b63 100644 --- a/scarlet_export/note.py +++ b/scarlet_export/note.py @@ -14,4 +14,5 @@ def __init__(self, note): self.updateTimestamp = int(note['updateTimestamp']) self.timestamp = int(note['timestamp']) self.tags = note['tags'].split(',') - self.content = loads(note['description'])['note']['text'] + noteContent = loads(note['description']) + self.content = noteContent['note'][0]['text']