summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoïc Guégan <loic@debianus>2024-04-19 07:48:27 +0200
committerLoïc Guégan <loic@debianus>2024-04-19 07:48:27 +0200
commite7d21b74a6dba14daa004295441ad66b5b27c704 (patch)
treefbeb983cabfbe283d618a3f4b88c61b0338c81aa
parentb36e130286061edf577e85a5d6e9649abd63c93e (diff)
Create export command
-rw-r--r--pnote/__main__.py4
-rw-r--r--pnote/tools/__init__.py1
-rw-r--r--pnote/tools/export.py54
-rw-r--r--pnote/tools/search.py38
4 files changed, 65 insertions, 32 deletions
diff --git a/pnote/__main__.py b/pnote/__main__.py
index e539c36..d652ddd 100644
--- a/pnote/__main__.py
+++ b/pnote/__main__.py
@@ -21,6 +21,8 @@ def main():
searcht.add_parser(subparsers)
tagt=ToolTag()
tagt.add_parser(subparsers)
+ exportt=ToolExport()
+ exportt.add_parser(subparsers)
admint=ToolAdmin()
admint.add_parser(subparsers)
@@ -35,6 +37,8 @@ def main():
searcht.run(project,args)
elif args.tool == "tag":
tagt.run(project,args)
+ elif args.tool == "export":
+ exportt.run(project,args)
elif args.tool == "admin":
admint.run(project,args)
else:
diff --git a/pnote/tools/__init__.py b/pnote/tools/__init__.py
index 20a6981..891c21d 100644
--- a/pnote/tools/__init__.py
+++ b/pnote/tools/__init__.py
@@ -1,3 +1,4 @@
from pnote.tools.search import *
from pnote.tools.tag import *
+from pnote.tools.export import *
from pnote.tools.admin import *
diff --git a/pnote/tools/export.py b/pnote/tools/export.py
new file mode 100644
index 0000000..711dec8
--- /dev/null
+++ b/pnote/tools/export.py
@@ -0,0 +1,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="")
+ \ No newline at end of file
diff --git a/pnote/tools/search.py b/pnote/tools/search.py
index ad8dee9..25266c6 100644
--- a/pnote/tools/search.py
+++ b/pnote/tools/search.py
@@ -3,9 +3,6 @@ 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")
@@ -16,28 +13,11 @@ 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):
- 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="")
-
+ 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
@@ -46,10 +26,9 @@ class ToolSearch(Tool):
print(subpath)
continue
if not content_only:
- if self.format_file is not None:
- if not first:
- print()
- self.printsubpath(subpath)
+ if not first:
+ print()
+ self.printsubpath(subpath)
self.catsubpath(project,subpath)
first=False
@@ -61,11 +40,6 @@ 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):