summaryrefslogtreecommitdiff
path: root/pnote/tools/export.py
blob: 711dec882d8606dd1b46f8dc1e620ceeec18bcfc (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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="")