From b36e130286061edf577e85a5d6e9649abd63c93e Mon Sep 17 00:00:00 2001
From: Loic Guegan <loic.guegan@mailbox.org>
Date: Thu, 18 Apr 2024 16:18:41 +0200
Subject: [PATCH] Debug a support for formatted output

---
 example/layout.txt    |  9 +++++++++
 pnote/metadata.py     | 10 ++++++++--
 pnote/project.py      |  2 +-
 pnote/tools/search.py | 40 +++++++++++++++++++++++++++++++++-------
 4 files changed, 51 insertions(+), 10 deletions(-)
 create mode 100644 example/layout.txt

diff --git a/example/layout.txt b/example/layout.txt
new file mode 100644
index 0000000..e7b9343
--- /dev/null
+++ b/example/layout.txt
@@ -0,0 +1,9 @@
+Created timestamp: {created}
+Created added: {added}
+Created on hostname: {hostname}
+Created on platform: {platform}
+Tags: {tags}
+Subpath: {subpath}
+Id: {id}
+Content:
+{content}
diff --git a/pnote/metadata.py b/pnote/metadata.py
index f84ebb0..d7d8b35 100644
--- a/pnote/metadata.py
+++ b/pnote/metadata.py
@@ -85,12 +85,18 @@ class Metadata:
             exit(1)
         return None
     
-    def delete(self,subpath):
+    def delete(self,subpath, ignore_error=False):
         """
         Delete subpath and its associated tags from the metadata.
         """
         cur=self.con.cursor()
-        subpath_id=self.subpathid(subpath, True)
+        subpath_id=None
+        if ignore_error:
+            subpath_id=self.subpathid(subpath, False)
+        else:
+            subpath_id=self.subpathid(subpath, True)
+        if subpath_id is None:
+            return
         cur.execute('DELETE FROM tags WHERE id={}'.format(subpath_id))
         cur.execute('DELETE FROM files WHERE id={}'.format(subpath_id))
         self.con.commit()
diff --git a/pnote/project.py b/pnote/project.py
index 432b688..8328861 100644
--- a/pnote/project.py
+++ b/pnote/project.py
@@ -260,7 +260,7 @@ class Project:
                         print("Empty note file detected => "+f.name)
                     else:
                         print("Fixing empty note file => "+f.name)
-                        self.metadata.delete(str(f))
+                        self.metadata.delete(str(f),ignore_error=True)
                         os.remove(path)
 
         self.metadata.fix_deleted(dry)
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):