def test_exits_because_id_not_satisfied(self, mocker): mock_exit = mocker.patch("pythonanywhere.scripts_commons.sys.exit") mock_snake = mocker.patch("pythonanywhere.scripts_commons.snakesay") mock_warning = mocker.patch("pythonanywhere.scripts_commons.logger.warning") schema = ScriptSchema({"<id>": ScriptSchema.id_required}) schema.validate_user_input({"<id>": None}) assert mock_exit.call_args == call(1) assert mock_warning.call_count == 1 assert mock_snake.call_args == call("<id> has to be an integer")
def test_exits_because_minute_not_satisfied(self, mocker): mock_exit = mocker.patch("pythonanywhere.scripts_commons.sys.exit") mock_snake = mocker.patch("pythonanywhere.scripts_commons.snakesay") mock_warning = mocker.patch("pythonanywhere.scripts_commons.logger.warning") schema = ScriptSchema({"--minute": ScriptSchema.minute}) schema.validate_user_input({"--minute": 60}) assert mock_exit.call_args == call(1) assert mock_warning.call_count == 1 assert mock_snake.call_args == call("--minute has to be in 0..59")
def test_exits_because_boolean_not_satisfied(self, mocker): mock_exit = mocker.patch("pythonanywhere.scripts_commons.sys.exit") mock_snake = mocker.patch("pythonanywhere.scripts_commons.snakesay") mock_warning = mocker.patch("pythonanywhere.scripts_commons.logger.warning") schema = ScriptSchema({"--toggle": ScriptSchema.boolean}) schema.validate_user_input({"--toggle": "not valid value"}) assert mock_exit.call_args == call(1) assert mock_warning.call_count == 1 assert mock_snake.call_args == call( "Key '--toggle' error:\nOr(None, <class 'bool'>) did not validate 'not valid value'\n" "'not valid value' should be instance of 'bool'" )
def test_exits_because_tabulate_format_not_satisfied(self, mocker): mock_exit = mocker.patch("pythonanywhere.scripts_commons.sys.exit") mock_snake = mocker.patch("pythonanywhere.scripts_commons.snakesay") mock_warning = mocker.patch("pythonanywhere.scripts_commons.logger.warning") schema = ScriptSchema({"--format": ScriptSchema.tabulate_format}) schema.validate_user_input({"--format": "non_existing_format"}) assert mock_exit.call_args == call(1) assert mock_warning.call_count == 1 assert mock_snake.call_args == call( "--format should match one of: plain, simple, github, grid, fancy_grid, pipe, orgtbl, " "jira, presto, psql, rst, mediawiki, moinmoin, youtrack, html, latex, latex_raw, " "latex_booktabs, textile" )
if input("This will irrevocably delete all your tasks, proceed? [y/N] ").lower() != "y": return None for task in TaskList().tasks: task.delete_schedule() def _delete_by_id(id_numbers): for task_id in id_numbers: task = get_task_from_id(task_id, no_exit=True) task.delete_schedule() def main(*, id_numbers, nuke, force): get_logger(set_info=True) if nuke: _delete_all(force) else: _delete_by_id(id_numbers) if __name__ == "__main__": schema = ScriptSchema( {"id": bool, "<num>": ScriptSchema.id_multi, "nuke": bool, "--force": ScriptSchema.boolean} ) arguments = schema.validate_user_input(docopt(__doc__), conversions={"num": "id_numbers"}) arguments.pop("id") main(**arguments)
else: table = [[spec, val] for spec, val in specs.items()] table.sort(key=lambda x: x[0]) logger.info(intro) logger.info(tabulate(table, tablefmt="simple")) if __name__ == "__main__": schema = ScriptSchema( { "<id>": ScriptSchema.id_required, "--command": ScriptSchema.boolean, "--enabled": ScriptSchema.boolean, "--expiry": ScriptSchema.boolean, "--hour": ScriptSchema.boolean, "--interval": ScriptSchema.boolean, "--logfile": ScriptSchema.boolean, "--minute": ScriptSchema.boolean, "--printable-time": ScriptSchema.boolean, "--no-spec": ScriptSchema.boolean, "--snakesay": ScriptSchema.boolean, } ) arguments = schema.validate_user_input( docopt(__doc__), conversions={ "id": "task_id", "no-": "no_", "printable-": "printable_", "snakesay": "snake", },
from docopt import docopt from tabulate import tabulate from pythonanywhere.scripts_commons import ScriptSchema, get_logger from pythonanywhere.snakesay import snakesay from pythonanywhere.task import TaskList def main(tablefmt): logger = get_logger(set_info=True) headers = "id", "interval", "at", "status", "command" attrs = "task_id", "interval", "printable_time", "enabled", "command" def get_right_value(task, attr): value = getattr(task, attr) if attr == "enabled": value = "enabled" if value else "disabled" return value table = [[get_right_value(task, attr) for attr in attrs] for task in TaskList().tasks] msg = tabulate(table, headers, tablefmt=tablefmt) if table else snakesay("No scheduled tasks") logger.info(msg) if __name__ == "__main__": schema = ScriptSchema({"--format": ScriptSchema.tabulate_format}) argument = schema.validate_user_input(docopt(__doc__)) main(argument.get("format", "simple"))
Once task is created its behavior may be altered later on with `pa_update_scheduled_task.py` or deleted with `pa_delete_scheduled_task.py` scripts.""" from docopt import docopt from pythonanywhere.scripts_commons import ScriptSchema, get_logger from pythonanywhere.task import Task def main(*, command, hour, minute, disabled): get_logger(set_info=True) hour = int(hour) if hour is not None else None task = Task.to_be_created(command=command, hour=hour, minute=int(minute), disabled=disabled) task.create_schedule() if __name__ == "__main__": schema = ScriptSchema({ "--command": str, "--hour": ScriptSchema.hour, "--minute": ScriptSchema.minute, "--disabled": ScriptSchema.boolean, }) arguments = schema.validate_user_input(docopt(__doc__)) main(**arguments)
}[enable_opt] params.update({"enabled": enabled}) try: task.update_schedule(params, porcelain=porcelain) except Exception as e: logger.warning(snakesay(str(e))) if __name__ == "__main__": schema = ScriptSchema({ "<id>": ScriptSchema.id_required, "--command": ScriptSchema.string, "--daily": ScriptSchema.boolean, "--disable": ScriptSchema.boolean, "--enable": ScriptSchema.boolean, "--hour": ScriptSchema.hour, "--hourly": ScriptSchema.boolean, "--minute": ScriptSchema.minute, "--porcelain": ScriptSchema.boolean, "--quiet": ScriptSchema.boolean, "--toggle-enabled": ScriptSchema.boolean, }) arguments = schema.validate_user_input(docopt(__doc__), conversions={ "id": "task_id", "toggle-": "toggle_" }) main(**arguments)
def test_validates_tabulate_format(self): schema = ScriptSchema({"--format": ScriptSchema.tabulate_format}) for val in tabulate_formats: result = schema.validate_user_input({"--format": val}) assert result == {"format": val}
def test_validates_using_conversions(self): schema = ScriptSchema({"<id>": ScriptSchema.id_required}) result = schema.validate_user_input({"<id>": 42}, conversions={"id": "task_id"}) assert result == {"task_id": 42}
def test_validates_minute(self): schema = ScriptSchema({"--minute": ScriptSchema.minute}) for val in (None, 1, 30, 59): result = schema.validate_user_input({"--minute": val}) assert result == {"minute": val}
def test_validates_bour(self): schema = ScriptSchema({"--hour": ScriptSchema.hour}) for val in (None, 0, 12, 23): result = schema.validate_user_input({"--hour": val}) assert result == {"hour": val}
def test_validates_boolean(self): schema = ScriptSchema({"--toggle": ScriptSchema.boolean}) for val in (True, False, None): result = schema.validate_user_input({"--toggle": val}) assert result == {"toggle": val}
def test_returns_unchanged_string(self): assert ScriptSchema({}).convert("will_not_be_changed") == "will_not_be_changed"
def test_replaces_default_strings(self): was = ("--option", "<arg>") should_be = ("option", "arg") for string, expected in zip(was, should_be): assert ScriptSchema({}).convert(string) == expected