# Copyright (C) 2010-2012 W. Trevor King # # This file is part of Hooke. # # Hooke is free software: you can redistribute it and/or modify it under the # terms of the GNU Lesser General Public License as published by the Free # Software Foundation, either version 3 of the License, or (at your option) any # later version. # # Hooke is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more # details. # # You should have received a copy of the GNU Lesser General Public License # along with Hooke. If not, see . """ We run this test in a temporary directory for easy cleanup. >>> import os >>> import shutil >>> import tempfile >>> temp_dir = tempfile.mkdtemp(prefix='tmp-hooke-') >>> from hooke.hooke import Hooke, HookeRunner >>> h = Hooke() >>> r = HookeRunner() Add add some commands to the stack. >>> h = r.run_lines(h, ['start_command_capture', ... 'load_playlist test/data/test', ... 'get_curve', ... 'stop_command_capture']) # doctest: +REPORT_UDIFF Success Success Success Success >>> h = r.run_lines(h, ['get_command_stack']) # doctest: +NORMALIZE_WHITESPACE [, ] Success Ensure we'll be saving in our temporary directory. >>> target_dir = os.path.join(temp_dir, 'resources', 'command_stack') >>> h = r.run_lines(h, ['set_config "command_stack plugin" path %s' ... % target_dir]) Success >>> h = r.run_lines(h, ['get_command_stack']) # doctest: +NORMALIZE_WHITESPACE [, ] Success Save the stack. >>> h = r.run_lines(h, ['get_command_stack']) # doctest: +NORMALIZE_WHITESPACE [, ] Success >>> h = r.run_lines(h, ['save_command_stack']) Success >>> os.listdir(temp_dir) ['resources'] >>> os.listdir(target_dir) ['default'] >>> with open(os.path.join(target_dir, 'default'), 'r') as f: ... print(f.read()) - arguments: {input: test/data/test} command: load playlist - arguments: {} command: get curve You can also specify the name explicitly. >>> h = r.run_lines(h, ['save_command_stack --output my_stack']) Success >>> sorted(os.listdir(target_dir)) ['default', 'my_stack'] >>> with open(os.path.join(target_dir, 'my_stack'), 'r') as f: ... print(f.read()) - arguments: {input: test/data/test} command: load playlist - arguments: {} command: get curve Further saves overwrite the last save/load path by default. >>> h = r.run_lines(h, ['restart_command_capture', ... 'curve_info', ... 'stop_command_capture']) # doctest: +REPORT_UDIFF Success Success Success >>> h = r.run_lines(h, ['save_command_stack']) Success >>> with open(os.path.join(target_dir, 'default'), 'r') as f: ... print(f.read()) - arguments: {input: test/data/test} command: load playlist - arguments: {} command: get curve >>> with open(os.path.join(target_dir, 'my_stack'), 'r') as f: ... print(f.read()) - arguments: {input: test/data/test} command: load playlist - arguments: {} command: get curve - arguments: {} command: curve info But starting command capture (which clears the stack), reverts the default save name to `default`. >>> h = r.run_lines(h, ['start_command_capture', ... 'version', ... 'stop_command_capture']) # doctest: +REPORT_UDIFF Success Success Success >>> h = r.run_lines(h, ['save_command_stack']) Success >>> with open(os.path.join(target_dir, 'default'), 'r') as f: ... print(f.read()) - arguments: {} command: version Clear the stack so loading behavior is more obvious. >>> h = r.run_lines(h, ['start_command_capture', ... 'stop_command_capture']) # doctest: +REPORT_UDIFF Success Success Loading is just the inverse of saving. >>> h = r.run_lines(h, ['load_command_stack']) Success >>> h = r.run_lines(h, ['get_command_stack']) [] Success >>> h = r.run_lines(h, ['load_command_stack --input my_stack']) Success >>> h = r.run_lines(h, ['get_command_stack']) # doctest: +NORMALIZE_WHITESPACE [, , ] Success Now that the current stack's name is `my_stack`, that will be the default stack loaded if you `load_command_stack` without `--input`. >>> with open(os.path.join(target_dir, 'my_stack'), 'w') as f: ... f.write('\\n'.join([ ... '- arguments: {}', ... ' command: debug', ... ''])) >>> h = r.run_lines(h, ['load_command_stack']) Success >>> h = r.run_lines(h, ['get_command_stack']) # doctest: +NORMALIZE_WHITESPACE [] Success Cleanup the temporary directory. >>> shutil.rmtree(temp_dir) """