#!/usr/bin/env python
from sys import exit
from os import system
from getpass import getpass
data_file = 'staff_list.txt'
data = open(data_file).readlines()
# define user and password
_user = 'root'
_passwd = 'zl'
# define _search, _add method
def _search(key):
result = []
for line in data:
newline = line.strip().split()
if key in newline:
result.append(line)
return result
def _add(values):
n = 0
for line in values:
data.append(line)
if line in data:
n += 1
while True:
username = raw_input('\033[32;1mPlease Enter your username:\033[0m')
if not username:
print '\033[31;1mNot allow empty for username!\033[0m'
continue
elif username == _user:
while True:
passwd = getpass('\033[32;1mEnter Password:\033[0m')
if not passwd:
print '\033[31;1mNot allow empty for password, try again!'
continue
elif passwd and passwd == _passwd:
print 'Hi, \033[35;1m%s\033[0m, Welcome to back!' % username
## start search,add,modify,delete
while True:
select = "Operation options:\n\t1. Search\n\t2. Add\n\t3. Modify\n\t4. Delete\n You can press 'q' to exit.\n>"
user_select = raw_input(select)
if user_select.lower() == 'q':
print '\033[32;1msystem exiting...\033[0m'
exit()
## 1. search
elif user_select == '1':
while True:
search_key = raw_input("\033[32;1mEnter something to search, press 'q' to quit: \
\033[0m\nYou can enter ID, UserName, Job or PhoneNumber.\n>").strip()
if search_key == 'q':
print '\033[32;1mexiting search mode...\033[0m'
break
# call _search function to get search result
search_result = _search(search_key)
if search_result:
print '\033[36;1mSearched item:\033[0m'
for ret in search_result:
print ret,
else:
print '\033[31;1mNot matched.\033[0m'
## 2. add items
elif user_select == '2':
add_prompt = "\033[32;1mInput format reference, press 'q' to exit: \
\033[0m \nID UserName Job PhoneNumber\n>"
while True:
add_input = raw_input(add_prompt).strip()
if add_input == 'q':
print '\033[32;1mexiting add mode...\033[0m'
break
add_input_list = add_input.split()
if len(add_input_list) == 4:
input_id, input_user, input_job, input_tel = add_input_list
else:
print '\033[31;1mID, UserName, Job or PhoneNumber parameter is incorrect.\033[0m'
continue
# Determine whether there ID
for line in data:
newline = line.strip().split()
if input_id == newline[0]:
print "\033[31;1mThe ID '%s' already exists, please re-enter.\033[0m" % input_id
break
else:
if input_id and input_user and input_job and input_tel:
# add input_line to data
user_line = "\t".join(add_input_list)+"\n"
data.append(user_line)
if user_line in data:
print '\033[35;1mSuccessfully added an records.\033[0m'
# write data to file
open(data_file, 'w').writelines(data)
# sort data_file
cmd = 'sort -n staff_list.txt >tmp_staff.txt; cat tmp_staff.txt > staff_list.txt; rm -f tmp_staff.txt'
system(cmd)
## 3. modify items
elif user_select == '3':
modify_prompt = "\033[32;1mWhat you want to modify, press 'q' to exit:\033[0m \
\nUserName, Job=IT or UserName, Phone=123456789\n>"
while True:
modify_input = raw_input(modify_prompt).strip()
if modify_input == 'q':
print '\033[32;1mexiting modify mode...\033[0m'
break
name, modify_item = [ s.strip() for s in modify_input.split(',')]
#print 'name=%s, modify_item=%s' % (name, modify_item)
item_key, item_value = modify_item.split('=')
#print 'item_key=%s, item_value=%s' %(item_key, item_value)
fina_data = []
for dataline in data:
newline = dataline.split()
username = newline[1]
#print username, job, phone
if username == name:
if item_key == 'Job':
newline[2] = item_value
elif item_key == 'Phone':
newline[3] = item_value
#print newline
this_line = ''
for s_item in newline:
this_line += s_item+'\t'
this_line = this_line.strip()+"\n"
fina_data.append(this_line)
else:
fina_data.append(dataline)
# write in file
try:
fp = open(data_file, 'w')
for fina_line in fina_data:
print fina_line,
fp.write(fina_line)
except IOError, err:
print 'Error:', err
else:
fp.close()
## 4. delete items
elif user_select == '4':
while True:
del_prompt = "\033[32;1mEnter the delete option, press 'q' to exit: \
\033[0m\neg: ID=1 or UserName=Jack or PhoneNumber=11223344\n>"
del_input = raw_input(del_prompt).strip()
if del_input == 'q':
'\033[32;1mexiting delete mode...\033[0m'
break
del_k, del_v = del_input.split('=')
print del_k, del_v
for del_line in data:
new_deline = del_line.split()
if del_v in new_deline:
confirm = raw_input("You select fowing line to delete:\n%s\n\033[33;1mAre you sure[yes/no]?\033[0m" % del_line)
if confirm == 'yes':
data.remove(del_line)
print '\033[35;1mSuccess delelted.\033[0m'
# write in file
try:
fp = open(data_file, 'w')
for lines in data:
fp.write(lines)
except IOError, err:
print 'Error:', err
else:
fp.close()
else:
print '\033[31;1mWrong password, try again!\033[0m'
continue
else:
print '\033[31;1mA valid username, Please re-enter!\033[0m'
阅读(3211) | 评论(0) | 转发(0) |