Debajo encontramos un pequeño script en Python que usa las funciones XML-RPC
proporcionadas por skyxmlrpcd para trabajar con los recursos de
OpenGroupware.org.
El script es muy fácil de usar. Despues de ponerlo en marcha, un pequeño menú muestra las opciones disponibles. Escoger una y seguir las instrucciones en pantalla. Échele un vistazo al ejemplo que sigue para hacerse a la idea.
Sample Runbjoern@grobi:xmlrpc # ./ogo_resource.py XML-RPC Daemon URL: http://localhost:20000/RPC2 Username: root Password: -- Options ----------------------------------------------- a - add a resource d - delete resource s - show resources q - quit ---------------------------------------------------------- Make your choice [S/a/d/q]:s ========================================================== Name | Category | EMail | Subject ---------------------------------------------------------- Resource | | Email | Subject Category | Cat | Email | Subject Inserted | Category | Email | Subject New | Added | Email | Test ========================================================== -- Options ----------------------------------------------- a - add a resource d - delete resource s - show resources q - quit ---------------------------------------------------------- Make your choice [S/a/d/q]:a ========================================================== --Adding new resource------------------------------------- Name: Foo Email: Bar Category: Baz Email Subject: Sub Create resource? [Y/n]:y ==> created resource with ID skyrix://grobi.in.skyrix.com/grobi/83780 -- Options ----------------------------------------------- a - add a resource d - delete resource s - show resources q - quit ---------------------------------------------------------- Make your choice [S/a/d/q]:d ========================================================== --Deleting resource------------------------------------- Name: New ==> deleted resource with ID skyrix://grobi.in.skyrix.com/grobi/83760 ========================================================== -- Options ----------------------------------------------- a - add a resource d - delete resource s - show resources q - quit ---------------------------------------------------------- Make your choice [S/a/d/q]:q bjoern@grobi:xmlrpc #
Para ejecutar este script necesita el 'parcheado' xmlrpclib.py que se encuentra
disponible aquí. Ver las instrucciones generales
Python para más detalles.
#!/usr/bin/env python
# $Id$
# resExample.py - how to work with OGo resources via XML-RPC
# this program needs the patchted xmlrpclib.py available at
# http://developer.skyrix.com/02_skyrix/xmlrpc/xmlrpclib.py
import getpass, string, sys, xmlrpclib
from types import *
class ResourceTool:
def __init__(self):
""" initialization """
self.__server = None
self.__newRes = {}
def getUserInput(self, _description, _pwdInput=0):
""" get information from the user """
if not _pwdInput:
# get normal input
response = raw_input("%-20s" % _description)
else:
# get password input
response = getpass.getpass("%-20s" % _description)
return response
def getDaemonInfo(self):
""" get xml-rpc daemon information, setup daemon connection """
self.__url = self.getUserInput('XML-RPC Daemon URL:',0)
self.__login = self.getUserInput('Username:',0)
self.__pwd = self.getUserInput('Password:',1)
try:
# try to init xml-rpc server proxy with the given information
# (you need the patched xmlrpclib.py for this)
self.__server = xmlrpclib.ServerProxy(self.__url,
login=self.__login,
password=self.__pwd)
except TypeError,e:
# seems you are using a xmlrpclib.py without basic authentication
# support - see the error message how to get the right one
sys.stderr.write("ERROR: You are probably using the wrong version"
"of xmlrpclib\nGet the right version at:\n")
sys.stderr.write("http://developer.skyrix.com/02_skyrix/xmlrpc/"
"xmlrpclib.py\n")
return 2
except IOError,e:
# these errors are caused by specifying a wrong XML-RPC protocol
# (XML-RPC only supports HTTP)
sys.stderr.write("ERROR: %s\n" % e)
return 3
return 0
def collectResourceInfo(self):
""" collect all information we need """
print "=========================================================="
print "--Adding new resource-------------------------------------"
# request user input
self.__newRes['name'] = self.getUserInput('Name:',0)
self.__newRes['email'] = self.getUserInput('Email:',0)
self.__newRes['category'] = self.getUserInput('Category:',0)
self.__newRes['emailSubject'] = self.getUserInput('Email Subject:',0)
def addResourceToSystem(self):
""" add the resource to SKYRiX via XML-RPC """
try:
# add resource to SKYRiX
result = self.__server.resource.insert(self.__newRes)
except:
sys.stderr.write("An XML-RPC error occured\n")
return 2
# resource.insert returns the inserted resource as dictionary if it was
# successful, otherwise a boolean False is returned
if type(result) is StringType:
print "==> created resource with ID %s" % result
return 0
else:
sys.stderr.write("Failed to create resource\n")
return 1
def showResourceInfo(self):
""" show infos about all resources """
result = None
try:
# add resource to SKYRiX
result = self.__server.resource.fetch("name like '*'")
except:
sys.stderr.write("An XML-RPC error occured\n")
return 2
print "=========================================================="
# print resource information in a table
if result != []:
print "Name | Category | EMail | Subject"
print "----------------------------------------------------------"
for r in result:
print "%-12s | %-10s | %-15s | %-10s" % (r['name'],
r['category'],
r['email'],
r['emailSubject'])
else:
print "No resources found"
print "=========================================================="
def deleteResource(self):
""" delete resource with given name """
print "=========================================================="
print "--Deleting resource-------------------------------------"
# get name of resource to delete
resName = self.getUserInput('Name:',0)
result = None
try:
# try to get the resource by its name
result = self.__server.resource.getByName(resName)
except:
sys.stderr.write("An XML-RPC error occured\n")
return 2
# check if a resource was found
if result != []:
delete = None
try:
# try to delete this resource by its id
delete = self.__server.resource.delete(result[0]['id'])
except:
sys.stderr.write("An XML-RPC error occured\n")
return 2
# check if deletion was successful
if delete != None:
print "==> deleted resource with ID %s" % result[0]['id']
else:
print "==> deleting resource failed"
print "=========================================================="
def printOptions(self):
""" print available options """
print "-- Options -----------------------------------------------"
print "a - add a resource"
print "d - delete resource"
print "s - show resources"
print "q - quit"
print "----------------------------------------------------------"
pass
def run(self):
""" run """
result = self.getDaemonInfo()
if result != 0: return result
option = 'n'
while option != 'q':
if option == 's':
self.showResourceInfo()
elif option == 'd':
self.deleteResource()
elif option == 'a':
self.collectResourceInfo()
ok = self.getUserInput("Create resource? [Y/n]:",0)
if not ok:
ok = "y"
if string.lower(ok) == "y":
result = self.addResourceToSystem()
if result != 0: return result
self.printOptions()
option = self.getUserInput("Make your choice [S/a/d/q]:",0)
if not option: option = "s"
return 0
if __name__ == "__main__":
tool = ResourceTool()
try:
returnCode = tool.run()
except KeyboardInterrupt:
sys.stderr.write("Program cancelled by user\n")
returnCode = 2
sys.exit(returnCode)