1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
'''
@author: smileorigin
@license: (C) Copyright 2017
@contact: smileorigin@foxmail.com
@file: hexo_utils.py
@time: 12/3/2017 7:02 PM
@desc:
'''
import os from tkinter import * from tkinter import messagebox
class MainView(Frame): bg = 'white' bt_bg = '#E91E63' text_color = '#fff' about_msg = 'Author: smileorigin\nBlog: smileorigin.site\nEmail: smileorigin@foxmail.com'
def __init__(self, generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback, master=None): Frame.__init__(self, master, bg=self.bg) self.pack(expand=YES, fill='both') self.createWidgets(generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback)
def createWidgets(self, generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback): self.generator_bt = Button(self, command=generatorCallback, text='Generator', width=20, bg=self.bt_bg, fg=self.text_color) self.generator_bt.pack(pady=10, padx=20)
self.deploy_bt = Button(self, command=deployCallback, text='Deploy', width=20, bg=self.bt_bg, fg=self.text_color) self.deploy_bt.pack()
self.generator_and_deploy_bt = Button(self, command=generatorAndDeployCallback, text='GeneratorAndDeploy', width=20, bg=self.bt_bg, fg=self.text_color) self.generator_and_deploy_bt.pack(pady=10)
self.server_bt = Button(self, command=serverCallback, text='Server', width=20, bg=self.bt_bg, fg=self.text_color) self.server_bt.pack()
self.server_custom_port = Button(self, command=serverCustomPortCallback, text='ServerWithCustomPort', width=20, bg=self.bt_bg, fg=self.text_color) self.server_custom_port.pack(pady=8)
self.custom_port_label = Label(self, text="Port:", bg=self.bg) self.custom_port_label.pack(fill='x')
hint = StringVar() hint.set('5000') self.custom_port_entry = Entry(self, textvariable=hint, bg=self.bg) self.custom_port_entry.pack()
self.about_bt = Button(self, text='(⓿_⓿)', command=self.showMessage, bg=self.bt_bg, fg=self.text_color) self.about_bt.pack(pady=10)
def showMessage(self): messagebox.showinfo('About', self.about_msg)
cmd_clean = 'hexo clean'
cmd_generator = 'hexo g'
cmd_deploy = 'hexo d'
cmd_server = 'hexo s'
width = 230 height = 280
star = '*'
star_num = 84
generator_start_text = ' generator start ' generator_done_text = ' generator done ' deploy_start_text = ' deploy start ' deploy_done_text = ' deploy done ' server_start_text = ' server start ' server_done_text = ' server done' welcome_text = ' welcome '
def serverWithAnotherPort(port): return cmd_server + ' -p ' + port
def executeCommand(cmd): os.system(command=cmd)
def regexFourNum(str): p = re.compile('^[0-9]{4}') return p.match(str)
def printStar(num): print(star * num)
def printStarNotEnter(num): print(star * num, end='')
def printStringWithStar(num, string): printStar(star_num) string_len = len(string) half_star_num = (int)((num - string_len) / 2) remainder = num - half_star_num * 2 - string_len printStarNotEnter(half_star_num + remainder) print(string, end='') printStar(half_star_num) printStar(star_num)
def generatorCallback(): printStringWithStar(star_num, generator_start_text) executeCommand(cmd_clean) executeCommand(cmd_generator) printStringWithStar(star_num, generator_done_text)
def deployCallback(): printStringWithStar(star_num, deploy_start_text) executeCommand(cmd_deploy) printStringWithStar(star_num, deploy_done_text)
def serverCallback(): printStringWithStar(star_num, server_start_text) try: executeCommand(cmd_server) except KeyboardInterrupt: printStringWithStar(star_num, server_done_text)
def serverCustomPortCallback(): custom_port = str(main_view.custom_port_entry.get()) if custom_port: if regexFourNum and custom_port.__len__() == 4: printStringWithStar(star_num, server_start_text)
try: executeCommand(serverWithAnotherPort(custom_port)) except KeyboardInterrupt: printStringWithStar(star_num, server_done_text) else: messagebox.showinfo('Input error', 'Port needs 4 digits.Example:5000') else: messagebox.showinfo('Error', 'Please input port.Example:5000')
def generatorAndDeployCallback(): generatorCallback() deployCallback()
printStringWithStar(star_num, welcome_text) root = Tk() main_view = MainView(generatorCallback, deployCallback, serverCallback, serverCustomPortCallback, generatorAndDeployCallback, master=root) root.title("") root.resizable(0, 0) size = '{}x{}+{}+{}'.format( width, height, (int)((root.winfo_screenwidth() - width) / 2), (int)((root.winfo_screenheight() - height) / 2)) root.geometry(size)
root.mainloop()
|