作业
流程图没有画,懒,不想画
readme没有写,懒,不想写。看注释吧233333
1 #! /usr/bin/env python
2 # -*- coding: utf-8 -*-
3 # __author__ == 'Djc'
4
5 LOGIN_IN = {'is_login': False} # 记录当前的普通登录用户
6
7
8 # 普通用户装饰器,此装饰器的功能是验证是否有用户登录
9 def outer(func):
10 def inner():
11 if LOGIN_IN['is_login']: # 检验是否有用户登录了
12 func()
13 else:
14 print("Please login!") # 若没有,跳转menu函数
15 return inner
16
17
18 # 管理员装饰器
19 def mag_outer(func):
20 def inner(*args, **kwargs):
21 if bool(int(LOGIN_IN['user_power'])):
22 func(*args, **kwargs)
23 else:
24 print("Sorry,you can't running this function!")
25 return inner
26
27
28 def login():
29 usr = input("Please enter your name: ")
30 pwd = input("Please enter your password: ")
31
32 f = open("user_information", 'r')
33 for i in f:
34 if usr == i.split("|")[0]: # 检验登录用户是否存在,不存在跳入注册函数
35 break
36 else:
37 print("This usr is not exist!")
38
39 f.seek(0)
40 # 遍历用户文件,登录成功将用户名放在全局字典中
41 for line in f:
42 # user_power = line.strip().split("|")[3]
43 if usr == line.split("|")[0] and pwd == line.strip().split("|")[1]:
44 user_power = line.strip().split("|")[3] # 获取用户权限
45 print("Log in successful,welcome to you")
46 LOGIN_IN['is_login'] = True
47 LOGIN_IN['current'] = usr
48 LOGIN_IN['password'] = pwd
49 LOGIN_IN['user_power'] = user_power
50 break
51 else: # 否则提示密码错误,退出程序
52 print("Sorry,your password is wrong!")
53 f.close()
54
55
56 # 注册
57 def register():
58 usr = input("Please enter your name: ")
59 pwd = input("Please enter your password: ")
60 signature = input("Please enter your signature: ")
61 f = open("user_information", 'r+')
62 flag = True # 设立一个标识符
63 for i in f:
64 if i.split("|")[0] == usr: # 检测此用户名是否存在,存在则不允许注册
65 print("Sorry,this name is exist,try again!")
66 flag = False
67 break
68
69 f.seek(0) # 将文件指针位置放回起始位置,这一步非常重要!!!!
70 for i in f:
71 if flag and i.strip():
72 new_usr = '\n' + usr + "|" + pwd + '|' + signature + "|" + '0'
73 f.write(new_usr)
74 print("Register successful")
75 break
76 f.close() # 关闭文件
77
78
79 # 用户查看自己信息,用装饰器装饰。具体可参考装饰器的应用
80 @outer
81 def usr_view():
82 print("Welcome to you!")
83 with open("user_information", 'r+') as f:
84 for line in f:
85 if line.strip().split("|")[0] == LOGIN_IN['current']: # 遍历文件,输出此时登录者的信息
86 print(line)
87
88
89 # 退出当前用户
90 @outer
91 def usr_exit():
92 LOGIN_IN['is_login'] = False
93
94
95 # 用户改变密码函数
96 @outer
97 def change_pwd():
98 print("Welcome to you!")
99 new_pwd = input("Please enter the password that you want to modify: ") # 新密码
100 in_put = open("user_information", 'r')
101 out_put = open("user_information", 'r+')
102 for line in in_put:
103 if LOGIN_IN['password'] == line.strip().split("|")[1]: # 验证是否是用户本人想修改密码
104 break
105 else:
106 print("Sorry,you may not own!")
107 menu()
108 in_put.seek(0) # 将文件指针返回文件起始位置
109 for line in in_put:
110 if "|" in line and LOGIN_IN['current'] in line and LOGIN_IN['is_login']:
111 temp = line.split("|")
112 temp2 = temp[0] + '|' + new_pwd + '|' + temp[2] + '|' + temp[3] # 将新密码写入原文件
113 out_put.write(temp2)
114 else:
115 out_put.write(line) # 将不需要做改动的用户写入原文件
116 print("Yeah,modify successful")
117 out_put.close()
118 in_put.close()
119
120
121 def user_log():
122 pass
123
124 '''
125 -----------------------------------------------------
126 * 管理员用户登录
127 * 可以登录,注册,查看本用户信息
128 * 删除,添加普通用户
129 * 查看所有普通用户,按照指定关键字搜索用户信息(模糊搜索)
130 * 提高普通用户权限?
131 -----------------------------------------------------
132 '''
133
134
135 @mag_outer
136 def manager():
137 print("Welcome to you,our manager!")
138 ma_pr = '''
139 (V)iew every user
140 (D)elete user
141 (A)dd user
142 (S)ercher user
143 (I)mprove user power
144 (B)ack
145 (Q)exit
146 Enter choice:'''
147 while True:
148 try:
149 choice = input(ma_pr).strip()[0].lower()
150 except (KeyError, KeyboardInterrupt):
151 choice = 'q'
152
153 if choice == 'v':
154 view_usr()
155 if choice == 'd':
156 name = input("Please enter the name that you want to delete? ")
157 count = 0
158 with open('user_information', 'r') as f:
159 for line in f:
160 if line.strip().split("|")[0] == name:
161 delete_usr(count)
162 break
163 count += 1
164
165 if choice == 'a':
166 add_user()
167 if choice == 's':
168 find_user()
169 if choice == 'i':
170 improve_user()
171 if choice == 'b':
172 menu()
173 if choice == 'q':
174 exit()
175
176
177 # 查看所有用户的信息
178 @mag_outer
179 def view_usr():
180 with open("user_information", 'r+') as f:
181 for line in f:
182 print(line, end='')
183
184
185 # 管理员删除用户
186 @mag_outer
187 def delete_usr(lineno):
188 fro = open('user_information', "r") # 文件用于读取
189
190 current_line = 0
191 while current_line < lineno:
192 fro.readline()
193 current_line += 1 # 将文件指针定位到想删除行的开头
194
195 seekpoint = fro.tell() # 将此时文件指针的位置记录下来
196 frw = open('user_information', "r+") # 文件用于写入,与用于读取的文件是同一文件
197 frw.seek(seekpoint, 0) # 把记录下来的指针位置赋到用于写入的文件
198
199 # read the line we want to discard
200 fro.readline() # 读入一行进内内存 同时! 文件指针下移实现删除
201
202 # now move the rest of the lines in the file
203 # one line back
204 chars = fro.readline() # 将要删除的下一行内容取出
205 while chars:
206 frw.writelines(chars) # 写入frw
207 chars = fro.readline() # 继续读取,注意此处的读取是按照fro文件的指针来读
208
209 print("Delete successful!")
210 fro.close()
211 frw.truncate() # 截断,把frw文件指针以后的内容清除
212 frw.close()
213
214
215 @mag_outer
216 def find_user():
217 name = input("Please enter the name that you want to find: ")
218 with open("user_information", "r") as f:
219 for line in f:
220 if line.strip().split("|")[0] == name:
221 print(line)
222 break
223
224
225 @mag_outer
226 def improve_user():
227 name = input("Please enter the name that you want to find: ")
228 power = input("What's power do you want to give ?")
229 in_put = open("user_information", 'r')
230 out_put = open("user_information", 'r+')
231 for line in in_put:
232 if line.strip().split("|")[0] == name:
233 temp = line.split("|")
234 temp1 = temp[0] + '|' + temp[1] + '|' + temp[2] + '|' + power + '\n'
235 out_put.write(temp1)
236 else:
237 out_put.write(line)
238 in_put.close()
239 out_put.close()
240
241
242 @mag_outer
243 def add_user():
244 register()
245
246
247 @mag_outer
248 def manager_log():
249 import logging
250
251 logger = logging.getLogger(LOGIN_IN['current']) # 设立当前登录的管理员为logger
252 logger.setLevel(logging.DEBUG) # 设立日志输出等级最低为DEBUG
253
254 file_handler = logging.FileHandler('user_log') # 创建向文件输出日志的handler
255 file_handler.setLevel(logging.DEBUG) # 文件中日志输出等级最低为DEBUG
256
257 formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s %(message)s") # 设立日志输出格式
258 file_handler.setFormatter(formatter) # 将格式添加到handler中
259
260 logger.addHandler(file_handler) # 将handler注册到logger
261
262 logger.debug()
263
264
265 # 菜单函数
266 def menu():
267 pr = '''
268 (L)ogin
269 (R)egister
270 (U)ser view
271 (C)hange pwd
272 (M)anager
273 (E)xit usr
274 (Q)exit
275 Enter choice:'''
276 while True:
277 try:
278 choice = input(pr).strip()[0].lower()
279 except (KeyboardInterrupt, KeyError):
280 choice = 'q'
281
282 if choice == 'l':
283 login()
284 if choice == 'r':
285 register()
286 if choice == 'u':
287 usr_view()
288 if choice == 'c':
289 change_pwd()
290 if choice == 'm':
291 manager()
292 if choice == 'e':
293 usr_exit()
294 if choice == 'q':
295 exit()
296
297
298 if __name__ == '__main__':
299 menu()