2
# THIS FILE IS PART OF THE JOKOSHER PROJECT AND LICENSED UNDER THE GPL. SEE
3
# THE 'COPYING' FILE FOR DETAILS
7
# This module handles the saving and loading of project templates.
10
#-------------------------------------------------------------------------------
15
import xml.dom.minidom as xml
16
from Utils import LoadListFromXML, StoreListToXML
18
#=========================================================================
20
class ProjectTemplate(gobject.GObject):
22
This class saves and loads templates and template information to disk.
25
#the extension put on the end of template files
26
TEMPLATE_EXT = "template"
30
"template-update" -- This template has changed.
34
"template-update" : ( gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, () )
37
#_____________________________________________________________________
41
Creates a new instance of ProjectTemplate.
43
gobject.GObject.__init__(self)
45
#_____________________________________________________________________
47
def SaveTemplateFile(self, name, instrlist):
49
This method will write a template file to JOKOSHER_DATA_HOME/templates/.
50
instrlist is a list containing type strings corresponding to each instrument.
51
e.g. ["audiofile", "acousticguitar"].
54
name -- the name of the template file.
55
instrlist -- a list containing the instrument type strings
58
head = doc.createElement("JokosherProjectTemplate")
61
for typeString in instrlist:
62
# create instrument tags for every instrument in the list
63
instrtag = doc.createElement("Instrument")
64
instrtag.setAttribute("type", typeString)
65
head.appendChild(instrtag)
67
if not name.endswith("." + self.TEMPLATE_EXT):
68
name += "." + self.TEMPLATE_EXT
69
namePath = os.path.join(Globals.TEMPLATES_PATH, name)
72
filename = open(namePath, "w")
73
filename.write(doc.toprettyxml())
75
Globals.debug("The template %s does not exist" % namePath)
79
self.emit("template-update")
81
#_____________________________________________________________________
83
def DeleteTemplateFile(self, name):
85
This method will delete a template file.
88
name -- the name of the template file which will be deleted.
90
if not name.endswith("." + self.TEMPLATE_EXT):
91
name += "." + self.TEMPLATE_EXT
92
namePath = os.path.join(Globals.TEMPLATES_PATH, name)
96
Globals.debug("Cannot remove template %s" % namePath)
98
self.emit("template-update")
100
#_____________________________________________________________________
102
def __LoadInstrumentsFromTemplateFile(self, name):
104
This method will return a list containing a list of instruments which are in the given template filename.
107
name -- the name of the template.
110
instrlist -- a list containing a list of instruments, e.g. [["audiofile", "Audio File", "path_to_image"]].
112
if not name.endswith("." + self.TEMPLATE_EXT):
113
name += "." + self.TEMPLATE_EXT
114
namePath = os.path.join(Globals.TEMPLATES_PATH, name)
115
if os.path.exists(namePath):
116
file = open(namePath, "r")
117
doc = xml.parse(file)
119
for node in doc.getElementsByTagName("Instrument"):
120
typeString = str(node.getAttribute("type"))
121
instrlist.append(typeString)
124
Globals.debug("The template %s does not exist" % namePath)
126
#_____________________________________________________________________
128
def __GetTemplateList(self):
130
This method will return a list of template files in the template directory (JOKOSHER_DATA_HOME/templates/")
133
filenames -- a list containing the names of file in the template directory.
136
for files in os.listdir(Globals.TEMPLATES_PATH):
137
filenames = files.split(".")[0]
138
templist.append(filenames)
141
#_____________________________________________________________________
143
def LoadDictionaryOfInstrumentsFromTemplateFile(self):
145
This method will return a dictionary containing the the template file name (key) and a list
146
of the associated instrument tupels.
147
e.g. {"Rock" : [ ("electricguitar", "Electric Guitar", pixbufImage) ]}
150
cached = Globals.getCachedInstruments()
151
for filename in self.__GetTemplateList():
153
typeStringList = self.__LoadInstrumentsFromTemplateFile(filename)
154
for type in typeStringList:
155
tuple_ = [x for x in cached if x[1] == type]
157
name, type, pixbuf, path = tuple_[0]
158
instrTuples.append( (name, type, pixbuf) )
160
instrdict[filename] = instrTuples
164
#=========================================================================