Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Make the program work
* Add debug configurations
* Make the program run with python -m

Signed-off-by: Timur Demin <me@tdem.in>
  • Loading branch information
tdemin committed Apr 9, 2019
1 parent 24d8830 commit 83d7f67
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
*.egg-info
**/__pycache__
build
81 changes: 81 additions & 0 deletions .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"
]
}
]
}
53 changes: 30 additions & 23 deletions scarlet_export/__init__.py
Expand Up @@ -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:
Expand All @@ -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)
3 changes: 3 additions & 0 deletions scarlet_export/__main__.py
@@ -0,0 +1,3 @@
from . import main

main()
33 changes: 23 additions & 10 deletions 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):
"""
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion scarlet_export/note.py
Expand Up @@ -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']

0 comments on commit 83d7f67

Please sign in to comment.