1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/MoinSupport.py Sun Jan 22 00:04:16 2012 +0100
1.3 @@ -0,0 +1,260 @@
1.4 +# -*- coding: iso-8859-1 -*-
1.5 +"""
1.6 + MoinMoin - MoinSupport library (derived from EventAggregatorSupport)
1.7 +
1.8 + @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk>
1.9 + @copyright: 2000-2004 Juergen Hermann <jh@web.de>,
1.10 + 2005-2008 MoinMoin:ThomasWaldmann.
1.11 + @license: GNU GPL (v2 or later), see COPYING.txt for details.
1.12 +"""
1.13 +
1.14 +from DateSupport import *
1.15 +from MoinMoin import wikiutil
1.16 +import re
1.17 +import time
1.18 +
1.19 +__version__ = "0.1"
1.20 +
1.21 +# Content type parsing.
1.22 +
1.23 +encoding_regexp_str = ur'(?P<content_type>[^\s;]*)(?:;\s*charset=(?P<encoding>[-A-Za-z0-9]+))?'
1.24 +encoding_regexp = re.compile(encoding_regexp_str)
1.25 +
1.26 +# Utility functions.
1.27 +
1.28 +def getContentTypeAndEncoding(content_type):
1.29 + m = encoding_regexp.search(content_type)
1.30 + if m:
1.31 + return m.group("content_type"), m.group("encoding")
1.32 + else:
1.33 + return None, None
1.34 +
1.35 +def int_or_none(x):
1.36 + if x is None:
1.37 + return x
1.38 + else:
1.39 + return int(x)
1.40 +
1.41 +# Utility classes and associated functions.
1.42 +
1.43 +class Form:
1.44 +
1.45 + """
1.46 + A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x
1.47 + environment.
1.48 + """
1.49 +
1.50 + def __init__(self, form):
1.51 + self.form = form
1.52 +
1.53 + def has_key(self, name):
1.54 + return not not self.form.getlist(name)
1.55 +
1.56 + def get(self, name, default=None):
1.57 + values = self.form.getlist(name)
1.58 + if not values:
1.59 + return default
1.60 + else:
1.61 + return values
1.62 +
1.63 + def __getitem__(self, name):
1.64 + return self.form.getlist(name)
1.65 +
1.66 +class ActionSupport:
1.67 +
1.68 + """
1.69 + Work around disruptive MoinMoin changes in 1.9, and also provide useful
1.70 + convenience methods.
1.71 + """
1.72 +
1.73 + def get_form(self):
1.74 + return get_form(self.request)
1.75 +
1.76 + def _get_selected(self, value, input_value):
1.77 +
1.78 + """
1.79 + Return the HTML attribute text indicating selection of an option (or
1.80 + otherwise) if 'value' matches 'input_value'.
1.81 + """
1.82 +
1.83 + return input_value is not None and value == input_value and 'selected="selected"' or ''
1.84 +
1.85 + def _get_selected_for_list(self, value, input_values):
1.86 +
1.87 + """
1.88 + Return the HTML attribute text indicating selection of an option (or
1.89 + otherwise) if 'value' matches one of the 'input_values'.
1.90 + """
1.91 +
1.92 + return value in input_values and 'selected="selected"' or ''
1.93 +
1.94 + def _get_input(self, form, name, default=None):
1.95 +
1.96 + """
1.97 + Return the input from 'form' having the given 'name', returning either
1.98 + the input converted to an integer or the given 'default' (optional, None
1.99 + if not specified).
1.100 + """
1.101 +
1.102 + value = form.get(name, [None])[0]
1.103 + if not value: # true if 0 obtained
1.104 + return default
1.105 + else:
1.106 + return int(value)
1.107 +
1.108 +def get_form(request):
1.109 +
1.110 + "Work around disruptive MoinMoin changes in 1.9."
1.111 +
1.112 + if hasattr(request, "values"):
1.113 + return Form(request.values)
1.114 + else:
1.115 + return request.form
1.116 +
1.117 +class send_headers_cls:
1.118 +
1.119 + """
1.120 + A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a
1.121 + 1.9.x environment.
1.122 + """
1.123 +
1.124 + def __init__(self, request):
1.125 + self.request = request
1.126 +
1.127 + def __call__(self, headers):
1.128 + for header in headers:
1.129 + parts = header.split(":")
1.130 + self.request.headers.add(parts[0], ":".join(parts[1:]))
1.131 +
1.132 +def get_send_headers(request):
1.133 +
1.134 + "Return a function that can send response headers."
1.135 +
1.136 + if hasattr(request, "http_headers"):
1.137 + return request.http_headers
1.138 + elif hasattr(request, "emit_http_headers"):
1.139 + return request.emit_http_headers
1.140 + else:
1.141 + return send_headers_cls(request)
1.142 +
1.143 +def escattr(s):
1.144 + return wikiutil.escape(s, 1)
1.145 +
1.146 +def getPathInfo(request):
1.147 + if hasattr(request, "getPathinfo"):
1.148 + return request.getPathinfo()
1.149 + else:
1.150 + return request.path
1.151 +
1.152 +# Action support functions.
1.153 +
1.154 +def getPageRevision(page):
1.155 +
1.156 + "Return the revision details dictionary for the given 'page'."
1.157 +
1.158 + # From Page.edit_info...
1.159 +
1.160 + if hasattr(page, "editlog_entry"):
1.161 + line = page.editlog_entry()
1.162 + else:
1.163 + line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x
1.164 +
1.165 + # Similar to Page.mtime_usecs behaviour...
1.166 +
1.167 + if line:
1.168 + timestamp = line.ed_time_usecs
1.169 + mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x
1.170 + comment = line.comment
1.171 + else:
1.172 + mtime = 0
1.173 + comment = ""
1.174 +
1.175 + # Leave the time zone empty.
1.176 +
1.177 + return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + (None,)), "comment" : comment}
1.178 +
1.179 +# User interface functions.
1.180 +
1.181 +def getParameter(request, name, default=None):
1.182 +
1.183 + """
1.184 + Using the given 'request', return the value of the parameter with the given
1.185 + 'name', returning the optional 'default' (or None) if no value was supplied
1.186 + in the 'request'.
1.187 + """
1.188 +
1.189 + return get_form(request).get(name, [default])[0]
1.190 +
1.191 +def getQualifiedParameter(request, prefix, argname, default=None):
1.192 +
1.193 + """
1.194 + Using the given 'request', 'prefix' and 'argname', retrieve the value of the
1.195 + qualified parameter, returning the optional 'default' (or None) if no value
1.196 + was supplied in the 'request'.
1.197 + """
1.198 +
1.199 + argname = getQualifiedParameterName(prefix, argname)
1.200 + return getParameter(request, argname, default)
1.201 +
1.202 +def getQualifiedParameterName(prefix, argname):
1.203 +
1.204 + """
1.205 + Return the qualified parameter name using the given 'prefix' and 'argname'.
1.206 + """
1.207 +
1.208 + if prefix is None:
1.209 + return argname
1.210 + else:
1.211 + return "%s-%s" % (prefix, argname)
1.212 +
1.213 +# Page-related functions.
1.214 +
1.215 +def getPrettyPageName(page):
1.216 +
1.217 + "Return a nicely formatted title/name for the given 'page'."
1.218 +
1.219 + title = page.split_title(force=1)
1.220 + return getPrettyTitle(title)
1.221 +
1.222 +def linkToPage(request, page, text, query_string=None):
1.223 +
1.224 + """
1.225 + Using 'request', return a link to 'page' with the given link 'text' and
1.226 + optional 'query_string'.
1.227 + """
1.228 +
1.229 + text = wikiutil.escape(text)
1.230 + return page.link_to_raw(request, text, query_string)
1.231 +
1.232 +def linkToResource(url, request, text, query_string=None):
1.233 +
1.234 + """
1.235 + Using 'request', return a link to 'url' with the given link 'text' and
1.236 + optional 'query_string'.
1.237 + """
1.238 +
1.239 + if query_string:
1.240 + query_string = wikiutil.makeQueryString(query_string)
1.241 + url = "%s?%s" % (url, query_string)
1.242 +
1.243 + formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter
1.244 +
1.245 + output = []
1.246 + output.append(formatter.url(1, url))
1.247 + output.append(formatter.text(text))
1.248 + output.append(formatter.url(0))
1.249 + return "".join(output)
1.250 +
1.251 +def getFullPageName(parent, title):
1.252 +
1.253 + """
1.254 + Return a full page name from the given 'parent' page (can be empty or None)
1.255 + and 'title' (a simple page name).
1.256 + """
1.257 +
1.258 + if parent:
1.259 + return "%s/%s" % (parent.rstrip("/"), title)
1.260 + else:
1.261 + return title
1.262 +
1.263 +# vim: tabstop=4 expandtab shiftwidth=4