home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 May / maximum-cd-2009-05.iso / DiscContents / XBMC_for_Windows-8.10.exe / system / python / spyce / spyceConfig.py < prev    next >
Encoding:
Python Source  |  2008-11-03  |  13.3 KB  |  370 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 5659 2006-04-27 16:15:15Z jwnmulder $
  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 = None # 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.     # process (order matters here!)
  100.     self.processConfigFile()
  101.     self.processSpycePath()
  102.     self.processSpyceDebug()
  103.     self.processSpyceImport()
  104.     self.processSpyceError()
  105.     self.processSpyceGlobals()
  106.     self.processSpyceConcurrency()
  107.     self.processSpyceCache()
  108.     self.processSpyceWWW()
  109.   # accessors
  110.   def getSpyceHome(self):
  111.     return SPYCE_HOME
  112.   def getSpycePath(self):
  113.     return self.spyce_path
  114.   def getSpyceImport(self):
  115.     return self.spyce_import
  116.   def getSpyceError(self):
  117.     return self.spyce_error
  118.   def getSpycePageError(self):
  119.     return self.spyce_pageerror
  120.   def getSpyceGlobals(self):
  121.     return self.spyce_globals
  122.   def getSpyceDebug(self):
  123.     return self.spyce_debug
  124.   def getSpyceConcurrency(self):
  125.     return self.spyce_concurrency
  126.   def getSpyceCache(self):
  127.     return self.spyce_cache
  128.   def getSpyceWWWRoot(self):
  129.     return self.spyce_www_root
  130.   def getSpyceWWWPort(self):
  131.     return self.spyce_www_port
  132.   def getSpyceWWWHandler(self):
  133.     return self.spyce_www_handler
  134.   def getSpyceWWWMime(self):
  135.     return self.spyce_www_mime
  136.   def __repr__(self):
  137.     return \
  138. '''path: %(path)s
  139. import: %(import)s
  140. error: %(error)s
  141. globals: %(globals)s
  142. debug: %(debug)s
  143. concurrency: %(concurrency)s
  144. cache: %(cache)s
  145. www root: %(wwwroot)s
  146. www port: %(wwwport)s
  147. www mime: %(wwwmime)s
  148. www handler: %(wwwhandler)s
  149. ''' % {
  150.   'path':        self.spyce_path,
  151.   'import':      self.spyce_import,
  152.   'error':       self.spyce_error,
  153.   'pageerror':   self.spyce_pageerror,
  154.   'globals':     self.spyce_globals,
  155.   'debug':       self.spyce_debug,
  156.   'concurrency': self.spyce_concurrency,
  157.   'cache':       self.spyce_cache,
  158.   'wwwroot':     self.spyce_www_root,
  159.   'wwwport':     self.spyce_www_port,
  160.   'wwwmime':     self.spyce_www_mime,
  161.   'wwwhandler':  self.spyce_www_handler,
  162. }
  163.  
  164.   # configuration processing
  165.   def processConfigFile(self):
  166.     # config file
  167.     self.spyce_config = {}
  168.     if not self.file:
  169.       self.file = self.findConfigFile()
  170.     if self.file:
  171.       if not os.path.exists(self.file):
  172.         raise spyceException.spyceNotFound(self.file)
  173.       if not os.access(self.file, os.R_OK):
  174.         raise spyceException.spyceForbidden(self.file)
  175.       self.spyce_config = self.parseConfigFile()
  176.   def processSpycePath(self, mod_path=None):
  177.     def processSpycePathString(str, self=self):
  178.       dpath = filter(None, map(string.strip, string.split(str, os.pathsep)))
  179.       for i in range(len(dpath)):
  180.         dpath[i] = os.path.abspath(dpath[i])
  181.       self.spyce_path = self.spyce_path + dpath
  182.       sys.path = sys.path + dpath
  183.     def filterPath(path):
  184.       path2 = []
  185.       for p in path:
  186.         if p not in path2:
  187.           path2.append(p)
  188.       return path2
  189.     self.spyce_path = [
  190.       os.path.join(SPYCE_HOME, 'modules'),
  191.       os.path.join(SPYCE_HOME, 'tags'),
  192.     ]
  193.     if mod_path:
  194.       processSpycePathString(mod_path)
  195.     if self.spyce_config.has_key('path') and not self.overide_path:
  196.       processSpycePathString(self.spyce_config['path'])
  197.     if self.overide_path:
  198.       processSpycePathString(overide_path)
  199.     else:
  200.       if (self.spyce_config and not self.spyce_config.has_key('path')) and os.environ.has_key('SPYCE_PATH'):
  201.         processSpycePathString(os.environ['SPYCE_PATH'])
  202.     self.spyce_path = filterPath(self.spyce_path)
  203.     sys.path = filterPath(sys.path)
  204.   def processSpyceImport(self):
  205.     self.spyce_import = []
  206.     if self.spyce_config.has_key('import'):
  207.       new_import = filter(None, map(string.strip, string.split(self.spyce_config['import'], ',')))
  208.       self.spyce_import = self.spyce_import + new_import
  209.     if self.overide_import:
  210.       new_import = filter(None, map(string.strip, string.split(self.overide_import, ',')))
  211.       self.spyce_import = self.spyce_import + new_import
  212.     for i in self.spyce_import:
  213.       exec('import %s'%i)
  214.   def processSpyceError(self):
  215.     # server-level
  216.     self.spyce_error = 'error:serverHandler'
  217.     if self.spyce_config.has_key('error'):
  218.       self.spyce_error = self.spyce_config['error']
  219.     if self.overide_error:
  220.       self.spyce_error = self.overide_error
  221.     self.spyce_error = string.split(self.spyce_error, ':')
  222.     if len(self.spyce_error)==1:
  223.       raise 'invalid error handler specification (file:function)'
  224.     # page-level
  225.     self.spyce_pageerror = 'string:error:defaultErrorTemplate'
  226.     if self.spyce_config.has_key('pageerror'):
  227.       self.spyce_pageerror = self.spyce_config['pageerror']
  228.     if self.overide_pageerror:
  229.       self.spyce_pageerror = self.overide_pageerror
  230.     self.spyce_pageerror = string.split(self.spyce_pageerror, ':')
  231.     if (len(self.spyce_pageerror)<2 or self.spyce_pageerror[0] not in ('string', 'file')):
  232.       raise 'invalid pageerror handler specification ("string":module:variable, or ("file":file)'
  233.   def processSpyceGlobals(self):
  234.     self.spyce_globals = {}
  235.     if self.spyce_config.has_key('globals'):
  236.       for k in self.spyce_config['globals'].keys():
  237.         self.spyce_globals[k] = self.spyce_config['globals'][k]
  238.     if self.overide_globals:
  239.       for k in self.overide_globals.keys():
  240.         self.spyce_globals[k] = self.overide_globals[k]
  241.     for k in self.spyce_globals.keys():
  242.       self.spyce_globals[k] = eval(self.spyce_globals[k])
  243.   def processSpyceDebug(self):
  244.     self.spyce_debug = 0
  245.     if self.spyce_config.has_key('debug'):
  246.       self.spyce_debug = string.strip(string.lower(self.spyce_config['debug'])) not in ('0', 'false', 'no')
  247.     if self.overide_debug:
  248.       self.spyce_debug = 1
  249.   def processSpyceConcurrency(self):
  250.     self.spyce_concurrency = SPYCE_CONCURRENCY_SINGLE
  251.     if self.default_concurrency!=None:
  252.       self.spyce_concurrency = self.default_concurrency
  253.     if self.spyce_config.has_key('concurrency'):
  254.       self.spyce_concurrency = string.lower(self.spyce_config['concurrency'])
  255.       if self.spyce_concurrency in ('thread', 'threading'):
  256.         self.spyce_concurrency = SPYCE_CONCURRENCY_THREAD
  257.       elif self.spyce_concurrency in ('fork', 'forking'):
  258.         self.spyce_concurrency = SPYCE_CONCURRENCY_FORK
  259.       else: 
  260.         self.spyce_concurrency = SPYCE_CONCURRENCY_SINGLE
  261.     if self.overide_concurrency!=None:
  262.       self.spyce_concurrency = self.overide_concurrency
  263.   def processSpyceCache(self):
  264.     cache = 'memory'
  265.     if self.spyce_config.has_key('cache'):
  266.       cache = self.spyce_config['cache']
  267.     cache = string.split(cache, ':')
  268.     self.spyce_cache = string.strip(string.lower(cache[0])), string.join(cache[1:], ':')
  269.   def processSpyceWWW(self):
  270.     # root
  271.     self.spyce_www_root = self.default_www_root
  272.     if self.spyce_config.has_key('www_root'):
  273.       self.spyce_www_root = self.spyce_config['www_root']
  274.     if self.overide_www_root!=None:
  275.       self.spyce_www_root = self.overide_www_root
  276.     # port
  277.     self.spyce_www_port = self.default_www_port
  278.     if self.spyce_config.has_key('www_port'):
  279.       self.spyce_www_port = int(self.spyce_config['www_port'])
  280.     if self.overide_www_port!=None:
  281.       self.spyce_www_port = int(self.overide_www_port)
  282.     # mime
  283.     self.spyce_www_mime = self.default_www_mime
  284.     if self.spyce_config.has_key('www_mime'):
  285.       mime = self.spyce_config['www_mime']
  286.       mime = map(string.strip, string.split(mime, ','))
  287.       self.spyce_www_mime = self.spyce_www_mime + mime
  288.     # handler
  289.     self.spyce_www_handler = self.default_www_handler
  290.     if self.spyce_config.has_key('www_handler'):
  291.       handler = self.spyce_config['www_handler']
  292.       for k in handler.keys():
  293.         self.spyce_www_handler[k] = handler[k]
  294.  
  295.   # helpers
  296.   def findConfigFile(self):
  297.     locations = [SPYCE_HOME] + SPYCE_CONFIG_LOCATIONS
  298.     for l in locations:
  299.       p = os.path.join(l, SPYCE_CONFIG_FILENAME)
  300.       if os.path.exists(p):
  301.         return p
  302.   def parseConfigFile(self):
  303.     # initial defaults
  304.     path = None
  305.     load = None
  306.     error = None
  307.     pageerror = None
  308.     globals = None
  309.     debug = None
  310.     concurrency = None
  311.     cache = None
  312.     www_root = None
  313.     www_port = None
  314.     www_mime = None
  315.     www_handler = {}
  316.     cfg = ConfigParser.ConfigParser()
  317.     # parse
  318.     cfg.read(self.file)
  319.     if cfg.has_section('spyce'):
  320.       if 'path' in cfg.options('spyce'):
  321.         path = cfg.get('spyce', 'path')
  322.       if 'import' in cfg.options('spyce'):
  323.         load = cfg.get('spyce', 'import')
  324.       if 'error' in cfg.options('spyce'):
  325.         error = cfg.get('spyce', 'error')
  326.       if 'pageerror' in cfg.options('spyce'):
  327.         pageerror = cfg.get('spyce', 'pageerror')
  328.       if 'debug' in cfg.options('spyce'):
  329.         debug = cfg.get('spyce', 'debug')
  330.       if 'concurrency' in cfg.options('spyce'):
  331.         concurrency = cfg.get('spyce', 'concurrency')
  332.       if 'cache' in cfg.options('spyce'):
  333.         cache = cfg.get('spyce', 'cache')
  334.     if cfg.has_section('globals'):
  335.       globals = {}
  336.       for o in cfg.options('globals'):
  337.         if o=='__name__': continue
  338.         globals[o] = cfg.get('globals', o)
  339.     if cfg.has_section('www'):
  340.       for o in cfg.options('www'):
  341.         if o=='__name__': continue
  342.         if o=='root':
  343.           www_root = cfg.get('www', o)
  344.           continue
  345.         if o=='port':
  346.           www_port = cfg.get('www', o)
  347.           continue
  348.         if o=='mime': 
  349.           www_mime = cfg.get('www', o)
  350.           continue
  351.         if o[:len('ext_')]=='ext_':
  352.           ext = o[len('ext_'):]
  353.           if not ext: ext = None
  354.           www_handler[ext] = cfg.get('www', o)
  355.     # results
  356.     config = {}
  357.     if path!=None: config['path'] = path
  358.     if load!=None: config['import'] = load
  359.     if error!=None: config['error'] = error
  360.     if pageerror!=None: config['pageerror'] = pageerror
  361.     if globals!=None: config['globals'] = globals
  362.     if debug!=None: config['debug'] = debug
  363.     if concurrency!=None: config['concurrency'] = concurrency
  364.     if cache!=None: config['cache'] = cache
  365.     if www_root!=None: config['www_root'] = www_root
  366.     if www_port!=None: config['www_port'] = www_port
  367.     if www_mime!=None: config['www_mime'] = www_mime
  368.     if www_handler!={}: config['www_handler'] = www_handler
  369.     return config
  370.