-
Notifications
You must be signed in to change notification settings - Fork 23
/
remind_sansprint.py
executable file
·172 lines (144 loc) · 5.22 KB
/
remind_sansprint.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# Author: Elyes Rayane Melbouci
# Purpose: This script sets up and manages the RemindEnchanted application, which includes initializing a SQLite database, handling JSON files, running various scripts, and managing processes through a menu bar application.
#!/usr/bin/env python3
import os
import subprocess
import threading
import time
import json
import sys
from pathlib import Path
import rumps
import signal
import psutil
import logging
# Configure logging
logging.basicConfig(level=logging.DEBUG)
def resource_path(relative_path):
""" Get absolute path to resource, works for dev and for PyInstaller """
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
# Get the base directory for the project
base_dir = Path.home() / 'Library' / 'Application Support' / 'RemindEnchanted'
base_dir.mkdir(parents=True, exist_ok=True)
# Path to the regular_db database
regular_db_path = base_dir / 'regular_data.db'
# Paths to JSON files
all_texts_path = base_dir / 'all_texts.json'
new_texts_path = base_dir / 'new_texts.json'
# Script to create the regular_db database if it does not exist
regular_db_script = resource_path('Regular_database.py')
# Scripts to run
scripts = {
"image_record": resource_path('record_photo.py'),
"pipeline": resource_path('pipeline_db.py'),
"ingestion": resource_path('ingestion.py'),
"adding_vectore": resource_path('adding_vectore.py'),
"swift": resource_path('swift.py'),
"deletedb": resource_path('delete_imagedb.py')
}
# Check and create JSON files if they do not exist
if not os.path.exists(all_texts_path):
sample_data = [
{
"date": "22 May 2024",
"entries": [
{
"time": "14:53",
"text": "Sample Data"
}
]
}
]
with open(all_texts_path, 'w') as f:
json.dump(sample_data, f, indent=4)
logging.debug(f"Created sample all_texts.json at {all_texts_path}")
if not os.path.exists(new_texts_path):
sample_data = [
{
"date": "22 May 2024",
"entries": [
{
"time": "14:53",
"text": "Sample Data"
}
]
}
]
with open(new_texts_path, 'w') as f:
json.dump(sample_data, f)
logging.debug(f"Created sample new_texts.json at {new_texts_path}")
# Check for the existence of the regular_db and run the script if it does not exist
if not os.path.exists(regular_db_path):
logging.debug(f"Database not found at {regular_db_path}, running {regular_db_script}")
subprocess.call(['python', regular_db_script])
# List to store all launched processes
all_processes = []
# Variable to control the execution of loops
running = True
# Function to run scripts and manage processes
def run_script(script):
logging.debug(f"Running script {script}")
process = subprocess.Popen(['python', script])
all_processes.append(process)
process.wait()
logging.debug(f"Completed script {script}")
# Function to run ingestion, adding vectors, and indexing
def pipeline():
global running
while running:
for script in ["ingestion", "adding_vectore", "deletedb"]:
if not running:
break
run_script(scripts[script])
time.sleep(10)
# Function to stop all processes
def stop_all_processes():
global running
running = False
logging.debug("Stopping all processes")
# Stop all launched processes
for process in all_processes:
try:
process.terminate()
process.wait(timeout=5)
logging.debug(f"Terminated process {process.pid}")
except Exception as e:
logging.error(f"Error terminating process {process.pid}: {e}")
process.kill()
# Get the PID of the current process
current_process = psutil.Process(os.getpid())
# Get all child processes
children = current_process.children(recursive=True)
# Terminate all child processes
for child in children:
try:
child.terminate()
child.wait(timeout=5)
logging.debug(f"Terminated child process {child.pid}")
except Exception as e:
logging.error(f"Error terminating child process {child.pid}: {e}")
child.kill()
# Terminate the main process
os.kill(os.getpid(), signal.SIGTERM)
# Class for the menu bar application
class RemindEnchantedApp(rumps.App):
def __init__(self):
icon_path = resource_path('remindbg.png')
super(RemindEnchantedApp, self).__init__("RemindEnchanted", icon=icon_path)
@rumps.clicked("Quit")
def quit(self, _):
stop_all_processes()
rumps.quit_application()
# Main function
def main():
# Run audio, image recording, and pipeline scripts
for script in ["swift", "image_record", "pipeline"]:
threading.Thread(target=run_script, args=(scripts[script],)).start()
# Start the pipeline in a separate thread
threading.Thread(target=pipeline).start()
logging.debug("Loading... server started!")
# Run the menu bar application
RemindEnchantedApp().run()
if __name__ == "__main__":
main()