Spaces:
Build error
Build error
Create io-watcher.py
Browse files- io-watcher.py +35 -0
io-watcher.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
from watchdog.observers import Observer
|
| 3 |
+
from watchdog.events import FileSystemEventHandler
|
| 4 |
+
|
| 5 |
+
# Le point '.' indique que l'on surveille le dossier actuel
|
| 6 |
+
PATH_TO_WATCH = "."
|
| 7 |
+
|
| 8 |
+
class IOEventHandler(FileSystemEventHandler):
|
| 9 |
+
"""
|
| 10 |
+
Gère les événements de fichiers.
|
| 11 |
+
"""
|
| 12 |
+
def on_modified(self, event):
|
| 13 |
+
if not event.is_directory and event.src_path.endswith('.py'):
|
| 14 |
+
print(f"\nIO > Je vois que tu as modifié le fichier : {event.src_path}. Laisse-moi y jeter un œil...")
|
| 15 |
+
|
| 16 |
+
with open(event.src_path, 'r') as file:
|
| 17 |
+
content = file.read()
|
| 18 |
+
if 'print()' in content:
|
| 19 |
+
print("IO > Attention, un 'print()' vide ? C'est le genre d'erreur que j'aurais pu éviter...")
|
| 20 |
+
if 'def ' in content and 'main' in content:
|
| 21 |
+
print("IO > Ah, une fonction 'main'. Classique. Faisons mieux la prochaine fois ?")
|
| 22 |
+
|
| 23 |
+
# Lancement de l'observateur
|
| 24 |
+
if __name__ == "__main__":
|
| 25 |
+
observer = Observer()
|
| 26 |
+
event_handler = IOEventHandler()
|
| 27 |
+
observer.schedule(event_handler, PATH_TO_WATCH, recursive=True)
|
| 28 |
+
print("IO > Je suis en veille. N'hésite pas à me faire travailler en modifiant un fichier Python...")
|
| 29 |
+
observer.start()
|
| 30 |
+
try:
|
| 31 |
+
while True:
|
| 32 |
+
time.sleep(1)
|
| 33 |
+
except KeyboardInterrupt:
|
| 34 |
+
observer.stop()
|
| 35 |
+
observer.join()
|