def main(argv): backend = 'be/backend' first_doc = "1.1.0.1.0.1" docs = [] # connected docs outf = sys.stdout ### Parse argv import getopt opts, args = getopt.getopt(argv[1:], "a:o:b:dv:") for o in opts: if '-a' in o: first_doc = o[1] elif '-o' in o: outf = open(o[1], 'w') elif '-b' in o: backend = o[1] elif '-d' in o: udxexp.chatty += 1 elif '-v' in o: udxexp.chatty = int(o[1]) docs = [x88.Address(first_doc)] # allow docs added manually for a in args: docs.append(x88.Address(a)) be_dir = os.path.dirname(backend) backend = os.path.basename(backend) cwd = os.getcwd() # must run from backend dir or else some request fail os.chdir(be_dir) ## Get 88.1 session if udxexp.chatty > 2: xs = x88.pipeconnect_debug(backend, sys.stderr) else: xs = x88.pipeconnect(backend) ### Find connections mutter(1, 'starting with', docs) links = find_connections(xs, docs) ## write dot file write_dot(outf, links) xs.quit()
def print_links(xs, addr): xs.open_document(addr, x88.READ_ONLY, x88.CONFLICT_FAIL) vspec = xs.retrieve_vspanset(addr) mutter(1, 'vspans', vspec) links= [] # Get all end sets in given document for vspan in vspec: if vspan.span.start[0] == 1: textspec = x88.SpecSet(x88.VSpec(addr, [vspan.span])) srcspecs, trgtspecs, tpspecs = xs.retrieve_endsets(textspec) if srcspecs: mutter(1, 'found from specs', srcspecs) links += xs.find_links(srcspecs) else: mutter(1, 'no from specs in', vspan) if trgtspecs: mutter(1, 'found end specs', trgtspecs) links += xs.find_links(x88.NOSPECS, trgtspecs) else: mutter(1, 'no end specs in', vspan) if tpspecs: mutter(1, 'found three specs', tpspecs) links += xs.find_links(x88.NOSPECS, x88.NOSPECS, tpspecs) else: mutter(1, 'no three specs in', vspan) elif vspan.span.start[0] == 2: mutter(1, 'linkvspan', vspan) else: mutter(1, 'ignoring vspan', vspan) # Filter out duplicate link addresses filtered = [] for laddr in links: if not laddr in filtered: filtered.append(laddr) else: mutter(1, 'ignoring duplicate link', laddr) links = filtered # output print 'Links with endsets in', addr print for laddr in links: print laddr, 'type:', srcspecs = xs.follow_link(laddr, x88.LINK_SOURCE) trgtspecs = xs.follow_link(laddr, x88.LINK_TARGET) tpspecs = xs.follow_link(laddr, x88.LINK_TYPE) for spec in tpspecs: if spec in x88.TYPE_NAMES: print x88.TYPE_NAMES[spec], else: print spec, print print '\tsource:', srcspecs for spec in srcspecs: sourceruns = [] for vspan in spec: if vspan.docid != addr: xs.open_document(vspan.docid, x88.READ_ONLY, x88.CONFLICT_FAIL) nspec = x88.SpecSet(x88.VSpec(vspan.docid, [vspan.span])) sourceruns += xs.retrieve_contents(nspec) if vspan.docid != addr: xs.close_document(vspan.docid) print '\t', sourceruns print '\ttarget:', trgtspecs for spec in trgtspecs: targetruns = [] for vspan in spec: if vspan.docid != addr: xs.open_document(vspan.docid, x88.READ_ONLY, x88.CONFLICT_FAIL) nspec = x88.SpecSet(x88.VSpec(vspan.docid, [vspan.span])) targetruns += xs.retrieve_contents(nspec) if vspan.docid != addr: xs.close_document(vspan.docid) print '\t', targetruns print xs.close_document(addr)
def doc_connections(xs, doc_addr): """Return a list of document ids linked from given document. """ docs = [] mutter(3, '> open (35)', doc_addr) xs.open_document(doc_addr, x88.READ_ONLY, x88.CONFLICT_FAIL) ### Find docs targetted from vspans in this document textspec = x88.SpecSet(x88.VSpec(doc_addr, [ENTIRE_DOC])) # find linked spans mutter(2, '> retrieve-endsets (28)', textspec) srcspecs, trgtspecs, tpspecs = xs.retrieve_endsets(textspec) mutter(2, '<', srcspecs, trgtspecs, tpspecs) links = [] # find links for source spans if srcspecs: mutter(2, '> find-links-from-to-three (30)', srcspecs) links += xs.find_links(srcspecs) mutter(2, '<', links) # find targets for links for laddr in links: mutter(1, '> follow-link (18)', laddr, x88.LINK_TARGET) trgtspecs = xs.follow_link(laddr, x88.LINK_TARGET) mutter(1, '<', trgtspecs) for vspec in trgtspecs: docs.append(vspec.docid) xs.close_document(doc_addr) return docs
def print_links(xs, addr): xs.open_document(addr, x88.READ_ONLY, x88.CONFLICT_FAIL) vspec = xs.retrieve_vspanset(addr) mutter(1, 'vspans', vspec) links = [] # Get all end sets in given document for vspan in vspec: if vspan.span.start[0] == 1: textspec = x88.SpecSet(x88.VSpec(addr, [vspan.span])) srcspecs, trgtspecs, tpspecs = xs.retrieve_endsets(textspec) if srcspecs: mutter(1, 'found from specs', srcspecs) links += xs.find_links(srcspecs) else: mutter(1, 'no from specs in', vspan) if trgtspecs: mutter(1, 'found end specs', trgtspecs) links += xs.find_links(x88.NOSPECS, trgtspecs) else: mutter(1, 'no end specs in', vspan) if tpspecs: mutter(1, 'found three specs', tpspecs) links += xs.find_links(x88.NOSPECS, x88.NOSPECS, tpspecs) else: mutter(1, 'no three specs in', vspan) elif vspan.span.start[0] == 2: mutter(1, 'linkvspan', vspan) else: mutter(1, 'ignoring vspan', vspan) # Filter out duplicate link addresses filtered = [] for laddr in links: if not laddr in filtered: filtered.append(laddr) else: mutter(1, 'ignoring duplicate link', laddr) links = filtered # output print 'Links with endsets in', addr print for laddr in links: print laddr, 'type:', srcspecs = xs.follow_link(laddr, x88.LINK_SOURCE) trgtspecs = xs.follow_link(laddr, x88.LINK_TARGET) tpspecs = xs.follow_link(laddr, x88.LINK_TYPE) for spec in tpspecs: if spec in x88.TYPE_NAMES: print x88.TYPE_NAMES[spec], else: print spec, print print '\tsource:', srcspecs for spec in srcspecs: sourceruns = [] for vspan in spec: if vspan.docid != addr: xs.open_document(vspan.docid, x88.READ_ONLY, x88.CONFLICT_FAIL) nspec = x88.SpecSet(x88.VSpec(vspan.docid, [vspan.span])) sourceruns += xs.retrieve_contents(nspec) if vspan.docid != addr: xs.close_document(vspan.docid) print '\t', sourceruns print '\ttarget:', trgtspecs for spec in trgtspecs: targetruns = [] for vspan in spec: if vspan.docid != addr: xs.open_document(vspan.docid, x88.READ_ONLY, x88.CONFLICT_FAIL) nspec = x88.SpecSet(x88.VSpec(vspan.docid, [vspan.span])) targetruns += xs.retrieve_contents(nspec) if vspan.docid != addr: xs.close_document(vspan.docid) print '\t', targetruns print xs.close_document(addr)