diff options
| author | 2021-08-05 17:28:28 -0400 | |
|---|---|---|
| committer | 2021-08-05 17:28:33 -0400 | |
| commit | f3b0d22c6219dd150169a227fdac830e9bcd68d9 (patch) | |
| tree | 7ac4673711328f1e0b03b37ffa54acbe704e8456 | |
| parent | modify schema (again) (diff) | |
retrieve blobs and files
| -rw-r--r-- | README.md | 3 | ||||
| -rwxr-xr-x | underwriter | 22 |
2 files changed, 22 insertions, 3 deletions
@@ -13,3 +13,6 @@ TODO * optimize some sql statements * Redirects? * Globs for untagging +* Confirmation for untagging +* Confirmation for overwriting blobs and filenames + diff --git a/underwriter b/underwriter index 968d51d..dd62ef3 100755 --- a/underwriter +++ b/underwriter @@ -11,6 +11,12 @@ import io def current_time(): return int(datetime.now(timezone.utc).timestamp()) +def cat(n): + try: + with open(n) as f: + sys.stdout.write(f.read()) + except: + sys.stderr.write("The associated file was not found!\n") class Cursor(sqlite3.Cursor): def new_tags(self, tags): @@ -94,10 +100,10 @@ class Context: def get_data(self, s): l = self.db.execute("""SELECT - dat NOT NULL, file NOT NULL FROM cards + dat, file FROM cards WHERE name = ?""", (s,)).fetchall() if len(l) == 0: - l = [(0,0)] + l = [(None, None)] return {"blob" : l[0][0], "filename" : l[0][1]} def make_cursor(self): @@ -138,7 +144,7 @@ class Context: def _check_dup(self, n, typ, f): od = self.get_data(n) - if od is not None and od[typ]: + if od[typ] is not None: r = input( f""""{n}" already has a {typ} associated with it. Overwrite? [y/N]: """) if len(r) == 0 or r.lower()[0] != "y": @@ -165,6 +171,14 @@ f""""{n}" already has a {typ} associated with it. Overwrite? [y/N]: """) cur.associate_blob(l[0], buf) self.db.commit() + def retrieve(self, l): + n = l[0] + od = self.get_data(n) + if od["filename"] is not None: + cat(od["filename"]) + else: + sys.stdout.write(od["blob"]) + def __init__(self, name): if not os.path.isfile(name): self.db = Context.initdb(name) @@ -180,6 +194,7 @@ untag file [tags...]: remove tags from a file set [filename | blob] name file: associate a filename (or the contents of the file) with a name. +retrieve name: get file data list [tags | cards]: list all tags or cards in the database """ @@ -208,5 +223,6 @@ if __name__ == "__main__": argparse.addarg(["set", "filename"], lambda x: x == 2, ctx.set_filename) argparse.addarg(["set", "blob"], lambda x: x == 2, ctx.set_blob) + argparse.addarg(["retrieve"], lambda x:x == 1, ctx.retrieve) argparse.execarg(args) |
