Skip to content

Commit

Permalink
deleted json functions
Browse files Browse the repository at this point in the history
  • Loading branch information
FischLord committed Feb 21, 2024
1 parent 2fc2095 commit ed24ecf
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 337 deletions.
Binary file modified ip-atlas/database/ip_atlas.db
Binary file not shown.
334 changes: 0 additions & 334 deletions ip-atlas/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
5 changes: 2 additions & 3 deletions ip-atlas/routes/atlas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

@bp_atlas.route("/")
def index():
createJson()
data = loadJson()
data = return_json_format()
return render_template("dashboard.html", data=data)


Expand Down Expand Up @@ -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", "")
Expand Down

0 comments on commit ed24ecf

Please sign in to comment.