54 lines
No EOL
2.5 KiB
Python
54 lines
No EOL
2.5 KiB
Python
from pnote.tools.tool import Tool
|
|
import argparse, os, sys
|
|
|
|
class ToolExport(Tool):
|
|
|
|
def __init__(self):
|
|
self.format_file=None
|
|
|
|
def add_parser(self,subparsers):
|
|
p = subparsers.add_parser("export", description="Export notes from subpaths in stdin")
|
|
p.add_argument("--format-file", help="Export notes according to a format file")
|
|
|
|
def catsubpath(self,project,subpath):
|
|
if self.format_file is not None:
|
|
with open(project.getpath(subpath),"r") as noteFile:
|
|
with open(self.format_file,"r") as tplFile:
|
|
variables={
|
|
"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}
|
|
for line in tplFile:
|
|
print(line.format(**variables),end="")
|
|
else:
|
|
with open(project.getpath(subpath),"r") as fp:
|
|
for line in fp:
|
|
print(line,end="")
|
|
|
|
def run(self, project, args):
|
|
if args.format_file:
|
|
if not os.path.exists(args.format_file):
|
|
print("Format file not found: {}".format(args.format_file))
|
|
exit(1)
|
|
self.format_file=args.format_file
|
|
for line in sys.stdin:
|
|
subpath=line.rstrip()
|
|
with open(project.getpath(subpath),"r") as noteFile:
|
|
with open(self.format_file,"r") as tplFile:
|
|
variables={
|
|
"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}
|
|
for line in tplFile:
|
|
print(line.format(**variables),end="")
|
|
|