from pnote.tools.tool import Tool import argparse, os, sys from datetime import datetime class ToolExport(Tool): def __init__(self): self.template_file=None self.date_format="%s" 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") def run(self, project, args): if args.date_format: self.date_format=args.date_format if args.template: if not os.path.exists(args.template): print("Template file not found: {}".format(args.template)) exit(1) 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="")