# Copyright 2012-2014 Brian May## This file is part of python-tldap.## python-tldap is free software: you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation, either version 3 of the License, or# (at your option) any later version.## python-tldap is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with python-tldap If not, see <http://www.gnu.org/licenses/>.from__future__importabsolute_importimportsixfrom.treeimportNode
[docs]classQ(Node):""" Encapsulates filters as objects that can then be combined logically (using ``&`` and ``|``). """# Connection typesAND='AND'OR='OR'default=ANDdef__init__(self,*args,**kwargs):super(Q,self).__init__(children=list(args)+list(six.iteritems(kwargs)))def_combine(self,other:'Q',conn:str)->'Q':ifnotisinstance(other,Q):raiseTypeError(other)iflen(self.children)<1:self.connector=connobj=type(self)()obj.connector=connobj.add(self,conn)obj.add(other,conn)returnobjdef__or__(self,other:'Q'):returnself._combine(other,self.OR)def__and__(self,other:'Q'):returnself._combine(other,self.AND)def__invert__(self):obj=type(self)()obj.add(self,self.AND)obj.negate()returnobj