Beispiel #1
0
    def _build_query(self, query_dict, limit=None, offset=None, shards=None):
        if shards is not None:
            if self._available_shards is None:
                self._load_available_shards()

            shard_specs = []
            for shard in shards:
                if shard not in self._available_shards:
                    raise EsgfSearchException('Shard %s is not available' % shard)
                else:
                    for port, suffix in self._available_shards[shard]:
                        # suffix should be ommited when querying
                        shard_specs.append('%s:%s/solr' % (shard, port))

            shard_str = ','.join(shard_specs)
        else:
            shard_str = None

        full_query = MultiDict({
            'format': RESPONSE_FORMAT,
            'limit': limit,
            'distrib': 'true' if self.distrib else 'false',
            'offset': offset,
            'shards': shard_str,
        })
        full_query.extend(query_dict)

        # Remove all None valued items
        full_query = MultiDict(item for item in full_query.items() if item[1] is not None)

        return full_query
    def _build_query(self, query_dict, limit=None, offset=None, shards=None):
        if shards is not None:
            if self._available_shards is None:
                self._load_available_shards()

            shard_specs = []
            for shard in shards:
                if shard not in self._available_shards:
                    raise EsgfSearchException('Shard %s is not available' %
                                              shard)
                else:
                    for port, suffix in self._available_shards[shard]:
                        # suffix should be ommited when querying
                        shard_specs.append('%s:%s/solr' % (shard, port))

            shard_str = ','.join(shard_specs)
        else:
            shard_str = None

        full_query = MultiDict({
            'format': RESPONSE_FORMAT,
            'limit': limit,
            'distrib': 'true' if self.distrib else 'false',
            'offset': offset,
            'shards': shard_str,
        })
        full_query.extend(query_dict)

        # Remove all None valued items
        full_query = MultiDict(item for item in full_query.items()
                               if item[1] is not None)

        return full_query
Beispiel #3
0
    def send_query(self, query_dict, limit=None, offset=None):
        """
        Generally not to be called directly by the user but via SearchContext
	instances.
        
        :param query_dict: dictionary of query string parameers to send.
        :return: ElementTree instance (TODO: think about this)
        
        """
        
        full_query = MultiDict({
            'format': RESPONSE_FORMAT,
            'limit': limit,
            'distrib': 'true' if self.distrib else 'false',
            'offset': offset,
            'shards': ','.join(self.shards) if self.shards else None,
            })
        full_query.extend(query_dict)

        # Remove all None valued items
        full_query = MultiDict(item for item in full_query.items() if item[1] is not None)


        query_url = '%s?%s' % (self.url, urllib.urlencode(full_query))
        log.debug('Query request is %s' % query_url)

        response = urllib2.urlopen(query_url)
        ret = json.load(response)

        return ret
Beispiel #4
0
    def send_query(self, query_dict, limit=None, offset=None):
        """
        Generally not to be called directly by the user but via SearchContext
	instances.
        
        :param query_dict: dictionary of query string parameers to send.
        :return: ElementTree instance (TODO: think about this)
        
        """

        full_query = MultiDict({
            'format':
            RESPONSE_FORMAT,
            'limit':
            limit,
            'distrib':
            'true' if self.distrib else 'false',
            'offset':
            offset,
            'shards':
            ','.join(self.shards) if self.shards else None,
        })
        full_query.extend(query_dict)

        # Remove all None valued items
        full_query = MultiDict(item for item in full_query.items()
                               if item[1] is not None)

        query_url = '%s?%s' % (self.url, urllib.urlencode(full_query))
        log.debug('Query request is %s' % query_url)

        response = urllib2.urlopen(query_url)
        ret = json.load(response)

        return ret
Beispiel #5
0
def convert_constraints(url):
    """
    converts esgf search query to constraints parameter.
    TODO: constraints parameter should have the same structure as the esgf query.
    """
    # FROM: project=CMIP5&time_frequency=mon&variable=tas,tasmax,tasmin
    # TO: project:CORDEX,experiment:historical,experiment:rcp26
    parsed_url = urlparse(url)
    constraints = MultiDict()
    for qpart in parsed_url.query.split('&'):
        key, value = qpart.split('=')
        for val in value.split(','):
            constraints.add(key.strip(), val.strip())
    converted = ','.join(["{0[0]}:{0[1]}".format(c) for c in constraints.items()])
    return converted
Beispiel #6
0
def convert_constraints(url):
    """
    converts esgf search query to constraints parameter.
    TODO: constraints parameter should have the same structure as the esgf query.
    """
    # FROM: project=CMIP5&time_frequency=mon&variable=tas,tasmax,tasmin
    # TO: project:CORDEX,experiment:historical,experiment:rcp26
    parsed_url = urlparse(url)
    constraints = MultiDict()
    for qpart in parsed_url.query.split('&'):
        key, value = qpart.split('=')
        for val in value.split(','):
            constraints.add(key.strip(), val.strip())
    converted = ','.join(
        ["{0[0]}:{0[1]}".format(c) for c in constraints.items()])
    return converted