# -*- coding: utf-8 -*-
#
# This file is part of pypuppetdbquery.
# Copyright © 2016 Chris Boot <bootc@bootc.net>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Abstract Syntax Tree (AST) for the PuppetDBQuery language. These simple classes
are used by the :class:`pypuppetdbquery.parser.Parser` in order to represent
the parsed syntax tree.
"""
import inspect
[docs]class Node(object):
def __repr__(self):
# Represent the variables defined in the constructor in the same order
# that they are listed in the constructor.
members = []
for var in inspect.getargspec(self.__init__).args:
if var == 'self':
continue
members.append(repr(getattr(self, var)))
# Put it together with the class name
return "{cls}({members})".format(
cls=self.__class__.__name__, members=', '.join(members))
[docs]class Literal(Node):
def __init__(self, value):
self.value = value
[docs]class Date(Literal):
pass
[docs]class Query(Node):
def __init__(self, expression):
self.expression = expression
[docs]class Expression(Node):
pass
[docs]class UnaryExpression(Node):
def __init__(self, expression):
self.expression = expression
[docs]class BinaryExpression(Node):
def __init__(self, left, right):
self.left = left
self.right = right
[docs]class AndExpression(BinaryExpression):
pass
[docs]class OrExpression(BinaryExpression):
pass
[docs]class NotExpression(UnaryExpression):
pass
[docs]class ParenthesizedExpression(UnaryExpression):
pass
[docs]class BlockExpression(UnaryExpression):
pass
[docs]class Comparison(Expression):
def __init__(self, operator, left, right):
self.operator = operator
self.left = left
self.right = right
[docs]class Identifier(Node):
def __init__(self, name):
self.name = name
[docs]class RegexpIdentifier(Identifier):
pass
[docs]class IdentifierPath(Node):
def __init__(self, components):
self.components = components
[docs]class Subquery(Node):
def __init__(self, endpoint, expression):
self.endpoint = endpoint
self.expression = expression
[docs]class Resource(Expression):
def __init__(self, res_type, title, exported, parameters=None):
self.res_type = res_type
self.title = title
self.exported = exported
self.parameters = parameters
[docs]class RegexpNodeMatch(Expression):
def __init__(self, value):
self.value = value