Compare commits
4 Commits
3ecb096271
...
f72c04ec2d
| Author | SHA1 | Date | |
|---|---|---|---|
| f72c04ec2d | |||
| 7381ae0aa4 | |||
| 28b879720f | |||
| 3ecc7b75c8 |
@ -5,8 +5,10 @@ import os
|
||||
import re
|
||||
import glob
|
||||
import shutil
|
||||
import unicodedata
|
||||
from datetime import datetime
|
||||
import openpyxl as xls
|
||||
from openpyxl.styles import PatternFill
|
||||
|
||||
|
||||
# Les noms de tous les dossier a créer
|
||||
@ -21,6 +23,8 @@ FOLDERNAMES = [
|
||||
"7_Plans_de_secteur",
|
||||
]
|
||||
|
||||
ANNEXE_FOLDER_ID = 4
|
||||
|
||||
# Les mots clef associés à chaque dossier
|
||||
KEYWORDS = {
|
||||
"Jugement": FOLDERNAMES[0],
|
||||
@ -35,6 +39,16 @@ KEYWORDS = {
|
||||
"Plan_de_secteur": FOLDERNAMES[7],
|
||||
}
|
||||
|
||||
def confirm_choice(choice) -> bool:
|
||||
"""
|
||||
Demande confirmation du choix de l'utilisateur
|
||||
"""
|
||||
print(f"vous avez choisi {choice} est-ce correct ?")
|
||||
answer = int(input("[1] - Oui \n[2] - Non \nReponse : "))
|
||||
|
||||
return answer == 1
|
||||
|
||||
|
||||
def test_date(date_input: str):
|
||||
"""teste si la date donnée est valide"""
|
||||
# Pattern pour vérifier qu'il y a bien 8 chiffres, 4pour l'année, 2 pour le mois, 2 pour le jour
|
||||
@ -71,7 +85,7 @@ def test_siren_insee(is_plu: bool, number: str):
|
||||
|
||||
def create_folders(is_plu: bool, prefixe, suffixe):
|
||||
"""Crée l'arborescence"""
|
||||
main_path = prefixe + "_" + ("PLU" if is_plu else "PLUi") + "_" + suffixe
|
||||
main_path = f"{prefixe}_PLU{"i" if not is_plu else ""}_{suffixe}"
|
||||
|
||||
try:
|
||||
os.mkdir(main_path)
|
||||
@ -103,8 +117,13 @@ def correct_file_name(file):
|
||||
espaces et tirets deviennent des underscore
|
||||
"""
|
||||
basename = os.path.basename(file)
|
||||
correct_file = basename.replace(" ","_")
|
||||
correct_file = basename.replace("-","_")
|
||||
correct_file = basename.replace(" ","_").replace("-","_")
|
||||
|
||||
# Supprime les accents et les convertis en minuscules
|
||||
correct_file = ''.join(
|
||||
(c if unicodedata.category(c) != 'Mn' else '')
|
||||
for c in unicodedata.normalize('NFD', correct_file)
|
||||
)
|
||||
correct_file = os.path.join(os.path.dirname(file),correct_file)
|
||||
os.rename(file,correct_file)
|
||||
return correct_file
|
||||
@ -121,6 +140,7 @@ def sort_files(prefixe, suffixe, sort_path):
|
||||
|
||||
#compte le nombre de fois mot clef apparait
|
||||
keyword_count = {key: 0 for key in KEYWORDS}
|
||||
folders_file_count = [0] * len(FOLDERNAMES)
|
||||
|
||||
for file in files:
|
||||
no_keyword = True
|
||||
@ -144,6 +164,7 @@ def sort_files(prefixe, suffixe, sort_path):
|
||||
os.path.dirname(file),
|
||||
prefixe + "_" + filename + "_" + suffixe + ".pdf",)
|
||||
|
||||
folders_file_count[int(folder[0])] +=1
|
||||
os.rename(file, renamed)
|
||||
shutil.move(renamed, new_path)
|
||||
no_keyword = False
|
||||
@ -171,15 +192,19 @@ def sort_files(prefixe, suffixe, sort_path):
|
||||
+ ".pdf",
|
||||
)
|
||||
os.rename(file, renamed)
|
||||
shutil.move(renamed, sort_path + "/" + FOLDERNAMES[4])
|
||||
shutil.move(renamed, sort_path + "/" + FOLDERNAMES[ANNEXE_FOLDER_ID])
|
||||
folders_file_count[ANNEXE_FOLDER_ID] += 1
|
||||
|
||||
return keyword_count
|
||||
return keyword_count, folders_file_count
|
||||
# return True
|
||||
|
||||
def error_report(data):
|
||||
def error_report(file_data,folder_data):
|
||||
"""
|
||||
Crée le rapport d'utilisation du script
|
||||
"""
|
||||
#Couleur rouge
|
||||
error_color =PatternFill(patternType='solid',fgColor='00FF0000')
|
||||
|
||||
report = xls.Workbook()
|
||||
report_worsheet = report.active
|
||||
report_worsheet.title = "Compte rendu"
|
||||
@ -192,15 +217,25 @@ def error_report(data):
|
||||
report_worsheet.column_dimensions['B'].width *= 3
|
||||
report_worsheet.column_dimensions['F'].width *= 3
|
||||
report_worsheet.column_dimensions['G'].width *= 3
|
||||
report_worsheet.column_dimensions['H'].width *= 3
|
||||
|
||||
currrent_cell = 2
|
||||
for key in data:
|
||||
for key in file_data:
|
||||
report_worsheet['A'+ str(currrent_cell)] = key
|
||||
report_worsheet['B'+ str(currrent_cell)] = data.get(key)
|
||||
report_worsheet['B'+ str(currrent_cell)] = file_data.get(key)
|
||||
currrent_cell+=1
|
||||
|
||||
report_worsheet['A'+str(3 + len(data.keys()))] = "Total fichiers"
|
||||
report_worsheet['B'+str(3 + len(data.keys()))] = '=SUM(B2:B' + str(1 + len(data.keys())) + ')'
|
||||
currrent_cell = 2
|
||||
for i, content in enumerate(folder_data):
|
||||
report_worsheet['F'+ str(currrent_cell)] = FOLDERNAMES[i]
|
||||
report_worsheet['G'+ str(currrent_cell)] = content
|
||||
if content == 0:
|
||||
report_worsheet['H'+ str(currrent_cell)] = "/!\\ aucun fichier dans le dossier /!\\"
|
||||
report_worsheet['H'+ str(currrent_cell)].fill = error_color
|
||||
currrent_cell+=1
|
||||
|
||||
report_worsheet['A'+str(3 + len(file_data.keys()))] = "Total fichiers"
|
||||
report_worsheet['B'+str(3 + len(file_data.keys()))] = '=SUM(B2:B' + str(1 + len(file_data.keys())) + ')'
|
||||
report.save("Rapport.xlsx")
|
||||
|
||||
def sortout():
|
||||
@ -210,7 +245,7 @@ def sortout():
|
||||
# demande si c'est un PLU ou un PLUi tant que l'input ne correspond pas à ce que l'on attend
|
||||
while True:
|
||||
answer = int(
|
||||
input("[1] - PLU \n[2] - PLUi \nQuel type de dossier est a étudier ? :")
|
||||
input("[1] - PLU \n[2] - PLUi \nQuel type de dossier est a étudier ? : ")
|
||||
)
|
||||
|
||||
if answer > 2 or answer < 1:
|
||||
@ -218,31 +253,27 @@ def sortout():
|
||||
# print("\033[A\033[K", end="") #supprime la dernière ligne
|
||||
else:
|
||||
is_plu = answer == 1
|
||||
break
|
||||
if confirm_choice("PLU" if answer == 1 else "PLUi"):
|
||||
break
|
||||
|
||||
|
||||
# On demande le numero INSEE ou SIREN tant que l'input est invalide
|
||||
while True:
|
||||
print("numero INSEE (Format 5 chiffres): "
|
||||
if is_plu else "numero siren (Format 9 chiffres): ", end="")
|
||||
prefixe = input()
|
||||
if test_siren_insee(is_plu, prefixe):
|
||||
if test_siren_insee(is_plu, prefixe) and confirm_choice(prefixe):
|
||||
break
|
||||
|
||||
# On demande la date d'approbation tant que l'input est invalide
|
||||
while True:
|
||||
print("Date d'approbation (Format AAAAMMJJ): ", end="")
|
||||
suffixe = input()
|
||||
if test_date(suffixe):
|
||||
if test_date(suffixe) and confirm_choice(suffixe):
|
||||
break
|
||||
|
||||
main_path = create_folders(is_plu, prefixe, suffixe)
|
||||
|
||||
donees_fichiers = sort_files(prefixe, suffixe, main_path)
|
||||
donees_fichiers, donnees_dossiers = sort_files(prefixe, suffixe, main_path)
|
||||
|
||||
error_report(donees_fichiers)
|
||||
# Chargement
|
||||
# for i in range(100):
|
||||
# print(f"{i}%")
|
||||
# time.sleep(0.5)
|
||||
# if i <100:
|
||||
# print("\033[A\033[K", end="")
|
||||
error_report(donees_fichiers,donnees_dossiers)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user