Re: [jokosher-devel] create_new_instrument_type function for Extension API



Luke Tilley wrote:
I made a little function so extensions can add their own instruments.


I've changed some things. See in the code below:



------------------------------------------------------------------------

Index: Extension.py
===================================================================
--- Extension.py	(revision 717)
+++ Extension.py	(working copy)
@@ -163,6 +163,41 @@
 		   that equals instrumentID.
 		"""
 		self.mainapp.project.DeleteInstrument(instrumentID)
+
+	def create_new_instrument_type(self, defaultName, typeString, imagePath):
+		"""
+ Creates and new instrument type in + the user's .jokosher/instruments folder
+		   return 0: sucess
+		   return 1: file exists
+		"""
+		instrument_file = os.path.join(Globals.INSTR_PATHS[1], typeString+".instr")
+
+		if not os.path.exists(instrument_file):
+			print "Creating instrument"

Printing to the terminal is not allowed as of last Monday. You must use Globals.debug() to print anything. If you then want to see it on the terminal, run "./Jokosher --debug".


+			core = {"icon": "value", "type": "value"}
+			
+			instr = ConfigParser.ConfigParser()
+			
+			instr.add_section("core")
+			instr.add_section("i18n")
+			
+			shutil.copyfile(imagePath, Globals.INSTR_PATHS[1]+"/images/"+os.path.basename(imagePath))

You forgot to import shutil.

+			
+			core["icon"] = os.path.basename(imagePath)
+			core["type"] = typeString
+			
+			for key in core:
+				instr.set("core", key, core[key])
+			instr.set("i18n", "en", defaultName)

A lot of people do for loops and dicts like this. Its not a big deal here but its faster and IMO easier to read if you do:

for key, value in core.iteritems():
	instr.set("core", key, value)


+
+			file = open(instrument_file, 'w')
+			instr.write(file)
+			file.close()
+			return 0
+		else:
+			print "Instrument Type already exists!"
+			return 1

Thanks, the patch has been applied. That's #2.

Laszlo



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]