ATLAS Offline Software
Loading...
Searching...
No Matches
selection.py
Go to the documentation of this file.
1# Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2
3from DQUtils.quick_retrieve import make_fieldselection, make_selection_vector
4
5from pyparsing import (Word, Literal, oneOf, QuotedString, Optional,
6 operatorPrecedence, opAssoc, nums, alphanums, StringStart, StringEnd,
7 Combine, CaselessLiteral)
8
9# Taken from "Good usage of setWhitespaceChars" on pyparsing's discussion
11 if t.is_float:
12 return float(t[0])
13 else:
14 return int(t[0])
15
16# Number defintion
17sign = oneOf('+ -')
18integer = Word(nums)
19
20number = Combine(
21 Optional(sign)
22 + ( ( integer + Optional('.')('is_float') + Optional(integer))
23 | ( Literal('.')('is_float') + Optional(integer))
24 )
25 + Optional( CaselessLiteral('E') + Optional(sign) + integer)('is_float')
26 ).setParseAction(convertNumber)
27
28boolValue = (Literal("True") .setParseAction(lambda t: True) |
29 Literal("False").setParseAction(lambda t: False))
30stringValue = QuotedString("'") | QuotedString('"')
31value = boolValue | number | stringValue
32
33variable = Word(alphanums, min=1)
34
35def make_parser(payload_specification):
36 """
37 The parser has to be payload_specification specific because it needs to know
38 the types of the records to build a FieldSelection.
39 """
40
41 def make_selection(string, location, tokens):
42 variable, comparator, value = tokens
43 if payload_specification and variable not in payload_specification:
44 raise RuntimeError("%s is not a value in the folder payload "
45 "specification")
46 elif not payload_specification:
47 return (variable, comparator, value)
48 typeid = payload_specification[variable].storageType().id()
49 return make_fieldselection(variable, typeid, comparator, value)
50
51 from PyCool import cool
52 EQ = Literal("==").setParseAction(lambda t: cool.FieldSelection.EQ)
53 NE = Literal("!=").setParseAction(lambda t: cool.FieldSelection.NE)
54 GE = Literal(">=").setParseAction(lambda t: cool.FieldSelection.GE)
55 LE = Literal("<=").setParseAction(lambda t: cool.FieldSelection.LE)
56 GT = Literal(">") .setParseAction(lambda t: cool.FieldSelection.GT)
57 LT = Literal("<") .setParseAction(lambda t: cool.FieldSelection.LT)
58 comparator = EQ | NE | GE | LE | GT | LT
59 operand = (variable + comparator + value).setParseAction(make_selection)
60
61 orop = Literal("or").suppress()
62 andop = Literal("and").suppress()
63
64 def logic_builder(connective):
65 def thunk(string, location, tokens):
67 for token in tokens[0]:
68 vec.push_back(token)
69
70 return cool.CompositeSelection(connective, vec)
71 return thunk
72
73 expr = StringStart() + operatorPrecedence( operand,
74 [(andop, 2, opAssoc.LEFT, logic_builder(cool.CompositeSelection.AND)),
75 (orop, 2, opAssoc.LEFT, logic_builder(cool.CompositeSelection.OR)),]
76 ) + StringEnd()
77
78 def go(selection):
79 return expr.parseString(selection)[0]
80 return go
81
82def make_browse_objects_selection(folder, selection):
83 parser = make_parser(folder.folderSpecification().payloadSpecification())
84 return parser(selection)
vector< const cool::IRecordSelection * > make_selection_vector()
cool::FieldSelection * make_fieldselection(const std::string &name, const cool::StorageType::TypeId typeId, cool::FieldSelection::Relation relation, PyObject *refValue)
make_parser(payload_specification)
Definition selection.py:35
make_browse_objects_selection(folder, selection)
Definition selection.py:82