def ParseKeyFilteredQuery(filters, orders):
 remaining_filters = []
 key_range = ValueRange()
 key_prop = datastore_types.KEY_SPECIAL_PROPERTY
 for f in filters:
  op = f.op()
  if (not ((f.property_size() == 1) and (f.property(0).name() == key_prop) and (not ((op == datastore_pb.Query_Filter.IN) or (op == datastore_pb.Query_Filter.EXISTS))))):
   remaining_filters.append(f)
   continue
  val = f.property(0).value()
  Check(val.has_referencevalue(), '__key__ kind must be compared to a key')
  limit = datastore_types.FromReferenceProperty(val)
  key_range.Update(op, limit)
 remaining_orders = []
 for o in orders:
  if (not ((o.direction() == datastore_pb.Query_Order.ASCENDING) and (o.property() == datastore_types.KEY_SPECIAL_PROPERTY))):
   remaining_orders.append(o)
  else:
   break
 Check((not remaining_filters), (('Only comparison filters on ' + key_prop) + ' supported'))
 Check((not remaining_orders), (('Only ascending order on ' + key_prop) + ' supported'))
 return key_range

def ParseKeyFilteredQuery ( filters , orders ) : remaining_filters = [ ] key_range = ValueRange ( ) key_prop = datastore_types . KEY_SPECIAL_PROPERTY for f in filters : op = f . op ( ) if ( not ( ( f . property_size ( ) == 1 ) and ( f . property ( 0 ) . name ( ) == key_prop ) and ( not ( ( op == datastore_pb . Query_Filter . IN ) or ( op == datastore_pb . Query_Filter . EXISTS ) ) ) ) ) : remaining_filters . append ( f ) continue val = f . property ( 0 ) . value ( ) Check ( val . has_referencevalue ( ) , '__key__ kind must be compared to a key' ) limit = datastore_types . FromReferenceProperty ( val ) key_range . Update ( op , limit ) remaining_orders = [ ] for o in orders : if ( not ( ( o . direction ( ) == datastore_pb . Query_Order . ASCENDING ) and ( o . property ( ) == datastore_types . KEY_SPECIAL_PROPERTY ) ) ) : remaining_orders . append ( o ) else : break Check ( ( not remaining_filters ) , ( ( 'Only comparison filters on ' + key_prop ) + ' supported' ) ) Check ( ( not remaining_orders ) , ( ( 'Only ascending order on ' + key_prop ) + ' supported' ) ) return key_range
parse queries which only allow filters and ascending-orders on __key__ .