Added illysam branch
[hooke.git] / plugins / playlist.py
1 #!/usr/bin/env python
2
3 '''
4 playlist.py
5
6 Playlist commands for Hooke.
7
8 Copyright 2010 by Dr. Rolf Schmidt (Concordia University, Canada)
9
10 This program is released under the GNU General Public License version 2.
11 '''
12
13 import lib.libhooke as lh
14 import wxversion
15 wxversion.select(lh.WX_GOOD)
16
17 import glob
18 import os.path
19 import wx
20
21 from lib.playlist import Playlist
22
23 class playlistCommands(object):
24     '''
25     Playlist commands to generate, save and load lists of force curves
26     '''
27
28     def _plug_init(self):
29         pass
30
31     def do_genlist(self, filemask='', folder=''):
32         '''
33         GENLIST
34         Generates a file playlist.
35         Note it doesn't *save* it: see savelist for this.
36
37         If [input files] is a directory, it will use all files in the directory for playlist.
38         So:
39         genlist dir
40         genlist dir/
41         genlist dir/*.*
42
43         are all equivalent syntax.
44         ------------
45         Syntax: genlist [input files]
46         '''
47         if filemask == '':
48             filemask = self.GetStringFromConfig('playlist', 'genlist', 'filemask')
49         if folder == '':
50             folder = self.GetStringFromConfig('playlist', 'genlist', 'folder')
51         if os.path.isdir(folder):
52             path = os.path.join(folder, filemask)
53             #expanding correctly the input list with the glob module :)
54             files = glob.glob(path)
55             files.sort()
56             playlist = Playlist()
57             for file_to_add in files:
58                 playlist.add_file(file_to_add)
59             if playlist.count > 0:
60                 playlist.name = self._GetUniquePlaylistName(os.path.basename(folder))
61                 playlist.reset()
62                 self.AddToPlaylists(playlist)
63             self.AppendToOutput('Playlist generated.')
64             self.AppendToOutput(playlist.get_status_string())
65         else:
66             self.AppendToOutput(''.join(['Cannot find folder ', folder]))
67
68     def do_loadlist(self, filename=''):
69         '''
70         LOADLIST
71         Loads a file playlist
72         -----------
73         Syntax: loadlist [playlist file]
74         '''
75         #TODO: check for duplicate playlists, ask the user for a unique name
76         #if self.playlist_name in self.playlists:
77         #activate playlist
78
79         if filename == '':
80             filename = self.GetStringFromConfig('playlist', 'loadlist', 'filename')
81
82         #add hkp extension if necessary
83         if not filename.endswith('.hkp'):
84             filename = ''.join([filename, '.hkp'])
85         #prefix with 'hookeDir' if just a filename or a relative path
86         filename = lh.get_file_path(filename)
87         if os.path.isfile(filename):
88             #load playlist
89             playlist_new = Playlist(filename)
90             if playlist_new.count > 0:
91                 self.AppendToOutput('Playlist loaded.')
92                 self.AddToPlaylists(playlist_new)
93                 self.AppendToOutput(playlist_new.get_status_string())
94             else:
95                 message = ''.join(['The file ', filename, ' does not contain any valid curve information.'])
96                 dialog = wx.MessageDialog(None, message, 'Info', wx.OK)
97                 dialog.ShowModal()
98         else:
99             message = ''.join(['File ', filename, ' not found.'])
100             dialog = wx.MessageDialog(None, message, 'Info', wx.OK)
101             dialog.ShowModal()
102
103     def do_savelist(self, filename=''):
104         '''
105         SAVELIST
106         Saves the current file playlist on disk.
107         ------------
108         Syntax: savelist [filename]
109         '''
110
111         if filename == '':
112             filename = self.GetStringFromConfig('playlist', 'savelist', 'filename')
113
114         #autocomplete filename if not specified
115         if not filename.endswith('.hkp'):
116             filename = filename.join(['.hkp'])
117
118         playlist = self.GetActivePlaylist()
119         try:
120             playlist.save(filename)
121             self.AppendToOutput('Playlist saved.')
122         except:
123             self.AppendToOutput('Playlist could not be saved.')
124
125