home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / spyceConfig.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  13.3 KB  |  371 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: spyceConfig.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. import sys, os, string, ConfigParser
  10. import spyceException
  11.  
  12. ##################################################
  13. # Constants
  14. #
  15.  
  16. SPYCE_CONCURRENCY_SINGLE = 0
  17. SPYCE_CONCURRENCY_FORK = 1
  18. SPYCE_CONCURRENCY_THREAD = 2
  19.  
  20. SPYCE_CONFIG_LOCATIONS = [
  21.   '/etc',
  22. ]
  23. SPYCE_CONFIG_FILENAME = 'spyce.conf'
  24. SPYCE_HOME = None
  25.  
  26. ##################################################
  27. # determine HOME (installation) directory
  28. #
  29.  
  30. if SPYCE_HOME == None:
  31.   try:
  32.     SPYCE_HOME = os.path.abspath(os.path.dirname(sys.modules['spyceConfig'].__file__))
  33.   except:
  34.     for p in sys.path:
  35.       path = os.path.join(p, 'spyceConfig.py')
  36.       if os.path.exists(path):
  37.         SPYCE_HOME = os.path.abspath(p)
  38.         break
  39.   if not SPYCE_HOME:
  40.     raise 'unable to determine Spyce home directory'
  41.  
  42. ##################################################
  43. # Server configuration object
  44. #
  45.  
  46. class spyceConfig:
  47.   def __init__(self,
  48.     file=None,
  49.     overide_path=None,
  50.     overide_import=None,
  51.     overide_error=None,
  52.     overide_pageerror=None,
  53.     overide_globals=None,
  54.     overide_debug=None,
  55.     default_concurrency=None,  # no concurrency
  56.     overide_concurrency=None,
  57.     overide_cache=None,
  58.     default_www_root='.',  # serve from current directory
  59.     overide_www_root=None,
  60.     default_www_port=80,  # default HTTP
  61.     overide_www_port=None,
  62.     default_www_handler={
  63.       'spy': 'spyce',
  64.       None: 'dump',
  65.     },
  66.     default_www_mime = [os.path.join(SPYCE_HOME,'spyce.mime')],
  67.   ):
  68.     self.file = file
  69.     self._procesed = 0
  70.     # config parameters
  71.     self.spyce_config = None # spyce configuration dictionary
  72.     self.spyce_path = None  # spyce module search path
  73.     self.spyce_import = None # python modules loaded at startup
  74.     self.spyce_error = None # spyce engine-level error handler
  75.     self.spyce_pageerror = None # spyce page-level error handler
  76.     self.spyce_globals = {} # spyce engine globals dictionary
  77.     self.spyce_debug = None # spyce engine debug flag
  78.     self.spyce_concurrency = None # concurrency model
  79.     self.spyce_www_root = None # root directory for spyce web server
  80.     self.spyce_www_port = None # port used by spyce web server
  81.     self.spyce_www_mime = None # files with Apache-like mime type listings
  82.     self.spyce_www_handler = None # assign handlers by extension
  83.     # store overides and defaults
  84.     self.overide_path = overide_path
  85.     self.overide_import = overide_import
  86.     self.overide_error = overide_error
  87.     self.overide_pageerror = overide_pageerror
  88.     self.overide_globals = overide_globals
  89.     self.overide_debug = overide_debug
  90.     self.overide_concurrency = overide_concurrency
  91.     self.default_concurrency = default_concurrency
  92.     self.overide_cache = overide_cache
  93.     self.default_www_root = default_www_root
  94.     self.overide_www_root = overide_www_root
  95.     self.default_www_port = default_www_port
  96.     self.overide_www_port = overide_www_port
  97.     self.default_www_handler = default_www_handler
  98.     self.default_www_mime = default_www_mime
  99.   def process(self):
  100.     # process (order matters here!)
  101.     self.processConfigFile()
  102.     self.processSpycePath()
  103.     self.processSpyceDebug()
  104.     self.processSpyceGlobals()
  105.     self.processSpyceImport()
  106.     self.processSpyceError()
  107.     self.processSpyceConcurrency()
  108.     self.processSpyceCache()
  109.     self.processSpyceWWW()
  110.   # accessors
  111.   def getSpyceHome(self):
  112.     return SPYCE_HOME
  113.   def getSpycePath(self):
  114.     return self.spyce_path
  115.   def getSpyceImport(self):
  116.     return self.spyce_import
  117.   def getSpyceError(self):
  118.     return self.spyce_error
  119.   def getSpycePageError(self):
  120.     return self.spyce_pageerror
  121.   def getSpyceGlobals(self):
  122.     return self.spyce_globals
  123.   def getSpyceDebug(self):
  124.     return self.spyce_debug
  125.   def getSpyceConcurrency(self):
  126.     return self.spyce_concurrency
  127.   def getSpyceCache(self):
  128.     return self.spyce_cache
  129.   def getSpyceWWWRoot(self):
  130.     return self.spyce_www_root
  131.   def getSpyceWWWPort(self):
  132.     return self.spyce_www_port
  133.   def getSpyceWWWHandler(self):
  134.     return self.spyce_www_handler
  135.   def getSpyceWWWMime(self):
  136.     return self.spyce_www_mime
  137.   def __repr__(self):
  138.     return \
  139. '''path: %(path)s
  140. import: %(import)s
  141. error: %(error)s
  142. globals: %(globals)s
  143. debug: %(debug)s
  144. concurrency: %(concurrency)s
  145. cache: %(cache)s
  146. www root: %(wwwroot)s
  147. www port: %(wwwport)s
  148. www mime: %(wwwmime)s
  149. www handler: %(wwwhandler)s
  150. ''' % {
  151.   'path':        self.spyce_path,
  152.   'import':      self.spyce_import,
  153.   'error':       self.spyce_error,
  154.   'pageerror':   self.spyce_pageerror,
  155.   'globals':     self.spyce_globals,
  156.   'debug':       self.spyce_debug,
  157.   'concurrency': self.spyce_concurrency,
  158.   'cache':       self.spyce_cache,
  159.   'wwwroot':     self.spyce_www_root,
  160.   'wwwport':     self.spyce_www_port,
  161.   'wwwmime':     self.spyce_www_mime,
  162.   'wwwhandler':  self.spyce_www_handler,
  163. }
  164.  
  165.   # configuration processing
  166.   def processConfigFile(self):
  167.     # config file
  168.     self.spyce_config = {}
  169.     if not self.file:
  170.       self.file = self.findConfigFile()
  171.     if self.file:
  172.       if not os.path.exists(self.file):
  173.         raise spyceException.spyceNotFound(self.file)
  174.       if not os.access(self.file, os.R_OK):
  175.         raise spyceException.spyceForbidden(self.file)
  176.       self.spyce_config = self.parseConfigFile()
  177.   def processSpycePath(self, mod_path=None):
  178.     def processSpycePathString(str, self=self):
  179.       dpath = filter(None, map(string.strip, string.split(str, os.pathsep)))
  180.       for i in range(len(dpath)):
  181.         dpath[i] = os.path.abspath(dpath[i])
  182.       self.spyce_path = self.spyce_path + dpath
  183.       sys.path = sys.path + dpath
  184.     def filterPath(path):
  185.       path2 = []
  186.       for p in path:
  187.         if p not in path2:
  188.           path2.append(p)
  189.       return path2
  190.     self.spyce_path = [
  191.       os.path.join(SPYCE_HOME, 'modules'),
  192.       os.path.join(SPYCE_HOME, 'tags'),
  193.     ]
  194.     if mod_path:
  195.       processSpycePathString(mod_path)
  196.     if self.spyce_config.has_key('path') and not self.overide_path:
  197.       processSpycePathString(self.spyce_config['path'])
  198.     if self.overide_path:
  199.       processSpycePathString(overide_path)
  200.     else:
  201.       if (self.spyce_config and not self.spyce_config.has_key('path')) and os.environ.has_key('SPYCE_PATH'):
  202.         processSpycePathString(os.environ['SPYCE_PATH'])
  203.     self.spyce_path = filterPath(self.spyce_path)
  204.     sys.path = filterPath(sys.path)
  205.   def processSpyceImport(self):
  206.     self.spyce_import = []
  207.     if self.spyce_config.has_key('import'):
  208.       new_import = filter(None, map(string.strip, string.split(self.spyce_config['import'], ',')))
  209.       self.spyce_import = self.spyce_import + new_import
  210.     if self.overide_import:
  211.       new_import = filter(None, map(string.strip, string.split(self.overide_import, ',')))
  212.       self.spyce_import = self.spyce_import + new_import
  213.     for i in self.spyce_import:
  214.       exec('import %s'%i)
  215.   def processSpyceError(self):
  216.     # server-level
  217.     self.spyce_error = 'error:serverHandler'
  218.     if self.spyce_config.has_key('error'):
  219.       self.spyce_error = self.spyce_config['error']
  220.     if self.overide_error:
  221.       self.spyce_error = self.overide_error
  222.     self.spyce_error = string.split(self.spyce_error, ':')
  223.     if len(self.spyce_error)==1:
  224.       raise 'invalid error handler specification (file:function)'
  225.     # page-level
  226.     self.spyce_pageerror = 'string:error:defaultErrorTemplate'
  227.     if self.spyce_config.has_key('pageerror'):
  228.       self.spyce_pageerror = self.spyce_config['pageerror']
  229.     if self.overide_pageerror:
  230.       self.spyce_pageerror = self.overide_pageerror
  231.     self.spyce_pageerror = string.split(self.spyce_pageerror, ':')
  232.     if (len(self.spyce_pageerror)<2 or self.spyce_pageerror[0] not in ('string', 'file')):
  233.       raise 'invalid pageerror handler specification ("string":module:variable, or ("file":file)'
  234.   def processSpyceGlobals(self):
  235.     self.spyce_globals.clear ()
  236.     if self.spyce_config.has_key('globals'):
  237.       for k in self.spyce_config['globals'].keys():
  238.         self.spyce_globals[k] = self.spyce_config['globals'][k]
  239.     if self.overide_globals:
  240.       for k in self.overide_globals.keys():
  241.         self.spyce_globals[k] = self.overide_globals[k]
  242.     for k in self.spyce_globals.keys():
  243.       self.spyce_globals[k] = eval(self.spyce_globals[k])
  244.   def processSpyceDebug(self):
  245.     self.spyce_debug = 0
  246.     if self.spyce_config.has_key('debug'):
  247.       self.spyce_debug = string.strip(string.lower(self.spyce_config['debug'])) not in ('0', 'false', 'no')
  248.     if self.overide_debug:
  249.       self.spyce_debug = 1
  250.   def processSpyceConcurrency(self):
  251.     self.spyce_concurrency = SPYCE_CONCURRENCY_SINGLE
  252.     if self.default_concurrency!=None:
  253.       self.spyce_concurrency = self.default_concurrency
  254.     if self.spyce_config.has_key('concurrency'):
  255.       self.spyce_concurrency = string.lower(self.spyce_config['concurrency'])
  256.       if self.spyce_concurrency in ('thread', 'threading'):
  257.         self.spyce_concurrency = SPYCE_CONCURRENCY_THREAD
  258.       elif self.spyce_concurrency in ('fork', 'forking'):
  259.         self.spyce_concurrency = SPYCE_CONCURRENCY_FORK
  260.       else: 
  261.         self.spyce_concurrency = SPYCE_CONCURRENCY_SINGLE
  262.     if self.overide_concurrency!=None:
  263.       self.spyce_concurrency = self.overide_concurrency
  264.   def processSpyceCache(self):
  265.     cache = 'memory'
  266.     if self.spyce_config.has_key('cache'):
  267.       cache = self.spyce_config['cache']
  268.     cache = string.split(cache, ':')
  269.     self.spyce_cache = string.strip(string.lower(cache[0])), string.join(cache[1:], ':')
  270.   def processSpyceWWW(self):
  271.     # root
  272.     self.spyce_www_root = self.default_www_root
  273.     if self.spyce_config.has_key('www_root'):
  274.       self.spyce_www_root = self.spyce_config['www_root']
  275.     if self.overide_www_root!=None:
  276.       self.spyce_www_root = self.overide_www_root
  277.     # port
  278.     self.spyce_www_port = self.default_www_port
  279.     if self.spyce_config.has_key('www_port'):
  280.       self.spyce_www_port = int(self.spyce_config['www_port'])
  281.     if self.overide_www_port!=None:
  282.       self.spyce_www_port = int(self.overide_www_port)
  283.     # mime
  284.     self.spyce_www_mime = self.default_www_mime
  285.     if self.spyce_config.has_key('www_mime'):
  286.       mime = self.spyce_config['www_mime']
  287.       mime = map(string.strip, string.split(mime, ','))
  288.       self.spyce_www_mime = self.spyce_www_mime + mime
  289.     # handler
  290.     self.spyce_www_handler = self.default_www_handler
  291.     if self.spyce_config.has_key('www_handler'):
  292.       handler = self.spyce_config['www_handler']
  293.       for k in handler.keys():
  294.         self.spyce_www_handler[k] = handler[k]
  295.  
  296.   # helpers
  297.   def findConfigFile(self):
  298.     locations = [SPYCE_HOME] + SPYCE_CONFIG_LOCATIONS
  299.     for l in locations:
  300.       p = os.path.join(l, SPYCE_CONFIG_FILENAME)
  301.       if os.path.exists(p):
  302.         return p
  303.   def parseConfigFile(self):
  304.     # initial defaults
  305.     path = None
  306.     load = None
  307.     error = None
  308.     pageerror = None
  309.     globals = None
  310.     debug = None
  311.     concurrency = None
  312.     cache = None
  313.     www_root = None
  314.     www_port = None
  315.     www_mime = None
  316.     www_handler = {}
  317.     cfg = ConfigParser.ConfigParser()
  318.     # parse
  319.     cfg.read(self.file)
  320.     if cfg.has_section('spyce'):
  321.       if 'path' in cfg.options('spyce'):
  322.         path = cfg.get('spyce', 'path')
  323.       if 'import' in cfg.options('spyce'):
  324.         load = cfg.get('spyce', 'import')
  325.       if 'error' in cfg.options('spyce'):
  326.         error = cfg.get('spyce', 'error')
  327.       if 'pageerror' in cfg.options('spyce'):
  328.         pageerror = cfg.get('spyce', 'pageerror')
  329.       if 'debug' in cfg.options('spyce'):
  330.         debug = cfg.get('spyce', 'debug')
  331.       if 'concurrency' in cfg.options('spyce'):
  332.         concurrency = cfg.get('spyce', 'concurrency')
  333.       if 'cache' in cfg.options('spyce'):
  334.         cache = cfg.get('spyce', 'cache')
  335.     if cfg.has_section('globals'):
  336.       globals = {}
  337.       for o in cfg.options('globals'):
  338.         if o=='__name__': continue
  339.         globals[o] = cfg.get('globals', o)
  340.     if cfg.has_section('www'):
  341.       for o in cfg.options('www'):
  342.         if o=='__name__': continue
  343.         if o=='root':
  344.           www_root = cfg.get('www', o)
  345.           continue
  346.         if o=='port':
  347.           www_port = cfg.get('www', o)
  348.           continue
  349.         if o=='mime': 
  350.           www_mime = cfg.get('www', o)
  351.           continue
  352.         if o[:len('ext_')]=='ext_':
  353.           ext = o[len('ext_'):]
  354.           if not ext: ext = None
  355.           www_handler[ext] = cfg.get('www', o)
  356.     # results
  357.     config = {}
  358.     if path!=None: config['path'] = path
  359.     if load!=None: config['import'] = load
  360.     if error!=None: config['error'] = error
  361.     if pageerror!=None: config['pageerror'] = pageerror
  362.     if globals!=None: config['globals'] = globals
  363.     if debug!=None: config['debug'] = debug
  364.     if concurrency!=None: config['concurrency'] = concurrency
  365.     if cache!=None: config['cache'] = cache
  366.     if www_root!=None: config['www_root'] = www_root
  367.     if www_port!=None: config['www_port'] = www_port
  368.     if www_mime!=None: config['www_mime'] = www_mime
  369.     if www_handler!={}: config['www_handler'] = www_handler
  370.     return config
  371.