#!/usr/bin/env python
#@+leo-ver=4
#@+node:@file leopm_server.py
#@@first #!/usr/bin/env python

"""A server implementing a plug-in manager for Leo

The server is based on an XML server architecture. Methods exist
to query the plug-ins on the server, find version numbers, help information and to
download the actual source. In the future it might be possible to upload
plug-ins as well."""

import SimpleXMLRPCServer

__version__ = "0.1"

PORT = 8001

#@+others
#@+node:class LeoPM
class LeoPM:
	#@	<< class LeoPM declarations >>
	#@+node:<< class LeoPM declarations >>
	"""The Leo Plugin manager XMLRPC server"""
	
	#@-node:<< class LeoPM declarations >>
	#@nl
	#@	@+others
	#@+node:__init__
	def __init__(self, plugin_dir="."):
	    """Start the server with plugins located in the specified directory"""
	#@-node:__init__
	#@+node:getPluginNames
	    #self._locatePlugins()
	    
	def getPluginNames(self):
	    """Return a list of the plug-ins by name"""
	    return ["one", "two", "three"]
	#@-node:getPluginNames
	#@+node:getPluginDescription
	def getPluginDescription(self, name):
	    """Return the description of the plugin"""
	    return "%s - v%s\n\n%s" % (name, len(name), "Some important details about it")
	#@-node:getPluginDescription
	#@+node:getPlugin
	def getPlugin(self, name):
	    """Get the source files for a plugin
	    
	    Files are returned as a list of tuples (filename, text)
	    
	    """
	    return [("%s1.py" % name, 'this is #1\n__version__ = "%d"' % len(name)),
	            ("%s2.py" % name, 'this is #2\n__version__ = "%d"' % len(name))]
	#@-node:getPlugin
	#@+node:getPluginVersion
	def getPluginVersion(self, name):
	    """Return the plugin version"""
	    return len(name)
	#@-node:getPluginVersion
	#@+node:getPluginFilenames
	def getPluginFilenames(self, name):
	    """Return a list of the files needed by a plugin"""
	    return ("%s1.py" % name, "%s2.py" % name)
	#@-node:getPluginFilenames
	#@-others
#@-node:class LeoPM
#@-others
        
if __name__ == "__main__":
    # Create the server
    leoPluginManager = LeoPM()
    server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost", PORT))
    server.register_instance(leoPluginManager)
    # Go into the main listener loop
    print "LeoPM (v%s) Started\nListening on port %s" % (__version__, PORT)
    server.serve_forever()
#@nonl
#@-node:@file leopm_server.py
#@-leo
