def getDatafileFormat(client, case):
    l = case['dfformat'].split(':')
    query = Query(client, "DatafileFormat", conditions={
        "name": "= '%s'" % l[0],
    })
    if len(l) > 1:
        query.addConditions({"version": "= '%s'" % l[1]})
    return (client.assertedSearch(query)[0])
Exemple #2
0
    def searchUniqueKey(self, key, objindex=None):
        """Search the object that belongs to a unique key.

        This is in a sense the inverse method to
        :meth:`icat.entity.Entity.getUniqueKey`, the key must
        previously have been generated by it.  This method searches
        the Entity object that the key has been generated for from the
        server.

        if objindex is not :const:`None`, it is used as a cache of
        previously retrieved objects.  It must be a dict that maps
        keys to Entity objects.  The object retrieved by this method
        call will be added to this index.

        This method uses the JPQL inspired query syntax introduced
        with ICAT 4.3.0.  It won't work with older ICAT servers.

        :param key: the unique key of the object to search for.
        :type key: :class:`str`
        :param objindex: cache of Entity objects.
        :type objindex: :class:`dict`
        :return: the object corresponding to the key.
        :rtype: :class:`icat.entity.Entity`
        :raise SearchResultError: if the object has not been found.
        :raise ValueError: if the key is not well formed.
        :raise VersionMethodError: if connected to an ICAT server
            older then 4.3.0.
        """

        if self.apiversion < '4.3':
            raise VersionMethodError("searchUniqueKey", self.apiversion)
        if objindex is not None and key in objindex:
            return objindex[key]
        us = key.index('_')
        beanname = key[:us]
        av = parse_attr_val(key[us+1:])
        info = self.getEntityInfo(beanname)
        query = Query(self, beanname)
        for f in info.fields:
            if f.name in av.keys():
                attr = f.name
                if f.relType == "ATTRIBUTE":
                    cond = "= '%s'" % simpleqp_unquote(av[attr])
                    query.addConditions({attr:cond})
                elif f.relType == "ONE":
                    rk = str("%s_%s" % (f.type, av[attr]))
                    ro = self.searchUniqueKey(rk, objindex)
                    query.addConditions({"%s.id" % attr:"= %d" % ro.id})
                else:
                    raise ValueError("malformed '%s': invalid attribute '%s'" 
                                     % (key, attr))
        obj = self.assertedSearch(query)[0]
        if objindex is not None:
            objindex[key] = obj
        return obj
Exemple #3
0
def wipe_data(client, dsquery):
    """Delete all datafiles from IDS relating to datasets matching the query.

    The argument dsquery must be a Query object searching for
    datasets.  All datafiles relating to corresponding datasets must
    have been uploaded to ids.server
    (Issue icatproject/ids.server #61).
    """
    require_ids_version("1.6.0", "Issue #42")
    if dsquery.entity.BeanName != 'Dataset':
        raise ValueError("Invalid query '%s'" % query)

    dfquery = Query(client,
                    "Datafile",
                    conditions={"location": "IS NOT NULL"},
                    limit=(0, 1))
    for a, c in dsquery.conditions.items():
        dfquery.addConditions({"dataset.%s" % a: c})

    while True:
        deleteDatasets = []
        restoreDatasets = []
        for ds in client.searchChunked(dsquery):
            status = client.ids.getStatus(DataSelection([ds]))
            if status == "ONLINE":
                deleteDatasets.append(ds)
                if len(deleteDatasets) >= 25:
                    try:
                        client.deleteData(deleteDatasets)
                        client.deleteMany(deleteDatasets)
                    except icat.IDSDataNotOnlineError:
                        pass
                    deleteDatasets = []
            elif status == "ARCHIVED":
                if len(restoreDatasets) < 25:
                    restoreDatasets.append(ds)
        if len(deleteDatasets) > 0:
            try:
                client.deleteData(deleteDatasets)
                client.deleteMany(deleteDatasets)
            except icat.IDSDataNotOnlineError:
                pass
        if len(restoreDatasets) > 0:
            client.ids.restore(DataSelection(restoreDatasets))
        client.autoRefresh()
        # If any Datafile is left we need to continue the loop.
        if client.search(dfquery):
            time.sleep(60)
        else:
            break
Exemple #4
0
def test_query_condition_list(client):
    """We may also add a list of conditions on a single attribute.
    """
    condition = {"datafileCreateTime": [">= '2012-01-01'", "< '2013-01-01'"]}
    query = Query(client, "Datafile", conditions=condition)
    print(str(query))
    qstr = str(query)
    res = client.search(query)
    assert len(res) == 3

    # The last example also works by adding the conditions separately.
    query = Query(client, "Datafile")
    query.addConditions({"datafileCreateTime": ">= '2012-01-01'"})
    query.addConditions({"datafileCreateTime": "< '2013-01-01'"})
    print(str(query))
    assert str(query) == qstr
    res = client.search(query)
    assert len(res) == 3
def test_query_condition_list(client):
    """We may also add a list of conditions on a single attribute.
    """
    condition = {"datafileCreateTime": [">= '2012-01-01'", "< '2013-01-01'"]}
    query = Query(client, "Datafile", conditions=condition)
    print(str(query))
    qstr = str(query)
    res = client.search(query)
    assert len(res) == 3

    # The last example also works by adding the conditions separately.
    query = Query(client, "Datafile")
    query.addConditions({"datafileCreateTime": ">= '2012-01-01'"})
    query.addConditions({"datafileCreateTime": "< '2013-01-01'"})
    print(str(query))
    assert str(query) == qstr
    res = client.search(query)
    assert len(res) == 3
Exemple #6
0
 def _get_investigation(invid):
     l = invid.split(':')
     query = Query(client, "Investigation")
     if len(l) == 1:
         # No colon, invid == name
         query.addConditions({
             "name": "='%s'" % l[0],
         })
     elif len(l) == 2:
         # one colon, invid == name:visitId
         query.addConditions({
             "name": "= '%s'" % l[0],
             "visitId": "= '%s'" % l[1],
         })
     else:
         # too many colons
         raise ValueError("Invalid investigation identifier '%s'" % invid)
     return client.assertedSearch(query)[0]
Exemple #7
0
    def searchMatching(self, obj, includes=None):
        """Search the matching object.

        Search the object from the ICAT server that matches the given
        object in the uniqueness constraint.

        >>> dataset = client.new("dataset", investigation=inv, name=dsname)
        >>> dataset = client.searchMatching(dataset)
        >>> dataset.id
        172383L

        :param obj: an entity object having the attrinutes for the
            uniqueness constraint set accordingly.
        :type obj: :class:`icat.entity.Entity`
        :param includes: list of related objects to add to the INCLUDE
            clause of the search query.
            See :meth:`icat.query.Query.addIncludes` for details.
        :type includes: iterable of :class:`str`
        :return: the corresponding object.
        :rtype: :class:`icat.entity.Entity`
        :raise SearchResultError: if the object has not been found.
        :raise ValueError: if the object's class does not have a
            uniqueness constraint or if any attribute needed for the
            constraint is not set.
        """
        if 'id' in obj.Constraint:
            raise ValueError("%s does not have a uniqueness constraint.")
        query = Query(self, obj.BeanName, includes=includes)
        for a in obj.Constraint:
            v = getattr(obj, a)
            if v is None:
                raise ValueError("%s is not set" % a)
            if a in obj.InstAttr:
                query.addConditions({a: "= '%s'" % v})
            elif a in obj.InstRel:
                query.addConditions({"%s.id" % a: "= %d" % v.id})
            else:
                raise InternalError("Invalid constraint '%s' in %s."
                                    % (a, obj.BeanName))
        return self.assertedSearch(query)[0]
Exemple #8
0
print("\nOther relations then equal may be used in the conditions too.")
condition = {"datafileCreateTime":">= '2012-01-01'"}
q = Query(client, "Datafile", conditions=condition)
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nWe may also add a list of conditions on a single attribute.")
condition = {"datafileCreateTime":[">= '2012-01-01'", "< '2013-01-01'"]}
q = Query(client, "Datafile", conditions=condition)
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nThe last example also works by adding the conditions separately.")
q = Query(client, "Datafile")
q.addConditions({"datafileCreateTime":">= '2012-01-01'"})
q.addConditions({"datafileCreateTime":"< '2013-01-01'"})
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nUsing \"id in (i)\" rather then \"id = i\" also works.")
print("(This may be needed to work around ICAT Issue 149.)")
q = Query(client, "Investigation", conditions={"id":"in (%d)" % invid})
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nRule does not have a constraint, id is included in the natural order.")
q = Query(client, "Rule", order=True)
print(str(q))
print("%d result(s)" % len(client.search(q)))
Exemple #9
0
print("\nOther relations then equal may be used in the conditions too.")
condition = {"datafileCreateTime": ">= '2012-01-01'"}
q = Query(client, "Datafile", conditions=condition)
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nWe may also add a list of conditions on a single attribute.")
condition = {"datafileCreateTime": [">= '2012-01-01'", "< '2013-01-01'"]}
q = Query(client, "Datafile", conditions=condition)
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nThe last example also works by adding the conditions separately.")
q = Query(client, "Datafile")
q.addConditions({"datafileCreateTime": ">= '2012-01-01'"})
q.addConditions({"datafileCreateTime": "< '2013-01-01'"})
print(str(q))
print("%d result(s)" % len(client.search(q)))

print("\nUsing \"id in (i)\" rather then \"id = i\" also works.")
print("(This may be needed to work around ICAT Issue 149.)")
q = Query(client, "Investigation", conditions={"id": "in (%d)" % invid})
print(str(q))
print("%d result(s)" % len(client.search(q)))

print(
    "\nRule does not have a constraint, id is included in the natural order.")
q = Query(client, "Rule", order=True)
print(str(q))
print("%d result(s)" % len(client.search(q)))