1 #!/usr/bin/env python3
2
3 import json #json Module for converting strings like dictionaries into Dictionaries
4 import re #re Module, Find Replacement
5 import shutil #copy File use
6 shutil.copyfile('conf','conf.bak') #shutil.copyfile(Document 1, document 2): Instead of opening the file, overwrite it directly with the name of the file copy.
7 #Global variable function
8 def env():
global user_dic,insert_addr,server,weight,maxconn,insert_conf,rec,ser,wei,max #global Define a global variable so that it can be called outside the function
user_dic = json.loads(user_input) # Converting user input into a dictionary-like string into a dictionary
insert_addr = list(user_dic.items())[0][0] + ' ' + list(user_dic.items())[0][1] # Input user information first keys and values take out
rec = list(user_dic.keys())[1]
ser = list(user_dic[rec].keys())[0]
wei = list(user_dic[rec].keys())[1]
max = list(user_dic[rec].keys())[2]
if rec in user_dic.keys():
if ser in user_dic[rec].keys():
server = user_dic[rec][ser]
if wei in user_dic[rec].keys():
weight = user_dic[rec][wei]
if max in user_dic[rec].keys():
maxconn = user_dic[rec][max]
insert_conf = '%s %s %s %s %s %s %s' % (ser, server, server, wei, weight, max, maxconn) # Format information
else:
print('Incorrect input')
26 # Add Files to List Functions
27 def list_lines():
global lines
with open('conf.bak', 'r') as f:
for line in f:
f.seek(0) # File pointer specified to start at 0
lines = f.readlines() # Read each line of the file to the list lines Including line breaks\n And indent\t.
33
34 def check_json_format():
try:
json.loads(user_input,encoding='utf-8') #Testing user_input Is it? json Format, not exception
except ValueError: #Exceptional error reporting, if there is no exception, this will never go.
return False #Exception Error Return False
return True #No exception return True
40 # Determines whether a string is a digital function
41 def isnub(s):
try:
nb = float(s) #Converting a string to a number returns if it succeeds True
return True
except ValueError as e:
return False #If an exception occurs, return False
47 # Index Value Existence Function
48 def if_index():
try:
lines.index(insert_index)
return False
except ValueError:
return True
54 # Judges that the input cannot be empty
55 def if_else():
if len(user_input) == 0:
print('Input cannot be empty, please re-enter!')
return True
else:
print('Input error, please enter json Format string!')
return False
62 # Output File Change
63 def change():
with open('conf', 'r') as f:
print('Before modification:')
for line in f:
if insert_addr in line:
print(line + next(f))
with open('conf', 'r') as f:
print('After modification:')
for line in f:
if insert_addr in line:
print(line + next(f) + next(f))
74 # Configuration File Add Function
75 def increase_conf():
global user_input,insert_index
while True:
user_input = input('Enter to add configuration file content:').strip()
if check_json_format() and isnub(user_input) == False: #If json Judgment function return True,Then for json format
env()
list_lines()
if insert_addr+'\n' in lines:
insert_index = lines.index(insert_addr+'\n') # Obtain insert_addr+\n List lines Index location in
if if_index():
lines.insert(insert_index + 2, ' ' + insert_conf + '\n') # Insert information at the specified index position in the list
with open('conf', '+w') as conf:
conf.write(''.join(lines)) # join Write the list contents into the file to''Separation, or'\n'Separate
print('Already will%s add to conf In file'%(insert_conf))
change()
return True
else:
print('%s stay conf Document does not exist'%(insert_addr))
return False
else:
print('%s stay conf Document does not exist'%(insert_addr))
return False
if if_else():
continue
else:
continue
101# Profile Delete Function
102 def del_conf():
global user_input,insert_index
while True:
user_input = input('Please enter configuration information that needs to be deleted:').strip()
if check_json_format() and isnub(user_input) == False: # If json Judgment function return True,Then for json format
env()
list_lines()
if insert_addr + '\n' in lines:
if ' '+insert_conf+'\n' in lines:
insert_index = lines.index(' '+insert_conf+'\n')
if if_index():
del[lines[insert_index]]
with open('conf', '+w') as conf:
conf.write(''.join(lines)) # join Write the list contents into the file to''Separation, or'\n'Separate
return True
else:
print('%s %s %s %s %s %s %s In document conf Medium does not exist.'%(ser, server, server, wei, weight, max, maxconn))
continue
else:
# print('%s Non-existent conf in'%(insert_conf))
print('%s %s %s %s %s %s %s In document conf Medium does not exist.' % (ser, server, server, wei, weight, max, maxconn))
continue
else:
print('%s In document conf Medium does not exist.'%(insert_addr))
continue
if if_else():
continue
else:
continue
131 # Profile Modification Function
132 def modify_conf():
global user_input
while True:
user_input = input('Please enter what you need to modify:').strip()
if check_json_format() and isnub(user_input) == False: # If json Judgment function return True,Then for json format
env()
list_lines()
if insert_addr+'\n' in lines:
if ' '+insert_conf+'\n' in lines:
while True:
user_input = input('It needs to be revised to read:').strip()
if len(user_input) == 0:
print('Input cannot be empty,Please re-enter!')
continue
else:
open('conf', 'w').write(re.sub(insert_conf, user_input,open('conf.bak').read()))#read file conf.bak in
# insert_conf Replace with user_input,Here re.sub Find substitutions, regular expressions.
print('Already will:%s\n'
'Modified to:%s'%(insert_conf,user_input))
return True
if if_else():
continue
else:
continue
156 # Configuration File View Function
157 def see_conf():
while True:
user_input = input('Please enter what you need to find:').strip()
if len(user_input) == 0:
print('Input cannot be empty, please re-enter')
continue
else:
with open('conf','r') as f:
for line in f:
if user_input in line.strip():
print(line)
print(next(f))
return
else:
print('Unable to find')
return
173 while True:
174 print ('1,% s 2,% s 3,% s 4,% s'%('increase','delete','modify','view')))
175 user_input = input ('Please select the action on the configuration file:'). strip()
176 if len(user_input) == 0:
177 print ('Input cannot be empty, please re-enter!')
178 continue
179 If user_input =='1'or user_input =='increase':
180 increase_conf()
181 break
182 If user_input =='2'or user_input =='delete':
183 del_conf()
184 break
185 if user_input =='3'or user_input =='modification':
186 modify_conf()
187 break
188 if user_input=='4'or user_input=='View':
189 see_conf()
190 break
191 else:
192 print ('incorrect input')
193 continue