|
|||||||||||
|
Re: bash vs. python scripts - which one is better?
From: Douglas A. Tutty <dtutty(at)porchlight.ca>
Date: Mon Aug 20 2007 - 20:35:33 EDT
Here's my samples from my notes directory. There's one for curses and one that does a couple of ways of using dialog, the latter way uses a dialog module I wrote and include at the end. The module takes care of packing the strings that go to the dialog command line making the main program line clean and neat. Disclaimer: I've only used this a couple of times since most of my stuff uses normal text i/o. Doug. curses:
#!/usr/bin/env python
import curses
try:
Dialog: #!/usr/bin/python -O """ Tests the Dialog examples, in python, using different ways """
# Import statements
# "Constants"
# Definition of classes and functions # Main Function if (__name__=="__main__"): # message box Dialog_title=' --title "Message Box"' Dialog_options=' --clear' Dialog_box_options=' --msgbox' Dialog_text=' "Hi, this is a simple message box. You can use this to' Dialog_text+=' display any message you like. The box will remain until' Dialog_text+=' you press the ENTER key."' Dialog_height=' 10' Dialog_width=' 41' Dialog_CMD=DIALOG + Dialog_title + Dialog_options + Dialog_box_options + Dialog_text Dialog_CMD+=Dialog_height + Dialog_width os.system(Dialog_CMD) #input box Dialog_CMD=DIALOG + ' --title "InputBox" --clear --inputbox \"Hi, this is an input dialog box. You can use this to ask questions \ that require the user to input a string as the answer. You can \ input strings longer than the width of the input box, in that case, the \ input field will be automatially scrolled. You can use BACKSPACE to \ correct errors.\ \ Try entering your name below:" \ 16 51' #rc=os.system(Dialog_CMD) # rc is 0 since dialog exited OK. we need to see stderr #use os.popen (or if necesary popen4) w,rc=os.popen4(Dialog_CMD) del w print rc.read() del rc ## file selector doesn't work right ## now try dialog module hello=dialog.Dialog() # message box hello.title='--title "Message Box" ' hello.boxtype='--msgbox ' hello.text='"Message box using dialog class" ' hello.height='10 ' hello.width='41 ' w,r=os.popen4(hello.show()) print r.read() Here's the dialog module:
#dialog.py
Module wrapper for /usr/bin/dialog. See man page.
Options for dialog are attributes of a dialog instance.
e.g. instead of adding --title, do:
Remember to include a trailing space in each attribute method pack() returns the complete dialog to run using: Dialog_CMD=dialog.show() w,r = os.popen4(Dialog_CMD) This allows access to stdin and stdout/stderr. Guage bars, for example, read from stdin until get EOF. """ class Dialog: """Object version of Dialog: attributes same as Dialog's options""" def __init__(self): self.DIALOG = " /usr/bin/dialog " # common options self.aspect='' self.backtitle='' self.beep='' self.beepafter='' self.begin='' self.cancellabel='' self.clear='' self.crwrap='' self.defaultno='' self.defaultitem='' self.helpbutton='' self.helplabel='' self.itemhelp='' self.maxinput='' self.nokill='' self.nocancel='' self.noshadow='' self.oklabel='' self.printmaxsize='' self.printsize='' self.separateoutput='' self.separatewidget='' self.shadow='' self.sleep='' self.stdout='' self.tabcorrect='' self.tablen='' self.timeout='' self.title='' self.trim='' # box type and box options #this is only the type, e.g. obj.box_type='infobox' self.boxtype='' self.text='' self.height='' self.width='' self.boxoptions='' #this is other box options specific to the type of box, placed # after text, height, and width def show(self): self.Dialog_CMD=self.DIALOG + self.aspect + self.backtitle\ + self.beep + self.beepafter + self.begin \ + self.cancellabel + self.clear + self.crwrap \ + self.defaultno + self.defaultitem + self.helpbutton\ + self.helplabel + self.itemhelp\ + self.maxinput + self.nokill + self.nocancel \ + self.nocancel + self.noshadow + self.oklabel \ + self.printmaxsize + self.printsize \ + self.separateoutput + self.separatewidget \ + self.shadow + self.sleep + self.stdout + self.tabcorrect \ + self.tablen + self.timeout + self.title + self.trim \ + self.boxtype + self.text + self.height + self.width \ + self.boxoptions return(self.Dialog_CMD) -- To UNSUBSCRIBE, email to debian-user-REQUEST@lists.debian.org with a subject of "unsubscribe". Trouble? Contact listmaster@lists.debian.orgReceived on Mon Aug 20 20:37:32 2007 This archive was generated by hypermail 2.1.8 : Sun Oct 07 2007 - 02:34:08 EDT |
||||||||||
|
|||||||||||