@@ -39,7 +39,6 @@ class CodegateSignatures:
3939 """Main class for detecting secrets in text using regex patterns."""
4040
4141 _instance_lock : ClassVar [Lock ] = Lock ()
42- _signatures_loaded : ClassVar [bool ] = False
4342 _signature_groups : ClassVar [List [SignatureGroup ]] = []
4443 _compiled_regexes : ClassVar [Dict [str , re .Pattern ]] = {}
4544 _yaml_path : ClassVar [Optional [str ]] = None
@@ -48,22 +47,23 @@ class CodegateSignatures:
4847 def reset (cls ) -> None :
4948 """Reset the cached patterns."""
5049 with cls ._instance_lock :
51- cls ._signatures_loaded = False
5250 cls ._signature_groups = []
5351 cls ._compiled_regexes = {}
5452 cls ._yaml_path = None
5553 logger .debug ("SecretFinder cache reset" )
5654
5755 @classmethod
5856 def initialize (cls , yaml_path : str ) -> None :
59- """Initialize the SecretFinder with a YAML file path."""
57+ """Initialize the SecretFinder with a YAML file path and load signatures ."""
6058 if not Path (yaml_path ).exists ():
6159 raise FileNotFoundError (f"Signatures file not found: { yaml_path } " )
6260
6361 with cls ._instance_lock :
64- cls ._yaml_path = yaml_path
65- cls ._signatures_loaded = False
66- logger .debug (f"SecretFinder initialized with { yaml_path } " )
62+ # Only initialize if not already initialized with this path
63+ if cls ._yaml_path != yaml_path :
64+ cls ._yaml_path = yaml_path
65+ cls ._load_signatures ()
66+ logger .debug (f"SecretFinder initialized with { yaml_path } " )
6767
6868 @classmethod
6969 def _preprocess_yaml (cls , content : str ) -> str :
@@ -172,6 +172,10 @@ def _add_signature_group(cls, name: str, patterns: Dict[str, str]) -> None:
172172 def _load_signatures (cls ) -> None :
173173 """Load signature patterns from the YAML file."""
174174 try :
175+ # Clear existing signatures before loading new ones
176+ cls ._signature_groups = []
177+ cls ._compiled_regexes = {}
178+
175179 yaml_data = cls ._load_yaml (cls ._yaml_path )
176180
177181 # Add custom GitHub token patterns
@@ -205,32 +209,14 @@ def _load_signatures(cls) -> None:
205209 logger .error (f"Error loading signatures: { e } " )
206210 raise
207211
208- @classmethod
209- def _ensure_signatures_loaded (cls ) -> None :
210- """Ensure signatures are loaded before use."""
211- if not cls ._signatures_loaded :
212- with cls ._instance_lock :
213- if not cls ._signatures_loaded :
214- if not cls ._yaml_path :
215- raise RuntimeError ("SecretFinder not initialized. Call initialize() first." )
216- try :
217- cls ._load_signatures ()
218- cls ._signatures_loaded = True
219- except Exception as e :
220- logger .error (f"Failed to load signatures: { e } " )
221- raise
222-
223212 @classmethod
224213 def find_in_string (cls , text : str ) -> List [Match ]:
225214 """Search for secrets in the provided string."""
226215 if not text :
227216 return []
228217
229- try :
230- cls ._ensure_signatures_loaded ()
231- except Exception as e :
232- logger .error (f"Failed to load signatures: { e } " )
233- return []
218+ if not cls ._yaml_path :
219+ raise RuntimeError ("SecretFinder not initialized." )
234220
235221 matches = []
236222 lines = text .splitlines ()
0 commit comments