diff options
Diffstat (limited to 'pnote/tools')
| -rw-r--r-- | pnote/tools/search.py | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/pnote/tools/search.py b/pnote/tools/search.py index ece7fff..ad8dee9 100644 --- a/pnote/tools/search.py +++ b/pnote/tools/search.py @@ -1,8 +1,11 @@ from pnote.tools.tool import Tool -import argparse +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") @@ -13,11 +16,28 @@ class ToolSearch(Tool): 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): - with open(project.getpath(subpath),"r") as fp: - for line in fp: - print(line,end="") + 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 @@ -26,9 +46,10 @@ class ToolSearch(Tool): print(subpath) continue if not content_only: - if not first: - print() - self.printsubpath(subpath) + if self.format_file is not None: + if not first: + print() + self.printsubpath(subpath) self.catsubpath(project,subpath) first=False @@ -40,6 +61,11 @@ class ToolSearch(Tool): 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): |
