[apparmor] [patch] Add __repr__() functions to BaseRule and BaseRuleset

Kshitij Gupta kgupta8592 at gmail.com
Wed Oct 28 20:06:36 UTC 2015


Hi,

On Fri, Oct 23, 2015 at 5:58 PM, Christian Boltz <apparmor at cboltz.de> wrote:

> Hello,
>
> __repr__() makes print()ing a class object much more helpful - instead of
>     <apparmor.rule.NetworkRule object at 0x7f416b239e48>
> we now get something like
>     <NetworkRule> network inet stream,
> (based on get_raw())
>
> A NetworkRuleset will be printed as (also based on get_raw())
>
> <NetworkRuleset>
>   network inet stream,
>   allow network inet stream, # comment
> </NetworkRuleset>
>
Nice! I like it.

>
> Also add tests to test-network.py to ensure that __repr__() works as
> expected.
>
>
>
> [ 06-baserule-add-repr.diff ]
>
> === modified file ./utils/apparmor/rule/__init__.py
> --- utils/apparmor/rule/__init__.py     2015-06-06 14:53:16.868029000 +0200
> +++ utils/apparmor/rule/__init__.py     2015-10-23 14:08:28.639214618 +0200
> @@ -50,6 +50,10 @@
>          # Set only in the parse() class method
>          self.raw_rule = None
>
> +    def __repr__(self):
> +        classname = self.__class__.__name__
> +        return '<%s> ' % classname + self.get_raw()
> +
>      @classmethod
>      def match(cls, raw_rule):
>          '''return True if raw_rule matches the class (main) regex, False
> otherwise
> @@ -211,6 +220,10 @@
>          '''called by __init__() and delete_all_rules() - override in
> child class to initialize more variables'''
>          pass
>
> +    def __repr__(self):
> +        classname = self.__class__.__name__
> +        return '<%s>\n' % classname + '\n'.join(self.get_raw(1)) +
> '</%s>' % classname
> +
>      def add(self, rule):
>          '''add a rule object'''
>          self.rules.append(rule)
> === modified file ./utils/test/test-network.py
> --- utils/test/test-network.py  2015-06-25 22:38:01.489218498 +0200
> +++ utils/test/test-network.py  2015-10-23 14:12:49.553087103 +0200
> @@ -353,6 +353,15 @@
>          obj = NetworkRule._parse(params)
>          self.assertEqual(obj.logprof_header(), expected)
>
> +class NetworkRuleReprTest(AATest):
> +    tests = [
> +        (NetworkRule('inet', 'stream'),
>  '<NetworkRule> network inet stream,'),
> +        (NetworkRule.parse(' allow  network  inet  stream, # foo'),
> '<NetworkRule> allow  network  inet  stream, # foo'),
> +    ]
> +    def _run_test(self, params, expected):
> +        self.assertEqual(str(params), expected)
> +
> +
>  ## --- tests for NetworkRuleset --- #
>
>  class NetworkRulesTest(AATest):
> @@ -439,6 +448,17 @@
>  class NetworkDeleteTestAATest(AATest):
>      pass
>
> +class NetworkRulesetReprTest(AATest):
> +    def test_network_ruleset_repr(self):
> +        obj = NetworkRuleset()
> +        obj.add(NetworkRule('inet', 'stream'))
> +        obj.add(NetworkRule.parse(' allow  network  inet  stream, # foo'))
> +
> +        expected = '<NetworkRuleset>\n  network inet stream,\n  allow
> network  inet  stream, # foo\n</NetworkRuleset>'
> +        self.assertEqual(str(obj), expected)
> +
> +
> +
>  setup_all_loops(__name__)
>  if __name__ == '__main__':
>      unittest.main(verbosity=2)
>
Thanks for the patch.

Acked-by: Kshitij Gupta <kgupta8592 at gmail.com>.

>
>
> Regards,
>
> Christian Boltz
> --
> > Mich nervt es tierisch an, wenn ich am Tag mehr Meldungen des Typs
> > "Sie haben eine Virus-Mail verschickt" bekomme als Spam-Mails.
> > Mittlerweile dürfte hinreichend bekannt sein, dass sich diese miesen
> > Schädlinge mit beliebigen Absenderadressen versorgen. Solche Mails
> > nutzen mir also gar nichts und erzeugen nur unnützen Traffic.
> Soll ich Dir ein paar spams schicken, damit das Verhältnis wieder
> stimmt? [> Michael Hoehne und Bernd Brodeßer in suse-linux]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/apparmor
>



-- 
Regards,

Kshitij Gupta
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/apparmor/attachments/20151029/ab770047/attachment.html>


More information about the AppArmor mailing list