Python 终端下的TUI开发,whiptail 的 Python 封装

Python 终端下的TUI开发

MrNeo Chen (netkiller)陈景峰(BG7NYT)

版权 © 2011, 2012 http://netkiller.github.com

摘要

 

 

 

Python代码   收藏代码
  1. #!/usr/bin/env python3  
  2. #/bin/env python3  
  3. #-*- coding: utf-8 -*-  
  4. ##############################################  
  5. # Home  : http://netkiller.sourceforge.net/  
  6. # Author: Neo <openunix@163.com>  
  7. ##############################################  
  8. # vim:ts=4:sw=4  
  9.   
  10. import os,subprocess  
  11.   
  12. class Whiptail():  
  13.       
  14.     def __init__(self,title = None, backtitle = ""):  
  15.         if title :  
  16.             self.t = '"''"'+ title +'"'  
  17.         if backtitle :  
  18.             self.backtitle = '"''"'+ backtitle +'"'  
  19.         else:  
  20.             backtitle = ""  
  21.         self.ismenu = False  
  22.     def title(self, tmp):  
  23.         self.t = tmp  
  24.         return(self)  
  25.     def inputbox(self, label, hight, width, init = ""):  
  26.         self.form = '--inputbox' + ' "'+label+'" ' +  str(hight) + ' ' + str(width) + ' '+init  
  27.         return(self)  
  28.     def passwordbox(self, label, hight, width):  
  29.         self.form = '--passwordbox' + ' "'+label+'" ' +  str(hight) + ' ' + str(width)  
  30.         return(self)  
  31.     def menu(self,lable, item, hight, width, listheight):  
  32.         menuitem = ''  
  33.         for i in item:  
  34.             key,value = i  
  35.             menuitem = menuitem + '"''"'+key + '" "' + value + '" ' 
  36.         self.form = '--menu' + ' "'+lable+'" ' +  str(hight) + ' ' + str(width) + ' ' + str(listheight) +' '+ menuitem 
  37.         self.ismenu = True 
  38.         return(self) 
  39.     def yesno(self, text, hight, width): 
  40.         self.form = '--yesno' + ' "'+text+'" ' +  str(hight) + ' ' + str(width) 
  41.         return(self) 
  42.     def radiolist(self, text, height, width, listheight, item): 
  43.         radioitem = '' 
  44.         for i in item: 
  45.             tag, item, status = i 
  46.             radioitem = radioitem + '"'+tag + '" "' + item + '" "' + status + '" '  
  47.         self.radiolist = '--yesno' + ' "'+text+'" ' +  str(height) + ' ' + str(width) + ' ' + str(listheight) + ' ' + radioitem  
  48.         return(self)  
  49.     def run(self):  
  50.         screen = None  
  51.         #whiptail = 'whiptail ' + ' --title ' + self.t + ' ' + self.form + ' 3>&1 1>&2 2>&3'  
  52.         #screen = os.system(whiptail)  
  53.         whiptail = 'whiptail ' + ' --title ' + self.t + ' --backtitle ' + self.backtitle +' ' + self.form + ' 3>&2 2>&1 1>&3 | tee /tmp/.whiptail'  
  54.         #screen = subprocess.getoutput(whiptail)  
  55.         try:  
  56.             #screen = subprocess.call('/usr/bin/whiptail', whiptail, shell=True)  
  57.             #p = subprocess.check_output(['/usr/bin/whiptail',whiptail], stderr = subprocess.PIPE, shell=False, universal_newlines=True)  
  58.               
  59.             #p = subprocess.Popen('/usr/bin/whiptail', whiptail, stderr = subprocess.PIPE, shell = False)     
  60.             #p.stdin.write('3\n')     
  61.             #p.stdin.write('4\n')     
  62.             #screen = p.stdout.read()   
  63.             #print(p)  
  64.             #screen = p.stderr.read()     
  65.             os.system(whiptail)  
  66.             f = open('/tmp/.whiptail','r')  
  67.             screen = f.read()  
  68.             f.close()  
  69.         except OSError as e:  
  70.             print(e)  
  71.   
  72.         #print(whiptail)  
  73.   
  74.         return(screen)  
  75.   
  76. class MySQL():  
  77.     mysql='mysql'  
  78.     def __init__(self, hosts = None):  
  79.         self.dialog = Whiptail('MySQL Adminstrator','MySQL Client')  
  80.         self.dbhost = None  
  81.         self.dbuser = None  
  82.         self.dbpass = None  
  83.         if hosts :  
  84.             self.hosts = hosts  
  85.     def test(self):  
  86.         dialog = Whiptail('Test')  
  87.         dialog.inputbox('Windows'1020).run()  
  88.     def menu(self):  
  89.         hosts = []  
  90.         hosts.append(('1','MySQL Manager'))  
  91.         hosts.append(('2','MySQL Backup'))  
  92.         hosts.append(('3','MySQL Restore'))  
  93.         m = self.dbhost = self.dialog.menu('Menu', hosts, 15303).run()  
  94.         return(m)  
  95.     def host(self, hosts):  
  96.         self.dbhost = self.dialog.menu('Database Host', hosts, 20405).run()  
  97.     def login(self):  
  98.         hight = 8  
  99.         width = 40  
  100.         self.dbuser = self.dialog.inputbox('User', hight, width, 'root').run()  
  101.         self.dbpass = self.dialog.passwordbox('Password', hight, width).run()  
  102.         pass  
  103.     def dump(self,hosts):  
  104.         hight = 8  
  105.         width = 40  
  106.         self.host(self.hosts)  
  107.         self.login()  
  108.         dbname = self.dialog.inputbox('Database', hight, width).run()  
  109.         dbfile = self.dialog.inputbox('File', hight, width).run()  
  110.         yesno = self.dialog.yesno('Backup?', hight, width).run()  
  111.         cmd = """mysqldump -h%s -u%s -p%s %s | gzip > /opt/backup/%s""" % (self.dbhost, self.dbuser, self.dbpass, dbname, dbfile)  
  112.         #print(cmd)  
  113.         #print(yesno)  
  114.         #if yesno :  
  115.         os.system(cmd)  
  116.     def client(self,hosts):  
  117.         hight = 8  
  118.         width = 40  
  119.         self.host(hosts)  
  120.         self.login()  
  121.         dbname = self.dialog.inputbox('Database', hight, width).run()  
  122.         cmd = """mysql -h%s -u%s -p%s %s""" % (self.dbhost, self.dbuser, self.dbpass, dbname)  
  123.         #print(cmd)  
  124.         os.system( cmd )  
  125.     def restore(self,hosts):  
  126.         hight = 8  
  127.         width = 40    
  128.         self.host(hosts)  
  129.         self.login()  
  130.         files = os.listdir('/opt/backup')  
  131.         #print(files)  
  132.         menu = []  
  133.         i = 0  
  134.         for f in files:  
  135.             menu.append((str(i),f))  
  136.             i = i + 1  
  137.         dbfile = self.dbhost = self.dialog.menu('Backup History', menu, 20307).run()  
  138.         dbname = self.dialog.inputbox('Database', hight, width).run()  
  139.         cmd = """zcat %s | mysql -h%s -u%s -p%s %s""" % (backup, self.dbhost, self.dbuser, self.dbpass, dbname)  
  140.         #print(cmd)  
  141.         os.system( cmd )  
  142.     def logout(self):  
  143.         pass  
  144.     def close(self):  
  145.         pass  
  146.       
  147.     def main(self):  
  148.         m = self.menu()  
  149.         print(m)  
  150.         if m == '2':  
  151.             self.dump(self.hosts)  
  152.         elif m == '3':  
  153.             self.restore(self.hosts)  
  154.         else:  
  155.             self.client(self.hosts)  
  156.       
  157. if __name__ == '__main__':  
  158.     try:  
  159.         hosts = []  
  160.         hosts.append(('127.0.0.1','localhost'))  
  161.         hosts.append(('172.16.0.1','mysql master'))  
  162.         hosts.append(('172.16.0.2','mysql slave'))  
  163.               
  164.         mysql = MySQL(hosts)  
  165.         mysql.main()  
  166.     except KeyboardInterrupt:  
  167.         print ("Crtl+C Pressed. Shutting down.")  
  168.         os.exit()  
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值