|
@@ -26,9 +26,17 @@ def get_parser():
|
|
|
add_p.add_argument("--tag", help="Comma-separated list of tags", default="")
|
|
|
add_p.add_argument("--title", help="If omitted, auto-fetch")
|
|
|
add_p.add_argument("--description", help="If omitted, auto-fetch")
|
|
|
+ add_p.add_argument("--edit", dest='edit', action='store_true', default=None)
|
|
|
+ add_p.add_argument("--no-edit", dest='edit', action='store_false', default=None)
|
|
|
add_p.add_argument("url", nargs="?")
|
|
|
add_p.set_defaults(func=main_add)
|
|
|
|
|
|
+ rm_p = subcommands.add_parser("rm")
|
|
|
+ rm_p.add_argument("--folder", default="")
|
|
|
+ rm_p.add_argument("path")
|
|
|
+ rm_p.set_defaults(func=main_rm)
|
|
|
+
|
|
|
+
|
|
|
return p
|
|
|
|
|
|
|
|
@@ -52,27 +60,33 @@ def main_list(store, args):
|
|
|
print(args.fmt.replace(r'\t', '\t').format(**markdata))
|
|
|
|
|
|
|
|
|
-def edit_before_add(data: dict, args) -> dict:
|
|
|
- ser = marxbook.Serializer()
|
|
|
- fd, fpath = tempfile.mkstemp()
|
|
|
- buf = os.fdopen(fd, "w")
|
|
|
- buf.write(ser.encode(data))
|
|
|
- buf.close()
|
|
|
- proc = subprocess.Popen([args.editor, fpath])
|
|
|
- proc.communicate()
|
|
|
+class Edit:
|
|
|
+ def __init__(self, args):
|
|
|
+ self.args = args
|
|
|
+
|
|
|
+ def edit_before_add(self, data: dict) -> dict:
|
|
|
+ ser = marxbook.Serializer()
|
|
|
+ fd, fpath = tempfile.mkstemp()
|
|
|
+ buf = os.fdopen(fd, "w")
|
|
|
+ buf.write(ser.encode(data))
|
|
|
+ buf.close()
|
|
|
+ proc = subprocess.Popen([self.args.editor, fpath])
|
|
|
+ proc.communicate()
|
|
|
+
|
|
|
+ with open(fpath) as buf:
|
|
|
+ read_data = ser.decode(buf.read())
|
|
|
+ os.unlink(fpath)
|
|
|
+ data = {}
|
|
|
+ for key in read_data:
|
|
|
+ data[key.lower()] = read_data[key]
|
|
|
+ return data
|
|
|
|
|
|
- with open(fpath) as buf:
|
|
|
- read_data = ser.decode(buf.read())
|
|
|
- os.unlink(fpath)
|
|
|
- data = {}
|
|
|
- for key in read_data:
|
|
|
- data[key.lower()] = read_data[key]
|
|
|
- return data
|
|
|
|
|
|
|
|
|
def main_add(store, args):
|
|
|
store = store.folder(args.folder)
|
|
|
batch = args.batch
|
|
|
+ editor = Edit(args)
|
|
|
if args.url is not None:
|
|
|
urls = [args.url]
|
|
|
else:
|
|
@@ -90,12 +104,23 @@ def main_add(store, args):
|
|
|
data["title"] = _title
|
|
|
if not args.description:
|
|
|
data["description"] = _description
|
|
|
- if not batch:
|
|
|
- data = edit_before_add(data, args)
|
|
|
+ # shall we edit?
|
|
|
+ edit_needed = not batch
|
|
|
+ if args.edit is not None:
|
|
|
+ edit_needed = args.edit
|
|
|
+ if edit_needed:
|
|
|
+ data = editor.edit_before_add(data)
|
|
|
store.add(**data)
|
|
|
print(urls)
|
|
|
|
|
|
|
|
|
+def main_rm(store, args):
|
|
|
+ store = store.folder(args.folder)
|
|
|
+ dirpath, name = os.path.split(args.path)
|
|
|
+ store = store.folder(dirpath)
|
|
|
+ store.find(name).unlink()
|
|
|
+
|
|
|
+
|
|
|
if __name__ == "__main__":
|
|
|
ret = main()
|
|
|
if type(ret) is int:
|