summaryrefslogtreecommitdiff
path: root/pnote/tools/export.py
blob: 68ce2605a3a424a0dc5ad91f24ccb266669538f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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="")