Package turbofeeds :: Module util
[hide private]

Source Code for Module turbofeeds.util

 1  # -*- coding: UTF-8 -*- 
 2  """TurboFeed utility functions.""" 
 3  __docformat__ = 'restructuredtext' 
 4   
 5  __all__ = [ 
 6      'absolute_url', 
 7      'xml_stylesheet', 
 8  ] 
 9   
10  try: 
11      from kid import ProcessingInstruction as KidPI 
12  except ImportError: 
13      KidPI = False 
14  try: 
15      from genshi.core import PI as GenshiPI 
16  except ImportError: 
17      GenshiPI = False 
18   
19  try: 
20      from turbogears.controllers import absolute_url 
21  except: 
22      # Copied from TG 1.1 branch for TG 1.0.x compatibility 
23      from turbogears import url, config 
24      from cherrypy import request 
25   
26 - def get_server_name():
27 """Return name of the server this application runs on.""" 28 29 get = config.get 30 h = request.headers 31 host = get('tg.url_domain') or h.get('X-Forwarded-Host', h.get('Host')) 32 if not host: 33 host = '%s:%s' % (get('server.socket_host', 'localhost'), 34 get('server.socket_port', 8080)) 35 return host
36
37 - def absolute_url(tgpath='/', params=None, **kw):
38 """Return absolute URL (including schema and host to this server).""" 39 40 get = config.get 41 use_xfh = get('base_url_filter.use_x_forwarded_host', False) 42 if request.headers.get('X-Use-SSL'): 43 scheme = 'https' 44 else: 45 scheme = get('tg.url_scheme') 46 if not scheme: 47 scheme = request.scheme 48 base_url = '%s://%s' % (scheme, get_server_name()) 49 if get('base_url_filter.on', False) and not use_xfh: 50 base_url = get('base_url_filter.base_url').rstrip('/') 51 return '%s%s' % (base_url, url(tgpath, params, **kw))
52
53 -def xml_stylesheet(href, type='text/css', engine='text'):
54 """Returns an xml-stylesheet processing instruction element for given URL. 55 56 ``href`` can be a string with the URL to the stylesheet or a dict with 57 members ``href`` and ``type`` (see below) or a callable returning either. 58 59 ``type`` specifies the value of the "type" atribute of the PI. 60 The default type is 'text/css'. 61 62 ``engine`` specifies the template engine used. Can be one of ``"kid"``, 63 ``"genshi"``, or ``"text"``. Defaults to ``"text"``. 64 65 """ 66 if callable(href): 67 href = href() 68 if isinstance(href, dict): 69 type = href.get('type', type) 70 href = href['href'] 71 if engine == 'kid' and KidPI: 72 return KidPI('xml-stylesheet type="%s" href="%s"' % (type, href)) 73 elif engine == 'genshi' and GenshiPI: 74 return [(GenshiPI, ('xml-stylesheet', 'type="%s" href="%s"' % 75 (type, href)), None)] 76 elif engine == 'text': 77 return '<?xml-stylesheet type="%s" href="%s"?>' % (type, href) 78 else: 79 raise ValueError("Unsupported template engine.")
80