Skip to content

Commit

Permalink
script: added variables; fixed lineno on error in case of empty or co…
Browse files Browse the repository at this point in the history
…mmented-out lines
  • Loading branch information
vgavro committed Dec 8, 2021
1 parent addb50c commit 0480eda
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -921,14 +921,14 @@ Usage: samsung-mdc [OPTIONS] TARGET script [OPTIONS] SCRIPT_FILE
command1 [ARGS]...
command2 [ARGS]...
Example: samsung-mdc ./targets.txt script -s 3 -r 1 ./commands.txt
Example: samsung-mdc ./targets.txt script -s 3 -r 1 -v KEY enter ./commands.txt
# commands.txt content
power on
sleep 5
clear_menu
virtual_remote key_menu
virtual_remote key_down
virtual_remote enter
virtual_remote {{ KEY }}
clear_menu
Arguments:
Expand All @@ -941,6 +941,8 @@ Options:
-r, --retry-script INTEGER Retry script if failed (count)
--retry-script-sleep FLOAT Sleep before script retry (seconds)
--ignore-nak Ignore negative acknowledgement errors
-v, --var NAME VALUE Variable "{{ NAME }}" in script will be
replaced by VALUE
--help Show this message and exit.
```
#### raw<a id="raw"></a>
Expand Down
28 changes: 23 additions & 5 deletions samsung_mdc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,17 +571,19 @@ async def call(connection, display_id):
command2 [ARGS]...
\b
Example: samsung-mdc ./targets.txt script -s 3 -r 1 ./commands.txt
Example: samsung-mdc ./targets.txt script -s 3 -r 1 -v KEY enter ./commands.txt
# commands.txt content
power on
sleep 5
clear_menu
virtual_remote key_menu
virtual_remote key_down
virtual_remote enter
virtual_remote {{ KEY }}
clear_menu
"""

VAR_TEMPLATE = re.compile(r'{{\s*(.*?)\s*}}')


@cli.command(help=SCRIPT_HELP, cls=FixedSubcommand)
@click.option('-s', '--sleep', default=0, type=float,
Expand All @@ -596,20 +598,24 @@ async def call(connection, display_id):
help='Sleep before script retry (seconds)')
@click.option('--ignore-nak', is_flag=True,
help='Ignore negative acknowledgement errors')
@click.option('--var', '-v', multiple=True, nargs=2, type=(str, str),
help='Variable "{{ NAME }}" in script will be replaced by VALUE',
metavar='NAME VALUE')
@click.argument('script_file', type=click.File(),
help='Text file with commands, separated by newline.',
cls=ArgumentWithHelp)
@click.pass_context
def script(ctx, script_file, sleep, retry_command, retry_command_sleep,
retry_script, retry_script_sleep, ignore_nak):
retry_script, retry_script_sleep, ignore_nak, var):
import shlex

var = dict(var)
retry_command_sleep = retry_command_sleep or sleep
retry_script_sleep = retry_script_sleep or sleep

def fail(lineno, line, reason):
raise click.UsageError(
f'{script_file.name}:{lineno}: "{line}": {reason}')
f'{script_file.name}:{lineno}:"{line}": {reason}')

def create_disconnect():
async def disconnect(connection, display_id):
Expand All @@ -630,10 +636,22 @@ async def sleep(connection, display_id):
lines = [
(i + 1, line.strip())
for i, line in enumerate(script_file.read().split('\n'))
if line.strip() and not line.strip().startswith('#')
]
calls = []
for lineno, line in lines:
if not line or line.startswith('#'):
continue

var_match = VAR_TEMPLATE.search(line)
while var_match:
var_name = var_match.groups()[0]
var_value = var.get(var_name)
if var_value is None:
fail(lineno, line, f'Unknown variable: {var_name}')
line = (line[0:var_match.start()] +
var_value + line[var_match.end():])
var_match = VAR_TEMPLATE.search(line)

command, *args = shlex.split(line)
command = command.lower()
if (command not in cli.commands
Expand Down
2 changes: 1 addition & 1 deletion samsung_mdc/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.8.1'
__version__ = '1.9.0'

0 comments on commit 0480eda

Please sign in to comment.