11from recorder .config import DEFAULT_CONFIG_PATH , DEFAULT_OUTPUT_FOLDER
22from recorder .device import Device , DEVICE_CLASSES
3+ from recorder .listener import Listener , LISTENER_CLASSES
34from recorder .io import yaml_dump
45from recorder import Recorder , Config
56
2526 DEFAULT_CONFIG_PATH ,
2627 help = "Path to a `yaml` config file. Run `config` command to generate one."
2728 )
28-
2929DEVICE_CLASS_ARGUMENT = typer .Argument (
3030 None ,
3131 metavar = 'DEVICE_CLASS' ,
4141 "Numerical id of the device to use. Use the show command to see the available devices for each device class."
4242 )
4343 )
44+ LISTENER_CLASS_ARGUMENT = typer .Argument (
45+ 'localhost' ,
46+ metavar = 'LISTENER_CLASS' ,
47+ help = (
48+ "Case independent name of the listener class to use. Available "
49+ f"options: { [d for d in DEVICE_CLASSES .keys ()]} ."
50+ )
51+ )
4452VERBOSE_OPTION = typer .Option (False , "--verbose" , "-v" , help = "Verbose output." )
4553
4654
@@ -58,7 +66,7 @@ def choose(message: str, choices: list):
5866 })['choice' ]
5967
6068
61- def get_device_class (name : str ):
69+ def get_device_class (name : str ) -> Device :
6270 try :
6371 return DEVICE_CLASSES [name .lower ()]
6472 except KeyError :
@@ -68,6 +76,16 @@ def get_device_class(name: str):
6876 )
6977
7078
79+ def get_listener_class (name : str ) -> Listener :
80+ try :
81+ return LISTENER_CLASSES [name .lower ()]
82+ except KeyError :
83+ raise RuntimeError (
84+ f"Invalid listener class `{ name } `. Available options: "
85+ f"{ list (LISTENER_CLASSES .keys ())} "
86+ )
87+
88+
7189@app .command (help = "Display the available devices" )
7290def show (
7391 device_class : Optional [str ] = DEVICE_CLASS_ARGUMENT ,
@@ -138,6 +156,7 @@ def record(
138156 seconds : int = typer .Argument (None , help = "Number of seconds to record" ),
139157 config_path : Optional [Path ] = DEFAULT_CONFIG_PATH_OPTION ,
140158 output_folder : Optional [Path ] = DEFAULT_OUTPUT_FOLDER_OPTION ,
159+ # TODO pass kwargs to listener. Or make subcommands for each listener
141160 ):
142161 config = get_config (config_path )
143162 recorder = Recorder (config , output_folder )
@@ -146,6 +165,18 @@ def record(
146165 typer .echo (f"Recording finished, data saved to { output_folder } " )
147166
148167
168+ @app .command (help = 'Listen to an external command to start recording' )
169+ def listen (
170+ listener_class : str = LISTENER_CLASS_ARGUMENT ,
171+ config_path : Optional [Path ] = DEFAULT_CONFIG_PATH_OPTION ,
172+ output_folder : Optional [Path ] = DEFAULT_OUTPUT_FOLDER_OPTION ,
173+ ):
174+ config = get_config (config_path )
175+ recorder = Recorder (config , output_folder )
176+ listener = get_listener_class (listener_class )(recorder )
177+ listener .listen ()
178+
179+
149180@app .command (help = "Test device recording" )
150181def test (
151182 device_class : Optional [str ] = DEVICE_CLASS_ARGUMENT ,
@@ -184,9 +215,6 @@ def test(
184215 recorder = Recorder (config , output_folder )
185216 output_folder = recorder (seconds = 5 )
186217
187- @app .command (help = 'Listen to an external command to start recording' )
188- def listen (to : str ):
189- raise NotImplementedError (f"Listen to { to } is not implemented." )
190218
191219def main ():
192220 # Launch the command line application
0 commit comments