From nobody Tue May 13 15:42:10 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=patchew-devel-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=patchew-devel-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1538145953140882.6281767469725; Fri, 28 Sep 2018 07:45:53 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C2EB33097031; Fri, 28 Sep 2018 14:45:51 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AFABF5DD87; Fri, 28 Sep 2018 14:45:51 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id A050318005CD; Fri, 28 Sep 2018 14:45:51 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8SEjoUH024580 for ; Fri, 28 Sep 2018 10:45:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id 0C67A2010DA4; Fri, 28 Sep 2018 14:45:50 +0000 (UTC) Received: from mx1.redhat.com (ext-mx08.extmail.prod.ext.phx2.redhat.com [10.5.110.32]) by smtp.corp.redhat.com (Postfix) with ESMTPS id F39BD2010DA2 for ; Fri, 28 Sep 2018 14:45:49 +0000 (UTC) Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 28069C05D3E5 for ; Fri, 28 Sep 2018 14:45:48 +0000 (UTC) Received: by mail-wm1-f45.google.com with SMTP id y26-v6so2501924wma.2 for ; Fri, 28 Sep 2018 07:45:48 -0700 (PDT) Received: from donizetti.par1.mozilla.com ([2a00:8c40:243:232:de:fba5:535e:2dc2]) by smtp.gmail.com with ESMTPSA id n68-v6sm1884636wmn.5.2018.09.28.07.45.45 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Fri, 28 Sep 2018 07:45:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:from:to:subject:date:message-id:in-reply-to:references; bh=rKhum7HJHP/40cTnurEVrgB753X78tREV3nRqNpZK+M=; b=R3VX8jLqoFuu/jW/cjIDOrYGSCLmdgDnLWQKDxT4rT/fzaKO6jXPgDQWA7J/C0/gKe nPUA0/jEU9zMn3N7syKd5/kmbdSE+FEg5uvdLz3kBVrLq6Q1iXNvb40bPOchJKeu62S3 i/MWigdSOaQAPoL2FvpZvIl+NKJhrhpkrP/VA2l7HVOfYSjyqyjFqBK6h/fkyMnilQn1 1CPmen1ULLmse+0ckgOojb/Z/6QVuHkQ6gM0T/0RLVqeGPT2T+aWj+bB04WibnNiaU/p HVGpTctApY4lvgzhbt1dXR2CXc5vxGxoF3cSrbW0sFMEeiNGZCU6Pwkh7+O8Mdj/TjW3 Eg/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:from:to:subject:date:message-id :in-reply-to:references; bh=rKhum7HJHP/40cTnurEVrgB753X78tREV3nRqNpZK+M=; b=MiY1YtXoNHXqvvoHHhz9D+FeYmySiwghdftRS79toYz3/XyfayfoWT1nExWIhntrX6 BkFdC1xnveTjBw6PX1KLZs+S1366ciC4gEfMnx1l8WNWSbZMmkuHhU9oOtdQiAkKKTsi WrhyKZoJL14UtEntZDycTnWOubrR3aHqJUXR9ehSk2hUxrtU9LxfHrR0PkbBXxQdF3Gi 7mo/BFwAPA6VxpWPUrW2SjVv+c45En+gHDkC6WppXYNb4wuUu3Na/87a7u7T0wj2p7mh A7H8DVB6STjyd3G8HaMpDYgX36igGm4eoIQa5neCuQ4qXD1MLFM6sRTUD/hvfvSVxcWv ayqQ== X-Gm-Message-State: ABuFfojGVVVn2joMqsGL3jYxWKekdLG8qBa6+C22w2qTd5AHYxyxORz+ I2biFHjlbKhX23QpLi5PjiA+JoL8hR8= X-Google-Smtp-Source: ACcGV61VMk2gWRXNX8xtGLTMeXBp5JMCL8/xC0xAHQgno4kFb75Ig2E2th0BVoV4rwftoJnbu9pqnw== X-Received: by 2002:a1c:4a:: with SMTP id 71-v6mr1942072wma.96.1538145946346; Fri, 28 Sep 2018 07:45:46 -0700 (PDT) From: Paolo Bonzini To: patchew-devel@redhat.com Date: Fri, 28 Sep 2018 16:45:32 +0200 Message-Id: <20180928144537.5051-6-pbonzini@redhat.com> In-Reply-To: <20180928144537.5051-1-pbonzini@redhat.com> References: <20180928144537.5051-1-pbonzini@redhat.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 28 Sep 2018 14:45:48 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 28 Sep 2018 14:45:48 +0000 (UTC) for IP:'209.85.128.45' DOMAIN:'mail-wm1-f45.google.com' HELO:'mail-wm1-f45.google.com' FROM:'paolo.bonzini@gmail.com' RCPT:'' X-RedHat-Spam-Score: 0.488 (DKIM_SIGNED, DKIM_VALID, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_PASS) 209.85.128.45 mail-wm1-f45.google.com 209.85.128.45 mail-wm1-f45.google.com X-RedHat-Possible-Forgery: Paolo Bonzini X-Scanned-By: MIMEDefang 2.78 on 10.5.110.32 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-loop: patchew-devel@redhat.com Subject: [Patchew-devel] [PATCH v2 05/10] search: split _process_term into separate functions X-BeenThere: patchew-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Patchew development and discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: patchew-devel-bounces@redhat.com Errors-To: patchew-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.43]); Fri, 28 Sep 2018 14:45:51 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDMRC_0 RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This is a first step towards turning it into something like a recursive descent parser. --- api/search.py | 116 +++++++++++++++++++++++++------------------------- 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/api/search.py b/api/search.py index b1d20ec..355de0e 100644 --- a/api/search.py +++ b/api/search.py @@ -129,19 +129,70 @@ Search text keyword in the email message. Example: message_ids =3D model.objects.filter(q).values('message_id') return Q(id__in=3Dmessage_ids) =20 + def _make_filter_age(self, cond): + import datetime + def human_to_seconds(n, unit): + if unit =3D=3D "d": + return n * 86400 + elif unit =3D=3D "w": + return n * 86400 * 7 + elif unit =3D=3D "m": + return n * 86400 * 30 + elif unit =3D=3D "y": + return n * 86400 * 365 + raise Exception("No unit specified") + + if cond.startswith("<"): + less =3D True + cond =3D cond[1:] + elif cond.startswith(">"): + less =3D False + cond =3D cond[1:] + else: + less =3D False + num, unit =3D cond[:-1], cond[-1].lower() + if not num.isdigit() or not unit in "dwmy": + raise InvalidSearchTerm("Invalid age string: %s" % cond) + sec =3D human_to_seconds(int(num), unit) + p =3D datetime.datetime.now() - datetime.timedelta(0, sec) + if less: + q =3D Q(date__gte=3Dp) + else: + q =3D Q(date__lte=3Dp) + return q + + def _make_filter_keywords(self, t): + self._last_keywords.append(t) + return Q(subject__icontains=3Dt) + + def _make_filter_is(self, cond): + if cond =3D=3D "complete": + return Q(is_complete=3DTrue) + elif cond =3D=3D "pull": + return Q(subject__contains=3D'[PULL') | Q(subject__contains=3D= '[GIT PULL') + elif cond =3D=3D "reviewed": + return self._make_filter_subquery(MessageProperty, Q(name=3D"r= eviewed", value=3DTrue)) + elif cond in ("obsoleted", "old"): + return self._make_filter_subquery(MessageProperty, + Q(name=3D"obsoleted-by", val= ue__isnull=3DFalse) & + ~Q(name=3D"obsoleted-by", va= lue__iexact=3D'')) + elif cond =3D=3D "applied": + return self._make_filter_subquery(MessageResult, Q(name=3D"git= ", status=3DResult.SUCCESS)) + elif cond =3D=3D "tested": + return self._make_filter_subquery(MessageProperty, Q(name=3D"t= esting.done", value=3DTrue)) + elif cond =3D=3D "merged": + return Q(is_merged=3DTrue) + return self._make_filter_keywords(cond) + def _process_term(self, term, neg=3DFalse): """ Return a Q object that will be applied to the query """ - def as_keywords(t): - self._last_keywords.append(t) - return Q(subject__icontains=3Dt) - if term.startswith("!"): return self._process_term(term[1:], not neg) if term.startswith("age:"): cond =3D term[term.find(":") + 1:] - q =3D self._process_age_term(cond) + q =3D self._make_filter_age(cond) elif term[0] in "<>" and len(term) > 1: - q =3D self._process_age_term(term) + q =3D self._make_filter_age(term) elif term.startswith("from:"): cond =3D term[term.find(":") + 1:] q =3D Q(sender__icontains=3Dcond) @@ -163,26 +214,9 @@ Search text keyword in the email message. Example: else: cond =3D term[term.find(":") + 1:] lneg =3D term.startswith("not:") - if cond =3D=3D "complete": - q =3D Q(is_complete=3DTrue) - elif cond =3D=3D "pull": - q =3D Q(subject__contains=3D'[PULL') | Q(subject__contains= =3D'[GIT PULL') - elif cond =3D=3D "reviewed": - q =3D self._make_filter_subquery(MessageProperty, Q(name= =3D"reviewed", value=3DTrue)) - elif cond in ("obsoleted", "old"): - q =3D self._make_filter_subquery(MessageProperty, - Q(name=3D"obsoleted-by", va= lue__isnull=3DFalse) & - ~Q(name=3D"obsoleted-by", v= alue__iexact=3D'')) - elif cond =3D=3D "applied": - q =3D self._make_filter_subquery(MessageResult, Q(name=3D"= git", status=3DResult.SUCCESS)) - elif cond =3D=3D "tested": - q =3D self._make_filter_subquery(MessageProperty, Q(name= =3D"testing.done", value=3DTrue)) - elif cond =3D=3D "merged": - q =3D Q(is_merged=3DTrue) - else: - q =3D as_keywords(term) if lneg: neg =3D not neg + q =3D self._make_filter_is(cond) elif term.startswith("has:"): cond =3D term[term.find(":") + 1:] if cond =3D=3D "replies": @@ -195,7 +229,7 @@ Search text keyword in the email message. Example: q =3D Q(project__name=3Dcond) | Q(project__parent_project__nam= e=3Dcond) else: # Keyword in subject is the default - q =3D as_keywords(term) + q =3D self._make_filter_keywords(term) if neg: return ~q else: @@ -215,35 +249,3 @@ Search text keyword in the email message. Example: if queryset is None: queryset =3D Message.objects.series_heads() return queryset.filter(q) - - def _process_age_term(self, cond): - import datetime - def human_to_seconds(n, unit): - if unit =3D=3D "d": - return n * 86400 - elif unit =3D=3D "w": - return n * 86400 * 7 - elif unit =3D=3D "m": - return n * 86400 * 30 - elif unit =3D=3D "y": - return n * 86400 * 365 - raise Exception("No unit specified") - - if cond.startswith("<"): - less =3D True - cond =3D cond[1:] - elif cond.startswith(">"): - less =3D False - cond =3D cond[1:] - else: - less =3D False - num, unit =3D cond[:-1], cond[-1].lower() - if not num.isdigit() or not unit in "dwmy": - raise InvalidSearchTerm("Invalid age string: %s" % cond) - sec =3D human_to_seconds(int(num), unit) - p =3D datetime.datetime.now() - datetime.timedelta(0, sec) - if less: - q =3D Q(date__gte=3Dp) - else: - q =3D Q(date__lte=3Dp) - return q --=20 2.17.1 _______________________________________________ Patchew-devel mailing list Patchew-devel@redhat.com https://www.redhat.com/mailman/listinfo/patchew-devel