file_parser/load_data_from_files_to_dict.py

88 lines
2.6 KiB
Python

#!/usr/bin/python3
import os
from pathlib import Path
for_check: list[str] = ["/home/punix/Downloads/itunnels/"]
def parse_files_to_dictionary(directorys: list[str]) -> str | None:
data = {}
# create a Path object for each element in the list.
for directory in directorys:
in_paths = Path(directory)
# Path object(s) created
if not in_paths.exists() or not in_paths.is_dir():
continue
# Get a list of all files in the directorys
files = [file for file in in_paths.iterdir() if file.is_file()]
if not files:
continue
# Search for the string in the files
for file in files:
try:
with open(file, "r") as f:
content = f.read()
# Hier parsen wir die relevanten Zeilen aus dem Inhalt
address_line = next(
line
for line in content.splitlines()
if line.startswith("Address")
)
dns_line = next(
line for line in content.splitlines() if line.startswith("DNS")
)
endpoint_line = next(
line
for line in content.splitlines()
if line.startswith("Endpoint")
)
# Extrahiere die Werte
address = address_line.split("=")[1].strip()
dns = dns_line.split("=")[1].strip()
endpoint = endpoint_line.split("=")[1].strip()
# Speichere im Dictionary
data[file.stem] = {
"Address": address,
"DNS": dns,
"Endpoint": endpoint,
}
except Exception:
# Ignore errors and continue to the next file
continue
return data
def print_data(data):
if not data:
print("No data found.")
return
for filename, values in data.items():
print(f"{filename}")
print()
tunnel = input("please entry name of Tunnel:\n\n")
if tunnel not in data:
print("Tunnel not found.")
return
values = data[tunnel]
os.system("clear")
print(f"Data for tunnel: {tunnel}")
print("-" * (17 + len(tunnel)) + "\n")
print(f"Address: {values['Address']}\n")
print(f"DNS: {values['DNS']}\n")
print(f"Endpoint: {values['Endpoint']}\n")
print()
data = parse_files_to_dictionary(for_check)
print_data(data)