[apparmor] [patch] ui.py - when using (N)ew, set selected path as default input
Christian Boltz
apparmor at cboltz.de
Wed Aug 6 10:13:10 UTC 2014
Hello,
Am Mittwoch, 6. August 2014 schrieb Kshitij Gupta:
> On Wed, Aug 6, 2014 at 4:45 AM, Christian Boltz wrote:
> > 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-ed
> > iting-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. :-)
Once you know "readline" is the keyword, searching is quite easy ;-)
> > 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.
Well, we'll survive it ;-)
> > - 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? ;-)
No, it doesn't. Good catch!
Slightly updated patch ('\n' added):
=== modified file 'utils/apparmor/ui.py'
--- utils/apparmor/ui.py 2014-02-24 19:56:28 +0000
+++ utils/apparmor/ui.py 2014-08-06 10:02:59 +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('\n' + text)
+ else:
+ string = raw_input('\n' + 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.
I'll build a new py2 libapparmor package when 2.9 beta2 is out. It's
just a build switch, but building for both py2 and py3 would make the
package more interesting[tm].
> 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.
I'm looking forward for a saner_than_python_input() function ;-)
Seriously: If we need it again somewhere, we really should split it
out (including the try/except stuff), but for now I'd keep it inside
UI_GetString().
Regards,
Christian Boltz
--
Nur so aus Interesse: Bist Du in die Gesellschaft "Rettet das
Semikolon!" eingetreten? ;-)
[Jan Trippler in suse-linux über ein Script von David Haller]
More information about the AppArmor
mailing list