1+ import asyncio
12import click
23import sqlalchemy .orm
34
1112
1213
1314@main .group ()
14- def recording ():
15+ @async_command ()
16+ async def recording (obj : ServiceRegistry ):
1517 """Recording management."""
18+ # Disable auto-import for use in a cli context
19+ obj .get ("importer" , RecordingManager , uninitialized_ok = True ).auto_import = False
1620
1721
1822@recording .command ("list" )
@@ -47,13 +51,45 @@ async def _delete(obj: ServiceRegistry, record_id):
4751 click .echo (f"Deleted { record .record_id } " )
4852
4953
54+ @recording .command ()
55+ @click .argument ("record_id" , nargs = - 1 )
56+ @async_command ()
57+ async def publish (obj : ServiceRegistry , record_id ):
58+ """Publish recordings"""
59+ await _change_publish_flag (obj , record_id , model .RecordingState .PUBLISHED )
60+
61+
62+ @recording .command ()
63+ @click .argument ("record_id" , nargs = - 1 )
64+ @async_command ()
65+ async def unpublish (obj : ServiceRegistry , record_id ):
66+ """Unpublish recordings"""
67+ await _change_publish_flag (obj , record_id , model .RecordingState .UNPUBLISHED )
68+
69+
70+ async def _change_publish_flag (obj : ServiceRegistry , record_id , state :model .RecordingState ):
71+ importer = await obj .use ("importer" , RecordingManager )
72+ db = await obj .use ("db" , DBContext )
73+
74+ async with db .session () as session :
75+ stmt = model .Recording .select (model .Recording .record_id .in_ (record_id ), model .Recording .state != state ).options (sqlalchemy .orm .joinedload (model .Recording .tenant ))
76+ records = (await session .execute (stmt )).scalars ().all ()
77+ for record in records :
78+ record .state = state
79+ await session .commit ()
80+ if state == model .RecordingState .PUBLISHED :
81+ await asyncio .to_thread (importer .publish , record .tenant .name , record .record_id )
82+ else :
83+ await asyncio .to_thread (importer .unpublish , record .tenant .name , record .record_id )
84+
85+
86+
5087@recording .command ("import" )
5188@click .option ("--tenant" , help = "Override the tenant found in the recording" )
5289@click .argument ("FILE" , type = click .Path (dir_okay = True ), default = "-" )
5390@async_command ()
54- async def _import (obj : ServiceRegistry , tenant : str , file : str ):
91+ async def _import (obj : ServiceRegistry , tenant : str , file : str , publish : bool ):
5592 """Import one or more recordings from a tar archive"""
56- obj .get ("importer" , RecordingManager , uninitialized_ok = True ).auto_import = False
5793 importer = await obj .use ("importer" , RecordingManager )
5894
5995 async def reader (file ):
@@ -66,6 +102,7 @@ async def reader(file):
66102 if task .error :
67103 click .echo (f"ERROR { task .error } " )
68104 raise SystemExit (1 )
105+
69106 click .echo ("OK" )
70107
71108
0 commit comments