@@ -87,16 +87,13 @@ def get_key_cmd(unit_id: str = typer.Option(..., prompt=True),
87
87
88
88
89
89
@app .command ('upload-metadata' )
90
- def upload_metadata (dir_path : Path = '. ' , unit_id : str = None , verbose : bool = False ):
90
+ def upload_metadata (dir_path : Path = '/home/panoptes/json_store/panoptes ' , unit_id : str = None , verbose : bool = False ):
91
91
"""Send json files in directory to firestore."""
92
- try :
93
- unit_id = unit_id or os .getenv ('unit_id' , get_config ('pan_id' , default = 'PAN000' ))
94
- except KeyError :
95
- print (f'Need to pass a unit_id param or set UNIT_ID envvar.' )
96
- return
92
+ unit_id = unit_id or _get_unit_id ()
93
+
94
+ if verbose :
95
+ print (f'Listening to { dir_path .absolute ()} for { unit_id } ' )
97
96
98
- print (f'Listening to { dir_path .absolute ()} for { unit_id } ' )
99
- event_handler = FileSystemEventHandler ()
100
97
firestore_db = firestore .Client ()
101
98
# Get the unit reference to link metadata to unit.
102
99
unit_ref = firestore_db .document (f'units/{ unit_id } ' )
@@ -134,22 +131,62 @@ def handleEvent(event):
134
131
except Exception as e :
135
132
print (f'Exception { e !r} ' )
136
133
137
- event_handler .on_modified = handleEvent
138
- file_observer = Observer ()
139
- file_observer .schedule (event_handler , dir_path .as_posix ())
140
- file_observer .start ()
134
+ file_observer = _start_event_handler (dir_path , handleEvent )
141
135
try :
142
136
while True :
143
137
time .sleep (1 )
144
138
except KeyboardInterrupt :
145
- print (f'Cleaning up file watcher' )
139
+ if verbose :
140
+ print (f'Cleaning up file watcher' )
141
+ file_observer .stop ()
142
+ finally :
143
+ file_observer .join ()
144
+
145
+
146
+ @app .command ('upload-images' )
147
+ def upload_images (dir_path : Path = '/home/panoptes/images' , unit_id : str = None , verbose : bool = False ):
148
+ """Send images in directory to google storage."""
149
+ unit_id = unit_id or _get_unit_id ()
150
+
151
+ if verbose :
152
+ print (f'Listening to { dir_path .absolute ()} for { unit_id } ' )
153
+
154
+ storage_client = storage .Client ()
155
+
156
+ def handleEvent (event ):
157
+ if event .is_directory :
158
+ return
159
+
160
+ if event .src_path .endswith ('.jpg' ) is False :
161
+ if verbose :
162
+ print (f'Skipping { event .src_path } ' )
163
+ return
164
+
165
+ try :
166
+ upload_image_cmd (
167
+ event .src_path ,
168
+ bucket_path = f'images/{ unit_id } ' ,
169
+ bucket_name = 'panoptes-images-pretty' ,
170
+ storage_client = storage_client
171
+ )
172
+ except Exception as e :
173
+ print (f'Exception { e !r} ' )
174
+
175
+ file_observer = _start_event_handler (dir_path , handleEvent )
176
+ try :
177
+ while True :
178
+ time .sleep (1 )
179
+ except KeyboardInterrupt :
180
+ if verbose :
181
+ print (f'Cleaning up pretty image file watcher' )
146
182
file_observer .stop ()
147
183
finally :
148
184
file_observer .join ()
149
185
150
186
151
187
@upload_app .command ('image' )
152
- def upload_image_cmd (file_path : Path , bucket_path : str ,
188
+ def upload_image_cmd (file_path : Path ,
189
+ bucket_path : str ,
153
190
bucket_name : str = 'panoptes-images-incoming' ,
154
191
timeout : float = 180. ,
155
192
storage_client = None
@@ -203,3 +240,23 @@ def upload_directory(directory_path: Path,
203
240
continue
204
241
205
242
return public_urls
243
+
244
+
245
+ def _get_unit_id ():
246
+ """Get the unit id from the environment or config."""
247
+ unit_id = os .getenv ('UNIT_ID' , get_config ('pan_id' ))
248
+
249
+ if unit_id is None :
250
+ raise ValueError ('No unit id found in environment or config' )
251
+
252
+ return unit_id
253
+
254
+
255
+ def _start_event_handler (dir_path : Path , handle_event : callable ):
256
+ """Start the event handler."""
257
+ event_handler = FileSystemEventHandler ()
258
+ event_handler .on_modified = handle_event
259
+ file_observer = Observer ()
260
+ file_observer .schedule (event_handler , dir_path .as_posix ())
261
+ file_observer .start ()
262
+ return file_observer
0 commit comments