Example #1
0
def create_from_file(session, pth, user):
    """Create a RO from a a file.

    Notes: if pth is a zipfile, it will be extracted and
           a ROContainer will be created with he content

    Args:
        session (DBSession):
        pth (str): path to file to read
        user (str): id of user

    Returns:
        (ResearchObject): or None if nothing has been recognized in
                          the file
    """
    # try to unpack zip files
    try:
        with ZipFile(pth, "r") as myzip:
            myzip.extractall(pj(dirname(pth), "archive"))

        remove(pth)
        # explore directory
        ros = []
        for fpth, fname in find_files(pj(dirname(pth), "archive"), ["*.wkf"]):
            ro_type = validate(fpth)
            if ro_type is not None:
                ros.append(create(session, fpth, ro_type))

        if len(ros) == 0:
            return None
        else:
            name = splitext(basename(pth))[0]
            cont = ROContainer()
            cont.init(session, dict(id=uuid1().hex, owner=user, name=name))
            for ro in ros:
                ROLink.connect(session, cont.id, ro.id, "contains")

            # search for project avatar
            avatar = fetch_avatar(pj(dirname(pth), "archive"))
            if avatar is not None:
                upload_ro_avatar(avatar, cont)

            # search project description in README
            descr = fetch_readme(pj(dirname(pth), "archive"))
            cont.store_description(descr)

            return cont
    except BadZipfile:
        # not a zip file, try to import single file
        ro_type = validate(pth)
        if ro_type is None:
            return None
        else:
            ro = create(session, pth, ro_type)
            return ro
Example #2
0
def main(session):
    ro_top = ROContainer()
    ro_top.init(session, dict(owner=users[0].id,
                           name="sample"))
    ro_top.public = True
    containers.append(ro_top)

    ro1 = ResearchObject()
    ro1.init(session, dict(owner=users[0].id, name="RO one"))

    ro1.add_policy(session, users[0], Role.view)
    ro1.add_policy(session, teams[2], Role.edit)

    ro2 = ResearchObject()
    ro2.init(session, dict(owner=users[0].id, name="RO two"))

    ROLink.connect(session, ro1.id, ro2.id, "contains")

    ro3 = ResearchObject()
    ro3.init(session, dict(owner=users[0].id, name="RO three"))

    ros = []
    for i in range(5):
        ro = ResearchObject()
        ro.init(session, dict(owner=users[0].id, name="RO%d" % i))
        ros.append(ro)

    roc = ROContainer()
    roc.init(session, dict(owner=users[0].id,
                           name="myproject",
                           remote="https://github.com/revesansparole/roc",
                           contents=ros))
    ROLink.connect(session, ro_top.id, roc.id, 'contains')

    ros = []
    for i in range(5):
        ro = ResearchObject()
        ro.init(session, dict(owner=users[1].id, name="ROcp%d" % i))
        ros.append(ro)

    ro = ROArticle()
    ro.init(session, dict(owner=users[2].id, name="cp article"))
    ro.add_policy(session, users[0], Role.view)
    ros.append(ro)

    roc2 = ROContainer()
    roc2.init(session, dict(owner=users[2].id,
                            name="CPproject",
                            contents=ros))
    ROLink.connect(session, ro_top.id, roc2.id, 'contains')

    roa = ROArticle()
    roa.init(session, dict(owner=users[0].id, name="test article"))
    roa.doi = "10.1016/S0304-3800(98)00100-8"
    descr = dedent("""
        We present a new approach to simulate the distribution of natural
        light within plant canopies. The canopy is described in 3D, each
        organ being represented by a set of polygons. Our model calculates
        the light incident on each polygon. The principle is to distinguish
        for each polygon the contribution of the light coming directly from
        light sources, the light scattered from close polygons and that
        scattered from far polygons. Close polygons are defined as located
        inside a sphere surrounding the studied polygon and having a
        diameter Ds. The direct light is computed by projection. The
        exchanges between close polygons are computed by the radiosity
        method, whereas the contribution from far polygons is estimated by
        a multi-layer model. The main part of computing time corresponds to
        the calculations of the geometric coefficients of the radiosity
        system. Then radiative exchanges can be quickly simulated for
        various conditions of the angular distribution of incoming light
        and various optical properties of soil and phytolelements.
        Simulations compare satisfactorily with those produced by a Monte
        Carlo ray tracing. They show that considering explicitly the close
        neighboring of each polygon improves the estimation of organs
        irradiance, by taking into account the local variability of fluxes.
        For a virtual maize canopy, these estimations are satisfying with
        Ds=0.5 m; in these conditions, the simulation time on a workstation
        was 25 min for a canopy of 100 plants.""")
    roa.store_description(descr)
    ROLink.connect(session, roc.id, roa.id, "contains")
    ROLink.connect(session, ro3.id, roa.id, "use")
def main(session, user, container):
    """Create ROs to test auth policies.

    Args:
        session (DBSession):
        user (User): default user
        container (ROContainer): top level container

    Returns:
        None
    """
    # create another user
    other = User.create(session,
                        uid='other',
                        name="Other User",
                        email="*****@*****.**")

    img = Image.open("seeweb/scripts/avatar/sartzet.png")
    upload_user_avatar(img, other)

    # user can view RO in container owner by other
    roa = ROArticle()
    roa.init(session, dict(owner=other.id, name="other article"))
    roa.store_description("Title\n=====\n\nLorem Ipsum\nlorem ipsum")
    roa.add_policy(session, user, Role.view)

    road = ROArticle()
    road.init(session, dict(owner=other.id, name="other editable article"))
    road.store_description("Title\n=====\n\nLorem Ipsum\nlorem ipsum")
    road.add_policy(session, user, Role.edit)

    roc = ROContainer()
    roc.init(session, dict(owner=other.id,
                           name="other project",
                           contents=[roa, road]))
    ROLink.connect(session, container.id, roc.id, 'contains')

    # access granted to ROs through their container policy
    roa = ROArticle()
    roa.init(session, dict(owner=other.id, name="other 'private' article"))
    roa.store_description("Title\n=====\n\nLorem Ipsum\nlorem ipsum")

    roc = ROContainer()
    roc.init(session, dict(owner=other.id,
                           name="other 'denied' project",
                           contents=[roa]))
    roc.add_policy(session, user, Role.denied)
    ROLink.connect(session, container.id, roc.id, 'contains')

    roc = ROContainer()
    roc.init(session, dict(owner=other.id,
                           name="other project",
                           contents=[roa]))
    roc.add_policy(session, user, Role.edit)
    ROLink.connect(session, container.id, roc.id, 'contains')

    # public container
    roa = ROArticle()
    roa.init(session, dict(owner=other.id, name="other article"))
    roa.store_description("Title\n=====\n\nLorem Ipsum\nlorem ipsum")

    road = ROArticle()
    road.init(session, dict(owner=other.id, name="other denied article"))
    road.store_description("Title\n=====\n\nLorem Ipsum\nlorem ipsum")
    road.add_policy(session, user, Role.denied)

    roc = ROContainer()
    roc.init(session, dict(owner=other.id,
                           name="other 'public' project",
                           contents=[roa, road]))
    roc.public = True
    ROLink.connect(session, container.id, roc.id, 'contains')
Example #4
0
def main(session, user, container):
    """Create workflow related projects.

    Args:
        session (DBSession):
        user (User): owner of created project
        container (ROContainer): top level container

    Returns:
        None
    """
    # default interfaces for openalea
    roa = ROContainer()
    roa.init(session, dict(owner=user.id, name="openalea.interfaces"))

    top = ROContainer()
    top.init(session, dict(owner=user.id, name="openalea", contents=[roa]))

    itrans = {}
    for iname in ("IBool", "ICodeStr", "IColor", "IData", "IDateTime",
                  "IDict", "IDirStr",
                  "IEnumStr", "IFileStr", "IFloat",
                  "IFunction", "IImage", "IInt", "IRef", "IRGBColor",
                  "ISequence", "ISlice", "IStr", "ITextStr",
                  "ITuple", "ITuple3"):
        standard_interface = get_interface_by_name(session, iname[1:].lower())
        if standard_interface is not None:
            ancestors = [standard_interface.id]
        else:
            ancestors = []

        roi = ROInterface()
        roi.init(session, dict(owner=user.id, name=iname, ancestors=ancestors))

        ROLink.connect(session, roa.id, roi.id, "contains")
        itrans[iname] = roi.id

    roc = ROContainer()
    roc.init(session, dict(owner=user.id, name="nodelib"))
    ROLink.connect(session, container.id, roc.id, 'contains')

    ndefs = []
    for i in range(3):
        node_def = dict(name="read%d" % i,
                        description="toto was here",
                        owner=user.id,
                        function="testio:read",
                        inputs=[dict(name="in1", interface=itrans["IInt"],
                                     default="0", description="counter"),
                                dict(name="in2", interface=itrans["IStr"],
                                     default="a", description="unit")],
                        outputs=[dict(name="ret", interface=itrans["IInt"],
                                      description="important result")])

        rown = ROWorkflowNode()
        rown.init(session, node_def)
        ndefs.append(rown)

        ROLink.connect(session, roc.id, rown.id, "contains")

    # alias = ContentItem.create(session, uuid1().hex, "alias", nodelib)
    # alias.author = "revesansparole"
    # alias.name = ndefs[2]['id']

    workflow_def = dict(name="sample_workflow",
                        description="trying some stuff",
                        owner="revesansparole",
                        nodes=[dict(id=ndefs[0].id, label="node1",
                                    x=-50, y=-80),
                               dict(id=ndefs[1].id, label=None,
                                    x=50, y=-80),
                               dict(id=ndefs[2].id, label=None,
                                    x=0, y=0),
                               dict(id='4094cf5a490711e6aa4cd4bed973e64a',
                                    label="fail",
                                    x=0, y=80)],
                        links=[dict(source=0, source_port="ret",
                                    target=2, target_port="in1"),
                               dict(source=1, source_port="ret",
                                    target=2, target_port="in2"),
                               dict(source=2, source_port="ret",
                                    target=3, target_port="in")])

    row = ROWorkflow()
    row.init(session, workflow_def)
    ROLink.connect(session, roc.id, row.id, "contains")

    roc = ROContainer()
    roc.init(session, dict(owner=user.id, name="provenance"))
    ROLink.connect(session, container.id, roc.id, 'contains')

    with open("seeweb/scripts/gallery/graph_pulse.png", 'rb') as f:
        graph_pulse = b64encode(f.read())

    with open("seeweb/scripts/sphere.json", 'r') as f:
        sphere = json.load(f)

    data = [dict(id=uuid1().hex, type="ref", value=itrans["IInt"]),
            dict(id=uuid1().hex, type="int", value=10),
            dict(id=uuid1().hex, type="str", value="Killroy was here"),
            dict(id=uuid1().hex, type="image", value=graph_pulse),
            dict(id=uuid1().hex, type="scene3d", value=sphere)
            ]

    prov_def = dict(name="sample_provenance",
                    description="trying some stuff",
                    owner=user.id,
                    workflow=row.id,
                    time_init=10,
                    time_end=14,
                    data=data,
                    parameters=[dict(node=0, port="in1", data=data[0]['id']),
                                dict(node=0, port="in2", data=data[2]['id'])
                                ],
                    executions=[
                        dict(node=0, time_init=10, time_end=11,
                             inputs=[
                                 {"port": "in1", "data": data[0]['id']},
                                 {"port": "in2", "data": data[2]['id']}
                             ],
                             outputs=[
                                 {"port": "ret", "data": data[1]['id']}
                             ]),
                        dict(node=1, time_init=10, time_end=11,
                             inputs=[
                                 {"port": "in1", "data": data[0]['id']}
                             ],
                             outputs=[
                                 {"port": "ret", "data": data[3]['id']}
                             ]),
                        dict(node=2, time_init=12, time_end=13,
                             inputs=[
                                 {"port": "in1", "data": data[1]['id']},
                                 {"port": "in2", "data": data[3]['id']}
                             ],
                             outputs=[
                                 {"port": "ret", "data": data[4]['id']}
                             ])
                    ]
                    )

    with open("../prov.wkf", 'w') as f:
        json.dump(prov_def, f)

    rop = ROWorkflowProv()
    rop.init(session, prov_def)
    ROLink.connect(session, roc.id, rop.id, "contains")
Example #5
0
def main(session, user, container):
    """Create a project that contains different types of data

    Args:
        session (DBSession):
        user (User): owner of created project
        container (ROContainer): top level container

    Returns:
        None
    """
    # register common data interfaces
    roc = ROContainer()
    roc.init(
        session,
        dict(
            id="81c69a8558a311e6afb6d4bed973e64a",
            owner=user.id,
            name="interfaces",
            public=True,
            description="Store commonly used interfaces",
        ),
    )

    roi = ROInterface()
    roi.init(
        session,
        dict(
            id=ROData.implements,
            owner=user.id,
            name="any",
            public=True,
            description="Interface used for data that don't " "have a specific interface",
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    ropy = ROInterface()
    ropy.init(session, dict(id="dc5b10c858a611e6afb6d4bed973e64a", owner=user.id, name="pyobj", public=True))
    ROLink.connect(session, roc.id, ropy.id, "contains")

    schema = dict(
        title="Interface for data of type 'string'", description="Just a simple chain of characters", type="string"
    )

    roi = ROInterface()
    roi.init(
        session,
        dict(
            id="006bca4c58a311e6afb6d4bed973e64a",
            owner=user.id,
            name="string",
            public=True,
            schema=cvt_schema(schema),
            ancestors=[ropy.id],
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    sid = roi.id
    for name in ("code", "path", "ref", "text"):
        roi = ROInterface()
        roi.init(session, dict(owner=user.id, name=name, public=True, ancestors=[sid]))
        ROLink.connect(session, roc.id, roi.id, "contains")

    schema = dict(title="Interface for data of type 'number'", description="Just a simple number", type="number")

    roi = ROInterface()
    roi.init(
        session,
        dict(
            id="c7f6a30a58a511e6afb6d4bed973e64a",
            owner=user.id,
            name="number",
            public=True,
            schema=cvt_schema(schema),
            ancestors=[ropy.id],
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    sid = roi.id
    for name in ("complex", "int", "float"):
        roi = ROInterface()
        roi.init(session, dict(owner=user.id, name=name, public=True, ancestors=[sid]))
        ROLink.connect(session, roc.id, roi.id, "contains")

    rgba_schema = dict(
        title="Interface for color of type rgba",
        description="Just a simple array of rgba quadruplets",
        type="array",
        minLength=4,
        maxLength=4,
        items=dict(type="int"),
    )

    roi = ROInterface()
    roi.init(
        session,
        dict(
            id="256203e058b911e6afb6d4bed973e64a",
            owner=user.id,
            name="rgba",
            public=True,
            schema=cvt_schema(rgba_schema),
            ancestors=[],
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    schema = dict(
        title="Interface for data of type 2D images",
        description="Just a simple 2d array of rgba quadruplets",
        type="array",
        minLength=1,
        items=dict(type="array", minLength=1, items=rgba_schema),
    )

    roi = ROInterface()
    roi.init(
        session,
        dict(id=ROImage.implements, owner=user.id, name="image", public=True, schema=cvt_schema(schema), ancestors=[]),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    schema = dict(
        title="Interface for data of type 3D scenes",
        description="Json description of a 3D scene",
        type="array",
        minLength=1,
        items=dict(type="array", minLength=1, items=rgba_schema),
    )

    roi = ROInterface()
    roi.init(
        session,
        dict(
            id=ROScene3d.implements, owner=user.id, name="scene3d", public=True, schema=cvt_schema(schema), ancestors=[]
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    # create a sample project with some common data examples
    roc = ROContainer()
    roc.init(session, dict(id="0a73ad11596111e6a3a6d4bed973e64a", owner=user.id, name="data", ctype="project"))
    ROLink.connect(session, container.id, roc.id, "contains")

    # raw data
    rod = ROData()
    rod.init(session, dict(owner=user.id, name="test number", value=3.14159))
    ROLink.connect(session, roc.id, rod.id, "contains")

    with open("seeweb/ro/data/static/default_avatar.png", "rb") as f:
        value = b64encode(f.read())

    rod = ROData()
    rod.init(session, dict(owner=user.id, name="test data", value=value))
    ROLink.connect(session, roc.id, rod.id, "contains")

    # image
    with open("seeweb/ro/data/static/default_avatar.png", "rb") as f:
        value = b64encode(f.read())

    roi = ROImage()
    roi.init(
        session,
        dict(
            id="03faa88158bb11e6afb6d4bed973e64a",
            owner=user.id,
            name="test image",
            value=value,
            description="Sample image for testing purpose",
        ),
    )
    ROLink.connect(session, roc.id, roi.id, "contains")

    # scene3D
    with open("seeweb/scripts/scene.json", "r") as f:
        sc = json.load(f)

    rosc = ROScene3d()
    rosc.init(session, dict(owner=user.id, name="test scene", value=sc))
    ROLink.connect(session, roc.id, rosc.id, "contains")