def fill_package_step_by_step(p, empty=False): if empty: yield "empty" p3 = Package("urn:xyz", create=True) m3 = p3.create_media("m3", "http://example.com/m3.ogm") a3 = p3.create_annotation("a3", m3, 123, 456, "text/plain") r3 = p3.create_relation("r3", "text/plain", members=[a3,]) L3 = p3.create_list("L3", items=[a3, m3, r3,]) t3 = p3.create_tag("t3") v3 = p3.create_view("v3", "text/html+tag") q3 = p3.create_query("q3", "x-advene/rules") R3 = p3.create_resource("R3", "text/css") p.uri = "http://example.com/my-package"; yield 1 i = p.create_import("i", p3); yield 2 m = p.create_media("m", "http://example.com/m.ogm"); yield 3 m.set_meta(rdfs_seeAlso, m3); yield 4 Rb = p.create_resource("Rb", "x-advene/regexp"); yield 5 Rb.content_data = "g.*g"; yield 6 a = p.create_annotation("a", m, 123, 456, "text/plain", Rb); yield 7 a.content_data = "goog moaning"; yield 8 r = p.create_relation("r", members=[a, a3]); yield 9 r2 = p.create_relation("r2", "text/plain"); yield 10 L = p.create_list("L", items=[a, m, r, m3]); yield 11 t = p.create_tag("t"); yield 12 v = p.create_view("v", "text/html+tag"); yield 13 v.content_url = "http://example.com/a-tal-view.html"; yield 14 q = p.create_query("q", "text/x-python"); yield 15 q.content_url = "file:%s" % pathname2url(__file__); yield 16 Ra = p.create_resource("Ra", "text/css"); yield 17 sorted_p_own = list(p.own); sorted_p_own.sort(key=lambda x: x._id) for e in sorted_p_own: e.set_meta(dc_creator, "pchampin"); yield 18, e.id p.associate_tag(e, t); yield 19, e.id p.associate_tag(e, t3); yield 20, e.id sorted_p3_own = list(p3.own); sorted_p3_own.sort(key=lambda x: x._id) for e in sorted_p3_own: p.associate_tag(e, t); yield 21, e.id p.associate_tag(e, t3); yield 22, e.id p.set_meta(dc_creator, "John Doe"); yield 23 p.set_meta(dc_description, "a package used for testing diff"); yield 24 p.set_meta(PARSER_META_PREFIX+"namespaces", "dc http://purl.org/dc/elements/1.1/") yield "done"
m6 = p6.create_media("m6", "http://example.com/m6.ogm") a3 = p3.create_annotation("a3", m4, 30, 39, "text/plain") a5 = p5.create_annotation("a5", m6, 50, 59, "text/plain") t2 = p2.create_tag("t2") t3 = p3.create_tag("t3") t4 = p4.create_tag("t4") t6 = p6.create_tag("t6") p3.associate_tag(a5, t4) p3.associate_tag(a5, t3) p3.associate_tag(a3, t3) p5.associate_tag(a3, t6) p5.associate_tag(a5, t6) p1.associate_tag(a3, t2) m3 = p3.create_media("m3", "urn:xyz") r3 = p3.create_relation("r3", members=[a5, a3]) L3 = p3.create_list("L3", items=[a5, r3, a3]) v3 = p3.create_view("v3", "text/plain") q3 = p3.create_query("q3", "text/plain") R3 = p3.create_resource("R3", "text/plain") print print [i.id for i in p3.own.annotations] print [i.id for i in p4.own.medias] print [i.id for i in p5.own.annotations] print [i.id for i in p6.own.medias] print print [i.id for i in p3.all.medias] print [i.id for i in p3.all.annotations] print [i.id for i in p5.all.medias] print [i.id for i in p5.all.annotations]
if __name__ == "__main__": if exists (package_filename): unlink (package_filename) p = Package(package_filename, create=True) #trace_wrap_all (p._backend) advene_ns = "%s%%s" % ADVENE_NS_PREFIX p.meta[dc_creator] = "pchampin" m1 = p.create_media("m1", "http://champin.net/stream.avi", advene_ns % "frame_of_reference/ms;o=0") a1 = p.create_annotation("a1", m1, 20, 30, "text/plain") a2 = p.create_annotation("a2", m1, 0, 20, "text/plain") r1 = p.create_relation("r1") try: a2 = p.create_annotation("a2", m1, 0, 20, "text/plain") except ModelError: pass else: raise Exception, "duplicate ID did raise any ModelException..." p.meta[advene_ns % "meta/main_media"] = m1 a1.begin += 1 a1.duration += 1 a1.content_mimetype = "application/binary" print a1.content_url # mimetype above forced packaged content a1.content_data = "You, stupid woman!" a1.meta[dc_creator] = "rartois"
class TestEvents(TestCase): def setUp(self): self.dirname = mkdtemp() self.db = join(self.dirname, "db") self.url = "sqlite:%s" % pathname2url(self.db) self.p1 = Package(self.url+";p1", create=True) self.p2 = Package(self.url+";p2", create=True) self.p3 = Package(self.url+";p3", create=True) self.buf = [] self.callback_errors = [] def tearDown(self): try: if self.p1 and not self.p1.closed: self.p1.close() if self.p2 and not self.p2.closed: self.p2.close() if self.p3 and not self.p3.closed: self.p3.close() except ValueError: pass unlink(self.db) rmdir(self.dirname) def default_handler(self, *args): self.buf.append(args) def attr_handler(self, obj, attr, val, pre=None): actual_val = getattr(obj, attr) if pre: if actual_val == val: self.callback_errors.append("%s should not be %r yet" % (attr, val)) else: if actual_val != val: self.callback_errors.append("%s = %r, should be %r" % (attr, actual_val, val)) self.default_handler(obj, attr, val) def meta_handler(self, obj, key, val, pre=None): actual_val = obj.get_meta(key, None) if pre: if actual_val == val: self.callback_errors.append("%s should not be %r yet" % (key, val)) else: if actual_val != val: self.callback_errors.append("%s : %r, should be %r" % (key, actual_val, val)) self.default_handler(obj, key, val) def elt_handler(self, pkg, elt, signal, params): self.default_handler(pkg, elt, signal, params) def test_create_media(self): hid = self.p1.connect("created::media", self.default_handler) m = self.p2.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) a = self.p2.create_annotation("a", m, 10, 20, "text/plain") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, [(self.p1, m,),]) del self.buf[:] self.p1.disconnect(hid) m = self.p1.create_media("m2", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) def test_create_annotation(self): hid = self.p1.connect("created::annotation", self.default_handler) m = self.p2.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) a = self.p2.create_annotation("a", m, 10, 20, "text/plain") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) a = self.p1.create_annotation("a", m, 10, 20, "text/plain") self.assertEqual(self.buf, [(self.p1, a,),]) del self.buf[:] self.p1.disconnect(hid) a = self.p1.create_annotation("a2", m, 10, 20, "text/plain") self.assertEqual(self.buf, []) def test_create_relation(self): hid = self.p1.connect("created::relation", self.default_handler) r = self.p2.create_relation("r") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) r = self.p1.create_relation("r") self.assertEqual(self.buf, [(self.p1, r,),]) del self.buf[:] self.p1.disconnect(hid) r = self.p1.create_relation("r2") self.assertEqual(self.buf, []) def test_create_list(self): hid = self.p1.connect("created::list", self.default_handler) L = self.p2.create_list("L") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) L = self.p1.create_list("L") self.assertEqual(self.buf, [(self.p1, L,),]) del self.buf[:] self.p1.disconnect(hid) r = self.p1.create_relation("L2") self.assertEqual(self.buf, []) def test_create_tag(self): hid = self.p1.connect("created::tag", self.default_handler) t = self.p2.create_tag("t") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) t = self.p1.create_tag("t") self.assertEqual(self.buf, [(self.p1, t,),]) del self.buf[:] self.p1.disconnect(hid) t = self.p1.create_tag("t2") self.assertEqual(self.buf, []) def test_create_query(self): hid = self.p1.connect("created::query", self.default_handler) q = self.p2.create_query("q", "text/plain") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) q = self.p1.create_query("q", "text/plain") self.assertEqual(self.buf, [(self.p1, q,),]) del self.buf[:] self.p1.disconnect(hid) q = self.p1.create_query("q2", "text/plain") self.assertEqual(self.buf, []) def test_create_view(self): hid = self.p1.connect("created::view", self.default_handler) v = self.p2.create_view("v", "text/plain") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) v = self.p1.create_view("v", "text/plain") self.assertEqual(self.buf, [(self.p1, v,),]) del self.buf[:] self.p1.disconnect(hid) v = self.p1.create_view("v2", "text/plain") self.assertEqual(self.buf, []) def test_create_resource(self): hid = self.p1.connect("created::resource", self.default_handler) r = self.p2.create_resource("r", "text/plain") self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) r = self.p1.create_resource("r", "text/plain") self.assertEqual(self.buf, [(self.p1, r,),]) del self.buf[:] self.p1.disconnect(hid) r = self.p1.create_resource("r2", "text/plain") self.assertEqual(self.buf, []) def test_create_import(self): hid = self.p1.connect("created::import", self.default_handler) i = self.p2.create_import("i", self.p1) self.assertEqual(self.buf, []) m = self.p1.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) i = self.p1.create_import("i", self.p2) self.assertEqual(self.buf, [(self.p1, i,),]) del self.buf[:] self.p1.disconnect(hid) i = self.p1.create_import("i2", self.p3) self.assertEqual(self.buf, []) def test_create_any(self): self.p1.connect("created", self.default_handler) m = self.p2.create_media("m", "file:/tmp/foo.avi") self.assertEqual(self.buf, []) a = self.p2.create_annotation("a", m, 10, 20, "text/plain") self.assertEqual(self.buf, []) r = self.p2.create_relation("r") self.assertEqual(self.buf, []) L = self.p2.create_list("L") self.assertEqual(self.buf, []) t = self.p2.create_tag("t") self.assertEqual(self.buf, []) q = self.p2.create_query("q", "text/plain") self.assertEqual(self.buf, []) v = self.p2.create_view("v", "text/plain") self.assertEqual(self.buf, []) R = self.p2.create_resource("R", "text/plain") self.assertEqual(self.buf, []) i = self.p2.create_import("i", self.p1) self.assertEqual(self.buf, []) ref = [] m = self.p1.create_media("m", "file:/tmp/foo.avi") ref += [(self.p1, m),] self.assertEqual(self.buf, ref) a = self.p1.create_annotation("a", m, 10, 20, "text/plain") ref += [(self.p1, a),] self.assertEqual(self.buf, ref) r = self.p1.create_relation("r") ref += [(self.p1, r),] self.assertEqual(self.buf, ref) L = self.p1.create_list("L") ref += [(self.p1, L),] self.assertEqual(self.buf, ref) t = self.p1.create_tag("t") ref += [(self.p1, t),] self.assertEqual(self.buf, ref) q = self.p1.create_query("q", "text/plain") ref += [(self.p1, q),] self.assertEqual(self.buf, ref) v = self.p1.create_view("v", "text/plain") ref += [(self.p1, v),] self.assertEqual(self.buf, ref) R = self.p1.create_resource("R", "text/plain") ref += [(self.p1, R),] self.assertEqual(self.buf, ref) i = self.p1.create_import("i", self.p2) ref += [(self.p1, i),] self.assertEqual(self.buf, ref) def test_closed(self): self.p1.connect("package-closed", self.default_handler) self.p2.close() self.assertEqual(self.buf, []) ref = [(self.p1, self.p1.url, self.p1.uri)] self.p1.close() self.assertEqual(self.buf, ref) def test_closed_disconnected(self): hid = self.p1.connect("package-closed", self.default_handler) self.p1.disconnect(hid) self.p1.close() self.assertEqual(self.buf, []) def test_modified_uri(self): hid1 = self.p1.connect("modified::uri", self.attr_handler) hid2 = self.p1.connect("pre-modified::uri", self.attr_handler, "pre") self.p2.uri = "urn:12345" self.assertEqual(self.buf, []) self.p1.uri = "urn:67890" self.assertEqual(self.buf, [(self.p1, "uri", "urn:67890"),]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p1.disconnect(hid1) self.p1.disconnect(hid2) self.p1.uri = "urn:abcdef" self.assertEqual(self.buf, []) def test_modified_any(self): hid1 = self.p1.connect("modified", self.attr_handler) hid2 = self.p1.connect("pre-modified", self.attr_handler, "pre") self.p2.uri = "urn:12345" self.assertEqual(self.buf, []) self.p1.uri = "urn:67890" self.assertEqual(self.buf, [(self.p1, "uri", "urn:67890"),]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p1.disconnect(hid1) self.p1.disconnect(hid2) self.p1.uri = "urn:abcdef" self.assertEqual(self.buf, []) def test_modified_meta(self): k = DC_NS_PREFIX + "creator" k2 = DC_NS_PREFIX + "title" hid1 = self.p1.connect("modified-meta::" + k, self.meta_handler) hid2 = self.p1.connect("pre-modified-meta::" + k, self.meta_handler, 1) self.p1.set_meta(k2, "hello world") self.assertEqual(self.buf, []) self.p2.set_meta(k, "pchampin") self.assertEqual(self.buf, []) self.p1.set_meta(k, "pchampin") self.assertEqual(self.buf, [(self.p1, k, "pchampin"),]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p1.del_meta(k2) self.assertEqual(self.buf, []) self.p2.del_meta(k) self.assertEqual(self.buf, []) self.p1.del_meta(k) self.assertEqual(self.buf, [(self.p1, k, None)]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p1.disconnect(hid1) self.p1.disconnect(hid2) self.p1.set_meta(k, "oaubert") self.assertEqual(self.buf, []) def test_modified_meta_any(self): k = DC_NS_PREFIX + "creator" hid1 = self.p1.connect("modified-meta", self.meta_handler) hid2 = self.p1.connect("pre-modified-meta", self.meta_handler, "pre") self.p2.set_meta(k, "pchampin") self.assertEqual(self.buf, []) self.p1.set_meta(k, "pchampin") self.assertEqual(self.buf, [(self.p1, k, "pchampin"),]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p2.del_meta(k) self.assertEqual(self.buf, []) self.p1.del_meta(k) self.assertEqual(self.buf, [(self.p1, k, None)]*2) self.assertEqual(self.callback_errors, []) del self.buf[:] self.p1.disconnect(hid1) self.p1.disconnect(hid2) self.p1.set_meta(k, "oaubert") self.assertEqual(self.buf, []) def test_modify_media(self): k = DC_NS_PREFIX + "creator" hid = self.p1.connect("media::modified", self.elt_handler) m = self.p1.create_media("m", "file:/tmp/foo.avi") m2 = self.p2.create_media("m", "file:/tmp/foo.avi") a = self.p1.create_annotation("a", m, 10, 20, "text/plain") self.assertEqual(self.buf, []) def do_changes(_=[1]): i = _[0] = _[0] + 1 m.url = "file:/tmp/foo%s.avi" % i m2.url = "file:/tmp/foo%s.avi" % i m.set_meta(k, "creator%s" % i) a.end = 100 + i a.set_meta(k, "creator%s" % i) do_changes() self.assertEqual(self.buf, [(self.p1, m, "modified", ("url", m.url,)),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("media::pre-modified", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, m, "pre-modified", ("url", m.url,)),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("media::modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, m, "modified-meta", (k, m.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("media::pre-modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, m, "pre-modified-meta", (k, m.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) do_changes() self.assertEqual(self.buf, []) def test_modify_annotation(self): k = DC_NS_PREFIX + "creator" hid = self.p1.connect("annotation::modified", self.elt_handler) m = self.p1.create_media("m", "file:/tmp/foo.avi") m2 = self.p2.create_media("m", "file:/tmp/foo.avi") a = self.p1.create_annotation("a", m, 10, 20, "text/plain") a2 = self.p2.create_annotation("a", m2, 10, 20, "text/plain") self.assertEqual(self.buf, []) def do_changes(_=[1]): i = _[0] = _[0] + 1 m.url = "file:/tmp/foo%s.avi" % i m2.url = "file:/tmp/foo%s.avi" % i m.set_meta(k, "creator%s" % i) a.end = 100 + i a.set_meta(k, "creator%s" % i) do_changes() self.assertEqual(self.buf, [(self.p1, a, "modified", ("end", a.end,)),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("annotation::pre-modified", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, a, "pre-modified", ("end", a.end,)),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("annotation::modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, a, "modified-meta", (k, a.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("annotation::pre-modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, a, "pre-modified-meta", (k, a.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) do_changes() self.assertEqual(self.buf, []) def test_modify_relation(self): k = DC_NS_PREFIX + "creator" hid = self.p1.connect("relation::modified-items", self.elt_handler) m = self.p1.create_media("m", "file:/tmp/foo.avi") m2 = self.p2.create_media("m", "file:/tmp/foo.avi") a = self.p1.create_annotation("a", m, 10, 20, "text/plain") a2 = self.p2.create_annotation("a", m2, 10, 20, "text/plain") r = self.p1.create_relation("r", members=[a, a,]) r2 = self.p2.create_relation("r", members=[a2,]) self.assertEqual(len(self.buf), 2) # initial members cause changes del self.buf[:] def do_changes(_=[1]): i = _[0] = _[0] + 1 m.url = "file:/tmp/foo%s.avi" % i m2.url = "file:/tmp/foo%s.avi" % i m.set_meta(k, "creator%s" % i) a.end = 100 + i a.set_meta(k, "creator%s" % i) r.append(a) r2.append(a2) r.set_meta(k, "creator%s" % i) r2.set_meta(k, "creator%s" % i) do_changes() self.assertEqual(self.buf, [(self.p1, r, "modified-items", (slice(2,2), [a,])),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("relation::pre-modified-items", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, r, "pre-modified-items",(slice(3,3), [a,])),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("relation::modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, r, "modified-meta", (k, r.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("relation::pre-modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, r, "pre-modified-meta", (k, r.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) do_changes() self.assertEqual(self.buf, []) def test_modify_list(self): k = DC_NS_PREFIX + "creator" hid = self.p1.connect("list::modified-items", self.elt_handler) m = self.p1.create_media("m", "file:/tmp/foo.avi") m2 = self.p2.create_media("m", "file:/tmp/foo.avi") a = self.p1.create_annotation("a", m, 10, 20, "text/plain") a2 = self.p2.create_annotation("a", m2, 10, 20, "text/plain") L = self.p1.create_list("L", items=[a, m,]) L2 = self.p2.create_list("L", items=[m2, a2,]) self.assertEqual(len(self.buf), 2) # initial members cause changes del self.buf[:] def do_changes(_=[1]): i = _[0] = _[0] + 1 m.url = "file:/tmp/foo%s.avi" % i m2.url = "file:/tmp/foo%s.avi" % i m.set_meta(k, "creator%s" % i) a.end = 100 + i a.set_meta(k, "creator%s" % i) L.append(a) L2.append(a2) L.set_meta(k, "creator%s" % i) L2.set_meta(k, "creator%s" % i) do_changes() self.assertEqual(self.buf, [(self.p1, L, "modified-items", (slice(2,2), [a,])),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("list::pre-modified-items", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, L, "pre-modified-items",(slice(3,3), [a,])),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("list::modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, L, "modified-meta", (k, L.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) hid = self.p1.connect("list::pre-modified-meta", self.elt_handler) do_changes() self.assertEqual(self.buf, [(self.p1, L, "pre-modified-meta", (k, L.get_meta(k))),]) del self.buf[:] self.p1.disconnect(hid) do_changes() self.assertEqual(self.buf, [])