def dbscript(lfn): """The first argument should be a command. Allowed commands are: add, set, del, list, info, pull, metas, file add takes a list of file names to add to the db. set takes a DB id, meta tag, and value, and sets the indicated tag on the indicated object to the value del deletes the listed DB ids. The first ID may be "all" to clear the db. list takes no arguments and prints a list of ids of the db entries info takes a list of ids and prints detailed info about each pull takes a db id and a file name, and downloads the datafile associated to the id into the named file. metas list all the defined metadata tags in the db file takes a db id, an attribute name (e.g. image) and a file name downloads the named file attribute into the named file """ cmd = lfn[0] PATH = "/CercalSystem/" CERCDB = PSDB(URL, PATH) CERCDB.auth("gic", "graham") if cmd == "del": lfn = lfn[1:] if lfn and lfn[0] == "all": lfn = CERCDB.getIDList() for name in lfn: CERCDB.delete(name) print "deleted entry %s" % name elif cmd == "add": for fn in lfn[1:]: doc = io.read(fn) groups = doc.getElements("Group", depth=1) groups = [g for g in groups if g.attrib("DBrecord")] if groups: for grp in groups: print ("Commiting db group %s from %s" % (grp.name(), fn)) m = dict([(k[5:], grp.attrib(k)) for k in grp.attributes if k.startswith("meta_")]) name = grp.name() name = name.replace(".", "_") s = serialize(None, grp) if grp.attrib("DBurl") == URL and grp.attrib("DBrecord") == PATH: db = CERCDB else: db = PSDB(grp.attrib("DBurl"), grp.attrib("DBrecord")) db.addOrUpdate(name, m, s) else: m = db_getmetas(doc) if "dbid" in m: name = m["dbid"] del (m["dbid"]) else: name = "%s_%s_%s" % (m["length"][0].upper(), str(m["class"]), str(m["slide_number"])) print "commiting %s as %s" % (fn, name) s = serialize(None, doc) CERCDB.addOrUpdate(name, m, s) elif cmd == "set": iid = lfn[1] tag = lfn[2] val = lfn[3] nmd = {tag: val} CERCDB.update(iid, nmd) elif cmd == "list": for iid in CERCDB.getIDList(): print (iid) elif cmd == "info": l = CERCDB.get(CERCDB.path) lidd = dict([(e["id"], e) for e in l]) for id_ in lfn[1:]: e = lidd.get(id_, "No such entry") print (e) elif cmd == "pull": record = lfn[1] fname = lfn[2] df = CERCDB.getFile(record) doc = deserialize(df) doc = io.write(doc, fname, format="guess") elif cmd == "metas": l = CERCDB.get(CERCDB.path) metas = set([]) for e in l: md = e["metadata"] for k in md: metas.add(k) for k in metas: print (k) elif cmd == "file": record = lfn[1] fpath = lfn[2] fname = lfn[3] df = CERCDB.getFile(record, fpath) open(fname, "wb").write(df)
def sendDoc(self): return mzip.serialize(None, self.doc)
def do_upload(self, name, doc, meta): name = name.replace(".", "_") s = serialize(None, doc) self.rest.addOrUpdate(name, meta, s) self.report("sent")