#!/usr/bin/python import os, sys, re, base64 def generate(): sections = {} exclude_sections = [] replacements = {} for keyword, prompt_text in repl_vars: replacements[keyword] = prompt(prompt_text) answer = '' while not answer.lower() in ['f', 't']: answer = prompt('Firefox or Thunderbird (f/t) ') sections['firefox'] = answer.lower() == 'f' sections['thunderbird'] = answer.lower() == 't' for keyword, prompt_text in section_vars: answer = '' while not answer.lower() in ['y', 'n']: answer = prompt('Include %s ? (y/n)' % prompt_text) sections[keyword] = answer.lower() == 'y' if sections['menuitem']: replacements['menuName'] = prompt('Menu item label') replacements['menuAccessKey'] = prompt('Menu item access key') if sections['contextmenu']: replacements['contextName'] = prompt('Contextmenu item label') replacements['contextAccessKey'] = prompt('Contextmenu item access key') extname = replacements['name'].lower() replacements['lname'] = extname replacements['llib'] = replacements['lib'].lower() #Make all dirs needed os.makedirs(extname) for d in dirs: os.makedirs(os.path.join(extname, d)) #Process library file specially libfile = open('mozlib/mozlib.js', 'r') libtext = libfile.read() libfile.close() libfileout = open(os.path.join(extname, 'chrome/content', replacements['llib']) + '.js', 'w') libfileout.write(libtext.replace('MozLib', replacements['lib'])) libfileout.flush() libfileout.close() #Proccess each file for filename, filecontent in files: is_img = filename[-3:] == 'png' print filename #Include or exclude sections of code exclude_pattern = r'\[%s].*?\[/%s]\s*\n' include_pattern = r'\[/?%s]\s*\n' for key in sections: if sections[key]: pattern = include_pattern % key else: pattern = exclude_pattern % (key, key) regex = re.compile(pattern, re.DOTALL) filecontent = re.sub(regex, '', filecontent) #Simple keyword replacements filename = filename % replacements if not is_img: filecontent = filecontent % replacements filecontent = filecontent.strip() #The whole file might be wrapped in a section, and if the section #was removed we don't want to create the file if filecontent: if is_img: filecontent = base64.b64decode(filecontent) path = extname + '/' + filename file = open(path, 'wb') file.write(filecontent) file.flush() file.close() def prompt(prompt_text): sys.stdout.write(prompt_text + ': ') return raw_input() #Directories to create dirs = ['chrome/content', 'chrome/locale/en-US', 'chrome/skin', 'defaults/preferences'] repl_vars = ( ('prettyname', 'Extension name'), ('name', 'Extension code name'), ('descr', 'Extension description'), ('lib', 'Library name'), ) section_vars = ( ('options', 'Options dialog'), ('menuitem', 'Menu item'), ('contextmenu', 'Context menu'), ) #Below here are only file templates files = ( ('install.rdf', """ %(lname)s@einaregilsson.com %(prettyname)s 0.1 Einar Egilsson %(descr)s chrome://%(lname)s/content/about.xul chrome://%(lname)s/content/options.xul chrome://%(lname)s/content/%(lname)s.png [firefox] {ec8030f7-c20a-464f-9b0e-13a3a9e97384} 2.0 2.0.0.* [/firefox] [thunderbird] {3550f703-e582-4d05-9a08-453d09bdfdc6} 1.5 1.5.0.* [/thunderbird] """), ('chrome.manifest', """ # $Id$ content %(lname)s file:chrome/content/ locale %(lname)s en-US file:chrome/locale/en-US/ skin %(lname)s classic/1.0 file:chrome/skin/ [thunderbird] overlay chrome://messenger/content/messenger.xul chrome://%(lname)s/content/overlay.xul [/thunderbird] [firefox] overlay chrome://browser/content/browser.xul chrome://%(lname)s/content/overlay.xul [/firefox] """), ('make.bat', """ :: :: Build script for extension :: :: Einar Thor Egilsson - 26.10.2006 :: :: $Id$ SET EXT=%(name)s.xpi IF NOT EXIST build md build IF EXIST build\%%EXT%% del build\%%EXT%% zip -r build\%%EXT%% *.* -x build* *.py *.bat """), ('defaults/preferences/%(lname)s.js', """ //// $Id$ pref("extensions.%(lname)s.debug", false); // See http://kb.mozillazine.org/Localize_extension_descriptions pref("extensions.%(lname)s@einaregilsson.com.description", "chrome://%(lname)s/locale/%(lname)s.properties"); """), ('chrome/content/%(lname)s.js', """ //// $Id$ var %(name)s = { id : "%(lname)s@einaregilsson.com", name : "%(name)s", initialized : false, strings : null, onLoad : function(event) { try { // initialization code %(lib)s.initialize(this); [contextmenu] [thunderbird] $('threadPaneContext') [/thunderbird] [firefox] $('contentAreaContextMenu') [/firefox] .addEventListener("popupshowing", function(e) { %(name)s.showContextMenu(e); }, false); [/contextmenu] %(lib)s.debug("Initializing..."); this.strings = document.getElementById("%(lname)s-strings"); %(lib)s.debug("Finished initialization"); this.initialized = true; } catch(e) { //Don't use %(lib)s because it's initialization might have failed. if (this.strings) { alert(this.strings.getString("initError")._(this.name) + "\\n\\n" + e); } else { alert(e); } } }, onUnload : function(event) { //Clean up here %(lib)s.debug("Finished cleanup"); }, [contextmenu] showContextMenu : function(event) { [thunderbird] $("%(lname)s-context").hidden = (GetNumSelectedMessages() > 0); [/thunderbird] [firefox] $("%(lname)s-context").hidden = gContextMenu.onImage; [/firefox] }, onContextMenuCommand: function(event) { //Do your thing alert("Context menu command"); }, [/contextmenu] [menuitem] onMenuItemCommand: function(event) { alert("Menu item command"); [options] window.open("chrome://%(lname)s/content/options.xul", "settings", "chrome,dialog,modal,centerscreen"); [/options] }, [/menuitem] }; window.addEventListener("load", function(event) { %(name)s.onLoad(event); }, false); window.addEventListener("unload", function(event) { %(name)s.onUnload(event); }, false); """), ('chrome/content/overlay.xul', """