Aggiunta gestione LOG file, check dell'estensione

This commit is contained in:
fabrizio.pappolla
2023-06-09 11:37:11 +02:00
parent 3a6564cf2c
commit df0eb03af4

41
main.py
View File

@@ -1,23 +1,36 @@
#!/usr/bin/python3 #!/usr/bin/python3
import csv import csv
import os import os
from datetime import datetime
path = '/srv/tftp/' path = '/srv/tftp/'
header_cfg = """#!version:1.0.0.1 header_cfg = """#!version:1.0.0.1
#Tasti rapidi del tastierino esterno #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""" 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: try:
os.chdir(path) os.chdir(path)
print("Current working directory: {0}".format(os.getcwd())) append_log("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError: except FileNotFoundError:
print("Directory: {0} does not exist".format(path)) append_log("Directory: {0} does not exist".format(path))
except NotADirectoryError: except NotADirectoryError:
print("{0} is not a directory".format(path)) append_log("{0} is not a directory".format(path))
except PermissionError: 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): def main(ofile):
with open(ofile, newline='') as csvfile: with open(ofile, newline='') as csvfile:
@@ -31,12 +44,20 @@ def main(ofile):
idx_name = row.index('name') idx_name = row.index('name')
else: else:
if 'idx_ext' not in locals() or 'idx_name' not in locals(): if 'idx_ext' not in locals() or 'idx_name' not in locals():
with open("error.txt", "w") as f: append_log("Errore: file csv senza i campi 'extension' e/o 'name'")
f.write("Errore: file csv senza i campi 'extension' e/o 'name'") try:
to_write = "\n".join([to_write, print_ext(line_count, row[idx_ext], row[idx_name])]) ext_n = int(row[idx_ext])
line_count += 1 except:
print(to_write) 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): def print_ext(n, n_ext, lbl_ext):
spamrow = f"""expansion_module.1.key.{n}.extension = spamrow = f"""expansion_module.1.key.{n}.extension =
@@ -53,4 +74,4 @@ if __name__ == '__main__':
try: try:
main(ofile) main(ofile)
except FileNotFoundError: except FileNotFoundError:
print("File non trovato") append_log("File CSV non trovato")