示例#1
0
def primitive_read(query, credentials=credentials, escape=escape, cursor=False):
   # Put the query in an envelope
   envelope = {'query':query, "cursor":cursor}

   # Add escape if needed
#    if escape != 'html':
#        envelope['escape'] = False #        if not escape else escape

   # Encode the result
   encoded = urllib.urlencode({'query': json.encode(envelope)})

   # Build the URL and create a Request object for it
   url = 'http://%s%s?%s' % (host, readservice, encoded)
   headers = {}
   if credentials is not None: headers["Cookie"] = credentials
   resp = fetch(url, method=GET, headers=headers)
   inner = json.decode(resp.content)      # Parse JSON response to an object

   # If anything was wrong with the invocation, mqlread will return an HTTP
   # error, and the code above with raise urllib2.HTTPError.
   # If anything was wrong with the query, we won't get an HTTP error, but
   # will get an error status code in the response envelope.  In this case
   # we raise our own MQLError exception.
   if not inner['code'].startswith('/api/status/ok'):
       if debug: 
           print "\n".join(map(str, q, inner, resp.headers['X-Metaweb-Cost'], resp.headers['X-Metaweb-TID']))
       error = inner['messages'][0]
       raise MQLError('%s: %s' % (error['code'], error['message']), error['message'])

   # If there was no error, then just return the result from the envelope
   return inner
示例#2
0
def _run_query(query):
    query = json.decode(query)
    query[0]["/common/topic/image"] = {"limit":1, "id":None}
    results = readiter(query)
    datables = []
    for result in results:
        types = result["type"]
        if type(types) is not list: types = [types]
        for tipe in types:
            datable = make_datable(result, tipe)
            if datable is not None: datables.append(datable)
    datables.sort()
    return datables
示例#3
0
 def pretty_query(self):
     return json.encode(json.decode(self.query))