Exemple #1
0
def make_query(store, query, prefix=""):
    """Creates a query object from query dict.
        >>> store = common.create_test_store()
        >>> make_query(store, {'type': '/type/page'})
        <query: ['type = ref:/type/page']>
        >>> make_query(store, {'type': '/type/page', 'title~': 'foo', 'life': 42})
        <query: ['life = int:42', 'type = ref:/type/page', 'title ~ str:foo']>
        >>> make_query(store, {'type': '/type/page', 'title~': 'foo', 'a:life<': 42, "b:life>": 420})        
        <query: ['life < int:42', 'type = ref:/type/page', 'title ~ str:foo', 'life > int:420']>
    """
    query = common.parse_query(query)
    q = Query()
    q.prefix = prefix
    q.offset = common.safeint(query.pop('offset', None), 0)
    q.limit = common.safeint(query.pop('limit', 20), 20)
    if q.limit > 1000:
        q.limit = 1000
    sort = query.pop('sort', None)

    nested = (prefix != "")

    for k, v in query.items():
        # key foo can also be written as label:foo
        k = k.split(':')[-1]
        if v is None:
            q.requested[k] = v
        elif isinstance(v, dict):
            # make sure op is ==
            v = dict((k + '.' + key, value) for key, value in v.items())
            q2 = make_query(store, v, prefix=prefix + k + ".")
            #@@ Anand: Quick-fix
            # dbstore.things looks for key to find whether type is required or not.
            q2.key = k
            if q2.conditions:
                q.conditions.append(q2)
            else:
                q.requested[k] = q2
        else:
            k, op = parse_key(k)
            q.add_condition(k, op, None, v)

    if not nested:
        q.assert_type_required()

    type = get_thing(store, q.get_type())
    #assert type is not None, 'Not found: ' + q.get_type()
    for c in q.conditions:
        if not isinstance(c, Query):
            c.datatype = find_datatype(type, c.key, c.value)

    if sort:
        parse_key(sort)  # to validate key
        q.sort = web.storage(key=sort,
                             datatype=find_datatype(type, sort, None))
    else:
        q.sort = None

    return q
Exemple #2
0
def make_query(store, query, prefix=""):
    """Creates a query object from query dict.
        >>> store = common.create_test_store()
        >>> make_query(store, {'type': '/type/page'})
        <query: ['type = ref:/type/page']>
        >>> make_query(store, {'type': '/type/page', 'title~': 'foo', 'life': 42})
        <query: ['life = int:42', 'type = ref:/type/page', 'title ~ str:foo']>
        >>> make_query(store, {'type': '/type/page', 'title~': 'foo', 'a:life<': 42, "b:life>": 420})        
        <query: ['life < int:42', 'type = ref:/type/page', 'title ~ str:foo', 'life > int:420']>
    """
    query = common.parse_query(query)
    q = Query()
    q.prefix = prefix
    q.offset = common.safeint(query.pop('offset', None), 0)
    q.limit = common.safeint(query.pop('limit', 20), 20)
    if q.limit > 1000:
        q.limit = 1000
    sort = query.pop('sort', None)
    
    nested = (prefix != "")
    
    for k, v in query.items():
        # key foo can also be written as label:foo
        k = k.split(':')[-1]
        if v is None:
            q.requested[k] = v
        elif isinstance(v, dict):
            # make sure op is ==
            v = dict((k + '.' + key, value) for key, value in v.items())
            q2 = make_query(store, v, prefix=prefix + k + ".")
            #@@ Anand: Quick-fix
            # dbstore.things looks for key to find whether type is required or not. 
            q2.key = k 
            if q2.conditions:
                q.conditions.append(q2)
            else:
                q.requested[k] = q2
        else:
            k, op = parse_key(k)
            q.add_condition(k, op, None, v)
             
    if not nested:
        q.assert_type_required()
        
    type = get_thing(store, q.get_type())
    #assert type is not None, 'Not found: ' + q.get_type()
    for c in q.conditions:
        if not isinstance(c, Query):
            c.datatype = find_datatype(type, c.key, c.value)
            
    if sort:
        parse_key(sort) # to validate key
        q.sort = web.storage(key=sort, datatype=find_datatype(type, sort, None))
    else:
        q.sort = None
            
    return q
Exemple #3
0
 def GET_query(self, sitename):
     i = input(type=None, name=None, value=None, limit=100, offset=0, include_docs="false")
     
     i.limit = common.safeint(i.limit, 100)
     i.offset = common.safeint(i.offset, 0)
     
     store = get_site(sitename).get_store()
     return store.query(
         type=i.type, 
         name=i.name, 
         value=i.value, 
         limit=i.limit, 
         offset=i.offset, 
         include_docs=i.include_docs.lower()=="true")
Exemple #4
0
    def GET(self, sitename, offset):
        i = web.input(timestamp=None, limit=1000)
        
        if not config.writelog:
            raise web.notfound("")
        else:
            log = self.get_log(offset, i)
            limit = min(1000, common.safeint(i.limit, 1000))
            
            try:
                # read the first line
                line = log.readline(do_update=False)
                # first line can be incomplete if the offset is wrong. Assert valid.
                self.assert_valid_json(line)
                
                web.header('Content-Type', 'application/json')
                yield '{"data": [\n'
                yield line.strip()

                for i in range(1, limit):
                    line = log.readline(do_update=False)
                    if line:
                        yield ",\n" + line.strip()
                    else:
                        break
                yield '], \n'
                yield '"offset": ' + simplejson.dumps(log.tell()) + "\n}\n"
            except Exception, e:
                print 'ERROR:', str(e)
Exemple #5
0
    def GET(self, sitename, offset):
        i = web.input(timestamp=None, limit=1000)

        if not config.writelog:
            raise web.notfound("")
        else:
            log = self.get_log(offset, i)
            limit = min(1000, common.safeint(i.limit, 1000))

            try:
                web.header('Content-Type', 'application/json')
                yield '{"data": [\n'

                sep = ""
                for i in range(limit):
                    line = log.readline().strip()
                    if line:
                        if self.valid_json(line):
                            yield sep + line.strip()
                            sep = ",\n"
                        else:
                            print >> sys.stderr, "ERROR: found invalid json before %s" % log.tell(
                            )
                    else:
                        break
                yield '], \n'
                yield '"offset": ' + simplejson.dumps(log.tell()) + "\n}\n"
            except Exception, e:
                print 'ERROR:', str(e)
Exemple #6
0
 def GET(self, sitename, offset):
     i = web.input(timestamp=None, limit=1000)
     
     if not config.writelog:
         raise web.notfound("")
     else:
         log = self.get_log(offset, i)
         limit = min(1000, common.safeint(i.limit, 1000))
         
         try:                
             web.header('Content-Type', 'application/json')
             yield '{"data": [\n'
             
             sep = ""
             for i in range(limit):
                 line = log.readline().strip()
                 if line:
                     if self.valid_json(line):
                         yield sep + line.strip()
                         sep = ",\n"
                     else:
                         print >> sys.stderr, "ERROR: found invalid json before %s" % log.tell()
                 else:
                     break
             yield '], \n'
             yield '"offset": ' + simplejson.dumps(log.tell()) + "\n}\n"
         except Exception, e:
             print 'ERROR:', str(e)
Exemple #7
0
    def GET_query(self, sitename):
        i = input(type=None,
                  name=None,
                  value=None,
                  limit=100,
                  offset=0,
                  include_docs="false")

        i.limit = common.safeint(i.limit, 100)
        i.offset = common.safeint(i.offset, 0)

        store = get_site(sitename).get_store()
        return store.query(type=i.type,
                           name=i.name,
                           value=i.value,
                           limit=i.limit,
                           offset=i.offset,
                           include_docs=i.include_docs.lower() == "true")
Exemple #8
0
def make_versions_query(store, query):
    """Creates a versions query object from query dict.
    """
    q = Query()
    
    q.offset = common.safeint(query.pop('offset', None), 0)
    q.limit = common.safeint(query.pop('limit', 20), 20)
    if q.limit > 1000:
        q.limit = 1000
    q.sort = query.pop('sort', '-created')
    
    columns = ['key', 'type', 'revision', 'author', 'comment', 'machine_comment', 'ip', 'created', 'bot']
    
    for k, v in query.items():
        if k not in columns:
            raise ValueError, k
        q.add_condition(k, '=', None, v)
        
    return q
Exemple #9
0
def make_versions_query(store, query):
    """Creates a versions query object from query dict.
    """
    q = Query()

    q.offset = common.safeint(query.pop('offset', None), 0)
    q.limit = common.safeint(query.pop('limit', 20), 20)
    if q.limit > 1000:
        q.limit = 1000
    q.sort = query.pop('sort', '-created')

    columns = [
        'key', 'type', 'revision', 'author', 'comment', 'machine_comment',
        'ip', 'created', 'bot'
    ]

    for k, v in query.items():
        if k not in columns:
            raise ValueError, k
        q.add_condition(k, '=', None, v)

    return q