Пример #1
0
 def add_query(self, query):
     if self.query is not None:
         raise APIError("Only one main query is supported by FromOperator")
     elif isinstance(query, (InOperator, ExtractOperator, BinaryOperator,
                             BooleanOperator, FunctionOperator)):
         self.query = query.json_data()
     else:
         raise APIError("FromOperator.add_field only supports "
                        "Operator Objects")
Пример #2
0
 def add_query(self, query):
     if self.query is not None:
         raise APIError("Only one query is supported by ExtractOperator")
     elif isinstance(query, str):
         self.query = json.loads(query)
     elif isinstance(query,
                     (BinaryOperator, SubqueryOperator, BooleanOperator)):
         self.query = query.json_data()
     else:
         raise APIError("ExtractOperator.add_query only supports "
                        "strings, BinaryOperator, BooleanOperator "
                        "and SubqueryOperator objects")
Пример #3
0
 def add_query(self, query):
     if self.query is not None:
         raise APIError("Only one query is supported by ExtractOperator")
     elif isinstance(query, str):
         self.query = True
         self.arr.append(json.loads(query))
     elif isinstance(query, (ExtractOperator, FromOperator)):
         self.query = True
         self.arr.append(query.json_data())
     else:
         raise APIError("InOperator.add_query only supports "
                        "strings, ExtractOperator, and"
                        "FromOperator objects")
Пример #4
0
    def __init__(self, function, field=None, fmt=None):
        if function not in ['count', 'avg', 'sum', 'min', 'max', 'to_string']:
            raise APIError("Unsupport function: {0}".format(function))
        elif function != "count" and field is None:
            raise APIError(
                "Function {0} requires a field value".format(function))
        elif function == 'to_string' and fmt is None:
            raise APIError("Function {0} requires an extra 'fmt' parameter")

        self.arr = ['function', function]

        if field is not None:
            self.arr.append(field)

        if function == 'to_string':
            self.arr.append(fmt)
Пример #5
0
    def add_order_by(self, fields):
        def depth(l):
            return isinstance(l, list) and max(map(depth, l)) + 1

        fields_depth = depth(fields)

        if isinstance(fields, list):
            if fields_depth == 1 or fields_depth == 2:
                self.order_by = fields
            else:
                raise APIError("ExtractOperator.add_order_by only "
                               "supports lists of fields of depth "
                               "one or two: [value, <desc/asc>] or "
                               "[value]")
        else:
            raise APIError("ExtractOperator.add_group_by only supports "
                           "lists of one or more fields")
Пример #6
0
    def __init__(self, endpoint):
        if endpoint not in [
                'catalogs', 'edges', 'environments', 'events', 'facts',
                'fact_contents', 'fact_paths', 'nodes', 'reports', 'resources'
        ]:
            raise APIError("Unsupported endpoint: {0}".format(endpoint))

        self.query = None
        self.arr = ['select_{0}'.format(endpoint)]
Пример #7
0
    def add_array(self, values):
        if self.query is not None:
            raise APIError("Only one array is supported by the InOperator")
        elif isinstance(values, list):

            def depth(l):
                return (isinstance(l, list) and len(l) != 0) \
                       and max(map(depth, l)) + 1

            if depth(values) == 1:
                self.query = True
                self.arr.append(['array', values])
            else:
                raise APIError("InOperator.add_array: cannot pass in "
                               "nested arrays (or empty arrays)")
        else:
            raise APIError("InOperator.add_array: Ill-formatted array, "
                           "must be of the format: "
                           "['array', [<array values>]]")
Пример #8
0
 def add(self, query):
     if type(query) == list:
         for i in query:
             self.add(i)
     elif type(query) == str:
         self.operations.append(json.loads(query))
     elif isinstance(query, (BinaryOperator, InOperator, BooleanOperator)):
         self.operations.append(query.json_data())
     else:
         raise APIError("Can only accept fixed-string queries, arrays " +
                        "or operator objects")
Пример #9
0
 def add_field(self, field):
     if isinstance(field, list):
         for i in field:
             self.add_field(i)
     elif isinstance(field, str):
         self.fields.append(field)
     elif isinstance(field, FunctionOperator):
         self.fields.append(field.json_data())
     else:
         raise APIError("ExtractOperator.add_field only supports "
                        "lists and strings")
Пример #10
0
    def json_data(self):
        if len(self.fields) == 0:
            raise APIError("ExtractOperator needs at least one field")

        arr = ['extract', self.fields]

        if self.query is not None:
            arr.append(self.query)
        if len(self.group_by) > 0:
            arr.append(self.group_by)

        return arr
Пример #11
0
    def json_data(self):
        if self.query is None:
            raise APIError("FromOperator needs one main query")

        arr = ['from', self.endpoint, self.query]

        if len(self.order_by) > 0:
            arr.append(['order_by', self.order_by])
        if self.limit is not None:
            arr.append(['limit', self.limit])
        if self.offset is not None:
            arr.append(['offset', self.offset])

        return arr
Пример #12
0
 def add_group_by(self, field):
     if isinstance(field, list):
         for i in field:
             self.add_group_by(i)
     elif isinstance(field, str):
         if len(self.group_by) == 0:
             self.group_by.append('group_by')
         self.group_by.append(field)
     elif isinstance(field, FunctionOperator):
         if len(self.group_by) == 0:
             self.group_by.append('group_by')
         self.group_by.append(field.json_data())
     else:
         raise APIError("ExtractOperator.add_group_by only supports "
                        "lists, strings, and FunctionOperator objects")
Пример #13
0
    def __init__(self, endpoint):
        valid_entities = [
            "aggregate_event_counts", "catalogs", "edges", "environments",
            "event_counts", "events", "facts", "fact_contents", "fact_names",
            "fact_paths", "nodes", "producers", "reports", "resources"
        ]

        if endpoint in valid_entities:
            self.endpoint = endpoint
        else:
            raise APIError("Endpoint is invalid. Must be "
                           "one of the following : %s" % valid_entities)

        self.query = None
        self.order_by = []
        self.limit = None
        self.offset = None
Пример #14
0
 def json_data(self):
     if len(self.operations) == 0:
         raise APIError("At least one query operation is required")
     return [self.operator] + self.operations
Пример #15
0
 def add_query(self, query):
     if self.query is not None:
         raise APIError("Only one query is supported by ExtractOperator")
     else:
         self.query = True
         self.arr.append(query.json_data())
Пример #16
0
 def add(self, query):
     if len(self.operations) > 0:
         raise APIError("This operator only accept one query string")
     elif isinstance(query, list) and len(query) > 1:
         raise APIError("This operator only accept one query string")
     super(NotOperator, self).add(query)
Пример #17
0
    def __init__(self, field, value):
        if type(value) != bool:
            raise APIError("NullOperator value must be boolean")

        super(NullOperator, self).__init__("null?", field, value)
Пример #18
0
 def add_limit(self, lim):
     if isinstance(lim, int):
         self.limit = lim
     else:
         raise APIError("ExtractOperator.add_limit only supports ints")
Пример #19
0
 def add_offset(self, off):
     if isinstance(off, int):
         self.offset = off
     else:
         raise APIError("ExtractOperator.add_offset only supports ints")