From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) by finch.gentoo.org (Postfix) with ESMTP id 4C029138200 for ; Thu, 13 Jun 2013 16:34:49 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id E73A7E09BA; Thu, 13 Jun 2013 16:34:36 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 6739AE09B9 for ; Thu, 13 Jun 2013 16:34:31 +0000 (UTC) Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3E48633E0B3 for ; Thu, 13 Jun 2013 16:34:30 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by hornbill.gentoo.org (Postfix) with ESMTP id B762AE546E for ; Thu, 13 Jun 2013 16:34:26 +0000 (UTC) From: "André Erdmann" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "André Erdmann" Message-ID: <1370378508.9a3e68c735ae1caa084dba046e2f317ea85f1d13.dywi@gentoo> Subject: [gentoo-commits] proj/R_overlay:master commit in: roverlay/packagerules/actions/ X-VCS-Repository: proj/R_overlay X-VCS-Files: roverlay/packagerules/actions/attach.py X-VCS-Directories: roverlay/packagerules/actions/ X-VCS-Committer: dywi X-VCS-Committer-Name: André Erdmann X-VCS-Revision: 9a3e68c735ae1caa084dba046e2f317ea85f1d13 X-VCS-Branch: master Date: Thu, 13 Jun 2013 16:34:26 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 1487d570-e597-495b-a9b6-4d505b13c74f X-Archives-Hash: f5b0b37d3cc81d2fbcb081d05c78a70c commit: 9a3e68c735ae1caa084dba046e2f317ea85f1d13 Author: André Erdmann mailerd de> AuthorDate: Tue Jun 4 20:41:48 2013 +0000 Commit: André Erdmann mailerd de> CommitDate: Tue Jun 4 20:41:48 2013 +0000 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/R_overlay.git;a=commit;h=9a3e68c7 package rules, actions/attach: lazy actions! Some actions cannot be applied when apply_action() is called. So-called "lazy" actions attach themselves to a PackageInfo instance. Afterwards, the pkg info has to take care about applying the action as soon as enough data (info dict) is available. Super lazy actions will try to apply an action directly before attaching themselves to a package. Don't use them unless you really don't care when (and if!) an action should be applied. PackageInfo currently lacks support for lazy actions, so trying to use them results in runtime exceptions (NotImplementedError). --- roverlay/packagerules/actions/attach.py | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) diff --git a/roverlay/packagerules/actions/attach.py b/roverlay/packagerules/actions/attach.py new file mode 100644 index 0000000..e70c8cb --- /dev/null +++ b/roverlay/packagerules/actions/attach.py @@ -0,0 +1,85 @@ +# R overlay -- package rule actions, "lazy" actions +# -*- coding: utf-8 -*- +# Copyright (C) 2013 André Erdmann +# Distributed under the terms of the GNU General Public License; +# either version 2 of the License, or (at your option) any later version. + +__all__ = [ 'LazyAction', 'SuperLazyAction', ] + + +import roverlay.packagerules.abstract.actions + + +class LazyAction ( roverlay.packagerules.abstract.actions.PackageRuleAction ): + """A lazy action simply adds an action to a PackageInfo object. + The action can then be applied later on (initiated by the pkg info). + + Note that this cannot be used to filter out packages. + """ + + def __init__ ( self, actual_action, priority=1000 ): + """Constructor for LazyAction. + + arguments: + * actual_action -- object implementing at least apply_action(^1). + * priority -- + """ + super ( LazyAction, self ).__init__ ( priority=priority ) + self._action = actual_action + # --- end of __init__ (...) --- + + def can_apply_action ( self, p_info ): + """Returns True if the stored action can be applied to p_info, + else False. + """ + raise NotImplementedError ( "derived classes have to implement this." ) + # --- end of can_apply_action (...) --- + + def apply_action ( self, p_info ): + """Attaches this action to p_info's lazy actions. + + arguments: + * p_info + """ + p_info.attach_lazy_action ( self ) + # --- end of apply_action (...) --- + + def try_apply_action ( self, p_info ): + """Tries to apply the stored action. + + Returns True if the action could be applied (action condition evaluated + to True), else False. + + Make sure to remove this action from p_info once it has been applied. + + arguments: + * p_info -- + """ + if self.can_apply_action ( p_info ): + if self._action.apply_action ( p_info ) is False: + raise RuntimeError ( "lazy actions cannot filter out packages." ) + else: + return True + else: + return False + # --- end of try_apply_action (...) --- + + def gen_str ( self, level ): + for s in self._action.gen_str ( level ): yield s + # --- end of gen_str (...) --- + +# --- end of LazyAction --- + + +class SuperLazyAction ( LazyAction ): + """Like LazyAction, but tries to apply the action before attaching it. + Useful if it's unknown whether an action can be applied a PackageInfo + instance directly or not, costs one more check per package if it cannot + be applied directly.""" + + def apply_action ( self, p_info ): + if not self.try_apply_action ( p_info ): + p_info.attach_lazy_action ( self ) + # --- end of apply_action (...) --- + +# --- end of SuperLazyAction ---