How to use the synapseclient.entity.Entity function in synapseclient

To help you get started, we’ve selected a few synapseclient examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Sage-Bionetworks / synapsePythonClient / tests / unit / unit_test_Entity.py View on Github external
assert_equals(folder.concreteType, 'org.sagebionetworks.repo.model.Folder')
    assert_equals(folder.__class__, Folder)
    assert_equals(folder.name, 'Testing123')
    assert_equals(folder.testing, 123)

    # In case of unknown concreteType, fall back on generic Entity object
    props = {
        "id": "syn123456",
        "concreteType": "org.sagebionetworks.repo.model.DoesntExist",
        "parentId": "syn445566",
        "name": "Whatsits"
    }
    whatsits = Entity.create(props)

    assert_equals(whatsits.concreteType, 'org.sagebionetworks.repo.model.DoesntExist')
    assert_equals(whatsits.__class__, Entity)
github Sage-Bionetworks / synapsePythonClient / tests / unit / unit_test_Entity.py View on Github external
def test_Entity():
    # Test the basics of creating and accessing properties on an entity
    for i in range(2):
        e = Entity(name='Test object', description='I hope this works',
                   annotations=dict(foo=123, nerds=['chris', 'jen', 'janey'], annotations='How confusing!'),
                   properties=dict(annotations='/repo/v1/entity/syn1234/annotations',
                                   md5='cdef636522577fc8fb2de4d95875b27c', parentId='syn1234'),
                   concreteType='org.sagebionetworks.repo.model.Data')

        # Should be able to create an Entity from an Entity
        if i == 1:
            e = Entity.create(e)
            
        assert_equals(e.parentId, 'syn1234')
        assert_equals(e['parentId'], 'syn1234')
        assert_equals(e.properties['parentId'], 'syn1234')
        assert_equals(e.properties.parentId, 'syn1234')

        assert_equals(e.foo, 123)
        assert_equals(e['foo'], 123)
github Sage-Bionetworks / synapsePythonClient / tests / unit / unit_test_Entity.py View on Github external
def test_subclassing():
    """Test ability to subclass and add a member variable"""
    
    # define a subclass of Entity to make sure subclassing and creating
    # a new member variable works
    class FoobarEntity(Entity):
        def __init__(self, x):
            self.__dict__['x'] = x

    foobar = FoobarEntity(123)
    assert_equals(foobar.x, 123)
    assert_in('x', foobar.__dict__)
    assert_equals(foobar.__dict__['x'], 123)
    foobar.id = 'syn999'
    assert_equals(foobar.properties['id'], 'syn999')
    foobar.n00b = 'henry'
    assert_equals(foobar.annotations['n00b'], 'henry')
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
See :py:func:`synapseclient.Synapse._getEntityBundle`.
        See :py:mod:`synapseclient.Entity`.
        """
        
        # Note: This version overrides the version of 'entity' (if the object is Mappable)
        version = kwargs.get('version', None)
        downloadFile = kwargs.get('downloadFile', True)
        downloadLocation = kwargs.get('downloadLocation', None)
        ifcollision = kwargs.get('ifcollision', 'keep.both')
        submission = kwargs.get('submission', None)

        # Make a fresh copy of the Entity
        local_state = entity.local_state() if entity and isinstance(entity, Entity) else None
        properties = entityBundle['entity']
        annotations = from_synapse_annotations(entityBundle['annotations'])
        entity = Entity.create(properties, annotations, local_state)

        # Handle both FileEntities and Locationables
        isLocationable = is_locationable(entity)
        if isinstance(entity, File) or isLocationable:
            fileName = entity['name']
            
            if not isLocationable:
                # Fill in information about the file, even if we don't download it
                # Note: fileHandles will be an empty list if there are unmet access requirements
                for handle in entityBundle['fileHandles']:
                    if handle['id'] == entityBundle['entity']['dataFileHandleId']:
                        entity.md5 = handle.get('contentMd5', '')
                        entity.fileSize = handle.get('contentSize', None)
                        entity.contentType = handle.get('contentType', None)
                        fileName = handle['fileName']
                        if handle['concreteType'] == 'org.sagebionetworks.repo.model.file.ExternalFileHandle':
github Sage-Bionetworks / synapsePythonClient / synapseclient / entity.py View on Github external
def __init__(self, targetId=None, targetVersion=None, parent=None, properties=None, annotations=None,
                 local_state=None, **kwargs):
        if targetId is not None and targetVersion is not None:
            kwargs['linksTo'] = dict(targetId=utils.id_of(targetId), targetVersionNumber=targetVersion)
        elif targetId is not None and targetVersion is None:
            kwargs['linksTo'] = dict(targetId=utils.id_of(targetId))
        elif properties is not None and 'linksTo' in properties:
            pass
        else:
            raise SynapseMalformedEntityError("Must provide a target id")
        super(Link, self).__init__(concreteType=Link._synapse_entity_type, properties=properties,
                                   annotations=annotations, local_state=local_state, parent=parent, **kwargs)


class File(Entity, Versionable):
    """
    Represents a file in Synapse.

    When a File object is stored, the associated local file or its URL will be stored in Synapse. A File must have a
    path (or URL) and a parent.

    :param path:                Location to be represented by this File
    :param name:                Name of the file in Synapse, not to be confused with the name within the path
    :param parent:              Project or Folder where this File is stored
    :param synapseStore:        Whether the File should be uploaded or if only the path should be stored when
                                :py:func:`synapseclient.Synapse.store` is called on the File object.
                                Defaults to True (file should be uploaded)
    :param contentType:         Manually specify Content-type header, for example "application/png" or
                                "application/json; charset=UTF-8"
    :param dataFileHandleId:    Defining an existing dataFileHandleId will use the existing dataFileHandleId
                                The creator of the file must also be the owner of the dataFileHandleId to have
github Sage-Bionetworks / synapsePythonClient / synapseclient / client.py View on Github external
if activity is not None:
                ## TODO: move this argument check closer to the front of the method
                raise SynapseProvenanceError('Provenance can be specified as an Activity object or as used/executed item(s), but not both.')
            activityName = kwargs.get('activityName', None)
            activityDescription = kwargs.get('activityDescription', None)
            activity = Activity(name=activityName, description=activityDescription, used=used, executed=executed)

        # If we have an Activity, set it as the Entity's provenance record
        if activity:
            activity = self.setProvenance(properties, activity)
            
            # 'etag' has changed, so get the new Entity
            properties = self._getEntity(properties)

        # Return the updated Entity object
        return Entity.create(properties, annotations, local_state)
github Sage-Bionetworks / synapsePythonClient / synapseclient / entity.py View on Github external
:param local_state: Internal use only
        """

        # Create a new Entity using an existing Entity as a prototype
        if isinstance(properties, Entity):
            if annotations is None:
                annotations = {}
            if local_state is None:
                local_state = {}
            annotations.update(properties.annotations)
            local_state.update(properties.local_state())
            properties = properties.properties
            if 'id' in properties:
                del properties['id']

        if cls == Entity \
                and 'concreteType' in properties \
                and properties['concreteType'] in _entity_type_to_class:
            cls = _entity_type_to_class[properties['concreteType']]
        return cls(properties=properties, annotations=annotations, local_state=local_state)
github Sage-Bionetworks / synapsePythonClient / synapseclient / entity.py View on Github external
Create an Entity or a subclass given dictionaries of properties and annotations, as might be received from the
        Synapse Repository.

        :param properties:  A map of Synapse properties

            If 'concreteType' is defined in properties, we create the proper subclass of Entity. If not, give back the
            type whose constructor was called:

            If passed an Entity as input, create a new Entity using the input entity as a prototype.

        :param annotations: A map of user defined annotations
        :param local_state: Internal use only
        """

        # Create a new Entity using an existing Entity as a prototype
        if isinstance(properties, Entity):
            if annotations is None:
                annotations = {}
            if local_state is None:
                local_state = {}
            annotations.update(properties.annotations)
            local_state.update(properties.local_state())
            properties = properties.properties
            if 'id' in properties:
                del properties['id']

        if cls == Entity \
                and 'concreteType' in properties \
                and properties['concreteType'] in _entity_type_to_class:
            cls = _entity_type_to_class[properties['concreteType']]
        return cls(properties=properties, annotations=annotations, local_state=local_state)