From 56725ab3fbcd43c517ea7ebbed17f951ae736db8 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 29 Sep 2010 09:47:50 -0400 Subject: [PATCH] Add os.path.expanduser() wrappers to user-supplied paths. This allows things like [gui user interface] perspective path = ~/.hooke/resources/gui/perspective to work as expected. --- doc/install.txt | 19 +++++++++++++++++++ hooke/plugin/command_stack.py | 6 ++++-- hooke/plugin/config.py | 3 ++- hooke/plugin/curve.py | 3 ++- hooke/plugin/cut.py | 4 +++- hooke/plugin/playlist.py | 8 +++++--- hooke/plugin/playlists.py | 4 +++- hooke/ui/gui/__init__.py | 17 ++++++++++------- 8 files changed, 48 insertions(+), 16 deletions(-) diff --git a/doc/install.txt b/doc/install.txt index 98ea4c1..270f99e 100644 --- a/doc/install.txt +++ b/doc/install.txt @@ -68,6 +68,25 @@ to install Hooke. Run:: to see a list of installation options you may want to configure. +If you install Hooke, you should probably setup a system- or user-wide +configuration file (:doc:`config`), and replace any relative paths +with appropriate absolute paths. For example:: + + # Commands for managing a command stack (similar to macros). + [command_stack plugin] + # Directory containing command stack files. + path = resources/command_stack + + # wxWindows graphical user interface. + [gui user interface] + # Path to the hooke icon image. + icon image = /usr/share/hooke/doc/img/microscope.ico + # Path to the Hooke splash screen image. + splash screen image = /usr/share/hooke/doc/img/hooke.jpg + # Directory containing perspective files. + perspective path = ~/.hooke/resources/gui/perspective + + Running Hooke from the source directory ======================================= diff --git a/hooke/plugin/command_stack.py b/hooke/plugin/command_stack.py index 59ca5dd..af75490 100644 --- a/hooke/plugin/command_stack.py +++ b/hooke/plugin/command_stack.py @@ -236,7 +236,8 @@ input command stack. If the command stack does not have an input file params['output'] = 'default' if params['output'] != None: params['output'] = os.path.join( - self.plugin.config['path'], params['output']) + os.path.expanduser(self.plugin.config['path']), + params['output']) return params class LoadCommand (CommandStackCommand): @@ -265,7 +266,8 @@ File name for the input command stack. params['input'] = 'default' if params['input'] != None: params['input'] = os.path.join( - self.plugin.config['path'], params['input']) + os.path.expanduser(self.plugin.config['path']), + params['input']) return params diff --git a/hooke/plugin/config.py b/hooke/plugin/config.py index ea1b039..71e0611 100644 --- a/hooke/plugin/config.py +++ b/hooke/plugin/config.py @@ -21,6 +21,7 @@ associated :class:`hooke.command.Command`\s for handling :mod:`hooke.config` classes. """ +import os.path from StringIO import StringIO from ..command import Command, Argument, Failure @@ -126,7 +127,7 @@ most local loaded config file. f = None try: if params['output'] != None: - f = open(params['output'], 'w') + f = open(os.path.expanduser(params['output']), 'w') hooke.config.write(fp=f) finally: if f != None: diff --git a/hooke/plugin/curve.py b/hooke/plugin/curve.py index 3ef141c..7f11900 100644 --- a/hooke/plugin/curve.py +++ b/hooke/plugin/curve.py @@ -25,6 +25,7 @@ associated :class:`hooke.command.Command`\s for handling """ import copy +import os.path import re import numpy @@ -459,7 +460,7 @@ True if you want the column-naming header line. def _run(self, hooke, inqueue, outqueue, params): data = self._block(hooke, params) - with open(params['output'], 'w') as f: + with open(os.path.expanduser(params['output']), 'w') as f: if params['header'] == True: f.write('# %s \n' % ('\t'.join(data.info['columns']))) numpy.savetxt(f, data, delimiter='\t') diff --git a/hooke/plugin/cut.py b/hooke/plugin/cut.py index 5d7d79c..6aef076 100644 --- a/hooke/plugin/cut.py +++ b/hooke/plugin/cut.py @@ -22,6 +22,8 @@ :class:`CutCommand`. """ +import os.path + import numpy from ..command import Command, Argument, Failure @@ -97,7 +99,7 @@ True if you want the column-naming header line. cut_data = data[i_min:i_max+1,:] # slice rows from row-major data # +1 to include data[i_max] row - f = open(params['output'], 'w') + f = open(os.path.expanduser(params['output']), 'w') if params['header'] == True: f.write('# %s \n' % ('\t'.join(cut_data.info['columns']))) numpy.savetxt(f, cut_data, delimiter='\t') diff --git a/hooke/plugin/playlist.py b/hooke/plugin/playlist.py index 616cfed..6dced28 100644 --- a/hooke/plugin/playlist.py +++ b/hooke/plugin/playlist.py @@ -247,7 +247,8 @@ created from scratch with 'new playlist'), this option is required. help=self.__doc__, plugin=plugin) def _run(self, hooke, inqueue, outqueue, params): - self._playlist(hooke, params).save(params['output']) + self._playlist(hooke, params).save( + os.path.expanduser(params['output'])) class LoadCommand (PlaylistAddingCommand): @@ -270,7 +271,8 @@ Drivers for loading curves. help=self.__doc__, plugin=plugin) def _run(self, hooke, inqueue, outqueue, params): - p = load(path=params['input'], drivers=params['drivers'], hooke=hooke) + p = load(os.path.expanduser(path=params['input']), + drivers=params['drivers'], hooke=hooke) self._set_playlist(hooke, params, p) outqueue.put(p) @@ -295,7 +297,7 @@ Additional information for the input :class:`hooke.curve.Curve`. def _run(self, hooke, inqueue, outqueue, params): self._playlist(hooke, params).append_curve_by_path( - params['input'], params['info'], hooke=hooke) + os.path.expanduser(params['input']), params['info'], hooke=hooke) class AddGlobCommand (PlaylistCommand): diff --git a/hooke/plugin/playlists.py b/hooke/plugin/playlists.py index e047cb8..d15740e 100644 --- a/hooke/plugin/playlists.py +++ b/hooke/plugin/playlists.py @@ -21,6 +21,8 @@ several associated :class:`hooke.command.Command`\s for handling lists of :class:`hooke.playlist.Playlist` classes. """ +import os.path + from ..command import Command, Argument, Failure from ..playlist import FilePlaylist from . import Builtin @@ -117,7 +119,7 @@ Default filename for future saves. def _run(self, hooke, inqueue, outqueue, params): p = FilePlaylist( drivers=hooke.drivers, - path=params['file'], + path=os.path.expanduser(params['file']), ) self._set_playlist(hooke, params, p) outqueue.put(p) diff --git a/hooke/ui/gui/__init__.py b/hooke/ui/gui/__init__.py index 6f1a021..a4d91f1 100644 --- a/hooke/ui/gui/__init__.py +++ b/hooke/ui/gui/__init__.py @@ -71,7 +71,9 @@ class HookeFrame (wx.Frame): self._perspectives = {} # {name: perspective_str} self._c = {} - self.SetIcon(wx.Icon(self.gui.config['icon image'], wx.BITMAP_TYPE_ICO)) + self.SetIcon(wx.Icon( + os.path.expanduser(self.gui.config['icon image']), + wx.BITMAP_TYPE_ICO)) # setup frame manager self._c['manager'] = aui.AuiManager() @@ -707,7 +709,7 @@ class HookeFrame (wx.Frame): self._perspectives = { 'Default': self._c['manager'].SavePerspective(), } - path = self.gui.config['perspective path'] + path = os.path.expanduser(self.gui.config['perspective path']) if os.path.isdir(path): files = sorted(os.listdir(path)) for fname in files: @@ -784,7 +786,7 @@ class HookeFrame (wx.Frame): if name == 'Default': name = 'New perspective' name = select_save_file( - directory=self.gui.config['perspective path'], + directory=os.path.expanduser(self.gui.config['perspective path']), name=name, extension=self.gui.config['perspective extension'], parent=self, @@ -793,7 +795,8 @@ class HookeFrame (wx.Frame): if name == None: return self._save_perspective( - perspective, self.gui.config['perspective path'], name=name, + perspective, + os.path.expanduser(self.gui.config['perspective path']), name=name, extension=self.gui.config['perspective extension']) def _on_delete_perspective(self, *args, **kwargs): @@ -814,8 +817,8 @@ class HookeFrame (wx.Frame): names = [options[i] for i in dialog.selected] dialog.Destroy() self._delete_perspectives( - self.gui.config['perspective path'], names=names, - extension=self.gui.config['perspective extension']) + os.path.expanduser(self.gui.config['perspective path']), + names=names, extension=self.gui.config['perspective extension']) def _on_select_perspective(self, _class, method, name): self._restore_perspective(name) @@ -867,7 +870,7 @@ class HookeApp (wx.App): def _setup_splash_screen(self): if self.gui.config['show splash screen'] == True: - path = self.gui.config['splash screen image'] + path = os.path.expanduser(self.gui.config['splash screen image']) if os.path.isfile(path): duration = self.gui.config['splash screen duration'] wx.SplashScreen( -- 2.26.2