示例#1
0
def view(request):
    session = DBSession()
    ro, view_params = edit_init(request, session, 'links')

    if "new_link" in request.params or request.params.get("ro_id", "") != "":
        if add_link(request, session, ro) is not None:
            loc = request.current_route_url()
            return HTTPFound(location=loc)

    # check for link removal
    for link in ro.out_links + ro.in_links:
        if "rm_%s" % link.id in request.params:
            ROLink.remove(session, link)
            request.session.flash("Link removed", 'success')

            loc = request.current_route_url()
            return HTTPFound(location=loc)

    links = []
    for link in ro.out_links:
        links.append((link.id, "self", link.type, link.target))
    for link in ro.in_links:
        links.append((link.id, link.source, link.type, "self"))

    view_params["links"] = links

    return view_params
示例#2
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
示例#3
0
    def init(self, session, ro_def):
        """Initialize this RO with a set of attributes

        Args:
            session (DBSession):
            ro_def (dict): set of properties to initialize this RO

        Returns:
            None
        """
        ResearchObject.init(self, session, ro_def)

        # link to nodes used by this workflow
        uids = set(ndef['id'] for ndef in ro_def.get('nodes', []))
        for uid in uids:
            ROLink.connect(session, self.id, uid, 'use')
示例#4
0
def add_link(request, session, ro):
    """Add a new 'use' link between this RO and another

    Args:
        request (Request):
        session (DBSession):
        ro (ResearchObject):

    Returns:
        (None|uid): None if something failed, created link
    """
    uid = request.params.get('ro_id', "")
    if len(uid) == 0:
        request.session.flash("Enter a RO id first", 'warning')
        return None

    # check whether the RO is already associated to this RO
    if uid == ro.id:
        request.session.flash("Can not create link with oneself", 'warning')
        return None

    linked = [link.target for link in ro.out_links if link.type == 'use']
    if uid in linked:
        request.session.flash("%s is already linked" % uid, 'warning')
        return None

    # check whether uid correspond to a valid RO
    tgt = ResearchObject.get(session, uid)
    if tgt is None:
        request.session.flash("%s is not a valid RO" % uid, 'warning')
        return None

    # create link
    link = ROLink.connect(session, ro.id, uid, "use")
    return link
示例#5
0
    def init(self, session, ro_def):
        """Initialize this RO with a set of attributes

        Args:
            session (DBSession):
            ro_def (dict): set of properties to initialize this RO

        Returns:
            None
        """
        # remove attributes locally stored
        loc_def = dict(ro_def)
        schema = loc_def.pop('schema', "{}")
        ancestors = loc_def.pop('ancestors', [])

        ResearchObject.init(self, session, loc_def)
        self.schema = schema
        for ancestor in ancestors:
            ROLink.connect(session, ancestor, self.id, 'is_ancestor_of')
示例#6
0
    def init(self, session, ro_def):
        """Initialize this RO with a set of attributes

        Args:
            session (DBSession):
            ro_def (dict): set of properties to initialize this RO

        Returns:
            None
        """
        # remove attributes locally stored
        loc_def = dict(ro_def)
        contents = loc_def.pop('contents', [])
        ctype = loc_def.pop('ctype', "container")

        ResearchObject.init(self, session, loc_def)
        self.ctype = ctype

        for ro in contents:
            ROLink.connect(session, self.id, ro.id, "contains")
示例#7
0
def view(request):
    session = DBSession()

    # gather data
    src = request.params["source"]
    tgt = request.params["target"]
    link_type = request.params["link_type"]

    # test ownership
    # TODO

    # find link
    query = session.query(ROLink)
    query = query.filter(ROLink.source == src)
    query = query.filter(ROLink.target == tgt)
    query = query.filter(ROLink.type == link_type)
    link, = query.all()

    ROLink.remove(session, link)

    return True
示例#8
0
def view(request):
    session = DBSession()

    # gather data
    src = request.params["src"]
    tgt = request.params["tgt"]
    link_type = request.params["link_type"]

    # test ownership
    # TODO

    # create link
    link = ROLink.connect(session, src, tgt, link_type)

    return True
示例#9
0
    def init(self, session, ro_def):
        """Initialize this RO with a set of attributes

        Args:
            session (DBSession):
            ro_def (dict): set of properties to initialize this RO

        Returns:
            None
        """
        loc_def = dict(ro_def)
        # check attributes
        if 'inputs' not in loc_def:
            loc_def['inputs'] = []
        if 'outputs' not in loc_def:
            loc_def['outputs'] = []

        ResearchObject.init(self, session, loc_def)

        # link to interfaces used by this node
        ports = chain(loc_def['inputs'], loc_def['outputs'])
        uids = set(port_def['interface'] for port_def in ports)
        for uid in uids:
            ROLink.connect(session, self.id, uid, 'use')
示例#10
0
    def init(self, session, ro_def):
        """Initialize this RO with a set of attributes

        Args:
            session (DBSession):
            ro_def (dict): set of properties to initialize this RO

        Returns:
            None
        """
        loc_def = dict(ro_def)
        wkf = loc_def.pop('workflow')

        ResearchObject.init(self, session, loc_def)
        self.workflow = wkf

        # link to workflow associated to this provenance
        ROLink.connect(session, self.id, wkf, 'use')

        # link to workflow nodes associated with each process?

        # link to external data consumed
        input_data = set()
        for pexec in ro_def["executions"]:
            for port in pexec['inputs']:
                if port['data'] is not None:
                    input_data.add(port['data'])

        input_ref = set()
        for did in input_data:
            ddef = get_data_def(ro_def, did)
            if ddef['type'] == 'ref':
                input_ref.add(ddef['value'])

        for did in input_ref:
            ROLink.connect(session, self.id, did, 'consume')

        # link to external data produced
        output_data = set()
        for pexec in ro_def["executions"]:
            for port in pexec['outputs']:
                if port['data'] is not None:
                    output_data.add(port['data'])

        output_ref = set()
        for did in output_data:
            ddef = get_data_def(ro_def, did)
            if ddef['type'] == 'ref':
                output_ref.add(ddef['value'])

        for did in output_ref:
            ROLink.connect(session, self.id, did, 'produce')
示例#11
0
def append_ro(request, session, container):
    """Add a new RO in this container.

    Args:
        request (Request):
        session (DBSession):
        container (ROContainer):

    Returns:
        (None|uid): None if something failed, created link
    """
    uid = request.params.get('ro_id', "")
    if len(uid) == 0:
        request.session.flash("Enter a RO id first", 'warning')
        return None

    # check whether the RO is already a member of the container
    if uid == container.id:
        request.session.flash("Can not contain oneself", 'warning')
        return None

    content = [link.target for link in container.out_links if link.type == 'contains']
    if uid in content:
        request.session.flash("%s is already in this container" % uid, 'warning')
        return None

    # check whether uid correspond to a valid RO
    ro = ResearchObject.get(session, uid)
    if ro is None:
        request.session.flash("%s is not a valid RO" % uid, 'warning')
        return None

    # check whether user has 'install' rights on this RO
    role = ro.access_role(session, request.unauthenticated_userid)
    if role < Role.install:
        request.session.flash("You're not allowed to add %s to this container" % uid, 'warning')
        return None

    # create link
    link = ROLink.connect(session, container.id, uid, "contains")
    return link
示例#12
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")
示例#13
0
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')
示例#14
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")
示例#15
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")