"Fossies" - the Fresh Open Source Software Archive

Member "anticms-0.2.1/cgi-bin/code/request.py" (5 Nov 2005, 2006 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 "request.py" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/env python
    2 # -*- coding: utf-8 -*-
    3 
    4 # $Id: request.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 os, re, urlparse, cgi
   10 
   11 class Request:
   12     cfg = None
   13     lang = ''
   14     page = ''
   15     pers_vars = ['lang']
   16     url_vars = {}
   17 
   18     def __init__(self, info, cfg):
   19         if os.environ.has_key('REQUEST_URI'):
   20             req = urlparse.urlparse(os.environ['REQUEST_URI'])
   21         else:
   22             req = ('http', 'localhost', '/', '', '', '')    # :-/
   23 
   24         self.url_vars = cgi.parse_qs(req[4])
   25         self.cfg = cfg
   26 
   27         # Determine current language: first, look if language is specified
   28         # in URL; if not, look in browser settings; then, if we still don't
   29         # know the language, use the default (or the first one, if there is
   30         # no default).
   31         if self.url_vars.has_key('lang') and self.url_vars['lang'][0] in info['langs']:
   32             self.lang = self.url_vars['lang'][0]
   33         else:
   34             if os.environ.has_key('HTTP_ACCEPT_LANGUAGE'):
   35                 for i in re.split('[,;-]', os.environ['HTTP_ACCEPT_LANGUAGE']):
   36                     if i in info['langs']:
   37                         self.lang = i
   38                         break
   39             if self.lang == '':
   40                 if info['default'].has_key('language'):
   41                     self.lang = info['default']['language']
   42                 elif info['langs']:
   43                     self.lang = info['langs'][0]
   44 
   45         # Determine the requested page.
   46         addr = req[2].lstrip('/')
   47         pref = cfg.getOption('addr_prefix')
   48         suff = cfg.getOption('addr_suffix')
   49 
   50         if suff and addr.endswith(suff):
   51             addr = addr[:-len(suff)]
   52 
   53         if pref and addr.startswith(pref):
   54             addr = addr[len(pref):]
   55 
   56         if addr:
   57             self.page = cfg.getOption('cont_separator').join(addr.split(cfg.getOption('addr_separator')))
   58         else:
   59             self.page = info['default']['page']
   60 
   61 
   62     def makeLink(self, page):
   63         pv = []
   64 
   65         for i in self.pers_vars:
   66             if self.url_vars.has_key(i):
   67                 pv.append(i + '=' + self.url_vars[i][0])
   68 
   69         if pv:
   70             suffix = '?' + '&'.join(pv)
   71         else:
   72             suffix = ''
   73 
   74         return self.cfg.getOption('addr_prefix') + self.cfg.getOption('addr_separator').join(page) + self.cfg.getOption('addr_suffix') + suffix