Example #1
0
def get_resource_history(col, resource):
    uid = doc_or_uid_to_uid(resource)
    cursor = col.find({'resource': uid})
    for doc in cursor:
        for k in ['new', 'old']:
            d = doc[k]
            d.pop('_id', None)
            d['id'] = d['uid']
            doc[k] = Document('resource', d)
        doc.pop('_id', None)
        yield Document('update', doc)
Example #2
0
def _find_local(fname, qparams, as_doct=False):
    """Find a document created using the local framework
    Parameters
    -----------
    fname: str
        Name of the query should be run
    qparams: dict
        Query parameters. Similar to online query methods

    Yields
    ------------
    c: doct.Document, StopIteration
        Result of the query if found

    """
    res_list = []
    try:
        with open(fname, 'r') as fp:
            local_payload = ujson.load(fp)
        qobj = mongoquery.Query(qparams)
        for i in local_payload:
            if qobj.match(i):
                res_list.append(i)
    except FileNotFoundError:
        raise RuntimeWarning('Local file {} does not exist'.format(fname))
    if as_doct:
        for c in res_list:
            yield Document(fname.split('.')[0], c)
    else:
        for c in res_list:
            yield c
Example #3
0
    def find(self, as_document=False, **kwargs):
        """Given a set of mongo search parameters, return a requests iterator

        Parameters
        -----------
        as_document: bool
            Format return type to doct.Document if set

        Yields
        ----------
        dict, doct.Document
            Result of the query

        Raises
        ---------
        StopIteration, requests.exceptions.HTTPError
            When nothing found or something is wrong on the server side. If server error occurs,
            a human friendly message is returned.

        """
        content = _get(self._req_url, kwargs)
        if as_document:
            for c in content:
                yield Document('Request', c)
        else:
            for c in content:
                yield c
Example #4
0
def test_find_sample_as_doc():
    m_sample = dict(name='comp_sam', uid=str(uuid.uuid4()),
                    time=ttime.time(), owner='arkilic', project='trial',
                    beamline_id='trial_b', container='legion1')
    s1 = SampleReference(host=TESTING_CONFIG['host'],
                         port=TESTING_CONFIG['port'])
    s1.create(**m_sample)
    s_ret = next(s1.find(uid=m_sample['uid'], as_document=True))
    assert s_ret == Document('Sample', m_sample)
Example #5
0
 def find(self, as_document=False, **kwargs):
     """Given a set of MongoDB search parameters, return a requests iterator"""
     content = _get(self._cont_url, kwargs)
     if as_document:
         for c in content:
             yield Document('container', c)
     else:
         for c in content:
             yield c
Example #6
0
def get_document(url, doc_type, as_json, contents):
    r = requests.get(url, params=ujson.dumps(contents))
    r.raise_for_status()
    content = ujson.loads(r.text)
    if as_json:
        return r.text
    else:
        for c in content:
            yield Document(doc_type, c)
def test_configuration_find(config_ref):
    config_data = dict(beamline_id='test_bl',
                       uid=str(uuid.uuid4()),
                       active=True,
                       time=ttime.time(),
                       key='test_config',
                       params=dict(param1='test1', param2='test2'))

    config_ref.create(**config_data)
    c_ret = next(config_ref.find(uid=config_data['uid'], as_document=True))
    assert c_ret == Document('Configuration', config_data)
Example #8
0
def test_find_sample_as_doc(conn=conn):
    m_sample = dict(name='comp_sam',
                    uid=str(uuid.uuid4()),
                    time=ttime.time(),
                    owner='arkilic',
                    project='trial',
                    beamline_id='trial_b',
                    container='legion1')
    conn.create_sample(**m_sample)
    s_ret = conn.find_sample(uid=m_sample['uid'], as_document=True)
    assert s_ret[0] == Document('Sample', m_sample)
Example #9
0
def insert_resource(col, spec, resource_path, resource_kwargs, known_spec,
                    root):
    resource_kwargs = dict(resource_kwargs)
    if spec in known_spec:
        js_validate(resource_kwargs, known_spec[spec]['resource'])

    resource_object = dict(spec=str(spec),
                           resource_path=str(resource_path),
                           root=str(root),
                           resource_kwargs=resource_kwargs,
                           uid=str(uuid.uuid4()))

    col.insert_one(resource_object)
    # maintain back compatibility
    resource_object['id'] = resource_object['uid']
    resource_object.pop('_id', None)
    return Document('resource', resource_object)
Example #10
0
    def find(self, as_document=False, **kwargs):
        """
        Parameters
        ----------
        as_document: bool
            Formats output to doct.Document if True

        Yields
        ------
        c : dict, doct.Document
            Documents which have all keys with the given values

        """
        content = _get(self._samp_url, params=kwargs)
        if as_document:
            for c in content:
                yield Document('Sample', c)
        else:
            for c in content:
                yield c
Example #11
0
def insert_datum(col, resource, datum_id, datum_kwargs, known_spec,
                 resource_col):
    try:
        resource['spec']
    except (AttributeError, TypeError):
        resource = resource_col.find_one({'uid': resource})

    spec = resource['spec']

    if spec in known_spec:
        js_validate(datum_kwargs, known_spec[spec]['datum'])

    datum = dict(resource=resource['uid'],
                 datum_id=str(datum_id),
                 datum_kwargs=dict(datum_kwargs))

    col.insert_one(datum)
    # do not leak mongo objectID
    datum.pop('_id', None)

    return Document('datum', datum)
Example #12
0
    def find(self, as_document=False, active_only=True, **kwargs):
        """
        Parameters
        ----------
        as_document: bool
            Formats output to doct.Document if True
        active_only: bool
            Retrieve only active configurations. Default is True

        Yields
        ------
        c: dict, doct.Document
            Documents which have all keys with the given values

        """
        kwargs['active_only'] = active_only
        content = _get(self._conf_url, params=kwargs)
        if as_document:
            for c in content:
                yield Document('Configuration', c)
        else:
            for c in content:
                yield c
Example #13
0
def resource_given_uid(col, resource):
    uid = doc_or_uid_to_uid(resource)
    ret = col.find_one({'uid': uid})
    ret.pop('_id', None)
    ret['id'] = ret['uid']
    return Document('resource', ret)