Commit 3a3d6907 authored by Antoine Millet's avatar Antoine Millet
Browse files

Added "#" separator.

parent ccc6335e
Loading
Loading
Loading
Loading
+48 −44
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@
import inspect
import logging
from sjrpc.utils import RpcHandler, pure
from tql import TqlQuery
from tql import TqlQuery, TqlCondition, TqlLimit
from conf import CCConf
from exceptions import AlreadyRegistered, AuthenticationError

@@ -70,6 +70,7 @@ class ClientHandler(OnlineCCHandler):
        query = TqlQuery(query)

        for condition in query.iterconditions():
            if isinstance(condition, TqlCondition):
                tag_name = condition.name
                
                if tag_name in ('a', 'hv', 'h', 'id'):
@@ -80,7 +81,7 @@ class ClientHandler(OnlineCCHandler):

                if tag_name in ('vm', 'h', 'id'):
                    hvs = [o for o in objects if o['role'] == 'hypervisor'
                               and o['status'] == 'online']
                                   and o['con'] != 'offline']
                    if not hvs:
                        for hy in self._server.iter_connected_role('hypervisor'):
                            tags = self._server.resolve_tags(hy.login, query.tags)
@@ -113,6 +114,9 @@ class ClientHandler(OnlineCCHandler):
                # Filter the tag
                objects = [o for o in objects if condition.check(o.get(tag_name))]

            elif isinstance(condition, TqlLimit):
                objects = objects[:condition.number]
                
        # Before to return, filter with the asked tags for each objects:
        tags = query.tags
        tags.add('id')
+20 −5
Original line number Diff line number Diff line
@@ -59,6 +59,14 @@ class TqlCondition(object):
    def check(self, value):
        return self.operator(value, self.value) ^ self.invert

class TqlLimit(object):

    def __init__(self, number):
        self.number = number

    def __repr__(self):
        return '<TqlLimit object [%s]>' % self.number

class TqlQuery(object):
    '''
    Parse a query written with TQL (Tag Query Language) and allow to filter a
@@ -75,12 +83,11 @@ class TqlQuery(object):
                 '<=': 'lte',
                 '~': 'regex'}

    RE_COND = re.compile(r'^(?P<name>[a-z-A-Z0-9_-]+)'
                         r'((?P<operator>[!$<>~:=]{1,3})'
                         r'(?P<value>[^&]+))?$')
    RE_COND = re.compile(r'^(?P<name>[a-z-A-Z0-9_?*-]+)'
                         r'((?P<operator>[!<>~:=]{1,3})'
                         r'(?P<value>[^&$#]+?))?$')
    RE_TAG = re.compile(r'^(?P<name>[a-z-A-Z0-9_-]+)')
    RE_SEPARATOR = re.compile(r'(&|\$)')
    
    RE_SEPARATOR = re.compile(r'(&|\$|#)')

    def __init__(self, query):
        self._conditions = []
@@ -151,6 +158,14 @@ class TqlQuery(object):
                    raise TqlParsingError('Error while parsing, invalid tag '
                                          'name %s' % repr(tok))

            elif sep == '#':
                if tok.isdigit():
                    lim = TqlLimit(int(tok))
                    self._conditions.append(lim)
                else:
                    raise TqlParsingError('Error while parsing, invalid limit '
                                          '%s' % repr(tok))

    def has_condition(self, tag_name):
        '''
        Return True if query has made a condition for the specified tag name.