From ed24ecffbd794537459736bc2c5cdb6d027acc49 Mon Sep 17 00:00:00 2001 From: FischLord Date: Wed, 21 Feb 2024 15:57:38 +0100 Subject: [PATCH] deleted json functions --- ip-atlas/database/ip_atlas.db | Bin 77824 -> 77824 bytes ip-atlas/helper.py | 334 ---------------------------------- ip-atlas/routes/atlas.py | 5 +- 3 files changed, 2 insertions(+), 337 deletions(-) diff --git a/ip-atlas/database/ip_atlas.db b/ip-atlas/database/ip_atlas.db index 13d74ddd86bbe8f445ac1fcb5446451fcb4688d0..8ec5e4607c1e0c4be5f8d753950846368d0e3f28 100644 GIT binary patch delta 217 zcmYMqv1$TA6h`5DXYRT)BKxnPl_}g82>~HZ3LCN26pPpxAuq6xkRbR3LOLOCv7Mzw zs%U2+ilB|8P(iVCx^(Bjmuw{2$VY{npAR)|ez!;UlEIj8!2w$=@rxf!F;0u&5Mf}1 zBJ&b=dvUe#+Ib<h9f9j4;O@Gh}Jd{sICW@jo)@?CmkhE1U^8obi_ypVo*xt(lc5 zZP|G2TKjn-QaBM#4jvp^q3jDg=H%Dcgk(^dyVyG7piNtE@u5Z92^bs^(z#w6zgRNr+6E=frBp4bK z(R6c#<@|G!Uzo`-lanU7p^Y{u7IhMhSC9xso;R9gjFDL*2 diff --git a/ip-atlas/helper.py b/ip-atlas/helper.py index 2dcb1d8..d08b6f2 100644 --- a/ip-atlas/helper.py +++ b/ip-atlas/helper.py @@ -48,340 +48,6 @@ def check_ipv4_exists(ipv4 ,method="bool"): print("Error: Method not found") - - - - - - - -# checks if the json document is there -def checkJson(): - if checkDokument(gPath): - return True - else: - return False - - -# checks if the json is empty -# open the file and if the content is {} then it is empty - - -def checkJsonEmpty(): - if os.path.exists(gPath): - with open(gPath) as f: - data = json.load(f) - f.close() - if data == {}: - return True - else: - return False - - -# simply creates the json document - - -def createJson(): - if not checkJson(): - # Create the directory if it doesn't exist - os.makedirs(os.path.dirname(gPath), exist_ok=True) - with open(gPath, "w") as f: - f.write("{}") - f.close() - else: - print("Json document already exists") - - -#! json use functions - -# this function will be used to load the json document - - -def loadJson(): - with open(gPath) as f: - data = json.load(f) - f.close() - return data - - -# this function will be used to save the json document - - -def saveJson(data): - with open(gPath, "w") as f: - json.dump(data, f) - f.close() - - -# this function will be used to add a new host inclusive an running id, his name, the ip, the openend ports to the json document - - -def writeJson(name, ip, tags, ipv6, ports): - data = loadJson() - if checkJsonEmpty(): - data["hosts"] = [] - data["hosts"].append( - { - "id": 1, - "name": name, - "tags": tags, - "ip": ip, - "ipv6": ipv6, - "ports": ports, - } - ) - saveJson(data) - else: - data["hosts"].append( - { - "id": len(data["hosts"]) + 1, - "name": name, - "tags": tags, - "ip": ip, - "ipv6": ipv6, - "ports": ports, - } - ) - saveJson(data) - - -# this function will be used to delete a host from the json document - - -def deleteHost(id): - ports = [] - data = loadJson() - if not checkJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - # write the host to the trash json document - writeTrashJson( - data["hosts"][i]["name"], - data["hosts"][i]["ip"], - data["hosts"][i]["tags"], - data["hosts"][i]["ipv6"], - data["hosts"][i]["ports"], - ) - del data["hosts"][i] - saveJson(data) - break - else: - print("Json document is empty") - - -# this function will be used to update a host in the json document - - -def updateJson(id, name, tags, ip, ipv6, ports): - data = loadJson() - if not checkJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - data["hosts"][i]["name"] = name - data["hosts"][i]["tags"] = tags - data["hosts"][i]["ip"] = ip - data["hosts"][i]["ipv6"] = ipv6 - data["hosts"][i]["ports"] = ports - saveJson(data) - break - else: - print("Json document is empty") - - -# this function will be used to get the host by id - - -def getHostById(id): - data = loadJson() - # cut spaces from the front and from the back of the id - # id = id.strip() - if not checkJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - print("Daten der ID: ", id, " :", data["hosts"][i]) - return data["hosts"][i] - else: - print("Host with id: ", id, " not found") - else: - print("Json document is empty") - - -def updateIpAddressById(id, data): - jsonData = loadJson() - if not checkJsonEmpty(): - for i, host in enumerate(jsonData["hosts"]): - if host["id"] == id: - # Convert ports to integers if they are not already - if "ports" in data: - data["ports"] = [ - int(port) if isinstance(port, str) and port.isdigit() else port - for port in data["ports"] - ] - - # Ensure tags are a list if not already - if "tags" in data and isinstance(data["tags"], str): - data["tags"] = [tag.strip() for tag in data["tags"].split(",")] - - jsonData["hosts"][i].update(data) - saveJson(jsonData) - print(f"Host with id: {id} updated successfully.") - return True - print(f"Host with id: {id} not found.") - return False - else: - print("JSON document is empty.") - return False - - - -#! trash json - -# will check if the trash json document exists - - -def checkTrashJson(): - if checkDokument(gTrashPath): - return True - else: - return False - - -# will check if the trash json document is empty - - -def checkTrashJsonEmpty(): - if os.path.exists(gTrashPath): - with open(gTrashPath) as f: - data = json.load(f) - f.close() - if data == {}: - return True - else: - return False - - -# will create the trash json document - - -def createTrashJson(): - if not checkTrashJson(): - os.makedirs(os.path.dirname(gTrashPath), exist_ok=True) - with open(gTrashPath, "w") as f: - f.write("{}") - f.close() - else: - print("Trash Json document already exists") - - -# will load the trash json document - - -def loadTrashJson(): - with open(gTrashPath) as f: - data = json.load(f) - f.close() - return data - - -# will save the trash json document - - -def saveTrashJson(data): - with open(gTrashPath, "w") as f: - json.dump(data, f) - f.close() - - -# will write the host to the trash json document - - -def writeTrashJson(name, ip, tags, ipv6, ports): - data = loadTrashJson() - if checkTrashJsonEmpty(): - data["hosts"] = [] - data["hosts"].append( - { - "id": 1, - "name": name, - "tags": tags, - "ip": ip, - "ipv6": ipv6, - "ports": ports, - } - ) - saveTrashJson(data) - else: - data["hosts"].append( - { - "id": len(data["hosts"]) + 1, - "name": name, - "tags": tags, - "ip": ip, - "ipv6": ipv6, - "ports": ports, - } - ) - saveTrashJson(data) - - -# will delete the host from the trash json document - - -def deleteTrashHost(id): - data = loadTrashJson() - if not checkTrashJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - # write it back to the json document - writeJson( - data["hosts"][i]["name"], - data["hosts"][i]["ip"], - data["hosts"][i]["tags"], - data["hosts"][i]["ipv6"], - data["hosts"][i]["ports"], - ) - del data["hosts"][i] - saveTrashJson(data) - break - else: - print("Trash Json document is empty") - - -# will update the host in the trash json document - - -def updateTrashJson(id, name, tags, ip, ipv6, ports): - data = loadTrashJson() - if not checkTrashJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - data["hosts"][i]["name"] = name - data["hosts"][i]["tags"] = tags - data["hosts"][i]["ip"] = ip - data["hosts"][i]["ipv6"] = ipv6 - data["hosts"][i]["ports"] = ports - saveTrashJson(data) - break - else: - print("Trash Json document is empty") - - -# will get the host by id from the trash json document - - -def getTrashHostById(id): - data = loadTrashJson() - if not checkTrashJsonEmpty(): - for i in range(len(data["hosts"])): - if data["hosts"][i]["id"] == id: - print("Daten der ID: ", id, " :", data["hosts"][i]) - return data["hosts"][i] - else: - print("Host with id: ", id, " not found") - else: - print("Trash Json document is empty") - - #! logging functions diff --git a/ip-atlas/routes/atlas.py b/ip-atlas/routes/atlas.py index f021a92..17b3099 100644 --- a/ip-atlas/routes/atlas.py +++ b/ip-atlas/routes/atlas.py @@ -9,8 +9,7 @@ @bp_atlas.route("/") def index(): - createJson() - data = loadJson() + data = return_json_format() return render_template("dashboard.html", data=data) @@ -130,7 +129,7 @@ def search(): @bp_atlas.route("/filter", methods=["GET"]) def filter(): - data = loadJson() + data = return_json_format() # Retrieve query parameters name = request.args.get("name", "") ipocted1 = request.args.get("ipocted1", "")