コード例 #1
0
ファイル: core_diff.py プロジェクト: oaubert/advene2
class TestDiffPackage(TestCase):
    def setUp(self):
        self.p1 = Package("file:/tmp/p1", create=True)
        self.p2 = Package("file:/tmp/p2", create=True)

    def tearDown(self):
        self.p1.close()
        self.p2.close()

    def test_empty(self):
        p1, p2 = self.p1, self.p2
        self.assertEqual([], diff_packages(p1, p2))
        self.assertEqual([], diff_packages(p2, p1))

    def test_step_by_step(self):
        p1, p2 = self.p1, self.p2
        p1.set_meta(PACKAGED_ROOT, mkdtemp(prefix="advene2_core_diff_"))
        p2.set_meta(PACKAGED_ROOT, mkdtemp(prefix="advene2_core_diff_"))
        fill_p2 = fill_package_step_by_step(p2)
        for i in fill_package_step_by_step(p1):
            diff = diff_packages(p1, p2)
            self.assertEqual(1, len(fix_diff(diff)), (i, diff))
            diff = diff_packages(p2, p1)
            self.assertEqual(1, len(fix_diff(diff)), (i, diff))
            fill_p2.next()
            diff = diff_packages(p1, p2)
            self.assertEqual([], fix_diff(diff), "%s\n%r" % (i, diff))
            diff = diff_packages(p2, p1)
            self.assertEqual([], fix_diff(diff), "%s\n%r" % (i, diff))

    def test_several_steps(self):
        p1, p2 = self.p1, self.p2
        for i in fill_package_step_by_step(p1):
            self.assertNotEqual([], diff_packages(p1, p2), i)
            self.assertNotEqual([], diff_packages(p2, p1), i)
コード例 #2
0
ファイル: core_package.py プロジェクト: oaubert/advene2
 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 = []
コード例 #3
0
ファイル: package.py プロジェクト: oaubert/advene2
    def __init__(self, url, create=False, readonly=False, force=False,
                 parser=None):
        CorePackage.__init__(self, url, create, readonly, force, parser)

        ns = self._get_namespaces_as_dict()
        ns.setdefault(DC_NS_PREFIX, "dc")
        self._set_namespaces_with_dict(ns)
        if create:
            bk.init(self, self)
        self.connect("modified-meta", bk.update)
        self.connect("created", bk.init)
        self.connect("tag::added", bk.update)
        self.connect("tag::removed", bk.update)
コード例 #4
0
ファイル: core_package.py プロジェクト: oaubert/advene2
    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.p4 = Package(self.url+";p4", create=True)

        self.d1 = frozenset(((self.p1._backend, self.p1._id),))
        self.d2 = frozenset(((self.p2._backend, self.p2._id),))
        self.d3 = frozenset(((self.p3._backend, self.p3._id),))
        self.d4 = frozenset(((self.p4._backend, self.p4._id),))
コード例 #5
0
ファイル: core_package.py プロジェクト: oaubert/advene2
 def test_create_transient(self):
     # use invalid URL scheme to force the package to be transient
     p = Package("x-invalid-scheme:xyz", create=True)
     p.close()
     # if the second times works (with create=True), then transient works
     p = Package("x-invalid-scheme:xyz", create=True)
     p.close()
コード例 #6
0
ファイル: core_diff.py プロジェクト: oaubert/advene2
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"
コード例 #7
0
ファイル: model_tales.py プロジェクト: oaubert/advene2
    def test_absolute_url(self):
        p = self.p; c = self.c
        p1 = CorePackage("tmp1", create=True)
        p2 = CorePackage("tmp2", create=True)
        p3 = CorePackage("tmp3", create=True)
        t = p.create_tag("t")
        t1 = p1.create_tag("t1")
        t2 = p2.create_tag("t2")
        t3 = p3.create_tag("t3")
        p.create_import("i1", p1)
        p1.create_import("i2", p2)
        options = { "packages": {"p": p, "p1": p1},
                    "aliases": {p: "p", p1: "p1"},
                    "p": p, "p1": p1, "p2": p2, "p3": p3,
                  }
        c.addGlobal("options", options)
        # check on package
        self.check_path("options/p/absolute_url", "/p")
        self.check_path("options/p1/absolute_url", "/p1")
        try:
            self.check_path("options/p2/absolute_url", "/p/i1:i2/package")
            # it is either one or the other, we can't predict
        except AssertionError:
            self.check_path("options/p2/absolute_url", "/p1/i2/package")
        self.check_path("options/p/absolute_url/a/b/c", "/p/a/b/c")
        base = options["package_url"] = "http://localhost:1234/packages"
        self.check_path("options/p/absolute_url/a/b/c", base+"/p/a/b/c")
        self.check_path("options/p3/absolute_url|nothing", None)
        del options["package_url"]

        # check on element
        self.check_path("options/p/t/absolute_url", "/p/t")
        self.check_path("options/p1/t1/absolute_url", "/p1/t1")
        try:
            self.check_path("options/p2/t2/absolute_url", "/p/i1:i2:t2")
            # it is either one or the other, we can't predict
        except AssertionError:
            self.check_path("options/p2/t2/absolute_url", "/p1/i2:t2")
        self.check_path("options/p/t/absolute_url/a/b/c", "/p/t/a/b/c")
        base = options["package_url"] = "http://localhost:1234/packages"
        self.check_path("options/p/t/absolute_url/a/b/c", base+"/p/t/a/b/c")
        self.check_path("options/p3/t3/absolute_url|nothing", None)
コード例 #8
0
ファイル: test_big.py プロジェクト: oaubert/advene2
def measure_time(label=None):
    global _t
    nt = time()
    if label:
        print "%s: %0.3fs" % (label, nt-_t)
    _t = nt

NBM = 100
NBA = 10000
Lm = [ ("m%s" % i, "http://example.com/m%s" % i) for i in xrange(NBM) ]
La = [ ("a%s" % i, Lm[i%NBM][0], (i//NBM)*10, (i//NBM)*10+9)
       for i in xrange(NBA) ]
foref = FOREF_PREFIX + "ms;o=0"

if __name__ == "__main__":
    p = Package("file:test/test_big.bxp", create=True)
    
    measure_time() # take origin
    for m, u in Lm:
        p.create_media(m, u, foref)
    for a, m, b, e in La:
        p.create_annotation(a, p[m], b, e, "text/plain")
    measure_time("creating %s medias and %s annotations" %(NBM, NBA))

    f = open("test/test_big.bxp", "w")
    measure_time() # take origin
    xml.serialize_to(p, f)
    measure_time("serializing XML")
    f.close()

    measure_time() # take origin
コード例 #9
0
ファイル: test2.py プロジェクト: oaubert/advene2
url1 = "sqlite:%s;p1" % filea
url2 = "sqlite:%s;p2" % filea
url3 = "sqlite:%s;p3" % filea
url4 = "sqlite:%s;p4" % fileb
url5 = "sqlite:%s;p5" % fileb
url6 = "sqlite:%s;p6" % fileb
url7 = "sqlite:%s;p7" % fileb

if __name__ == "__main__":

    # testing cyclic import and backend multiplexing

    if exists (filea): unlink (filea)
    if exists (fileb): unlink (fileb)

    p1 = Package(url1, create=True)
    p2 = Package(url2, create=True)
    p3 = Package(url3, create=True)
    p4 = Package(url4, create=True)
    p5 = Package(url5, create=True)
    p6 = Package(url6, create=True)
    p7 = Package(url7, create=True)

    assert p1._backend is p2._backend is p3._backend
    assert p4._backend is p5._backend is p6._backend is p7._backend
    assert p1._backend is not p4._backend

    p1.create_import("p2", p2)
    p1.create_import("p3", p3)
    p3.create_import("p4", p4)
    p3.create_import("p5", p5)
コード例 #10
0
ファイル: example_view.py プロジェクト: oaubert/advene2
from libadvene.model.core.package import Package

p = Package("file:new-package.bzp", create=True)

v1 = p.create_view("v1", "application/x-advene-builtin-view")
v1.content_data = "method = hello_world"
v2 = p.create_view("is_view", "application/x-advene-builtin-view")
v2.content_data = "method = has_type \n type = VIEW"
v3 = p.create_view("is_package", "application/x-advene-builtin-view")
v3.content_data = "method = has_type \n type = PACKAGE"

for v in (v1, v2, v3):
    print "content-type:", v.output_mimetype, "\n"
    print v.apply_to(p)
    print "=========="
コード例 #11
0
ファイル: test1.py プロジェクト: oaubert/advene2
    od = obj.__dict__
    for k,v in cd.iteritems():
        if k[0] != "_" and callable (v):
            f = getattr (obj, k)
            od[k] = trace_wrapper (f)

def print_elements(p):
    print [(k,id(v)) for k,v in p._elements.items()]



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:
コード例 #12
0
ファイル: core_package.py プロジェクト: oaubert/advene2
class TestImports(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.p4 = Package(self.url+";p4", create=True)

        self.d1 = frozenset(((self.p1._backend, self.p1._id),))
        self.d2 = frozenset(((self.p2._backend, self.p2._id),))
        self.d3 = frozenset(((self.p3._backend, self.p3._id),))
        self.d4 = frozenset(((self.p4._backend, self.p4._id),))

    def _dependencies(self, p):
        return frozenset(
            (be, pid) for be, pdict in p._backends_dict.items()
                      for pid in pdict
        )

    def test_dependancies(self):
        p1, p2, p3, p4 = self.p1, self.p2, self.p3, self.p4
        d1, d2, d3, d4 = self.d1, self.d2, self.d3, self.d4
        dall = d1.union(d2).union(d3).union(d4)
        _dependencies = self._dependencies

        self.assertEqual(d1, _dependencies(p1))
        self.assertEqual(d2, _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p2.create_import("p4", self.p4)
        self.assertEqual(d1, _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p1.create_import("p2", self.p2)
        self.assertEqual(d1.union(d2).union(d4), _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p3.create_import("p4", self.p4)
        self.assertEqual(d1.union(d2).union(d4), _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3.union(d4), _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p1.create_import("p3", self.p3)
        self.assertEqual(dall, _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3.union(d4), _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p4.create_import("p1", self.p1)
        self.assertEqual(dall, _dependencies(p1))
        self.assertEqual(dall, _dependencies(p2))
        self.assertEqual(dall, _dependencies(p3))
        self.assertEqual(dall, _dependencies(p4))

        self.p4["p1"].delete()
        self.assertEqual(dall, _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3.union(d4), _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p1["p3"].delete()
        self.assertEqual(d1.union(d2).union(d4), _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3.union(d4), _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p3["p4"].delete()
        self.assertEqual(d1.union(d2).union(d4), _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p1["p2"].delete()
        self.assertEqual(d1, _dependencies(p1))
        self.assertEqual(d2.union(d4), _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

        self.p2["p4"].delete()
        self.assertEqual(d1, _dependencies(p1))
        self.assertEqual(d2, _dependencies(p2))
        self.assertEqual(d3, _dependencies(p3))
        self.assertEqual(d4, _dependencies(p4))

    def test_close_unimported(self):
        self.p1.create_import("p2", self.p2)
        self.p1.close()
        self.assert_(self.p1.closed)
        self.assert_(not self.p2.closed)
        self.assert_(not self.p3.closed)
        self.assert_(not self.p4.closed)

    def test_close_simply_imported(self):
        self.p1.create_import("p2", self.p2)
        self.assertRaises(ValueError, self.p2.close)
        self.assert_(not self.p1.closed)
        self.assert_(not self.p2.closed)
        self.assert_(not self.p3.closed)
        self.assert_(not self.p4.closed)

    def test_close_cycle(self):
        self.p1.create_import("p2", self.p2)
        self.p2.create_import("p1", self.p1)
        self.p1.create_import("p3", self.p3)
        self.p2.create_import("p4", self.p4)
        self.p1.close()
        self.assert_(self.p1.closed)
        self.assert_(self.p2.closed)
        self.assert_(not self.p3.closed)
        self.assert_(not self.p4.closed)

    def test_close_imported_cycle(self):
        self.p1.create_import("p2", self.p2)
        self.p2.create_import("p1", self.p1)
        self.p3.create_import("p1", self.p1)
        self.assertRaises(ValueError, self.p1.close)
        self.assert_(not self.p1.closed)
        self.assert_(not self.p2.closed)
        self.assert_(not self.p3.closed)
        self.assert_(not self.p4.closed)

    def test_close_multiple_cycles(self):
        self.p1.create_import("p2", self.p2)
        self.p1.create_import("p3", self.p3)
        self.p2.create_import("p4", self.p4)
        self.p3.create_import("p4", self.p4)
        self.p4.create_import("p1", self.p1)
        self.p2.close()
        self.assert_(self.p1.closed)
        self.assert_(self.p2.closed)
        self.assert_(self.p3.closed)
        self.assert_(self.p4.closed)

    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()
            if self.p4 and not self.p4.closed: self.p4.close()
        except ValueError:
            pass
        unlink(self.db)
        rmdir(self.dirname)
コード例 #13
0
ファイル: core_package.py プロジェクト: oaubert/advene2
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, [])
コード例 #14
0
ファイル: example1.py プロジェクト: oaubert/advene2
from libadvene.model.core.package import Package
from libadvene.model.core.media import FOREF_PREFIX

frame_of_reference = FOREF_PREFIX + "ms;o=0"

# This demonstrates how to create and save a package

# create a package with a relative URL
# NB: this is a *URL*, not a filename (that could be http://...)
# so use "/" as a separator, even in MS Windows
p = Package("examples/example1.bxp", create=True)

# add a resource to that package,
# the content is given by a URL, relative to the package's URL
r = p.create_resource("r", "text/x-python", url="example1.py")

# we save the current state of our package to another file
# NB: this is a filename, the separator is OS dependant
p.save_as("examples/example1-sav.bxp", erase=True)

# we now add a media
m = p.create_media("m", "dvd://", frame_of_reference)
p.save()

# we now save under a new name, and work on the new file
p.save_as("examples/example1-new.bxp", erase=True, change_url=True)
a = p.create_annotation("a", m, 0, 60, "text/plain")
p.save()

# Don't forget to close your package once you are done
p.close()
コード例 #15
0
ファイル: core_diff.py プロジェクト: oaubert/advene2
 def setUp(self):
     self.p1 = Package("file:/tmp/p1", create=True)
     self.p2 = Package("file:/tmp/p2", create=True)
コード例 #16
0
ファイル: advene_zip.py プロジェクト: oaubert/advene2

def _recurse(z, dirname, base=""):
    for f in listdir(dirname):
        abspath = path.join(dirname, f)
        if isdir(abspath):
            _recurse(z, abspath, path.join(base, f))
        else:
            #print "=== zipping", abspath, path.join(base, f)
            z.write(abspath, path.join(base, f).encode('utf-8'))


if __name__ == "__main__":
    # example of using ZIP serialization and parsing
    from libadvene.model.core.package import Package
    p = Package("file:/tmp/foo.bzp", create=True)

    # prepare directories
    if not exists("/tmp/foo"): mkdir("/tmp/foo")
    if not exists("/tmp/foo/data"): mkdir("/tmp/foo/data")

    # it is now safe to define the packaged-root of the package...
    p.set_meta(PACKAGED_ROOT, "/tmp/foo")
    # ... and to create packaged contents
    r = p.create_resource("R1", "text/plain", None, "packaged:/data/R1.txt")
    # this actually writes in /tmp/foo/data/R1.txt
    r.content_data = "good moaning\n"

    # let us serialize it...
    f = open("/tmp/foo.bzp", "w")
    serialize_to(p, f)