Advertisement
Bubu-Droid

Untitled

May 30th, 2025
3
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import os
  2.  
  3. from watchdog.events import FileSystemEventHandler
  4. from watchdog.observers import Observer
  5.  
  6. import aops
  7. import bon
  8.  
  9. envs = {
  10. "problem": ("\\begin{problem}", "\\end{problem}"),
  11. "solution": ("\\begin{soln}", "\\end{soln}"),
  12. }
  13.  
  14. WATCH_FILE = os.path.join(bon.TMP_PATH, bon.TMP_FILE_NAME)
  15. OUTPUT_FILE = os.path.join(bon.HOME, "bon/bon-db/", f"{bon.DB_NAME}{bon.DB_FILE_EXT}")
  16.  
  17.  
  18. # class TexChangeHandler(FileSystemEventHandler):
  19. # def on_modified(self, event):
  20. # log(f"Modified: {event.src_path}")
  21. # if os.path.abspath(event.src_path) == os.path.abspath(WATCH_FILE):
  22. # log("Ignored: Not the target file.")
  23. # with open(WATCH_FILE, "r", encoding="utf-8") as f:
  24. # tex_content = f.read()
  25. # with open(OUTPUT_FILE, "r", encoding="utf-8") as f:
  26. # db_content = f.read()
  27. # with open(OUTPUT_FILE, "w", encoding="utf-8") as out:
  28. # out.write(transform(db_content, tex_content))
  29. # print("yes")
  30. #
  31. class TexChangeHandler(FileSystemEventHandler):
  32. def on_modified(self, event):
  33. log(f"🔔 Modified: {event.src_path}")
  34.  
  35. if os.path.abspath(event.src_path) != os.path.abspath(WATCH_FILE):
  36. log("❌ Ignored: Not the target file.")
  37. return
  38.  
  39. try:
  40. with open(WATCH_FILE, "r", encoding="utf-8") as f:
  41. tex_content = f.read()
  42. with open(OUTPUT_FILE, "r", encoding="utf-8") as f:
  43. db_content = f.read()
  44. updated = transform(db_content, tex_content)
  45. with open(OUTPUT_FILE, "w", encoding="utf-8") as out:
  46. out.write(updated)
  47. log("✅ Wrote updated content to DB file.")
  48. except Exception as e:
  49. log(f"❌ ERROR: {e}")
  50.  
  51.  
  52. def transform(db_content: str, tex_content: str) -> str:
  53. fin_list = []
  54. db_list = db_content.split(bon.SEPARATOR)
  55. prob_spos = tex_content.index(envs["problem"][0])
  56. prob_epos = tex_content.index(envs["problem"][1])
  57. prob_cont = tex_content[prob_spos : prob_epos + len(envs["problem"][1])]
  58. fin_list.append(db_list[0])
  59. fin_list.append(prob_cont)
  60. i = 0
  61. while tex_content[i:].find(envs["solution"][0]) != -1:
  62. j = tex_content[i:].index(envs["solution"][0])
  63. j_cpos = aops.bracket_cpos(tex_content[i:], envs["solution"], j)
  64. fin_list.append(tex_content[j : j_cpos + len(envs["solution"][1])])
  65. i = j_cpos
  66. return (bon.SEPARATOR).join(fin_list)
  67.  
  68.  
  69. def start_watcher():
  70. log("Watcher started")
  71. event_handler = TexChangeHandler()
  72. observer = Observer()
  73. observer.schedule(event_handler, path=os.path.dirname(WATCH_FILE), recursive=False)
  74. observer.start()
  75. log("Watcher actually started")
  76. return observer
  77.  
  78.  
  79. def log(msg):
  80. with open("/tmp/tmptex_watcher.log", "a", encoding="utf-8") as f:
  81. f.write(msg + "\n")
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement