diff options
| author | Loic Guegan <loic.guegan@mailbox.org> | 2024-04-19 10:51:04 +0200 |
|---|---|---|
| committer | Loic Guegan <loic.guegan@mailbox.org> | 2024-04-19 10:51:04 +0200 |
| commit | aec3a51ab44c95dfbcb1265d8bfefc5361799166 (patch) | |
| tree | 46dd11296cefd556a0bb80931ddc5891722b92c1 | |
| parent | e7d21b74a6dba14daa004295441ad66b5b27c704 (diff) | |
Improve note export
| -rw-r--r-- | example/template.txt (renamed from example/layout.txt) | 0 | ||||
| -rw-r--r-- | pnote/tools/export.py | 44 |
2 files changed, 15 insertions, 29 deletions
diff --git a/example/layout.txt b/example/template.txt index e7b9343..e7b9343 100644 --- a/example/layout.txt +++ b/example/template.txt diff --git a/pnote/tools/export.py b/pnote/tools/export.py index 711dec8..68ce260 100644 --- a/pnote/tools/export.py +++ b/pnote/tools/export.py @@ -1,49 +1,35 @@ from pnote.tools.tool import Tool import argparse, os, sys +from datetime import datetime class ToolExport(Tool): def __init__(self): - self.format_file=None + self.template_file=None + self.date_format="%s" 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="") + p.add_argument("--template", help="Export notes following a template file") + p.add_argument("--date-format", help="Specify a format use by date in the output") 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)) + if args.date_format: + self.date_format=args.date_format + + if args.template: + if not os.path.exists(args.template): + print("Template file not found: {}".format(args.template)) exit(1) - self.format_file=args.format_file + self.template_file=args.template for line in sys.stdin: subpath=line.rstrip() with open(project.getpath(subpath),"r") as noteFile: - with open(self.format_file,"r") as tplFile: + with open(self.template_file,"r") as tplFile: variables={ "content":noteFile.read(), - "created":project.getfileinfo(subpath,"created"), - "added":project.getfileinfo(subpath,"added"), + "created":datetime.fromtimestamp(project.getfileinfo(subpath,"created")).strftime(self.date_format), + "added":datetime.fromtimestamp(project.getfileinfo(subpath,"added")).strftime(self.date_format), "id":project.getfileinfo(subpath,"id"), "hostname":project.getfileinfo(subpath,"hostname"), "platform":project.getfileinfo(subpath,"platform"), |
