-*- coding: utf8 -*-
#! /usr/bin/env 3.8.0 (3413)
#coding=utf-8
#source path: 账户延期.py
#Compiled at: 1995-09-27 16:18:56
#Powered by BugScaner
import calendar, tkinter as tk
import tkinter.font as tkFont
import tkinter
from tkinter import ttk, messagebox
import datetime, time, pymssql
datetime = calendar.datetime.datetime
timedelta = calendar.datetime.timedelta
sqldic = {'192.168.7.7': "'ftp7'", '192.168.18.7': "'ftp18.7'", '192.168.18.6': "'ftp18.6'", '192.168.7.4': "'ftp4'",
'192.168.7.5': "'ftp7.5'", '192.168.17.6': "'ftp17.6'", '192.168.7.18': "'ftp7-18'",
'192.168.7.22': "'ftp7-22'", '192.168.17.4': "'ftp17-4'", '192.168.17.7': "'ftp17.7'",
'192.168.18.3': "'ftp18.3'", '192.168.19.3': "'ftp19-3'"}
class Calendar:
def __init__(s, point=None, position=None):
s.master = tk.Toplevel()
s.master.withdraw()
fwday = calendar.SUNDAY
year = datetime.now().year
month = datetime.now().month
locale = None
sel_bg = '#ecffc4'
sel_fg = '#05640e'
s._date = datetime(year, month, 1)
s._selection = None
s.G_Frame = ttk.Frame(s.master)
s._cal = s._Calendar__get_calendar(locale, fwday)
s._Calendar__setup_styles()
s._Calendar__place_widgets()
s._Calendar__config_calendar()
s._Calendar__setup_selection(sel_bg, sel_fg)
s._items = [s._calendar.insert('', 'end', values='') for _ in range(6)]
s._update()
s.G_Frame.pack(expand=1, fill='both')
s.master.overrideredirect(1)
s.master.update_idletasks()
width, height = s.master.winfo_reqwidth(), s.master.winfo_reqheight()
if point:
if position:
if position == 'ur':
x, y = point[0], point[1] - height
elif position == 'lr':
x, y = point[0], point[1]
else:
if position == 'ul':
x, y = point[0] - width, point[1] - height
else:
if position == 'll':
x, y = point[0] - width, point[1]
else:
x, y = (s.master.winfo_screenwidth() - width) / 2, (s.master.winfo_screenheight() - height) / 2
s.master.geometry('%dx%d+%d+%d' % (width, height, x, y))
s.master.after(300, s._main_judge)
s.master.deiconify()
s.master.focus_set()
s.master.wait_window()
def __get_calendar(s, locale, fwday):
if locale is None:
return calendar.TextCalendar(fwday)
return calendar.LocaleTextCalendar(fwday, locale)
def __setitem__(s, item, value):
if item in ('year', 'month'):
raise AttributeError("attribute '%s' is not writeable" % item)
else:
if item == 'selectbackground':
s._canvas['background'] = value
else:
if item == 'selectforeground':
s._canvas.itemconfigure((s._canvas.text), item=value)
else:
s.G_Frame.__setitem__(s, item, value)
def __getitem__(s, item):
if item in ('year', 'month'):
return getattr(s._date, item)
if item == 'selectbackground':
return s._canvas['background']
if item == 'selectforeground':
return s._canvas.itemcget(s._canvas.text, 'fill')
r = ttk.tclobjs_to_py({item: ttk.Frame.__getitem__(s, item)})
return r[item]
def __setup_styles(s):
style = ttk.Style(s.master)
arrow_layout = lambda dir: [
(
'Button.focus', {'children': [('Button.%sarrow' % dir, None)]})]
style.layout('L.TButton', arrow_layout('left'))
style.layout('R.TButton', arrow_layout('right'))
def __place_widgets(s):
Input_judgment_num = s.master.register(s.Input_judgment)
hframe = ttk.Frame(s.G_Frame)
gframe = ttk.Frame(s.G_Frame)
bframe = ttk.Frame(s.G_Frame)
hframe.pack(in_=(s.G_Frame), side='top', pady=5, anchor='center')
gframe.pack(in_=(s.G_Frame), fill=(tk.X), pady=5)
bframe.pack(in_=(s.G_Frame), side='bottom', pady=5)
lbtn = ttk.Button(hframe, style='L.TButton', command=(s._prev_month))
lbtn.grid(in_=hframe, column=0, row=0, padx=12)
rbtn = ttk.Button(hframe, style='R.TButton', command=(s._next_month))
rbtn.grid(in_=hframe, column=5, row=0, padx=12)
s.CB_year = ttk.Combobox(hframe, width=5, values=[str(year) for year in range(datetime.now().year, datetime.now().year - 11, -1)], validate='key', validatecommand=(Input_judgment_num, '%P'))
s.CB_year.current(0)
s.CB_year.grid(in_=hframe, column=1, row=0)
s.CB_year.bind('<KeyPress>', lambda event: s._update(event, True))
s.CB_year.bind('<<ComboboxSelected>>', s._update)
tk.Label(hframe, text='年', justify='left').grid(in_=hframe, column=2, row=0, padx=(0,
5))
s.CB_month = ttk.Combobox(hframe, width=3, values=['%02d' % month for month in range(1, 13)], state='readonly')
s.CB_month.current(datetime.now().month - 1)
s.CB_month.grid(in_=hframe, column=3, row=0)
s.CB_month.bind('<<ComboboxSelected>>', s._update)
tk.Label(hframe, text='月', justify='left').grid(in_=hframe, column=4, row=0)
s._calendar = ttk.Treeview(gframe, show='', selectmode='none', height=7)
s._calendar.pack(expand=1, fill='both', side='bottom', padx=5)
ttk.Button(bframe, text='确 定', width=6, command=(lambda: s._exit(True))).grid(row=0, column=0, sticky='ns', padx=20)
ttk.Button(bframe, text='取 消', width=6, command=(s._exit)).grid(row=0, column=1, sticky='ne', padx=20)
tk.Frame((s.G_Frame), bg='#565656').place(x=0, y=0, relx=0, rely=0, relwidth=1, relheigh=0.01)
tk.Frame((s.G_Frame), bg='#565656').place(x=0, y=0, relx=0, rely=0.99, relwidth=1, relheigh=0.01)
tk.Frame((s.G_Frame), bg='#565656').place(x=0, y=0, relx=0, rely=0, relwidth=0.01, relheigh=1)
tk.Frame((s.G_Frame), bg='#565656').place(x=0, y=0, relx=0.99, rely=0, relwidth=0.01, relheigh=1)
def __config_calendar(s):
cols = [
"'日'", "'一'", "'二'", "'三'", "'四'", "'五'", "'六'"]
s._calendar['columns'] = cols
s._calendar.tag_configure('header', background='grey90')
s._calendar.insert('', 'end', values=cols, tag='header')
font = tkFont.Font()
maxwidth = max((font.measure(col) for col in cols))
for col in cols:
s._calendar.column(col, width=maxwidth, minwidth=maxwidth, anchor='center')
def __setup_selection(s, sel_bg, sel_fg):
def __canvas_forget(evt):
canvas.place_forget()
s._selection = None
s._font = tkFont.Font()
s._canvas = canvas = tk.Canvas((s._calendar), background=sel_bg, borderwidth=0, highlightthickness=0)
canvas.text = canvas.create_text(0, 0, fill=sel_fg, anchor='w')
canvas.bind('<Button-1>', _Calendar__canvas_forget)
s._calendar.bind('<Configure>', _Calendar__canvas_forget)
s._calendar.bind('<Button-1>', s._pressed)
def _build_calendar(s):
year, month = s._date.year, s._date.month
header = s._cal.formatmonthname(year, month, 0)
cal = s._cal.monthdayscalendar(year, month)
for indx, item in enumerate(s._items):
week = cal[indx] if indx < len(cal) else []
fmt_week = ['%02d' % day if day else '' for day in week]
s._calendar.item(item, values=fmt_week)
def _show_select(s, text, bbox):
"""为新的选择配置画布。"""
x, y, width, height = bbox
textw = s._font.measure(text)
canvas = s._canvas
canvas.configure(width=width, height=height)
canvas.coords(canvas.text, (width - textw) / 2, height / 2 - 1)
canvas.itemconfigure((canvas.text), text=text)
canvas.place(in_=(s._calendar), x=x, y=y)
def _pressed(s, evt=None, item=None, column=None, widget=None):
"""在日历的某个地方点击。"""
if not item:
x, y, widget = evt.x, evt.y, evt.widget
item = widget.identify_row(y)
column = widget.identify_column(x)
else:
if not column or item not in s._items:
return
else:
item_values = widget.item(item)['values']
if not len(item_values):
return
text = item_values[int(column[1]) - 1]
return text or None
bbox = widget.bbox(item, column)
bbox or s.master.after(20, lambda: s._pressed(item=item, column=column, widget=widget))
return return None
text = '%02d' % text
s._selection = (text, item, column)
s._show_select(text, bbox)
def _prev_month(s):
"""更新日历以显示前一个月。"""
s._canvas.place_forget()
s._selection = None
s._date = s._date - timedelta(days=1)
s._date = datetime(s._date.year, s._date.month, 1)
s.CB_year.set(s._date.year)
s.CB_month.set(s._date.month)
s._update()
def _next_month(s):
"""更新日历以显示下一个月。"""
s._canvas.place_forget()
s._selection = None
year, month = s._date.year, s._date.month
s._date = s._date + timedelta(days=(calendar.monthrange(year, month)[1] + 1))
s._date = datetime(s._date.year, s._date.month, 1)
s.CB_year.set(s._date.year)
s.CB_month.set(s._date.month)
s._update()
def _update(s, event=None, key=None):
"""刷新界面"""
if key:
if event.keysym != 'Return':
return
else:
year = int(s.CB_year.get())
month = int(s.CB_month.get())
if year == 0 or year > 9999:
return
s._canvas.place_forget()
s._date = datetime(year, month, 1)
s._build_calendar()
if year == datetime.now().year:
if month == datetime.now().month:
day = datetime.now().day
for _item, day_list in enumerate(s._cal.monthdayscalendar(year, month)):
if day in day_list:
item = 'I00' + str(_item + 2)
column = '#' + str(day_list.index(day) + 1)
s.master.after(100, lambda: s._pressed(item=item, column=column, widget=(s._calendar)))
def _exit(s, confirm=False):
"""退出窗口"""
if not confirm:
s._selection = None
s.master.destroy()
def _main_judge(s):
"""判断窗口是否在最顶层"""
try:
if s.master.focus_displayof() == None or 'toplevel' not in str(s.master.focus_displayof()):
s._exit()
else:
s.master.after(10, s._main_judge)
except:
s.master.after(10, s._main_judge)
def selection(s):
"""返回表示当前选定日期的日期时间。"""
if not s._selection:
return
year, month = s._date.year, s._date.month
return str(datetime(year, month, int(s._selection[0])))[:10]
def Input_judgment(s, content):
"""输入判断"""
if content.isdigit() or content == '':
return True
return False
class Update_t(object):
def __init__(self, sqlsrv_ip=None, date=None):
self.sqlsrv_ip = sqlsrv_ip
self.date = date
def update(self):
if self.sqlsrv_ip and self.date:
log = ''
logtime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
sqlsrv = sqldic[self.sqlsrv_ip]
ExpiresOn_date = time.mktime(time.strptime(self.date, '%Y-%m-%d'))
with open('.\\\\LoginID.txt', 'r') as (f):
Loginlist = f.read().splitlines()
for LoginID in Loginlist:
try:
db_conn = pymssql.connect(host='192.168.2.39', user='aaa', password='bbb', database=sqlsrv)
cursor = db_conn.cursor()
cursor.execute("select ExpiresOn from SUUsers where LoginID = '%s'" % LoginID)
ExpiresOn = cursor.fetchall()
if len(ExpiresOn) > 0 and int(ExpiresOn[0][0]) < int(ExpiresOn_date):
sql = "update SUUsers\n set ExpiresOn=%d where LoginID = '%s'\n " % (
int(ExpiresOn_date), LoginID)
cursor.execute(sql)
db_conn.commit()
log += '%s----账户%s延期成功\n' % (logtime, LoginID)
else:
with open('.\\\\Log.txt', 'a+') as (f):
f.write('%s----账户%s不存在或者无需延期\n' % (logtime, LoginID))
log += '%s----账户%s不存在或者无需延期\n' % (logtime, LoginID)
finally:
cursor.close()
else:
messagebox.showinfo('延期结果', log)
else:
pass
class Login(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.title('FTP账户延期')
width, height = self.root.winfo_reqwidth() + 50, 150
x, y = (self.root.winfo_screenwidth() - width) / 2, (self.root.winfo_screenheight() - height) / 2
self.root.geometry('%dx%d+%d+%d' % (width, height, x, y))
self.date_str = tk.StringVar()
self.date = ttk.Entry((self.root), textvariable=(self.date_str))
self.canvas = tkinter.Canvas((self.root), height=100, width=500)
self.label_ftpsrv = tkinter.Label((self.root), text='FTP服务器: ')
self.ftpsrv = tkinter.Label((self.root), text='11')
self.ftpCombobox = ttk.Combobox(self.root)
self.ftpCombobox['value'] = ["'192.168.7.7'", "'192.168.18.7'", "'192.168.18.6'", "'192.168.7.4'", "'192.168.7.5'",
"'192.168.17.6'", "'192.168.7.18'", "'192.168.7.22'", "'192.168.17.4'",
"'192.168.17.7'", "'192.168.18.3'", "'192.168.19.3'"]
self.ftpCombobox['state'] = 'readonly'
self.text = tkinter.Text(self.root)
def func(event):
self.text.insert('insert', self.ftpCombobox.get() + '\n')
self.ftpCombobox.bind('<<ComboboxSelected>>', func)
self.date_str_gain = lambda: [self.date_str.set(date) for date in (Calendar((x, y), 'lr').selection(),) if date]
self.datebt = tk.Button((self.root), text='日期:', command=(self.date_str_gain))
self.ensurebt = tk.Button((self.root), text='延期', activebackground='red', command=(self.get_srv))
self.input_account = tkinter.Entry((self.root), width=30)
self.input_password = tkinter.Entry((self.root), show='*', width=30)
def gui_arrang(self):
self.label_ftpsrv.grid(row=0, column=0)
self.ftpCombobox.grid(row=0, column=1)
self.datebt.grid(row=1, column=0)
self.date.grid(row=1, column=1)
self.ensurebt.grid(row=2, column=1)
def get_srv(self):
sqlsrv_ip = self.ftpCombobox.get()
date_t = self.date_str.get()
U = Update_t(sqlsrv_ip, date_t)
U.update()
return return self.ftpCombobox.get()
def main():
L = Login()
L.gui_arrang()
tkinter.mainloop()
if __name__ == '__main__':
main()