Console interface to Storyboard
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.9 KiB

# Copyright 2014 Hewlett-Packard Development Company, L.P.
# 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
# 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.
import sqlalchemy.sql.expression
from sqlalchemy.sql.expression import and_
from import tokenizer, parser
import boartty.db
class SearchSyntaxError(Exception):
class SearchCompiler(object):
def __init__(self, username):
self.username = username
self.lexer = tokenizer.SearchTokenizer()
self.parser = parser.SearchParser()
def findTables(self, expression):
tables = set()
stack = [expression]
while stack:
x = stack.pop()
if hasattr(x, 'table'):
if (x.table != boartty.db.story_table
and hasattr(x.table, 'name')):
for child in x.get_children():
if not isinstance(child, sqlalchemy.sql.selectable.Select):
return tables
def parse(self, data):
self.parser.username = self.username
result = self.parser.parse(data, lexer=self.lexer)
tables = self.findTables(result)
if boartty.db.project_table in tables:
result = and_(boartty.db.story_table.c.project_key == boartty.db.project_table.c.key,
if boartty.db.user_table in tables:
result = and_(boartty.db.story_table.c.user_key == boartty.db.user_table.c.key,
#if boartty.db.file_table in tables:
# result = and_(boartty.db.file_table.c.revision_key == boartty.db.revision_table.c.key,
# boartty.db.revision_table.c.story_key == boartty.db.story_table.c.key,
# result)
# tables.remove(boartty.db.file_table)
if tables:
raise Exception("Unknown table in search: %s" % tables)
return result
if __name__ == '__main__':
class Dummy(object):
query = 'recentlyseen:24 hours'
lexer = tokenizer.SearchTokenizer()
while True:
token = lexer.token()
if not token:
app = Dummy()
app.config = Dummy()
app.config.username = 'bob'
search = SearchCompiler(app.config.username)
x = search.parse(query)