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 / modules / include.py < prev    next >
Encoding:
Python Source  |  2008-11-03  |  4.0 KB  |  110 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: include.py 5659 2006-04-27 16:15:15Z jwnmulder $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. import os, re
  11.  
  12. __doc__ = """Include module is used to assist the inclusion of abitrary
  13. elements/files into a Spyce file. It can also support the notion of an
  14. 'inclusion context'."""
  15.  
  16. class include(spyceModule):
  17.   def start(self):
  18.     self.context = None
  19.     self.vars = None
  20.     self.fromFile = None
  21.   def spyce(self, file, context=None):
  22.     "Include a Spyce file"
  23.     file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  24.     result = s = code = None
  25.     try:
  26.       code = self._api.spyceFile(file)
  27.       s = code.newWrapper()
  28.       modules = self._api.getModules()
  29.       for name in modules.keys():
  30.         s.setModule(name, modules[name])  # include module as well!
  31.       s.spyceInit(self._api.getRequest(), self._api.getResponse())
  32.       incmod = s._startModule('include', None, None, 1)
  33.       incmod.context = context
  34.       if type(context)==type({}):
  35.         incmod.vars = spyceVars(context)
  36.       incmod.fromFile = self._api.getFilename()
  37.       result = s.spyceProcess()
  38.     finally:
  39.       if s:
  40.         s.spyceDestroy()
  41.         code.returnWrapper(s)
  42.     return result
  43.   def dump(self, file, binary=0):
  44.     "Include a plain text file, verbatim"
  45.     file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  46.     f = None
  47.     try:
  48.       if binary: mode='rb'
  49.       else: mode='r'
  50.       f = open(file, mode)
  51.       buf = f.read()
  52.     finally:
  53.       if f: f.close()
  54.     return buf
  55.   def spycecode(self, file=None, string=None, html=None, code=None, eval=None, directive=None, comment=None):
  56.     "Emit formatted Spyce code"
  57.     if not html: html = ('<font color="#000000"><b>', '</b></font>')
  58.     if not code: code = ('<font color="#0000CC">', '</font>')
  59.     if not eval: eval = ('<font color="#CC0000">', '</font>')
  60.     if not directive: directive = ('<font color="#CC00CC">', '</font>')
  61.     if not comment: comment = ('<font color="#777777">', '</font>')
  62.     import spyceCompile
  63.     from StringIO import StringIO
  64.     if (file and string) or (not file and not string):
  65.       raise 'must specify either file or string, and not both'
  66.     if file:
  67.       f = None
  68.       try:
  69.         file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  70.         f = open(file, 'r')
  71.         string = f.read()
  72.       finally:
  73.         if f: f.close()
  74.     html_encode = self._api.getModule('transform').html_encode
  75.     try:
  76.       tokens = spyceCompile.spyceTokenize(string)
  77.       buf = StringIO()
  78.       markupstack = []
  79.       buf.write(html[0]); markupstack.append(html[1])
  80.       for type, text, _, _ in tokens:
  81.         if type in (spyceCompile.T_STMT, spyceCompile.T_CHUNK, spyceCompile.T_CHUNKG,):
  82.           buf.write(code[0]); markupstack.append(code[1])
  83.         if type in (spyceCompile.T_LAMBDA,):
  84.           buf.write(html[0]); markupstack.append(html[1])
  85.         if type in (spyceCompile.T_EVAL,):
  86.           buf.write(eval[0]); markupstack.append(eval[1])
  87.         if type in (spyceCompile.T_DIRECT,):
  88.           buf.write(directive[0]); markupstack.append(directive[1])
  89.         if type in (spyceCompile.T_CMNT,):
  90.           buf.write(comment[0]); markupstack.append(comment[1])
  91.         buf.write(html_encode(text))
  92.         if type in (spyceCompile.T_END_CMNT, spyceCompile.T_END,):
  93.           buf.write(markupstack.pop())
  94.       while markupstack:
  95.         buf.write(markupstack.pop())
  96.       return buf.getvalue()
  97.     except:
  98.       raise
  99.       raise 'error tokenizing!'
  100.  
  101. class spyceVars:
  102.   def __init__(self, vars):
  103.     self.__dict__['vars'] = vars
  104.   def __getattr__(self, name):
  105.     try: return self.__dict__['vars'][name]
  106.     except KeyError: raise AttributeError
  107.   def __setattr__(self, name, value):
  108.     self.__dict__['vars'][name] = value
  109.  
  110.