python实现tui_python tui

场景:Python 终端上的TUI开发,whiptail 的 Python 封装

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

Python 终端下的TUI开发

Mr. Neo Chen (netkiller), 陈景峰(BG7NYT)

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

摘要

下面是我多年积累下来的经验总结,整理成文档供大家参考:

Netkiller Architect 手札

Netkiller Linux 手札

Netkiller Developer 手札

Netkiller Database 手札

Netkiller Debian 手札

Netkiller CentOS 手札

Netkiller FreeBSD 手札

Netkiller Shell 手札

Netkiller Web 手札

Netkiller Monitoring 手札

Netkiller Storage 手札

Netkiller Mail 手札

Netkiller Security 手札

Netkiller Multimedia 手札

Netkiller Writer 手札

Netkiller Version 手札

Netkiller PostgreSQL 手札

Netkiller MySQL 手札

Netkiller Cryptography 手札

Netkiller Cisco IOS 手札

Netkiller LDAP 手札

Netkiller Intranet 手札

#!/usr/bin/env python3

#/bin/env python3

#-*- coding: utf-8 -*-

##############################################

# Home : http://netkiller.sourceforge.net/

# Author: Neo

##############################################

# 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()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值