def selectDB(self, event, url=None, clas="/CercalSystem/"): if not url: if len(URLS) == 1: self.report("Only one datastore available. Using it") url = URLS[0] else: d = self.askParam([{"Name": "Select Database Site", "Type": "List", "Value": URLS}]) if not d: return url = d[0] if not clas or type(clas) == int: cl = self.classList(url) if len(cl) == 1: clas = cl[0] elif type(clas) == int: clas = cl[clas] else: d = self.askParam([{"Name": "Select Database Table", "Type": "List", "Value": cl}]) if not d: return clas = d[0] self.rest = PSDB(url, clas) self.dbid.SetLabel("%s%s" % (url, clas)) n = len(self.list(None)) self.dbreccount.SetLabel("%i records" % n)
# This program is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A # PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 59 Temple # Place, Suite 330, Boston, MA 02111-1307 USA # from mien.interface.cellview3D import CellViewer import wx, os, sys, tempfile from mien.parsers.mzip import deserialize sys.path.append("/home/gic/mienblocks") from ccbcv.rest import PSDB URL = 'http://cercus.cns.montana.edu:8090' PATH = '/CercalCellAfferent/' CERCDB = PSDB(URL, PATH) os.environ['DISPLAY'] = 'localhost:1.0' record = sys.argv[1] wxapp = wx.PySimpleApp() x = CellViewer() x.Show(True) df = CERCDB.getFile(record) doc = deserialize(df) x.newDoc(doc) fid, fname=tempfile.mkstemp('.png') x.graph.screenShot(fname) CERCDB.putFile(record, open(fname,'rb').read(), "snapshot", 'image/png') os.unlink(fname)
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)
# You will also need a very recent update of mien for some of this stuff to work #1) Get a data file. To do this you need a database URL, class path, and id #these my be set as constants, downloaded from some url, or whatever. URL = 'http://cercus.cns.montana.edu:8090' PATH = '/CercalCellAfferent/' #construct a Rest resource from ccbcv.rest import PSDB JSONH = {'Content-type': 'application/json', 'Accept':'application/json'} CERCDB = PSDB(URL, PATH) # you can request a list of database records: idlist = PSDB.getIDList() # see ccbcv.scripts or ccbcv.yogo for examples of polling, searching, etc from the DB #select some record record = idlist[0] #obviously, there are more useful ways to do this. This is just an example # now download the record to a local document:
# PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., 59 Temple # Place, Suite 330, Boston, MA 02111-1307 USA # import mien.parsers.fileIO as io import mien.parsers.nmpml as nmpml from ccbcv.rest import PSDB from mien.parsers.mzip import deserialize URL = 'http://cercus.cns.montana.edu:8090' PATH = '/CercalSystem/' CERCDB = PSDB(URL, PATH) def getVaric(iid): df = CERCDB.getFile(iid) doc = deserialize(df) var = doc.getElements('Fiducial', {"Style":"spheres"}) return var records = CERCDB.get(PATH) doc = nmpml.blankDocument() for rec in records: if 'afferent' in rec['metadata']['anatomy_type']: iid = rec['id'] print iid els = getVaric(iid) if not els:
class DBTool(BaseGui): def __init__(self, parent, **opts): BaseGui.__init__(self, parent, title="Database Tool", menus=["Connection", "Get", "Put"], pycommand=True) controls = [ ["Connection", "Choose Data Store", self.selectDB], ["Get", "List All Entries", self.list], ["Get", "Show Details", self.infoprint], ["Get", "Search", self.do_search], ["Get", "Download Single File", self.download], ["Get", "Download Several Files", self.downloadGroup], ["Put", "Upload Current File", self.upload], ] self.fillMenus(controls) self.mainSizer = wx.BoxSizer(wx.VERTICAL) self.main.SetSizer(self.mainSizer) self.main.SetAutoLayout(True) self.SetAutoLayout(True) self.mainSizer.Add(wx.StaticText(self.main, -1, "Connected To:"), 1, wx.GROW | wx.ALL | wx.ALIGN_CENTRE, 5) self.dbid = wx.StaticText(self.main, -1, "") self.mainSizer.Add(self.dbid, 1, wx.GROW | wx.ALL | wx.ALIGN_CENTRE, 5) self.dbreccount = wx.StaticText(self.main, -1, "0 Records") self.mainSizer.Add(self.dbreccount, 1, wx.GROW | wx.ALL | wx.ALIGN_CENTRE, 5) self.selectDB(None, URLS[0], "/CercalSystem/") self.doc = self.Parent.document self.SetSize(wx.Size(500, 200)) def getMetas(self): conflict = [] metas = {} for e in self.doc.elements: m = dict([(k[5:], e.attrib(k)) for k in e.attributes if k.startswith("meta_")]) for k in m: if k in conflict: continue elif not k in metas: metas[k] = m[k] elif metas[k] != m[k]: del (metas[k]) conflict.append(k) return metas def classList(self, url): z = Resource(url) d = z.get("/Class/") cl = ["/" + a["id"] + "/" for a in d if not a.get("core")] return cl def selectDB(self, event, url=None, clas="/CercalSystem/"): if not url: if len(URLS) == 1: self.report("Only one datastore available. Using it") url = URLS[0] else: d = self.askParam([{"Name": "Select Database Site", "Type": "List", "Value": URLS}]) if not d: return url = d[0] if not clas or type(clas) == int: cl = self.classList(url) if len(cl) == 1: clas = cl[0] elif type(clas) == int: clas = cl[clas] else: d = self.askParam([{"Name": "Select Database Table", "Type": "List", "Value": cl}]) if not d: return clas = d[0] self.rest = PSDB(url, clas) self.dbid.SetLabel("%s%s" % (url, clas)) n = len(self.list(None)) self.dbreccount.SetLabel("%i records" % n) def list(self, event): ids = self.rest.getIDList() if event == None: return ids self.report("\n".join(ids)) def upload(self, event): groups = self.doc.getElements("Group") groups = [g for g in groups if g.attrib("DBrecord")] if any(groups): self.report("found existing records. Committing them to the DB") for g in groups: self.report(g.name()) self.upload_group(g) else: self.report("no existing DB groups. Uploading whole file as new record") self.upload_doc() def upload_group(self, g): m = dict([(k[5:], g.attrib(k)) for k in g.attributes if k.startswith("meta_")]) name = g.name() self.do_upload(name, g, m) def upload_doc(self): m = self.getMetas() name = "%s_%s_%s" % (m["length"][0].upper(), str(m["class"]), str(m["slide_number"])) self.do_upload(name, self.doc, m) def do_upload(self, name, doc, meta): name = name.replace(".", "_") s = serialize(None, doc) self.rest.addOrUpdate(name, meta, s) self.report("sent") def infoprint(self, event): ids = self.list(None) d = self.askParam([{"Name": "Which Record?", "Type": "List", "Value": ids}]) if not d: return print self.rest.getInfo(d[0]) def do_search(self, event): j = self.rest.get(self.rest.path) import ccbcv.yogo reload(ccbcv.yogo) s = ccbcv.yogo.SearchGui(self, j) def download(self, event=None, record=None, append=True, refresh=True): if not record: ids = self.list(None) d = self.askParam([{"Name": "Which Record?", "Type": "List", "Value": ids}]) if not d: return record = d[0] df = self.rest.getFile(record) doc = deserialize(df) if not append: self.Parent.newDoc(doc) else: m = self.rest.getInfo(record)["metadata"] m = dict([("meta_" + k, m[k]) for k in m]) m["Name"] = record m["DBrecord"] = self.rest.path m["DBurl"] = self.rest.url m["color"] = (255, 255, 255) group = createElement("Group", m) self.Parent.document.newElement(group) for e in doc.elements[:]: e.move(group) if refresh: self.Parent.update_all(element=self.Parent.document, action="rebuild") def downloadGroup(self, event=None, records=None): if not records: ids = self.list(None) d = self.askParam([{"Name": "Which Record?", "Type": "Select", "Value": ids}]) if not d: return records = d[0] for r in records: self.download(None, r, True, False) self.Parent.update_all(element=self.Parent.document, action="rebuild")