from WikiPlugin import * from AppData import * from WikiLoaderXML import * from WikiLoaderHtml import * def getName(): # # This is the name that appears on the 'plugin' menu. # It should be unique # return "Lesson 05 - Import/Export" def getVendorName(): # # The name of the person who developed the plugin # return "Dogmelon" def getVendorVersionString(): # # A version string for this plugin # return "1.0" def getDescription(): # # A brief, helpful description of the plugin # return "Lesson 05 - Importing and Exporting Books" def preExecute(): # # Do anything you need to do before running the plugin. # This could include popping up a dialog to collect parameters. # returns a list of parameters, which will be passed to the execute() method # STAGE_NAME = "preExecute" return [] def lesson05_exportBookToXML( outputFile, bookName, collection ): outputFilename = "C:/lesson05.xml" loader = WikiLoaderXML( collection ) outputFile.write( "Exporting book '%s' to XML..."%(bookName) ) loader.save( outputFilename, bookName) outputFile.write( "done\n" ) def lesson05_exportBookToHTML( outputFile, bookName ): outputDirectory = "C:/lesson05/" # this directory must already exist bogusFilename = "blah.html" loader = WikiLoaderHtml() outputFile.write( "Exporting book '%s' to Html..."%(bookName) ) loader.save( outputDirectory + bogusFilename, bookName) outputFile.write( "done\n" ) def lesson05_importBookFromXML( outputFile, book, collection ): bookName = book.getDisplayName() newName = "Copy of " + bookName book.setDisplayName( newName ) outputFile.write( "Renamed book '%s' to '%s' to avoid name clash\nwhen we import the book.\n"%(bookName, newName) ) filename = "C:/lesson05.xml" loader = WikiLoaderXML( collection ) outputFile.write( "Loading XML book '%s'..."%(filename) ) loader.load( filename ) outputFile.write( "done\n" ) def execute( parameterList ): # # Here is where you actually do the plugin tasks. # # If anything goes wrong, you can throw a WikiPluginException # as follows: # STAGE_NAME = "execute" bThrowException = False outputFile = open( "C:/lesson05.txt", "w" ) outputFile.write( "Lesson 05\n\n" ) # don't run the plugin if we don't have any books if( theAppData.library.getNumBooks() == 0 ): return collectionName, bookId = theAppData.library.getBookSignature( 0 ) collectionIndex = theAppData.library.findDataSourceIndex( collectionName ) collection = theAppData.library.getDataSource( collectionIndex ) book = theAppData.library.getBook(0) bookName = book.getDisplayName() outputFile.write( "Book %d-\n"%0 ) outputFile.write( " collectionName: '%s'\n"%(collectionName) ) outputFile.write( " bookId: '%s'\n"%(bookId) ) outputFile.write( " bookName: '%s'\n\n"%(bookName) ) lesson05_exportBookToXML( outputFile, bookName, collection ) lesson05_exportBookToHTML( outputFile, bookName ) lesson05_importBookFromXML( outputFile, book, collection ) outputFile.write( "\nFinished.\n" ) outputFile.close() theAppData.appFrame.onRefresh() if bThrowException: raise WikiPluginException( STAGE_NAME, "Some error string describing the problem" ) def postExecute(): # # Do anything you need to clean up after running the plugin. # STAGE_NAME = "postExecute" pass