Browse Source

Make the program work

* Add debug configurations
* Make the program run with python -m

Signed-off-by: Timur Demin <me@tdem.in>
tags/v0.1.0
Timur Demin 3 months ago
parent
commit
83d7f678d6
Signed by: Timur Demin <me@tdem.in> GPG Key ID: 9EDF3F9D9286FA20
6 changed files with 140 additions and 34 deletions
  1. 1
    0
      .gitignore
  2. 81
    0
      .vscode/launch.json
  3. 30
    23
      scarlet_export/__init__.py
  4. 3
    0
      scarlet_export/__main__.py
  5. 23
    10
      scarlet_export/notable.py
  6. 2
    1
      scarlet_export/note.py

+ 1
- 0
.gitignore View File

@@ -1,2 +1,3 @@
1 1
 *.egg-info
2 2
 **/__pycache__
3
+build

+ 81
- 0
.vscode/launch.json View File

@@ -0,0 +1,81 @@
1
+{
2
+    // Use IntelliSense to learn about possible attributes.
3
+    // Hover to view descriptions of existing attributes.
4
+    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5
+    "version": "0.2.0",
6
+    "configurations": [
7
+        {
8
+            "name": "Python: Current File (Integrated Terminal)",
9
+            "type": "python",
10
+            "request": "launch",
11
+            "program": "${file}",
12
+            "console": "integratedTerminal"
13
+        },
14
+        {
15
+            "name": "Python: Remote Attach",
16
+            "type": "python",
17
+            "request": "attach",
18
+            "port": 5678,
19
+            "host": "localhost",
20
+            "pathMappings": [
21
+                {
22
+                    "localRoot": "${workspaceFolder}",
23
+                    "remoteRoot": "."
24
+                }
25
+            ]
26
+        },
27
+        {
28
+            "name": "Python: Module",
29
+            "type": "python",
30
+            "request": "launch",
31
+            "module": "enter-your-module-name-here",
32
+            "console": "integratedTerminal"
33
+        },
34
+        {
35
+            "name": "Python: Django",
36
+            "type": "python",
37
+            "request": "launch",
38
+            "program": "${workspaceFolder}/manage.py",
39
+            "console": "integratedTerminal",
40
+            "args": [
41
+                "runserver",
42
+                "--noreload",
43
+                "--nothreading"
44
+            ],
45
+            "django": true
46
+        },
47
+        {
48
+            "name": "Python: Flask",
49
+            "type": "python",
50
+            "request": "launch",
51
+            "module": "flask",
52
+            "env": {
53
+                "FLASK_APP": "app.py"
54
+            },
55
+            "args": [
56
+                "run",
57
+                "--no-debugger",
58
+                "--no-reload"
59
+            ],
60
+            "jinja": true
61
+        },
62
+        {
63
+            "name": "Python: Current File (External Terminal)",
64
+            "type": "python",
65
+            "request": "launch",
66
+            "program": "${file}",
67
+            "console": "externalTerminal"
68
+        },
69
+        {
70
+            "name": "Run scarlet_export",
71
+            "type": "python",
72
+            "request": "launch",
73
+            "cwd": "${workspaceFolder}/build",
74
+            "module": "scarlet_export",
75
+            "args": [
76
+                "-o", "testing",
77
+                "test.txt"
78
+            ]
79
+        }
80
+    ]
81
+}

+ 30
- 23
scarlet_export/__init__.py View File

@@ -2,48 +2,55 @@ from argparse import ArgumentParser
2 2
 from pathlib import Path
3 3
 import json
4 4
 
5
-from note import Note
6
-from notable import exportNotes as exportToNotable
5
+from .note import Note
6
+from .notable import exportNotes as exportToNotable
7 7
 
8
-if __name__ == '__main__':
9
-    parser = ArgumentParser('Scarlet Notes data export program')
8
+def main():
9
+    """
10
+    Program entry point.
11
+    """
12
+    parser = ArgumentParser(
13
+        prog='scarlet_export',
14
+        description='Scarlet Notes data export program'
15
+    )
10 16
     parser.add_argument(
11 17
         '-t', dest='program',
12
-        type='str',
18
+        type=str,
13 19
         help='the file format the data will be exported to [notable]',
14
-        optional=True
20
+        required=False,
21
+        default='notable'
15 22
     )
16 23
     parser.add_argument(
17 24
         '-o', dest='outputDirName',
18
-        type='str',
25
+        type=str,
19 26
         help='output directory (where the data will be saved to)',
20
-        optional=False,
27
+        required=True
21 28
     )
22 29
     parser.add_argument(
23
-        'inputFile', dest='inputFileName',
24
-        type='str',
25
-        help='input file (the .txt file from Scarlet Notes)',
26
-        optional=False
30
+        dest='inputFileName',
31
+        type=str,
32
+        help='input file (the .txt file from Scarlet Notes)'
27 33
     )
28 34
     args = parser.parse_args()
29 35
     # check if the input file exists
30
-    inputPath = Path(args['inputFileName'])
36
+    inputPath = Path(args.inputFileName)
31 37
     if not inputPath.is_file():
32 38
         print('File {0} does not exist. Exiting.'.format(
33
-            args['inputFileName']
39
+            args.inputFileName
34 40
         ))
35 41
         exit(1)
36 42
     # create the output dir if it doesn't exist yet
37
-    outputPath = Path(args['outputDirName'])
38
-    if not outputPath.exists() and not outputPath.is_file():
39
-        outputPath.mkdir()
40
-    else:
43
+    outputPath = Path(args.outputDirName)
44
+    try:
45
+        if not outputPath.exists() and not outputPath.is_file():
46
+            outputPath.mkdir()
47
+    except IOError:
41 48
         print('Cannot create directory {0}. Exiting.'.format(
42
-            args['outputDirName']
49
+            args.outputDirName
43 50
         ))
44 51
         exit(1)
45 52
     # read and parse the file as JSON
46
-    with open(args['inputFileName']) as inputFile:
53
+    with open(args.inputFileName, encoding='utf-8') as inputFile:
47 54
         try:
48 55
             data = json.load(inputFile)
49 56
         except json.JSONDecodeError:
@@ -56,11 +63,11 @@ if __name__ == '__main__':
56 63
         notes.append(parsedNote)
57 64
     # currently the script only supports a single program to export the
58 65
     # data to
59
-    if args['program'] == 'notable':
60
-        exportToNotable(notes, args['outputDirName'])
66
+    if args.program == 'notable':
67
+        exportToNotable(notes, args.outputDirName)
61 68
     else:
62 69
         print('The program {0} is currently not supported.'.format(
63
-            args['program']
70
+            args.program
64 71
         ))
65 72
         print('Exiting.')
66 73
         exit(1)

+ 3
- 0
scarlet_export/__main__.py View File

@@ -0,0 +1,3 @@
1
+from . import main
2
+
3
+main()

+ 23
- 10
scarlet_export/notable.py View File

@@ -1,5 +1,6 @@
1
-from datetime import datetime
1
+from time import gmtime, strftime
2 2
 from json import loads
3
+from pathlib import Path
3 4
 
4 5
 def exportNotes(notesList, outputDir):
5 6
     """
@@ -21,16 +22,28 @@ def exportNotes(notesList, outputDir):
21 22
         for tag in note.tags:
22 23
             tags += tag + ','
23 24
         tags.rstrip(',')
24
-        created = datetime.utcfromtimestamp(note.timestamp)
25
-        modified = datetime.utcfromtimestamp(note.updateTimestamp)
26
-        with open(outputFileName) as output:
25
+        createdTime = gmtime(note.timestamp / 1000)
26
+        modifiedTime = gmtime(note.updateTimestamp / 1000)
27
+        created = strftime(
28
+            '%Y-%m-%dT%H:%M:%SZ',
29
+            createdTime
30
+        )
31
+        modified = strftime(
32
+            '%Y-%m-%dT%H:%M:%SZ',
33
+            modifiedTime
34
+        )
35
+        # check if the folder exists
36
+        outputPath = Path(outputFolder)
37
+        if not outputPath.is_dir():
38
+            outputPath.mkdir()
39
+        with open(outputFileName, encoding='utf-8', mode='w') as output:
27 40
             # print YAML front matter first
28
-            output.write('---')
29
-            output.write('created: {0}'.format(created))
30
-            output.write('modified: {0}'.format(modified))
31
-            output.write('tags: [{0}]'.format(tags))
32
-            output.write('---')
33
-            output.write('') # an empty string
41
+            output.write('---\n')
42
+            output.write('created: {0}\n'.format(created))
43
+            output.write('modified: {0}\n'.format(modified))
44
+            output.write('tags: [{0}]\n'.format(tags))
45
+            output.write('---\n')
46
+            output.write('\n') # an empty string
34 47
             # dump the note text as well, contained in another
35 48
             # JSON-formatted string
36 49
             output.write(note.content)

+ 2
- 1
scarlet_export/note.py View File

@@ -14,4 +14,5 @@ class Note:
14 14
         self.updateTimestamp = int(note['updateTimestamp'])
15 15
         self.timestamp = int(note['timestamp'])
16 16
         self.tags = note['tags'].split(',')
17
-        self.content = loads(note['description'])['note']['text']
17
+        noteContent = loads(note['description'])
18
+        self.content = noteContent['note'][0]['text']

Loading…
Cancel
Save