summaryrefslogtreecommitdiff
path: root/pnote/tools/search.py
blob: ad8dee9dfe259c483221184a6b0af8f1c9a5f8bd (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from pnote.tools.tool import Tool
import argparse, os

class ToolSearch(Tool):

    def __init__(self):
        self.format_file=None

    def add_parser(self,subparsers):
        p = subparsers.add_parser("search", description="Perform search operation on your notes")
        p.add_argument("-g", "--grep", help="Grep an expression")
        p.add_argument("-n", "--name", help="Search for a note path")
        p.add_argument("-i", "--ignore-case", help="Ignore case during search", action='store_true')
        p.add_argument("-t", "--tag", help="Search for a note with a tag")
        p.add_argument("-c", "--content-only", help="Show content only", action='store_true')
        p.add_argument("-s", "--subpath-only", help="Show file subpath only", action='store_true')
        p.add_argument("--last-created", help="Get last n created note files")
        p.add_argument("--last-added", help="Get last n added note files")
        p.add_argument("--format-file", help="Format output 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 catsubpaths(self, project, subpaths, content_only=False, subpath_only=False):
        first=True
        for subpath in subpaths:
            if subpath_only:
                print(subpath)
                continue
            if not content_only:
                if self.format_file is not None:
                    if not first:
                        print()
                        self.printsubpath(subpath)
            self.catsubpath(project,subpath)
            first=False

    def run(self, project, args):
        ignore_case=True if args.ignore_case else False
        content_only=True if args.content_only else False
        subpath_only=True if args.subpath_only else False

        if content_only and subpath_only:
            print("content and file-path options cannot be used at the same time")
            exit(1)
        if args.format_file:
            if not os.path.exists(args.format_file):
                print("Template file not found: {}".format(args.format_file))
                exit(1)
            self.format_file=args.format_file
        if args.grep:
            first=True
            for entry in project.grep(args.grep, ignore_case):
                subpath=entry[0]
                if subpath_only:
                    print(subpath)
                    continue
                if not content_only:
                    if not first:
                        print()
                    self.printsubpath(subpath)
                for line in entry[1]:
                    ln=line[0]
                    content=line[1]
                    if content_only:
                        print(content)
                    else:
                        print("L{}: {}".format(ln,content))
                first=False

        elif args.tag:
            self.catsubpaths(project, project.searchtag(args.tag),content_only,subpath_only)

        elif args.last_created:
            subpaths=project.listlastcreated()
            self.catsubpaths(project, subpaths[-abs(int(args.last_created)):],content_only,subpath_only)

        elif args.last_added:
            subpaths=project.listlastadded()
            self.catsubpaths(project, subpaths[-abs(int(args.last_added)):],content_only,subpath_only)

        else:
            if args.name:
                self.catsubpaths(project, project.find(args.name,ignore_case),content_only,subpath_only)
            else:
                self.catsubpaths(project, project.find(None),content_only,subpath_only)