@@ -60,6 +60,48 @@ def __init__(self):
6060 self .cleaned_files = 0
6161 self .cleaned_dirs = 0
6262
63+ def _get_config (self , ctx : PreCompileContext ) -> dict :
64+ try :
65+ cfg = ctx .get_workspace_config () or {}
66+ plugins_cfg = cfg .get ("plugins" , {}) if isinstance (cfg , dict ) else {}
67+ entry = plugins_cfg .get (self .meta .id , {}) if isinstance (plugins_cfg , dict ) else {}
68+ plugin_cfg = entry .get ("config" , {}) if isinstance (entry , dict ) else {}
69+ if isinstance (plugin_cfg , dict ):
70+ return dict (plugin_cfg )
71+ except Exception :
72+ pass
73+ return {}
74+
75+ def build_config_tab (self , parent , ctx : PreCompileContext , config : dict ):
76+ try :
77+ from PySide6 .QtWidgets import QWidget , QVBoxLayout , QLabel , QCheckBox
78+ except Exception :
79+ return None
80+
81+ w = QWidget (parent )
82+ lay = QVBoxLayout (w )
83+ lay .addWidget (QLabel ("Cleaner settings" ))
84+
85+ chk_confirm = QCheckBox ("Ask confirmation before cleaning" , w )
86+ chk_pyc = QCheckBox ("Remove .pyc files" , w )
87+ chk_pycache = QCheckBox ("Remove __pycache__ folders" , w )
88+
89+ chk_confirm .setChecked (bool (config .get ("confirm" , True )))
90+ chk_pyc .setChecked (bool (config .get ("clean_pyc" , True )))
91+ chk_pycache .setChecked (bool (config .get ("clean_pycache" , True )))
92+
93+ lay .addWidget (chk_confirm )
94+ lay .addWidget (chk_pyc )
95+ lay .addWidget (chk_pycache )
96+
97+ def on_save (cfg : dict ):
98+ cfg ["confirm" ] = bool (chk_confirm .isChecked ())
99+ cfg ["clean_pyc" ] = bool (chk_pyc .isChecked ())
100+ cfg ["clean_pycache" ] = bool (chk_pycache .isChecked ())
101+ return cfg
102+
103+ return ("Cleaner" , w , on_save )
104+
63105 def on_pre_compile (self , ctx : PreCompileContext ) -> None :
64106 """Nettoie le workspace avant la compilation.
65107
@@ -72,16 +114,25 @@ def on_pre_compile(self, ctx: PreCompileContext) -> None:
72114 log .log_warn ("Workspace is not valid or bcasl.yml not found" )
73115 return
74116
117+ cfg = self ._get_config (ctx )
118+ ask_confirm = bool (cfg .get ("confirm" , True ))
119+ clean_pyc = bool (cfg .get ("clean_pyc" , True ))
120+ clean_pycache = bool (cfg .get ("clean_pycache" , True ))
121+ if not clean_pyc and not clean_pycache :
122+ log .log_info ("Cleaner: nothing to do (both options disabled)" )
123+ return
124+
75125 # Demander confirmation à l'utilisateur
76- response = dialog .msg_question (
77- title = "Cleaner" ,
78- text = "Do you want to clean the workspace (.pyc and __pycache__)?" ,
79- default_yes = True ,
80- )
126+ if ask_confirm :
127+ response = dialog .msg_question (
128+ title = "Cleaner" ,
129+ text = "Do you want to clean the workspace (.pyc and __pycache__)?" ,
130+ default_yes = True ,
131+ )
81132
82- if not response :
83- log .log_info ("Cleaner cancelled by user" )
84- return
133+ if not response :
134+ log .log_info ("Cleaner cancelled by user" )
135+ return
85136
86137 # Réinitialiser les compteurs
87138 self .cleaned_files = 0
@@ -107,46 +158,51 @@ def on_pre_compile(self, ctx: PreCompileContext) -> None:
107158 try :
108159 # Utiliser les patterns d'exclusion depuis bcasl.yml
109160 exclude_patterns = ctx .get_exclude_patterns ()
110- for file_path in ctx .iter_files (["**/*.pyc" ], exclude_patterns ):
111- pyc_files .append (file_path )
161+ if clean_pyc :
162+ for file_path in ctx .iter_files (["**/*.pyc" ], exclude_patterns ):
163+ pyc_files .append (file_path )
112164 except Exception as e :
113165 log .log_warn (f"Error iterating .pyc files: { e } " )
114166
115167 # Étape 2: Supprimer les fichiers .pyc
116- progress .set_message ("Removing .pyc files..." )
117- progress .set_progress (0 , len (pyc_files ))
118-
119- for idx , file_path in enumerate (pyc_files ):
120- if progress .is_canceled ():
121- break
122- try :
123- Path (file_path ).unlink ()
124- self .cleaned_files += 1
125- except Exception as e :
126- log .log_warn (f"Failed to remove { file_path } : { e } " )
127- progress .set_progress (idx + 1 , len (pyc_files ))
168+ if clean_pyc :
169+ progress .set_message ("Removing .pyc files..." )
170+ progress .set_progress (0 , len (pyc_files ))
171+
172+ for idx , file_path in enumerate (pyc_files ):
173+ if progress .is_canceled ():
174+ break
175+ try :
176+ Path (file_path ).unlink ()
177+ self .cleaned_files += 1
178+ except Exception as e :
179+ log .log_warn (f"Failed to remove { file_path } : { e } " )
180+ progress .set_progress (idx + 1 , len (pyc_files ))
128181
129182 # Étape 3: Parcourir et supprimer les dossiers __pycache__
130- progress .set_message ("Removing __pycache__ directories..." )
183+ if clean_pycache :
184+ progress .set_message ("Removing __pycache__ directories..." )
131185
132186 pycache_dirs = []
133187 try :
134- for pycache_dir in workspace_path .rglob ("__pycache__" ):
135- pycache_dirs .append (pycache_dir )
188+ if clean_pycache :
189+ for pycache_dir in workspace_path .rglob ("__pycache__" ):
190+ pycache_dirs .append (pycache_dir )
136191 except Exception as e :
137192 log .log_warn (f"Error iterating __pycache__ directories: { e } " )
138193
139- progress .set_progress (0 , len (pycache_dirs ))
140-
141- for idx , pycache_dir in enumerate (pycache_dirs ):
142- if progress .is_canceled ():
143- break
144- try :
145- shutil .rmtree (pycache_dir )
146- self .cleaned_dirs += 1
147- except Exception as e :
148- log .log_warn (f"Failed to remove { pycache_dir } : { e } " )
149- progress .set_progress (idx + 1 , len (pycache_dirs ))
194+ if clean_pycache :
195+ progress .set_progress (0 , len (pycache_dirs ))
196+
197+ for idx , pycache_dir in enumerate (pycache_dirs ):
198+ if progress .is_canceled ():
199+ break
200+ try :
201+ shutil .rmtree (pycache_dir )
202+ self .cleaned_dirs += 1
203+ except Exception as e :
204+ log .log_warn (f"Failed to remove { pycache_dir } : { e } " )
205+ progress .set_progress (idx + 1 , len (pycache_dirs ))
150206
151207 finally :
152208 progress .close ()
0 commit comments