6 Playlist class for Hooke.
8 Copyright 2010 by Dr. Rolf Schmidt (Concordia University, Canada)
10 This program is released under the GNU General Public License version 2.
14 import xml.dom.minidom
19 class Playlist(object):
21 def __init__(self, filename=None):
26 self.generics_dict = {}
27 self.hidden_attributes = ['curve', 'data', 'driver', 'fits', 'name', 'plot', 'plots']
31 self.plot_panel = None
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)
43 self.count = len(self.files)
45 def delete_file(self, name):
46 for index, item in enumerate(self.files):
50 self.count = len(self.files)
51 if self.index > self.count - 1:
54 def filter_curves(self, curves_to_keep=[]):
56 for curve_index in curves_to_keep:
57 playlist.files.append(self.files[curve_index])
58 playlist.count = len(playlist.files)
62 def get_active_file(self):
63 return self.files[self.index]
65 #def get_active_plot(self):
66 ##TODO: is this the only active (or default?) plot?
67 #return self.files[self.index].plots[0]
69 def load(self, filename):
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
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)
105 generics_list = self.xml.getElementsByTagName('generics')
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:
119 if self.index > self.count - 1:
125 self.index = self.count - 1
133 def save(self, filename):
135 Saves a playlist from a list of files.
136 A playlist is an XML document with the following syntax:
138 <element path="/my/file/path/"/ attribute="attribute">
143 output_file = file(filename, 'w')
145 self.AppendToOutput('Cannot save playlist. Wrong path or filename')
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
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]))
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]))
173 self.xml.writexml(output_file, indent='\n')