"Fossies" - the Fresh Open Source Software Archive

Member "anticms-0.2.1/cgi-bin/code/content.py" (5 Nov 2005, 4510 Bytes) of package /linux/www/old/anticms-0.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "content.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/env python
    2 # -*- coding: utf-8 -*-
    3 
    4 # $Id: content.py,v 1.2 2005/11/05 12:43:26 aurb Exp $
    5 
    6 # Copyright (C) 2005 Aurelius Bruzas
    7 # See COPYING for details.
    8 
    9 import xml.dom.minidom, os
   10 
   11 class Content:
   12     doc = None
   13     cfg = None
   14     page = None
   15     path = []
   16     pages = []
   17 
   18     def __init__(self, cfg):
   19         self.cfg = cfg
   20         self.setFile(cfg.getOption('content_file'))
   21 
   22 
   23     def setFile(self, file):
   24         self.doc = xml.dom.minidom.parse(file)
   25 
   26 
   27     def setRequest(self, req):
   28         self.req = req
   29         self.page = None
   30         self.path = []
   31         self.pages = self.extractPages(self.doc.getElementsByTagName('pages'))
   32 
   33 
   34     def getInfo(self):
   35         res = {
   36             'langs': [],
   37             'expires': {},
   38             'default': {}}
   39 
   40         for info in self.doc.getElementsByTagName('info'):
   41             for i in info.getElementsByTagName('item'):
   42                 type = i.getAttribute('type')
   43 
   44                 if type == 'language':
   45                     res['langs'].append(i.getAttribute('code'))
   46 
   47                 elif type == 'expires':
   48                     res['expires']['days'] = int(i.getAttribute('days'))
   49                     res['expires']['hours'] = int(i.getAttribute('hours'))
   50                     res['expires']['minutes'] = int(i.getAttribute('minutes'))
   51                     res['expires']['seconds'] = int(i.getAttribute('seconds'))
   52                     res['expires']['enabled'] = i.getAttribute('enabled') == 'true'
   53 
   54                 elif type == 'default':
   55                     res['default'][i.getAttribute('name')] = i.getAttribute('value')
   56 
   57         return res
   58 
   59 
   60     def getPath(self):
   61         return self.path
   62 
   63 
   64     def getPage(self):
   65         return self.page
   66 
   67 
   68     def getPages(self):
   69         return self.pages
   70 
   71 
   72     def getContent(self):
   73         cont = {}
   74 
   75         for i in self.doc.getElementsByTagName('content'):
   76             for j in i.childNodes:
   77                 if j.nodeName == 'item':
   78                     cont[j.getAttribute('container')] = self.extractContent(j)
   79 
   80         return cont
   81 
   82 # ----------------------------------------------------------------------- #
   83 
   84     def childList(self, item, names):
   85         for i in item.childNodes:
   86             if i.nodeName in names:
   87                 yield i
   88 
   89 
   90     def extractPages(self, topel, link = []):
   91         pages = []
   92 
   93         for i in topel:
   94             for j in self.childList(i, ('page')):
   95                 name = j.getAttribute('name')
   96                 pageName = self.cfg.getOption('cont_separator').join(link + [name])
   97 
   98                 pge = {
   99                     'link': self.req.makeLink(link + [name]),
  100                     'name': pageName,
  101                     'subpages': []}
  102 
  103                 for k in self.childList(j, ('attribute', 'subpages')):
  104                     if k.nodeName == 'attribute':
  105                         pge[k.getAttribute('name')] = self.getText(k, self.req.lang)
  106                     elif k.nodeName == 'subpages':
  107                         pge['subpages'] = self.extractPages([k], link + [name])
  108 
  109                 if pageName == self.req.page:
  110                     pge['current'] = 'true'
  111                 else:
  112                     pge['current'] = 'false'
  113 
  114                 if self.req.page.startswith(pageName):
  115                     pge['inpath'] = 'true'
  116                 else:
  117                     pge['inpath'] = 'false'
  118 
  119                 if pageName == self.req.page:
  120                     self.page = pge
  121 
  122                 if self.req.page.startswith(pageName):
  123                     self.path.insert(0, pge)
  124 
  125                 pages.append(pge)
  126 
  127         return pages
  128 
  129 
  130     def extractContent(self, item):
  131         # If there's a 'page' node in this peace of content, we check
  132         # whether it matches the current page. In case it doesn't, this
  133         # peace of content is not included.
  134         ok = 0
  135         have_pages = 0
  136 
  137         for i in self.childList(item, 'page'):
  138             have_pages = 1
  139             if i.getAttribute('subpages') == 'true':
  140                 if self.req.page.startswith(i.getAttribute('name')):
  141                     ok = 1
  142                     break
  143             else:
  144                 if self.req.page == i.getAttribute('name'):
  145                     ok = 1
  146                     break
  147 
  148         if ok == 0 and have_pages == 1: return None
  149 
  150         type = item.getAttribute('type')
  151 
  152         if type == 'string':
  153             return self.getText(item, self.req.lang)
  154 
  155         elif type == 'struct':
  156             list = {}
  157             for i in self.childList(item, ('item')):
  158                 data = self.extractContent(i)
  159                 if data != None:
  160                     list[i.getAttribute('name')] = data
  161             return list
  162 
  163         elif type == 'list':
  164             list = []
  165             for i in self.childList(item, ('item')):
  166                 data = self.extractContent(i)
  167                 if data != None:
  168                     list.append(data)
  169             return list
  170 
  171 
  172     def getText(self, elem, lang):
  173         e = None
  174 
  175         for i in self.childList(elem, ('text', 'file')):
  176             if not e: e = i
  177             if i.attributes:
  178                 if i.getAttribute('lang') == lang:
  179                     e = i
  180                     break
  181 
  182         if e:
  183             if e.nodeName == 'text':
  184                 return e.firstChild.nodeValue
  185 
  186             elif e.nodeName == 'file':
  187                 try:
  188                     f = open(os.path.join(os.getcwd(), self.cfg.getOption('data_dir'), e.getAttribute('name')), 'r')
  189                 except:
  190                     return "Can't read file <strong>" + e.getAttribute('name') + "</strong>.";
  191                 else:
  192                     t = unicode(f.read().decode(self.cfg.getOption('input_encoding')))
  193                     f.close()
  194                     return t
  195 
  196             else:
  197                 print e.nodeName
  198 
  199         return ''