Serialize a search result.

        :param pid_fetcher: Persistent identifier fetcher.
        :param search_result: Elasticsearch search result.
        :param links: Dictionary of links to add to response.
541932
def serialize_search(self, pid_fetcher, search_result, links=None,
                         item_links_factory=None, **kwargs):
        
        return json.dumps(dict(
            hits=dict(
                hits=[self.transform_search_hit(
                    pid_fetcher(hit['_id'], hit['_source']),
                    hit,
                    links_factory=item_links_factory,
                    **kwargs
                ) for hit in search_result['hits']['hits']],
                total=search_result['hits']['total'],
            ),
            links=links or {},
            aggregations=search_result.get('aggregations', dict()),
        ), **self._format_args())
508714
def pid(self):
        """"""
        if self.model.status == ReleaseStatus.PUBLISHED and self.record:
            fetcher = current_pidstore.fetchers[
                current_app.config.get('GITHUB_PID_FETCHER')]
            return fetcher(self.record.id, self.record)
1142349
def get(identifier, namespace='cid', domain='compound', operation=None, output='JSON', searchtype=None, **kwargs):
    """"""
    if (searchtype and searchtype != 'xref') or namespace in ['formula']:
        response = request(identifier, namespace, domain, None, 'JSON', searchtype, **kwargs).read()
        status = json.loads(response.decode())
        if 'Waiting' in status and 'ListKey' in status['Waiting']:
            identifier = status['Waiting']['ListKey']
            namespace = 'listkey'
            while 'Waiting' in status and 'ListKey' in status['Waiting']:
                time.sleep(2)
                response = request(identifier, namespace, domain, operation, 'JSON', **kwargs).read()
                status = json.loads(response.decode())
            if not output == 'JSON':
                response = request(identifier, namespace, domain, operation, output, searchtype, **kwargs).read()
    else:
        response = request(identifier, namespace, domain, operation, output, searchtype, **kwargs).read()
    return response
115956
def search_responsify(serializer, mimetype):
    
    def view(pid_fetcher, search_result, code=200, headers=None, links=None,
             item_links_factory=None):
        response = current_app.response_class(
            serializer.serialize_search(pid_fetcher, search_result,
                                        links=links,
                                        item_links_factory=item_links_factory),
            mimetype=mimetype)
        response.status_code = code
        if headers is not None:
            response.headers.extend(headers)

        if links is not None:
            add_link_header(response, links)

        return response

    return view
378590
def serialize_relations(pid):
    """"""
    data = {}
    relations = PIDRelation.get_child_relations(pid).all()
    for relation in relations:
        rel_cfg = resolve_relation_type_config(relation.relation_type)
        dump_relation(rel_cfg.api(relation.parent),
                      rel_cfg, pid, data)
    parent_relations = PIDRelation.get_parent_relations(pid).all()
    rel_cfgs = set([resolve_relation_type_config(p) for p in parent_relations])
    for rel_cfg in rel_cfgs:
        dump_relation(rel_cfg.api(pid), rel_cfg, pid, data)
    return data
230096
def _fetch_result(self):
        
        self._result = self.conn.query_single(self.object_type, self.url_params, self.query_params)
1005870
def get_data(doi_id):
    

    fetch_dict = {}

    try:
        # Send request to grab metadata at URL
        print("Requesting : {}".format(doi_id))
        url = "http://dx.doi.org/" + doi_id
        headers = {"accept": "application/rdf+xml;q=0.5, application/citeproc+json;q=1.0"}
        r = requests.get(url, headers=headers)


        # DOI 404. Data not retrieved. Log and return original pub
        if r.status_code == 400 or r.status_code == 404:
            print("HTTP 404: DOI not found, {}".format(doi_id))

        # Ignore other status codes. Run when status is 200 (good response)
        elif r.status_code == 200:
            # Load data from http response
            raw = json.loads(r.text)
            # Create a new pub dictionary with metadata received
            fetch_dict = compile_fetch(raw, doi_id)
            fetch_dict['pubDataUrl'] = 'doi.org'

    except urllib.error.URLError as e:
        fetch_dict["ERROR"] = e
        fetch_dict["doi"] = doi_id
        print("get_data: URLError: malformed doi: {}, {}".format(doi_id, e))
    except Exception as e:
        fetch_dict["ERROR"] = e
        fetch_dict["doi"] = doi_id
        print("get_data: ValueError: cannot resolve dois from this publisher: {}, {}".format(doi_id, e))
    return fetch_dict
517926
def get_search_results(portal_type=None, uid=None, **kw):
    

    # If we have an UID, return the object immediately
    if uid is not None:
        logger.info("UID '%s' found, returning the object immediately" % uid)
        return u.to_list(get_object_by_uid(uid))

    # allow to search search for the Plone Site with portal_type
    include_portal = False
    if u.to_string(portal_type) == "Plone Site":
        include_portal = True

    # The request may contain a list of portal_types, e.g.
    # `?portal_type=Document&portal_type=Plone Site`
    if "Plone Site" in u.to_list(req.get("portal_type")):
        include_portal = True

    # Build and execute a catalog query
    results = search(portal_type=portal_type, uid=uid, **kw)

    if include_portal:
        results = list(results) + u.to_list(get_portal())

    return results
1189497
def query(self, query):
    ''''''
    results = super(SymlinkDatastore, self).query(query)
    return self._follow_link_gen(results)
462121
def get_result_files(self, result):
        
        if isinstance(result, dict):
            result_id = result['meta']['id']
        else:  # Should already be a string containing the id
            result_id = result

        result_data_dir = os.path.join(self.get_data_dir(), result_id)

        filenames = next(os.walk(result_data_dir))[2]

        filename_path_pairs = [
            (f, os.path.join(self.get_data_dir(), result_id, f))
            for f in filenames]

        return {k: v for k, v in filename_path_pairs}
33086
def fetch(elastic, backend, limit=None, search_after_value=None, scroll=True):
    

    logging.debug("Creating a elastic items generator.")

    elastic_scroll_id = None
    search_after = search_after_value

    while True:
        if scroll:
            rjson = get_elastic_items(elastic, elastic_scroll_id, limit)
        else:
            rjson = get_elastic_items_search(elastic, search_after, limit)

        if rjson and "_scroll_id" in rjson:
            elastic_scroll_id = rjson["_scroll_id"]

        if rjson and "hits" in rjson:
            if not rjson["hits"]["hits"]:
                break
            for hit in rjson["hits"]["hits"]:
                item = hit['_source']
                if 'sort' in hit:
                    search_after = hit['sort']
                try:
                    backend._fix_item(item)
                except Exception:
                    pass
                yield item
        else:
            logging.error("No results found from %s", elastic.index_url)
            break

    return
916082
def fetch_model_data(model_querysets, model_ids_to_fetch):
    
    return {
        model: id_dict(model_querysets[model].filter(id__in=ids_to_fetch))
        for model, ids_to_fetch in model_ids_to_fetch.items()
    }
766886
def serialize(self, pid, record, links_factory=None):
        
        return simpledc.tostring(
            self.transform_record(pid, record, links_factory))
607131
def persistent_load(self, pid):
        
        try:
            type_tag, key_id = pid
        except Exception as exc:
            # Sometimes we get a string such as ('Element', u'C') instead
            # of a real tuple. Use ast to evalute the expression (much safer
            # than eval).
            import ast
            type_tag, key_id = ast.literal_eval(pid)

        if type_tag == "Element":
            return Element(key_id)
        else:
            # Always raises an error if you cannot return the correct object.
            # Otherwise, the unpickler will think None is the object referenced
            # by the persistent ID.
            raise pickle.UnpicklingError(
                "unsupported persistent object with pid %s" % pid)
1281701
def paged_search_ext_s(self, base, scope, filterstr='(objectClass=*)', attrlist=None, attrsonly=0,
                           serverctrls=None, clientctrls=None, timeout=-1, sizelimit=0):
        
        req_ctrl = SimplePagedResultsControl(True, size=self.conf_LDAP_SYNC_BIND_PAGESIZE, cookie='')

        # Send first search request
        msgid = self.search_ext(base, ldap.SCOPE_SUBTREE, filterstr, attrlist=attrlist,
                                serverctrls=(serverctrls or []) + [req_ctrl])
        results = []

        while True:
            rtype, rdata, rmsgid, rctrls = self.result3(msgid)
            results.extend(rdata)
            # Extract the simple paged results response control
            pctrls = [c for c in rctrls if c.controlType == SimplePagedResultsControl.controlType]

            if pctrls:
                if pctrls[0].cookie:
                    # Copy cookie from response control to request control
                    req_ctrl.cookie = pctrls[0].cookie
                    msgid = self.search_ext(base, ldap.SCOPE_SUBTREE, filterstr, attrlist=attrlist,
                                            serverctrls=(serverctrls or []) + [req_ctrl])
                else:
                    break

        return results
873210
def _get_search_result(self, query_url, **query_params):
        
        param_q = query_params.get('q')
        param_query = query_params.get('query')
        # Either q or query parameter is required
        if bool(param_q) == bool(param_query):
            raise CloudantArgumentError(104, query_params)

        # Validate query arguments and values
        for key, val in iteritems_(query_params):
            if key not in list(SEARCH_INDEX_ARGS.keys()):
                raise CloudantArgumentError(105, key)
            if not isinstance(val, SEARCH_INDEX_ARGS[key]):
                raise CloudantArgumentError(106, key, SEARCH_INDEX_ARGS[key])
        # Execute query search
        headers = {'Content-Type': 'application/json'}
        resp = self.r_session.post(
            query_url,
            headers=headers,
            data=json.dumps(query_params, cls=self.client.encoder)
        )
        resp.raise_for_status()
        return response_to_json_dict(resp)
1297986
def data(link):
    ''''''
    link = _remove_api_url_from_link(link)
    req = _get_from_dapi_or_mirror(link)
    return _process_req(req)