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

 

Python 终端下的TUI开发

MrNeo Chen (netkiller)陈景峰(BG7NYT)

摘要

 

 

 

#!/usr/bin/env python3
#/bin/env python3
#-*- coding: utf-8 -*-
##############################################
# Home  : http://netkiller.sourceforge.net/
# Author: Neo <[email protected]>
##############################################
# vim:ts=4:sw=4

import os,subprocess

class Whiptail():
	
	def __init__(self,title = None, backtitle = ""):
		if title :
			self.t = '"'+ title +'"'
		if backtitle :
			self.backtitle = '"'+ backtitle +'"'
		else:
			backtitle = ""
		self.ismenu = False
	def title(self, tmp):
		self.t = tmp
		return(self)
	def inputbox(self, label, hight, width, init = ""):
		self.form = '--inputbox' + ' "'+label+'" ' +  str(hight) + ' ' + str(width) + ' '+init
		return(self)
	def passwordbox(self, label, hight, width):
		self.form = '--passwordbox' + ' "'+label+'" ' +  str(hight) + ' ' + str(width)
		return(self)
	def menu(self,lable, item, hight, width, listheight):
		menuitem = ''
		for i in item:
			key,value = i
			menuitem = menuitem + '"'+key + '" "' + value + '" '
		self.form = '--menu' + ' "'+lable+'" ' +  str(hight) + ' ' + str(width) + ' ' + str(listheight) +' '+ menuitem
		self.ismenu = True
		return(self)
	def yesno(self, text, hight, width):
		self.form = '--yesno' + ' "'+text+'" ' +  str(hight) + ' ' + str(width)
		return(self)
	def radiolist(self, text, height, width, listheight, item):
		radioitem = ''
		for i in item:
			tag, item, status = i
			radioitem = radioitem + '"'+tag + '" "' + item + '" "' + status + '" '
		self.radiolist = '--yesno' + ' "'+text+'" ' +  str(height) + ' ' + str(width) + ' ' + str(listheight) + ' ' + radioitem
		return(self)
	def run(self):
		screen = None
		#whiptail = 'whiptail ' + ' --title ' + self.t + ' ' + self.form + ' 3>&1 1>&2 2>&3'
		#screen = os.system(whiptail)
		whiptail = 'whiptail ' + ' --title ' + self.t + ' --backtitle ' + self.backtitle +' ' + self.form + ' 3>&2 2>&1 1>&3 | tee /tmp/.whiptail'
		#screen = subprocess.getoutput(whiptail)
		try:
			#screen = subprocess.call('/usr/bin/whiptail', whiptail, shell=True)
			#p = subprocess.check_output(['/usr/bin/whiptail',whiptail], stderr = subprocess.PIPE, shell=False, universal_newlines=True)
			
			#p = subprocess.Popen('/usr/bin/whiptail', whiptail, stderr = subprocess.PIPE, shell = False)   
			#p.stdin.write('3\n')   
			#p.stdin.write('4\n')   
			#screen = p.stdout.read() 
			#print(p)
			#screen = p.stderr.read() 	
			os.system(whiptail)
			f = open('/tmp/.whiptail','r')
			screen = f.read()
			f.close()
		except OSError as e:
			print(e)

		#print(whiptail)

		return(screen)

class MySQL():
	mysql='mysql'
	def __init__(self, hosts = None):
		self.dialog = Whiptail('MySQL Adminstrator','MySQL Client')
		self.dbhost = None
		self.dbuser = None
		self.dbpass = None
		if hosts :
			self.hosts = hosts
	def test(self):
		dialog = Whiptail('Test')
		dialog.inputbox('Windows', 10, 20).run()
	def menu(self):
		hosts = []
		hosts.append(('1','MySQL Manager'))
		hosts.append(('2','MySQL Backup'))
		hosts.append(('3','MySQL Restore'))
		m = self.dbhost = self.dialog.menu('Menu', hosts, 15, 30, 3).run()
		return(m)
	def host(self, hosts):
		self.dbhost = self.dialog.menu('Database Host', hosts, 20, 40, 5).run()
	def login(self):
		hight = 8
		width = 40
		self.dbuser = self.dialog.inputbox('User', hight, width, 'root').run()
		self.dbpass = self.dialog.passwordbox('Password', hight, width).run()
		pass
	def dump(self,hosts):
		hight = 8
		width = 40
		self.host(self.hosts)
		self.login()
		dbname = self.dialog.inputbox('Database', hight, width).run()
		dbfile = self.dialog.inputbox('File', hight, width).run()
		yesno = self.dialog.yesno('Backup?', hight, width).run()
		cmd = """mysqldump -h%s -u%s -p%s %s | gzip > /opt/backup/%s""" % (self.dbhost, self.dbuser, self.dbpass, dbname, dbfile)
		#print(cmd)
		#print(yesno)
		#if yesno :
		os.system(cmd)
	def client(self,hosts):
		hight = 8
		width = 40
		self.host(hosts)
		self.login()
		dbname = self.dialog.inputbox('Database', hight, width).run()
		cmd = """mysql -h%s -u%s -p%s %s""" % (self.dbhost, self.dbuser, self.dbpass, dbname)
		#print(cmd)
		os.system( cmd )
	def restore(self,hosts):
		hight = 8
		width = 40	
		self.host(hosts)
		self.login()
		files = os.listdir('/opt/backup')
		#print(files)
		menu = []
		i = 0
		for f in files:
			menu.append((str(i),f))
			i = i + 1
		dbfile = self.dbhost = self.dialog.menu('Backup History', menu, 20, 30, 7).run()
		dbname = self.dialog.inputbox('Database', hight, width).run()
		cmd = """zcat %s | mysql -h%s -u%s -p%s %s""" % (backup, self.dbhost, self.dbuser, self.dbpass, dbname)
		#print(cmd)
		os.system( cmd )
	def logout(self):
		pass
	def close(self):
		pass
	
	def main(self):
		m = self.menu()
		print(m)
		if m == '2':
			self.dump(self.hosts)
		elif m == '3':
			self.restore(self.hosts)
		else:
			self.client(self.hosts)
	
if __name__ == '__main__':
	try:
		hosts = []
		hosts.append(('127.0.0.1','localhost'))
		hosts.append(('172.16.0.1','mysql master'))
		hosts.append(('172.16.0.2','mysql slave'))
		    
		mysql = MySQL(hosts)
		mysql.main()
	except KeyboardInterrupt:
		print ("Crtl+C Pressed. Shutting down.")
		os.exit()
 

 

 

你可能感兴趣的:(mysql,python,netkiller,MysqlDump,whiptail)