[apparmor] [patch] ui.py - when using (N)ew, set selected path as default input

Kshitij Gupta kgupta8592 at gmail.com
Wed Aug 6 08:30:20 UTC 2014


Hello,

On Wed, Aug 6, 2014 at 4:45 AM, Christian Boltz <apparmor at cboltz.de> wrote:
> Hello,
>
> this patch sets the currently selected path as (editable) default when
> using "(N)ew" in aa-logprof or aa-genprof.
>
> Credits go to
> http://stackoverflow.com/questions/2533120/show-default-value-for-editing-on-python-input-possible ;-)
>

Wow you found it! I recall struggling with curses (a module) for the
same. Didn't know the solution was so simple. :-)

> Also, some hate goes to the python devs because they changed the meaning
> of input() between py2 and py3 in a terrible way. (Yes, what py2 input()
> did was crazy, but it's even more crazy to change its behaviour.)
>

A consistent way for input/output is using sys.stdin/sys.stdout but as
docs state:
"Settings made using this module affect the behaviour of both the
interpreter’s interactive prompt and the prompts offered by the
built-in raw_input() and input() function."

I guess we have to live with the "sys.version_info[0] >= 3" part for this.

> If someone knows a sane replacement for input() / raw_input() that avoids
> the version switch, I'm open for suggestions ;-)
>
> Note: I tested this patch only with py3.
>
>
> === modified file 'utils/apparmor/ui.py'
> --- utils/apparmor/ui.py        2014-02-24 19:56:28 +0000
> +++ utils/apparmor/ui.py        2014-08-05 23:05:47 +0000
> @@ -13,6 +13,7 @@
>  # ----------------------------------------------------------------------
>  import sys
>  import re
> +import readline
>  from apparmor.yasti import yastLog, SendDataToYast, GetDataFromYast
>
>  from apparmor.common import readkey, AppArmorException, DebugLogger
> @@ -170,8 +171,16 @@
>      debug_logger.debug('UI_GetString: %s: %s %s' % (UI_mode, text, default))
>      string = default
>      if UI_mode == 'text':
> -        sys.stdout.write('\n' + text)
> -        string = sys.stdin.readline()
> +        readline.set_startup_hook(lambda: readline.insert_text(default))
> +        try:
> +            if sys.version_info[0] >= 3:
> +                string = input(text)

You gobbled a: '\n' before text
Does it look better without the newline? ;-)

> +            else:
> +                string = raw_input(text)
> +        except EOFError:
> +            string = ''
> +        finally:
> +            readline.set_startup_hook()
>      else:
>          SendDataToYast({'type': 'dialog-getstring',
>                          'label': text,
>
>

Tested the readline part in py2 and py3, no reason rest of it
shouldn't work for py2(unfortunately I dont have the libapparmor for
py2 to test) if it did for py3.

Would it be worth the effort to wrap it in a function and move it to
common.py? Maybe other tools could use it at some point.

Regards,

Kshitij Gupta

> Regards,
>
> Christian Boltz
> --
> Ich denk in Prag werd ich dann hauptsächlich für Euch Kaffee kochen. :)
> [Lars Müller in opensuse-de]
>
>
> --
> AppArmor mailing list
> AppArmor at lists.ubuntu.com
> Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/apparmor



More information about the AppArmor mailing list