Skip to content

Commit 05de5e6

Browse files
authored
* Creating a listener for jpgs that will upload to the panoptes-images-pretty bucket. (#1272)
* Not a service yet.
1 parent 311e777 commit 05de5e6

File tree

1 file changed

+71
-14
lines changed

1 file changed

+71
-14
lines changed

src/panoptes/pocs/utils/cli/network.py

Lines changed: 71 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,13 @@ def get_key_cmd(unit_id: str = typer.Option(..., prompt=True),
8787

8888

8989
@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):
9191
"""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}')
9796

98-
print(f'Listening to {dir_path.absolute()} for {unit_id}')
99-
event_handler = FileSystemEventHandler()
10097
firestore_db = firestore.Client()
10198
# Get the unit reference to link metadata to unit.
10299
unit_ref = firestore_db.document(f'units/{unit_id}')
@@ -134,22 +131,62 @@ def handleEvent(event):
134131
except Exception as e:
135132
print(f'Exception {e!r}')
136133

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)
141135
try:
142136
while True:
143137
time.sleep(1)
144138
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')
146182
file_observer.stop()
147183
finally:
148184
file_observer.join()
149185

150186

151187
@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,
153190
bucket_name: str = 'panoptes-images-incoming',
154191
timeout: float = 180.,
155192
storage_client=None
@@ -203,3 +240,23 @@ def upload_directory(directory_path: Path,
203240
continue
204241

205242
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

Comments
 (0)