-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddDevices.py
More file actions
67 lines (60 loc) · 3.34 KB
/
AddDevices.py
File metadata and controls
67 lines (60 loc) · 3.34 KB
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
import os
import sys
import json
import constants
import datetime
import time
def checkFile(pathToFile):
if os.path.exists(pathToFile) == True:
with open(pathToFile, 'r', encoding=constants.Encoding) as File:
try:
getjson = json.loads(File.read())
if len(getjson) == 0:
createJSON(pathToFile)
print("Данные записаны!")
except Exception:
createJSON(pathToFile)
print("Данные записаны!")
else:
createJSON(pathToFile)
print("Данные записаны!")
def createJSON(pathToFile):
file = open(str(pathToFile), 'w')
file.close()
jsonFileDict = {"index": {"0":{"hostname": "", "IPadd": "", "Description": "", "past state": "Offline", "currently state": "Offline", "currently time": ""}}}
with open(pathToFile, 'r+', encoding=constants.Encoding) as File:
jsonFileDict["index"]["0"]["hostname"] = input('Введите имя устройства (hostname): ')
jsonFileDict["index"]["0"]["IPadd"] = input('Введите IP-адрес устройства: ')
jsonFileDict["index"]["0"]["Description"] = input('Введите описание устройства: ')
nowTime = datetime.datetime.now()
jsonFileDict["index"]["0"]["currently time"] = nowTime.strftime("%H:%M:%S %d.%m.%Y")
File.seek(0) #очистка файла json
File.write(json.dumps(jsonFileDict, indent=constants.indent)) #запись файла json
File.truncate()
#----------------------------MAIN-------------------------------
checkFile(constants.devicesJson)
while True:
Answer = ""
Answer = input("Добавить новое устройство? y/n \n")
if Answer.lower() == ("y" or "yes"):
with open(constants.devicesJson, 'r+', encoding=constants.Encoding) as File:
DevicesData = json.loads(File.read())
i = len(DevicesData["index"])
insertDict = {"hostname": "", "IPadd": "", "Description": "", "past state": "Offline", "currently state": "Offline", "currently time": ""}
insertDict["hostname"] = input('Введите имя устройства (hostname): ')
insertDict["IPadd"] = input('Введите IP-адрес устройства: ')
insertDict["Description"] = input('Введите описание устройства: ')
nowTime = datetime.datetime.now()
insertDict["currently time"] = nowTime.strftime("%H:%M:%S %d.%m.%Y")
DevicesData["index"][str(i)] = insertDict
File.seek(0) #очистка файла json
File.write(json.dumps(DevicesData, indent=constants.indent)) #запись файла json
File.truncate()
else:
Exit = input('Вы действительно хотите завершить ввод новых устройств и выйти? \n Введите "no", если хотите продолжить ввод: ')
if Exit.lower() != ('no' or '"no"'):
print('Завершение работы программы!')
time.sleep(1)
sys.exit()
else:
continue