home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / modules / include.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  4.2 KB  |  118 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 20864 2009-06-02 06:16:47Z ceros7 $
  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 spyceStr(self, file, context=None):
  44.     stdout = self._api.getModule('stdout')
  45.     stdout.push()
  46.     try:
  47.       result = self.spyce(file, context)
  48.     finally:
  49.       output = stdout.pop()
  50.     return output
  51.   def dump(self, file, binary=0):
  52.     "Include a plain text file, verbatim"
  53.     file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  54.     f = None
  55.     try:
  56.       if binary: mode='rb'
  57.       else: mode='r'
  58.       f = open(file, mode)
  59.       buf = f.read()
  60.     finally:
  61.       if f: f.close()
  62.     return buf
  63.   def spycecode(self, file=None, string=None, html=None, code=None, eval=None, directive=None, comment=None):
  64.     "Emit formatted Spyce code"
  65.     if not html: html = ('<font color="#000000"><b>', '</b></font>')
  66.     if not code: code = ('<font color="#0000CC">', '</font>')
  67.     if not eval: eval = ('<font color="#CC0000">', '</font>')
  68.     if not directive: directive = ('<font color="#CC00CC">', '</font>')
  69.     if not comment: comment = ('<font color="#777777">', '</font>')
  70.     import spyceCompile
  71.     from StringIO import StringIO
  72.     if (file and string) or (not file and not string):
  73.       raise 'must specify either file or string, and not both'
  74.     if file:
  75.       f = None
  76.       try:
  77.         file = os.path.join(os.path.dirname(self._api.getFilename()), file)
  78.         f = open(file, 'r')
  79.         string = f.read()
  80.       finally:
  81.         if f: f.close()
  82.     html_encode = self._api.getModule('transform').html_encode
  83.     try:
  84.       tokens = spyceCompile.spyceTokenize(string)
  85.       buf = StringIO()
  86.       markupstack = []
  87.       buf.write(html[0]); markupstack.append(html[1])
  88.       for type, text, _, _ in tokens:
  89.         if type in (spyceCompile.T_STMT, spyceCompile.T_CHUNK, spyceCompile.T_CHUNKG,):
  90.           buf.write(code[0]); markupstack.append(code[1])
  91.         if type in (spyceCompile.T_LAMBDA,):
  92.           buf.write(html[0]); markupstack.append(html[1])
  93.         if type in (spyceCompile.T_EVAL,):
  94.           buf.write(eval[0]); markupstack.append(eval[1])
  95.         if type in (spyceCompile.T_DIRECT,):
  96.           buf.write(directive[0]); markupstack.append(directive[1])
  97.         if type in (spyceCompile.T_CMNT,):
  98.           buf.write(comment[0]); markupstack.append(comment[1])
  99.         buf.write(html_encode(text))
  100.         if type in (spyceCompile.T_END_CMNT, spyceCompile.T_END,):
  101.           buf.write(markupstack.pop())
  102.       while markupstack:
  103.         buf.write(markupstack.pop())
  104.       return buf.getvalue()
  105.     except:
  106.       raise
  107.       raise 'error tokenizing!'
  108.  
  109. class spyceVars:
  110.   def __init__(self, vars):
  111.     self.__dict__['vars'] = vars
  112.   def __getattr__(self, name):
  113.     try: return self.__dict__['vars'][name]
  114.     except KeyError: raise AttributeError
  115.   def __setattr__(self, name, value):
  116.     self.__dict__['vars'][name] = value
  117.  
  118.