From ec8f0b586953b23dea6ff24060610f56a7dbf975 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Tue, 17 Aug 2010 00:02:33 -0400 Subject: [PATCH] Added 'save config'. Adjusted test/tutorial.py (and test/ in general) passes. --- doc/config.txt | 8 ++++--- hooke/plugin/config.py | 30 +++++++++++++++++++++-- test/polymer_fit.py | 10 ++++---- test/tutorial.py | 54 +++++++++++++++++++++++++++--------------- 4 files changed, 73 insertions(+), 29 deletions(-) diff --git a/doc/config.txt b/doc/config.txt index d2a8d0a..6b72780 100644 --- a/doc/config.txt +++ b/doc/config.txt @@ -22,7 +22,11 @@ Examples Run:: - $ hooke --save-config -c '' + $ ./bin/hk.py --save-config -c '' + +or:: + + $ ./bin/hk.py -c 'save_config --output .hooke.cfg' To write a well commented example config file to :file:`.hooke.cfg`. @@ -50,5 +54,3 @@ Hooke. run will *not be saved* when Hooke exits. To save the changes, either run the `save config` command before closing Hooke, or start Hooke with the ``--save-config`` option. - -.. todo:: Implement `save config` command. diff --git a/hooke/plugin/config.py b/hooke/plugin/config.py index 466ca1f..ea1b039 100644 --- a/hooke/plugin/config.py +++ b/hooke/plugin/config.py @@ -32,7 +32,7 @@ class ConfigPlugin (Builtin): def __init__(self): super(ConfigPlugin, self).__init__(name='config') self._commands = [GetCommand(self), SetCommand(self), - PrintCommand(self)] + PrintCommand(self), SaveCommand(self),] # Define common or complicated arguments @@ -95,7 +95,7 @@ class SetCommand (Command): outqueue.put(ReloadUserInterfaceConfig(hooke.config)) class PrintCommand (Command): - """Get the current value of all configuration options. + """Get the current configuration file text. """ def __init__(self, plugin): super(PrintCommand, self).__init__( @@ -105,3 +105,29 @@ class PrintCommand (Command): out = StringIO() hooke.config.write(out) outqueue.put(out.getvalue()) + + +class SaveCommand (Command): + """Save the current configuration options. + """ + def __init__(self, plugin): + super(SaveCommand, self).__init__( + name='save config', + arguments=[ + Argument(name='output', type='file', + help=""" +File name for the output configuration. Defaults to overwriting the +most local loaded config file. +""".strip()), + ], + help=self.__doc__, plugin=plugin) + + def _run(self, hooke, inqueue, outqueue, params): + f = None + try: + if params['output'] != None: + f = open(params['output'], 'w') + hooke.config.write(fp=f) + finally: + if f != None: + f.close() diff --git a/test/polymer_fit.py b/test/polymer_fit.py index 40d654e..fe5ccc7 100644 --- a/test/polymer_fit.py +++ b/test/polymer_fit.py @@ -84,11 +84,11 @@ Data([[ NaN, NaN], [ NaN, NaN], [ NaN, NaN]]) >>> retract[1097:1103,-2:] # doctest: +ELLIPSIS -Data([[ NaN, 5.220...e-10], - [ NaN, 5.592...e-10], - [ NaN, 6.104...e-10], - [ NaN, 6.261...e-10], - [ NaN, 7.058...e-10], +Data([[ NaN, 5.234...e-10], + [ NaN, 5.612...e-10], + [ NaN, 6.132...e-10], + [ NaN, 6.292...e-10], + [ NaN, 7.105...e-10], [ NaN, NaN]]) >>> retract[-5:,-2:] Data([[ NaN, NaN], diff --git a/test/tutorial.py b/test/tutorial.py index 32992ef..967cda8 100644 --- a/test/tutorial.py +++ b/test/tutorial.py @@ -219,31 +219,27 @@ Success *Taking notes* ->>> h = r.run_lines(h, ['''set_note "Hi there.\\nI'm a note"''']) -Success - ->>> h = r.run_lines(h, ['note_filter_playlist --output_playlist filtered']) ->>> h = r.run_lines(h, ['get_playlist']) ->>> h = r.run_lines(h, ['jump_to_playlist -- -1']) ->>> h = r.run_lines(h, ['get_playlist']) ->>> h = r.run_lines(h, ['get_curve']) ->>> h = r.run_lines(h, ['curve_index']) ->>> h = r.run_lines(h, ['jump_to_curve -- -1']) ->>> h = r.run_lines(h, ['get_curve']) ->>> h = r.run_lines(h, ['curve_index']) ->>> h = r.run_lines(h, ['get_note']) ->>> h = r.run_lines(h, ['set_note ""']) ->>> h = r.run_lines(h, ['get_note']) +See :file:`note.py`. *Exporting curves* ->>> os.path.exists('mycurve.dat') ->>> h = r.run_lines(h, ['export_bock --output mycurve.dat']) +>>> export_already_exists = os.path.exists('mycurve.dat') +>>> export_already_exists +False +>>> h = r.run_lines(h, ['export_block --output mycurve.dat']) +Success + >>> os.path.isfile('mycurve.dat') +True +>>> if export_already_exists == False: +... os.remove('mycurve.dat') *Measuring distances and forces* ->>> h = r.run_lines(h, ['delta 300 500']) +>>> h = r.run_lines(h, ['delta 300 500']) # doctest: +ELLIPSIS +[('z piezo (m)', -4.913...e-08), ('deflection (m)', -2.834...e-08)] +Success + *Worm like chain and freely jointed chain fitting* @@ -254,9 +250,29 @@ Success *Configuring Hooke* >>> h = r.run_lines(h, ['set_config conditions temperature 300.0']) +Success + >>> h = r.run_lines(h, ['get_config conditions temperature']) +300.0 +Success + >>> h = r.run_lines(h, ['set_config conditions temperature 295.3']) +Success + >>> h = r.run_lines(h, ['get_config conditions temperature']) ->>> h = r.run_lines(h, ['print_config']) +295.3 +Success + +>>> h = r.run_lines(h, ['print_config']) # doctest: +ELLIPSIS +# Default environmental conditions in case they are not specified in +# the force curve data. Configuration options in this section are +# available to every plugin. +[conditions] +# Temperature in Kelvin +temperature = 295.3 + +... >>> h = r.run_lines(h, ['save_config']) +Success + """ -- 2.26.2