diff --git a/main.py b/main.py index 8adbb8f..4f96bbc 100644 --- a/main.py +++ b/main.py @@ -1,23 +1,36 @@ #!/usr/bin/python3 import csv import os +from datetime import datetime path = '/srv/tftp/' header_cfg = """#!version:1.0.0.1 #Tasti rapidi del tastierino esterno +#ATTENZIONE: NON MODIFICARE QUESTO FILE +#Questo file viene generato automaticamente a partire dal file +#tastierino_esterno.csv, modificare quest'ultimo ed attendere qualche minuto account.1.sip_server.1.address = 192.168.1.7""" + +def append_log(text): + with open("error.txt", "a") as f: + now = datetime.now() + now_formatted = now.strftime("%d/%m/%Y, %H:%M:%S") + log = " ".join([now_formatted, text]) + f.write(log) + + try: os.chdir(path) - print("Current working directory: {0}".format(os.getcwd())) + append_log("Current working directory: {0}".format(os.getcwd())) except FileNotFoundError: - print("Directory: {0} does not exist".format(path)) + append_log("Directory: {0} does not exist".format(path)) except NotADirectoryError: - print("{0} is not a directory".format(path)) + append_log("{0} is not a directory".format(path)) except PermissionError: - print("You do not have permissions to change to {0}".format(path)) + append_log("You do not have permissions to change to {0}".format(path)) def main(ofile): with open(ofile, newline='') as csvfile: @@ -30,13 +43,21 @@ def main(ofile): idx_ext = row.index('extension') idx_name = row.index('name') else: - if 'idx_ext' not in locals() or 'idx_name' not in locals(): - with open("error.txt", "w") as f: - f.write("Errore: file csv senza i campi 'extension' e/o 'name'") - to_write = "\n".join([to_write, print_ext(line_count, row[idx_ext], row[idx_name])]) - line_count += 1 - print(to_write) + if 'idx_ext' not in locals() or 'idx_name' not in locals(): + append_log("Errore: file csv senza i campi 'extension' e/o 'name'") + try: + ext_n = int(row[idx_ext]) + except: + continue + ext_name = row[idx_name] + to_write = "\n".join([to_write, print_ext(line_count, ext_n, ext_name)]) + line_count += 1 + write_f(to_write,"tastierino_esterno.cfg") + +def write_f(text,file_name): + with open(file_name, "w") as file: + file.write(text) def print_ext(n, n_ext, lbl_ext): spamrow = f"""expansion_module.1.key.{n}.extension = @@ -53,4 +74,4 @@ if __name__ == '__main__': try: main(ofile) except FileNotFoundError: - print("File non trovato") + append_log("File CSV non trovato")