Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
Formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Dec 25, 2014
1 parent b265b1c commit 75e7d4d
Showing 1 changed file with 53 additions and 28 deletions.
81 changes: 53 additions & 28 deletions synoptic/query.py
@@ -1,26 +1,32 @@
# logic that preserves NotImplemented -----------------------------------------


# {{{ logic that preserves NotImplemented

def not_ni(a):
if a == NotImplemented:
return NotImplemented
else:
return not a


def and_ni(a, b):
if a == NotImplemented or b == NotImplemented:
return NotImplemented
else:
return a and b


def or_ni(a, b):
if a == NotImplemented or b == NotImplemented:
return NotImplemented
else:
return a or b

# }}}


# {{{ query objects

# query objects ---------------------------------------------------------------
class Query(object):
def __str__(self):
return self.visit(StringifyVisitor())
Expand All @@ -44,8 +50,6 @@ def __ge__(self, other):
return not_ni(self.__lt__(other))




class IdQuery(Query):
def __init__(self, id):
self.id = id
Expand All @@ -62,6 +66,7 @@ def __lt__(self, other):
else:
return self.id < other.id


class TagQuery(Query):
def __init__(self, name):
self.name = name
Expand All @@ -78,6 +83,7 @@ def __lt__(self, other):
else:
return self.name < other.name


class TagWildcardQuery(Query):
def __init__(self, name):
self.name = name
Expand All @@ -94,6 +100,7 @@ def __lt__(self, other):
else:
return self.name < other.name


class FulltextQuery(Query):
def __init__(self, substr):
self.substr = substr
Expand All @@ -110,6 +117,7 @@ def __lt__(self, other):
else:
return self.substr < other.substr


class NotQuery(Query):
def __init__(self, child):
self.child = child
Expand All @@ -126,6 +134,7 @@ def __lt__(self, other):
else:
return self.child < other.child


class AndQuery(Query):
def __init__(self, children):
self.children = children[:]
Expand All @@ -143,6 +152,7 @@ def __lt__(self, other):
else:
return self.children < other.children


class OrQuery(Query):
def __init__(self, children):
self.children = children[:]
Expand All @@ -161,6 +171,7 @@ def __lt__(self, other):
else:
return self.children < other.children


class DateQuery(Query):
def __init__(self, is_before, timestamp):
self.is_before = is_before
Expand All @@ -171,7 +182,7 @@ def visit(self, visitor, *args):

def __eq__(self, other):
return (
isinstance(other, DateQuery)
isinstance(other, DateQuery)
and self.is_before == other.is_before
and self.timestamp == other.timestamp)

Expand All @@ -183,6 +194,7 @@ def __lt__(self, other):
<
(other.is_before, other.timestamp))


class StatelessQueryTerminal(Query):
def __eq__(self, other):
return isinstance(other, type(self))
Expand All @@ -193,29 +205,33 @@ def __lt__(self, other):
else:
return False


class DatedQuery(StatelessQueryTerminal):
def visit(self, visitor, *args):
return visitor.visit_dated_query(self, *args)


class NoHideQuery(StatelessQueryTerminal):
def visit(self, visitor, *args):
return visitor.visit_no_hide_query(self, *args)


class SortByDateQuery(StatelessQueryTerminal):
def visit(self, visitor, *args):
return visitor.visit_sort_by_date(self, *args)

# }}}


# {{{ normalizing query constructors


# normalizing query constructors ----------------------------------------------
def make_tag_query(tag):
if "?" in tag or "*" in tag:
return TagWildcardQuery(tag)
else:
return TagQuery(tag)


def make_not_query(child):
if isinstance(child, NotQuery):
return child.child
Expand All @@ -226,6 +242,7 @@ def make_not_query(child):
else:
return NotQuery(child)


def _make_flattened_children_query(klass, children):
new_children = []
for ch in children:
Expand All @@ -236,24 +253,28 @@ def _make_flattened_children_query(klass, children):

return klass(new_children)


def make_and_query(children):
return _make_flattened_children_query(AndQuery, children)


def make_or_query(children):
return _make_flattened_children_query(OrQuery, children)

# }}}


# {{{ operator precedence

# operator precedence ---------------------------------------------------------
_PREC_OR = 10
_PREC_AND = 20
_PREC_NOT = 30

# }}}


# {{{ query visitors

# query visitors --------------------------------------------------------------
class StringifyVisitor(object):
def visit_id_query(self, q, enclosing_prec=0):
return "id(%d)" % q.id
Expand Down Expand Up @@ -314,11 +335,7 @@ def visit_sort_by_date(self, q, enclosing_prec=0):
return "sortbydate"



class ReprVisitor(object):
def visit_tag_query(self, q):
return "%s(%s)" % (type(q).__name__, repr(q.id))

def visit_tag_query(self, q):
return "%s(%s)" % (type(q).__name__, repr(q.name))

Expand Down Expand Up @@ -348,7 +365,6 @@ def visit_dated_query(self, q):
visit_sort_by_date = visit_dated_query



class TagListVisitor(object):
def visit_id_query(self, q):
return []
Expand Down Expand Up @@ -384,10 +400,11 @@ def visit_date_query(self, q):
visit_no_hide_query = visit_date_query
visit_sort_by_date = visit_date_query

# }}}


# {{{ lexer data

# lexer data ------------------------------------------------------------------
_and = intern("and")
_or = intern("or")
_not = intern("not")
Expand All @@ -405,8 +422,6 @@ def visit_date_query(self, q):
_whitespace = intern("whitespace")




from synoptic.lex import RE
_LEX_TABLE = [
(_and, RE(r"and\b")),
Expand All @@ -427,20 +442,21 @@ def visit_date_query(self, q):
]




_STATELESS_TERMINALS = {
_dated: DatedQuery(),
_dated: DatedQuery(),
_nohide: NoHideQuery(),
_sortbydate: SortByDateQuery()
}

_TERMINALS = [_tag, _negtag, _fulltext, _id, _before, _after] + list(_STATELESS_TERMINALS.iterkeys())
_TERMINALS = (
[_tag, _negtag, _fulltext, _id, _before, _after]
+ list(_STATELESS_TERMINALS.iterkeys()))

# }}}


# {{{parser

# parser ----------------------------------------------------------------------
def parse_query(expr_str):
def parse_terminal(pstate):
next_tag = pstate.next_tag()
Expand All @@ -466,7 +482,7 @@ def parse_terminal(pstate):
timetup = cal.parse(pstate.next_match_obj().group(1))
pstate.advance()
import time
return DateQuery(next_tag==_before, time.mktime(timetup[0]))
return DateQuery(next_tag == _before, time.mktime(timetup[0]))
else:
pstate.expected("terminal")

Expand Down Expand Up @@ -494,19 +510,22 @@ def inner_parse(pstate, min_precedence=0):

if next_tag is _and and _PREC_AND > min_precedence:
pstate.advance()
left_query = make_and_query([left_query, inner_parse(pstate, _PREC_AND)])
left_query = make_and_query(
[left_query, inner_parse(pstate, _PREC_AND)])
did_something = True
elif next_tag is _or and _PREC_OR > min_precedence:
pstate.advance()
left_query = make_or_query([left_query, inner_parse(pstate, _PREC_OR)])
left_query = make_or_query(
[left_query, inner_parse(pstate, _PREC_OR)])
did_something = True
elif next_tag in _TERMINALS + [_not, _openpar] and _PREC_AND > min_precedence:
left_query = make_and_query([left_query, inner_parse(pstate, _PREC_AND)])
elif (next_tag in _TERMINALS + [_not, _openpar]
and _PREC_AND > min_precedence):
left_query = make_and_query(
[left_query, inner_parse(pstate, _PREC_AND)])
did_something = True

return left_query


from synoptic.lex import LexIterator, lex
pstate = LexIterator(
[(tag, s, idx, matchobj)
Expand All @@ -522,8 +541,10 @@ def inner_parse(pstate, min_precedence=0):

return result

# }}}


# {{{ 'test'

if __name__ == "__main__":
v = parse_query('not (yuck "yy!" and (not (not them and (yes or me)) and you))')
Expand All @@ -541,3 +562,7 @@ def inner_parse(pstate, min_precedence=0):
print v2
v = parse_query('pic ("test" or "validation")')
print repr(v)

# }}}

# vim: foldmethod=marker

0 comments on commit 75e7d4d

Please sign in to comment.