Browse Source

Fix: Improved method for generating the label filename from the pcap filename

Stefan Schmidt 6 years ago
parent
commit
e6839a723d
2 changed files with 14 additions and 2 deletions
  1. 3 2
      code/ID2TLib/LabelManager.py
  2. 11 0
      code/ID2TLib/Utility.py

+ 3 - 2
code/ID2TLib/LabelManager.py

@@ -3,6 +3,7 @@ from datetime import datetime
 from xml.dom.minidom import *
 
 import ID2TLib.Label as Label
+import ID2TLib.Utility as Utility
 
 
 class LabelManager:
@@ -28,7 +29,7 @@ class LabelManager:
         self.labels = list()
 
         if filepath_pcap is not None:
-            self.label_file_path = filepath_pcap.replace('.pcap', '_labels.xml')
+            self.label_file_path = Utility.rreplace(filepath_pcap, '.pcap', '_labels.xml', 1)
             # only load labels if label file is existing
             if os.path.exists(self.label_file_path):
                 self.load_labels()
@@ -83,7 +84,7 @@ class LabelManager:
             return timestamp_root
 
         if filepath is not None:
-            self.label_file_path = filepath.replace('.pcap', '_labels.xml')
+            self.label_file_path = Utility.rreplace(filepath, '.pcap', '_labels.xml', 1)
 
         # Generate XML
         doc = Document()

+ 11 - 0
code/ID2TLib/Utility.py

@@ -341,3 +341,14 @@ def get_attacker_config(ip_source_list, ipAddress: str):
     # return port and TTL
     return next_port, ttl
 
+
+def rreplace(s, old, new, maxreplace):
+    """
+    Replaces occurences of a sub-string with a new sub-string, but, unlike replace(), it starts at the end
+    :param s: The string to search and replace from.
+    :param old: The old sub-string you wish to replace.
+    :param new: The new sub-string you wish to put in-place of the old one.
+    :param maxreplace: The maximum number of times you wish to replace the sub-string.
+    :return: The result of the reverse replace operation.
+    """
+    return new.join(s.rsplit(old, maxreplace))