Attachment 'template2.py'

Download

   1 """ template - simple template using python % syntax
   2 
   3 The template does not contain any logic, just static source and macro
   4 calls whenever you need dynamic data
   5 
   6 Each template has a controller object, that must support all macro calls
   7 in runtime. The controller decide if an item should print, and it can
   8 call model objects to get the data needed for the template.
   9 
  10 This is being a bit naughty and relying on __str__() being implicitly
  11 called. This isn't necessary with a bit of code cleaning.
  12 """
  13 
  14 class TextTemplate:
  15     def __init__(self, text, controller):
  16         self.text = text.strip()
  17         self.controller = controller
  18     def __str__(self):
  19         return self.text%(self.controller)
  20 
  21 class FileTemplate(TextTemplate):
  22     def __init__(self, path, controller):
  23         text = file(path).read().strip()
  24         TextTemplate.__init__(self, text, controller)
  25 
  26 class MacroBase:
  27     def _get(self,item):
  28         return getattr(self, item, None)
  29     def __getitem__(self, item):
  30         """
  31          This is the clever bit
  32         """
  33         macro = self._get(item)
  34         if callable(macro):
  35             return macro()
  36         elif macro is not None:
  37             return macro
  38         else:
  39             # we have a choice here between throwing an exception and returning an empty string
  40             return ""
  41     def __contains__(self, item):
  42         """
  43          May not be neccessary but good form if we're acting like a dict
  44         """
  45         func = self._get(item)
  46         return (func is not None)
  47 
  48 import os
  49 
  50 class MacroController(MacroBase):
  51     """
  52      Static strings can be member variables.
  53      Computed strings are member functions that will only be called if used.
  54     """
  55     title = "Quick Test"
  56     def author(self):
  57         return os.environ.get('USER', 'unknown author')
  58     def filelist(self):
  59         items = ['<li>%s</li>' % item for item in os.listdir(os.getcwd())
  60                  if not item.startswith('.')]
  61         items.sort()
  62         return '\n'.join(items)
  63     def recursive(self):
  64         """
  65          A bit of silliness that may turn out to be useful
  66         """
  67         return TextTemplate('<em>Author is %(author)s</em>', self)
  68 
  69 if __name__ == '__main__':
  70     c = MacroController()
  71     t = FileTemplate('test.txt', c)
  72     print t

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-08-06 23:29:47, 2.6 KB) [[attachment:template.py]]
  • [get | view] (2004-08-10 15:53:39, 2.2 KB) [[attachment:template2.py]]
  • [get | view] (2004-08-06 23:13:10, 0.2 KB) [[attachment:test.txt]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.