Ran update_copyright.py
[hooke.git] / hooke / ui / gui / playlist.py
1 #!/usr/bin/env python
2
3 '''
4 playlist.py
5
6 Playlist class 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 os.path
14 import xml.dom.minidom
15
16 import lib.libhooke
17 import lib.file
18
19 class Playlist(object):
20
21     def __init__(self, filename=None):
22         self._saved = False
23         self.count = 0
24         self.figure = None
25         self.files = []
26         self.generics_dict = {}
27         self.hidden_attributes = ['curve', 'data', 'driver', 'fits', 'name', 'plot', 'plots']
28         self.index = -1
29         self.name = None
30         self.path = None
31         self.plot_panel = None
32         self.plot_tab = None
33         if filename is None:
34             self.filename = None
35         else:
36             self.load(filename)
37
38     def add_file(self, filename):
39         if os.path.isfile(filename):
40             file_to_add = lib.file.File(filename)
41             self.files.append(file_to_add)
42             self._saved = False
43         self.count = len(self.files)
44
45     def delete_file(self, name):
46         for index, item in enumerate(self.files):
47             if item.name == name:
48                 del self.files[index]
49                 self.index = index
50         self.count = len(self.files)
51         if self.index > self.count - 1:
52             self.index = 0
53
54     def filter_curves(self, curves_to_keep=[]):
55         playlist = Playlist()
56         for curve_index in curves_to_keep:
57             playlist.files.append(self.files[curve_index])
58         playlist.count = len(playlist.files)
59         playlist.index = 0
60         return playlist
61
62     def get_active_file(self):
63         return self.files[self.index]
64
65     #def get_active_plot(self):
66         ##TODO: is this the only active (or default?) plot?
67         #return self.files[self.index].plots[0]
68
69     def load(self, filename):
70         '''
71         Loads a playlist file
72         '''
73         self.filename = filename
74         self.path, self.name = os.path.split(filename)
75         playlist_file = lib.libhooke.delete_empty_lines_from_xmlfile(filename)
76         self.xml = xml.dom.minidom.parseString(playlist_file)
77         #TODO: rename 'element' to 'curve' or something in hkp file
78         #TODO: rename 'path' to 'filename'
79         #TODO: switch from attributes to nodes, it's cleaner XML in my eyes
80
81         element_list = self.xml.getElementsByTagName('element')
82         #populate playlist with files
83         for index, element in enumerate(element_list):
84             #rebuild a data structure from the xml attributes
85             #the next two lines are here for backwards compatibility, newer playlist files use 'filename' instead of 'path'
86             if element.hasAttribute('path'):
87                 #path, name = os.path.split(element.getAttribute('path'))
88                 #path = path.split(os.sep)
89                 #filename = lib.libhooke.get_file_path(name, path)
90                 filename = element.getAttribute('path')
91             if element.hasAttribute('filename'):
92                 #path, name = os.path.split(element.getAttribute('filename'))
93                 #path = path.split(os.sep)
94                 #filename = lib.libhooke.get_file_path(name, path)
95                 filename = element.getAttribute('filename')
96             if os.path.isfile(filename):
97                 data_file = lib.file.File(filename)
98                 if element.hasAttribute('note'):
99                     data_file.note = element.getAttribute('note')
100                 self.files.append(data_file)
101         self.count = len(self.files)
102         if self.count > 0:
103             #populate generics
104             genericsDict = {}
105             generics_list = self.xml.getElementsByTagName('generics')
106             if generics_list:
107                 for attribute in generics_list[0].attributes.keys():
108                     genericsDict[attribute] = generics_list[0].getAttribute(attribute)
109             if genericsDict.has_key('pointer'):
110                 index = int(genericsDict['pointer'])
111                 if index >= 0 and index < self.count:
112                     self.index = index
113                 else:
114                     index = 0
115             self._saved = True
116
117     def next(self):
118         self.index += 1
119         if self.index > self.count - 1:
120             self.index = 0
121
122     def previous(self):
123         self.index -= 1
124         if self.index < 0:
125             self.index = self.count - 1
126
127     def reset(self):
128         if self.count > 0:
129             self.index = 0
130         else:
131             self.index = -1
132
133     def save(self, filename):
134         '''
135         Saves a playlist from a list of files.
136         A playlist is an XML document with the following syntax:
137         <playlist>
138         <element path="/my/file/path/"/ attribute="attribute">
139         <element path="...">
140         </playlist>
141         '''
142         try:
143             output_file = file(filename, 'w')
144         except IOError:
145             self.AppendToOutput('Cannot save playlist. Wrong path or filename')
146             return
147         #create the output playlist, a simple XML document
148         implementation = xml.dom.minidom.getDOMImplementation()
149         #create the document DOM object and the root element
150         self.xml = implementation.createDocument(None, 'playlist', None)
151         root = self.xml.documentElement
152
153         #save generics variables
154         playlist_generics = self.xml.createElement('generics')
155         root.appendChild(playlist_generics)
156         self.generics_dict['pointer'] = self.index
157         for key in self.generics_dict.keys():
158         #for key in generics.keys():
159             self.xml.createAttribute(key)
160             playlist_generics.setAttribute(key, str(self.generics_dict[key]))
161
162         #save files and their attributes
163         for item in self.files:
164             #playlist_element=newdoc.createElement("file")
165             playlist_element = self.xml.createElement('element')
166             root.appendChild(playlist_element)
167             for key in item.__dict__:
168                 if not (key in self.hidden_attributes):
169                     self.xml.createAttribute(key)
170                     playlist_element.setAttribute(key, str(item.__dict__[key]))
171         self._saved = False
172
173         self.xml.writexml(output_file, indent='\n')
174         output_file.close()
175         self._saved = True