<p dir="ltr">Hello,</p>
<p dir="ltr">On Aug 6, 2014 3:43 PM, "Christian Boltz" <<a href="mailto:apparmor@cboltz.de">apparmor@cboltz.de</a>> wrote:<br>
><br>
> Hello,<br>
><br>
> Am Mittwoch, 6. August 2014 schrieb Kshitij Gupta:<br>
> > On Wed, Aug 6, 2014 at 4:45 AM, Christian Boltz wrote:<br>
> > > this patch sets the currently selected path as (editable) default<br>
> > > when using "(N)ew" in aa-logprof or aa-genprof.<br>
> > ><br>
> > > Credits go to<br>
> > > <a href="http://stackoverflow.com/questions/2533120/show-default-value-for-ed">http://stackoverflow.com/questions/2533120/show-default-value-for-ed</a><br>
> > > iting-on-python-input-possible ;-)<br>
> > Wow you found it! I recall struggling with curses (a module) for the<br>
> > same. Didn't know the solution was so simple. :-)<br>
><br>
> Once you know "readline" is the keyword, searching is quite easy ;-)<br>
><br>
> > > Also, some hate goes to the python devs because they changed the<br>
> > > meaning of input() between py2 and py3 in a terrible way. (Yes,<br>
> > > what py2 input() did was crazy, but it's even more crazy to change<br>
> > > its behaviour.)<br>
> > A consistent way for input/output is using sys.stdin/sys.stdout but as<br>
> > docs state:<br>
> > "Settings made using this module affect the behaviour of both the<br>
> > interpreter’s interactive prompt and the prompts offered by the<br>
> > built-in raw_input() and input() function."<br>
> ><br>
> > I guess we have to live with the "sys.version_info[0] >= 3" part for<br>
> > this.<br>
><br>
> Well, we'll survive it ;-)<br>
><br>
> > > -        sys.stdout.write('\n' + text)<br>
> > > -        string = sys.stdin.readline()<br>
> > > +        readline.set_startup_hook(lambda:<br>
> > > readline.insert_text(default)) +        try:<br>
> > > +            if sys.version_info[0] >= 3:<br>
> > > +                string = input(text)<br>
> ><br>
> > You gobbled a: '\n' before text<br>
> > Does it look better without the newline? ;-)<br>
><br>
> No, it doesn't. Good catch!<br>
><br>
> Slightly updated patch ('\n' added):<br>
><br>
> === modified file 'utils/apparmor/ui.py'<br>
> --- utils/apparmor/ui.py        2014-02-24 19:56:28 +0000<br>
> +++ utils/apparmor/ui.py        2014-08-06 10:02:59 +0000<br>
> @@ -13,6 +13,7 @@<br>
>  # ----------------------------------------------------------------------<br>
>  import sys<br>
>  import re<br>
> +import readline<br>
>  from apparmor.yasti import yastLog, SendDataToYast, GetDataFromYast<br>
><br>
>  from apparmor.common import readkey, AppArmorException, DebugLogger<br>
> @@ -170,8 +171,16 @@<br>
>      debug_logger.debug('UI_GetString: %s: %s %s' % (UI_mode, text, default))<br>
>      string = default<br>
>      if UI_mode == 'text':<br>
> -        sys.stdout.write('\n' + text)<br>
> -        string = sys.stdin.readline()<br>
> +        readline.set_startup_hook(lambda: readline.insert_text(default))<br>
> +        try:<br>
> +            if sys.version_info[0] >= 3:<br>
> +                string = input('\n' + text)<br>
> +            else:<br>
> +                string = raw_input('\n' + text)<br>
> +        except EOFError:<br>
> +            string = ''<br>
> +        finally:<br>
> +            readline.set_startup_hook()<br>
>      else:<br>
>          SendDataToYast({'type': 'dialog-getstring',<br>
>                          'label': text,<br>
><br>
><br>
> > Tested the readline part in py2 and py3, no reason rest of it<br>
> > shouldn't work for py2(unfortunately I dont have the libapparmor for<br>
> > py2 to test) if it did for py3.<br>
><br>
> I'll build a new py2 libapparmor package when 2.9 beta2 is out. It's<br>
> just a build switch, but building for both py2 and py3 would make the<br>
> package more interesting[tm].<br>
><br>
Thanks. I look forward to that. :-)</p>
<p dir="ltr">> > Would it be worth the effort to wrap it in a function and move it to<br>
> > common.py? Maybe other tools could use it at some point.<br>
><br>
> I'm looking forward for a saner_than_python_input() function ;-)<br>
><br>
> Seriously: If we need it again somewhere, we really should split it<br>
> out (including the try/except stuff), but for now I'd keep it inside<br>
> UI_GetString().<br>
><br>
><br>
Fair enough.</p>
<p dir="ltr">Thanks fixing this. :-)</p>
<p dir="ltr">Acked-by: Kshitij Gupta <<a href="mailto:kgupta8592@gmail.com">kgupta8592@gmail.com</a>>.</p>
<p dir="ltr">Regards,</p>
<p dir="ltr">Kshitij Gupta</p>
<p dir="ltr">> Regards,<br>
><br>
> Christian Boltz<br>
> --<br>
> Nur so aus Interesse: Bist Du in die Gesellschaft "Rettet das<br>
> Semikolon!" eingetreten? ;-)<br>
> [Jan Trippler in suse-linux  über ein Script von David Haller]<br>
><br>
><br>
> --<br>
> AppArmor mailing list<br>
> <a href="mailto:AppArmor@lists.ubuntu.com">AppArmor@lists.ubuntu.com</a><br>
> Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/apparmor">https://lists.ubuntu.com/mailman/listinfo/apparmor</a></p>