@@ -36,19 +36,9 @@ def __init__(self, chemin_log):
3636 Raises:
3737 FileNotFoundError: Si le fichier à analyser est introuvable.
3838 """
39- if not self . __fichier_existe (chemin_log ):
39+ if not os . path . isfile (chemin_log ):
4040 raise FileNotFoundError (f"Le fichier { chemin_log } est introuvable." )
4141 self .chemin_log = chemin_log
42-
43- def __fichier_existe (self , chemin_fichier ):
44- """
45- Vérifie que le chemin passé en paramètre correspond à une fichier existant.
46- Returns:
47- bool: True s'il existe, False sinon.
48- """
49- if not os .path .isfile (chemin_fichier ):
50- return False
51- return True
5242
5343 def parse_fichier (self ):
5444 """
@@ -60,17 +50,17 @@ def parse_fichier(self):
6050 FormatLogApacheInvalideException: Format du fichier log invalide.
6151 """
6252 log_analyse = FichierLogApache (self .chemin_log )
63- numero_ligne = 1
6453 with open (self .chemin_log , "r" ) as log :
65- for ligne in log :
54+ for numero_ligne , ligne in enumerate ( log , start = 1 ) :
6655 try :
67- log_analyse . ajoute_entree ( self .parse_entree (ligne ) )
68- numero_ligne += 1
56+ entree = self .parse_entree (ligne )
57+ log_analyse . ajoute_entree ( entree )
6958 except FormatLogApacheInvalideException as ex :
7059 raise FormatLogApacheInvalideException (
71- f"Le format de l'entrée à la ligne { numero_ligne } "
72- f"('{ ligne } ') est invalide."
60+ f"Le format de l'entrée à la ligne { numero_ligne } "
61+ f"('{ ligne . strip () } ') est invalide."
7362 ) from ex
63+
7464 return log_analyse
7565
7666 def parse_entree (self , entree ):
@@ -89,40 +79,61 @@ def parse_entree(self, entree):
8979 analyse = match (self .PATTERN_ENTREE_LOG_APACHE , entree )
9080 if not analyse :
9181 raise FormatLogApacheInvalideException ()
82+
83+ # Extraction des résultats d'analyse
9284 resultat_analyse = analyse .groupdict ()
85+
9386 # Récupération des informations liées au client
9487 adresse_ip = self .get_information_entree (resultat_analyse , "ip" )
88+ if adresse_ip is None :
89+ raise FormatLogApacheInvalideException ("L'adresse IP est obligatoire." )
9590 identifiant_rfc = self .get_information_entree (resultat_analyse , "rfc" )
9691 utilisateur = self .get_information_entree (resultat_analyse , "utilisateur" )
9792 agent_utilisateur = self .get_information_entree (resultat_analyse , "agent_utilisateur" )
93+
9894 informations_client = ClientInformations (
9995 adresse_ip , identifiant_rfc , utilisateur , agent_utilisateur
10096 )
97+
10198 # Récupération des informations liées à la requête
10299 horodatage = self .get_information_entree (resultat_analyse , "horodatage" )
103100 if horodatage :
104101 horodatage = datetime .strptime (horodatage , "%d/%b/%Y:%H:%M:%S %z" )
102+
103+ if horodatage is None :
104+ raise FormatLogApacheInvalideException ("L'horodatage est obligatoire." )
105+
105106 methode_http = self .get_information_entree (resultat_analyse , "methode" )
106107 url = self .get_information_entree (resultat_analyse , "url" )
107108 protocole_http = self .get_information_entree (resultat_analyse , "protocole" )
108109 ancienne_url = self .get_information_entree (resultat_analyse , "ancienne_url" )
110+
109111 informations_requete = RequeteInformations (
110112 horodatage , methode_http , url , protocole_http , ancienne_url
111113 )
114+
112115 # Récupération des informations liées à la réponse
113116 code_statut = self .get_information_entree (resultat_analyse , "code_status" )
114117 if code_statut :
115118 code_statut = int (code_statut )
119+
120+ if code_statut is None :
121+ raise FormatLogApacheInvalideException ("Le code de statut HTTP est obligatoire." )
122+
116123 taille_octets = self .get_information_entree (resultat_analyse , "taille_octets" )
117124 if taille_octets :
118125 taille_octets = int (taille_octets )
126+
119127 informations_reponse = ReponseInformations (
120128 code_statut , taille_octets
121129 )
130+
131+ # Retour des informations regroupées dans l'objet EntreeLogApache
122132 return EntreeLogApache (
123133 informations_client , informations_requete , informations_reponse
124134 )
125135
136+
126137 def get_information_entree (self , analyse_regex , nom_information ):
127138 """
128139 Retourne la valeur de l'information dans l'analyse si elle possède une valeur
@@ -134,11 +145,8 @@ def get_information_entree(self, analyse_regex, nom_information):
134145 Union[str, None]: La valeur sous forme de chaîne de caractère ou None si
135146 aucune valeur n'a été trouvée.
136147 """
137- if nom_information in analyse_regex :
138- valeur = analyse_regex [nom_information ]
139- if valeur != "-" and valeur != "" :
140- return valeur
141- return None
148+ valeur = analyse_regex .get (nom_information )
149+ return valeur if valeur != "" and valeur != "-" else None
142150
143151class FormatLogApacheInvalideException (Exception ):
144152
0 commit comments