summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoic Guegan <loic.guegan@mailbox.org>2024-04-19 11:28:45 +0200
committerLoic Guegan <loic.guegan@mailbox.org>2024-04-19 11:28:45 +0200
commit9b55d1a2495b843411c5185b9035a837328bdf67 (patch)
tree3287b3497679c9d738e486f5fc31fc1db4399a11
parentaec3a51ab44c95dfbcb1265d8bfefc5361799166 (diff)
Add json export
-rw-r--r--pnote/tools/export.py51
1 files changed, 36 insertions, 15 deletions
diff --git a/pnote/tools/export.py b/pnote/tools/export.py
index 68ce260..2785a29 100644
--- a/pnote/tools/export.py
+++ b/pnote/tools/export.py
@@ -1,17 +1,36 @@
from pnote.tools.tool import Tool
-import argparse, os, sys
+import argparse, os, sys, json
from datetime import datetime
class ToolExport(Tool):
def __init__(self):
self.template_file=None
- self.date_format="%s"
+ self.date_format=None
def add_parser(self,subparsers):
p = subparsers.add_parser("export", description="Export notes from subpaths in stdin")
p.add_argument("--template", help="Export notes following a template file")
p.add_argument("--date-format", help="Specify a format use by date in the output")
+ p.add_argument("--json", help="Export notes in json format", action='store_true')
+
+ def get_subpath_data(self, project, subpath):
+ data=None
+ with open(project.getpath(subpath),"r") as noteFile:
+ data={
+ "content":noteFile.read(),
+ "created":project.getfileinfo(subpath,"created"),
+ "added":project.getfileinfo(subpath,"added"),
+ "id":project.getfileinfo(subpath,"id"),
+ "hostname":project.getfileinfo(subpath,"hostname"),
+ "platform":project.getfileinfo(subpath,"platform"),
+ "tags":project.listtags(subpath),
+ "subpath":subpath
+ }
+ if self.date_format is not None:
+ data["created"]=datetime.fromtimestamp(data["created"]).strftime(self.date_format)
+ data["added"]=datetime.fromtimestamp(data["added"]).strftime(self.date_format)
+ return data
def run(self, project, args):
if args.date_format:
@@ -24,17 +43,19 @@ class ToolExport(Tool):
self.template_file=args.template
for line in sys.stdin:
subpath=line.rstrip()
- with open(project.getpath(subpath),"r") as noteFile:
- with open(self.template_file,"r") as tplFile:
- variables={
- "content":noteFile.read(),
- "created":datetime.fromtimestamp(project.getfileinfo(subpath,"created")).strftime(self.date_format),
- "added":datetime.fromtimestamp(project.getfileinfo(subpath,"added")).strftime(self.date_format),
- "id":project.getfileinfo(subpath,"id"),
- "hostname":project.getfileinfo(subpath,"hostname"),
- "platform":project.getfileinfo(subpath,"platform"),
- "tags":project.listtags(subpath),
- "subpath":subpath}
- for line in tplFile:
- print(line.format(**variables),end="")
+ with open(self.template_file,"r") as tplFile:
+ variables=self.get_subpath_data(project,subpath)
+ for line in tplFile:
+ print(line.format(**variables),end="")
+ elif args.json:
+ print("[")
+ first=True
+ for line in sys.stdin:
+ subpath=line.rstrip()
+ if not first:
+ print(",",end="")
+ print(json.dumps(self.get_subpath_data(project,subpath),indent=4))
+ first=False
+ print("]")
+
\ No newline at end of file