baseControl.py :  » Web-Services » Py-Xmlrpc » py-xmlrpc-0.8.8.3 » examples » crj » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Services » Py Xmlrpc 
Py Xmlrpc » py xmlrpc 0.8.8.3 » examples » crj » baseControl.py
#!/usr/bin/python -O
#
# baseControl.py
#  Description:  Contains the most basic implementation of a control class
#      for my standard server architecture.  To use, make a class
#      the using baseControl as a base class.  Then add your
#      functions there.  Be sure to use a '_' as the first
#      character in the names of functions you do NOT want to be
#      externally callable.  The list of externally callable
#      functions is generated automagically.
#      A small example is given.  
#      Be carefule of one thing, None is not in the xmlrpc spec,
#      so xmlrpc does not support the None type. 
#
#  Requires:  xmlrpc
#
#
#       Copyright:      LGPL
#       Created:        March 20, 2001
#       Author:         Chris Jensen - chris@sourcelight.com
#
#  Last Update:  04/10/2001
#
#####################################################################################

import string
import sys

# baseControl
#
# baseControl(usage, logobj=sys.stderr)
#  Constructor.  usage is the usage string for your subclass. logobj is
#  an object that has a write method (ex. sys.stderr (default), file obj, etc.)
#
# dispatch(*args)
#  This only should be called by the xmlrpc stuff.  It is called when a command
#  comes into the xmlrpc server.
#
# link(cmdlist, cmduse)
#  This only needs to be called be the baseServer object on initialization.
#
# Nothing else is really public, except for the functions in a sub-class, which
# are called through xmlrpc.
#
class baseControl:
  def __init__(self, usage, logobj=None):
    self.closed  = 0
    self.usage  = usage        # command usage
    self.logobj  = (logobj or sys.stderr)  # log to logobj if
                # given, else stderr
    self.__loadmethods__()

  def __del__(self):
    if not self.closed:
      self.close()

  def __getattr__(self, name):
    if name == 'dispatch':
      return self._dispatch
    if name == 'link':
      return self._link
    raise AttributeError, "'%s' instance has no attribute '%s'" % (
              self.__class__, name)

  def _close(self):
    self.closed  = 1
    return 1

  def _dispatch(self, *args):
    (cmd, argv)  = (args[3], args[4])
    if self.closed:
      raise 'Access Error', 'Control object has been closed.'
    return apply(self.fnmap.get(cmd), tuple(argv))

  def _link(self, cmdlist, cmduse):
    cmduse  = cmduse + self.usage
    for i in self.fnmap.keys():
      cmdlist[i]  = self.dispatch
    return (cmdlist, cmduse)

  def _log(self, msg):
    self.logobj.write('%s\n' % string.strip(msg))

  def __loadmethods__(self):
    self.fnmap  = {}
    for i in dir(self.__class__) + dir(self.__class__.__bases__[0]):
      if ((i[0] == '_')
      or  (not callable(getattr(self, i)))):
        continue
      self.fnmap[i]  = getattr(self, i)


exusage  = '''
  echo  arg
    returns arg as a string
  echo2  arg1 arg2
    returns (arg1, arg2) as a string
'''

# example Control class
#
class exControl(baseControl):
  def __init__(self, logobj=None):
    baseControl.__init__(self, exusage, logobj)

  def echo(self, arg):
    return str(arg)

  def echo2(self, arg1, arg2):
    return str((arg1, arg2))
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.