1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - MoinSupport library (derived from EventAggregatorSupport) 4 5 @copyright: 2008, 2009, 2010, 2011, 2012 by Paul Boddie <paul@boddie.org.uk> 6 @copyright: 2000-2004 Juergen Hermann <jh@web.de>, 7 2005-2008 MoinMoin:ThomasWaldmann. 8 @license: GNU GPL (v2 or later), see COPYING.txt for details. 9 """ 10 11 from DateSupport import * 12 from MoinMoin import wikiutil 13 import re 14 import time 15 16 __version__ = "0.1" 17 18 # Content type parsing. 19 20 encoding_regexp_str = ur'(?P<content_type>[^\s;]*)(?:;\s*charset=(?P<encoding>[-A-Za-z0-9]+))?' 21 encoding_regexp = re.compile(encoding_regexp_str) 22 23 # Utility functions. 24 25 def getContentTypeAndEncoding(content_type): 26 m = encoding_regexp.search(content_type) 27 if m: 28 return m.group("content_type"), m.group("encoding") 29 else: 30 return None, None 31 32 def int_or_none(x): 33 if x is None: 34 return x 35 else: 36 return int(x) 37 38 # Utility classes and associated functions. 39 40 class Form: 41 42 """ 43 A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x 44 environment. 45 """ 46 47 def __init__(self, form): 48 self.form = form 49 50 def has_key(self, name): 51 return not not self.form.getlist(name) 52 53 def get(self, name, default=None): 54 values = self.form.getlist(name) 55 if not values: 56 return default 57 else: 58 return values 59 60 def __getitem__(self, name): 61 return self.form.getlist(name) 62 63 class ActionSupport: 64 65 """ 66 Work around disruptive MoinMoin changes in 1.9, and also provide useful 67 convenience methods. 68 """ 69 70 def get_form(self): 71 return get_form(self.request) 72 73 def _get_selected(self, value, input_value): 74 75 """ 76 Return the HTML attribute text indicating selection of an option (or 77 otherwise) if 'value' matches 'input_value'. 78 """ 79 80 return input_value is not None and value == input_value and 'selected="selected"' or '' 81 82 def _get_selected_for_list(self, value, input_values): 83 84 """ 85 Return the HTML attribute text indicating selection of an option (or 86 otherwise) if 'value' matches one of the 'input_values'. 87 """ 88 89 return value in input_values and 'selected="selected"' or '' 90 91 def _get_input(self, form, name, default=None): 92 93 """ 94 Return the input from 'form' having the given 'name', returning either 95 the input converted to an integer or the given 'default' (optional, None 96 if not specified). 97 """ 98 99 value = form.get(name, [None])[0] 100 if not value: # true if 0 obtained 101 return default 102 else: 103 return int(value) 104 105 def get_form(request): 106 107 "Work around disruptive MoinMoin changes in 1.9." 108 109 if hasattr(request, "values"): 110 return Form(request.values) 111 else: 112 return request.form 113 114 class send_headers_cls: 115 116 """ 117 A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a 118 1.9.x environment. 119 """ 120 121 def __init__(self, request): 122 self.request = request 123 124 def __call__(self, headers): 125 for header in headers: 126 parts = header.split(":") 127 self.request.headers.add(parts[0], ":".join(parts[1:])) 128 129 def get_send_headers(request): 130 131 "Return a function that can send response headers." 132 133 if hasattr(request, "http_headers"): 134 return request.http_headers 135 elif hasattr(request, "emit_http_headers"): 136 return request.emit_http_headers 137 else: 138 return send_headers_cls(request) 139 140 def escattr(s): 141 return wikiutil.escape(s, 1) 142 143 def getPathInfo(request): 144 if hasattr(request, "getPathinfo"): 145 return request.getPathinfo() 146 else: 147 return request.path 148 149 # Action support functions. 150 151 def getPageRevision(page): 152 153 "Return the revision details dictionary for the given 'page'." 154 155 # From Page.edit_info... 156 157 if hasattr(page, "editlog_entry"): 158 line = page.editlog_entry() 159 else: 160 line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x 161 162 # Similar to Page.mtime_usecs behaviour... 163 164 if line: 165 timestamp = line.ed_time_usecs 166 mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x 167 comment = line.comment 168 else: 169 mtime = 0 170 comment = "" 171 172 # Leave the time zone empty. 173 174 return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + (None,)), "comment" : comment} 175 176 # User interface functions. 177 178 def getParameter(request, name, default=None): 179 180 """ 181 Using the given 'request', return the value of the parameter with the given 182 'name', returning the optional 'default' (or None) if no value was supplied 183 in the 'request'. 184 """ 185 186 return get_form(request).get(name, [default])[0] 187 188 def getQualifiedParameter(request, prefix, argname, default=None): 189 190 """ 191 Using the given 'request', 'prefix' and 'argname', retrieve the value of the 192 qualified parameter, returning the optional 'default' (or None) if no value 193 was supplied in the 'request'. 194 """ 195 196 argname = getQualifiedParameterName(prefix, argname) 197 return getParameter(request, argname, default) 198 199 def getQualifiedParameterName(prefix, argname): 200 201 """ 202 Return the qualified parameter name using the given 'prefix' and 'argname'. 203 """ 204 205 if prefix is None: 206 return argname 207 else: 208 return "%s-%s" % (prefix, argname) 209 210 # Page-related functions. 211 212 def getPrettyPageName(page): 213 214 "Return a nicely formatted title/name for the given 'page'." 215 216 title = page.split_title(force=1) 217 return getPrettyTitle(title) 218 219 def linkToPage(request, page, text, query_string=None): 220 221 """ 222 Using 'request', return a link to 'page' with the given link 'text' and 223 optional 'query_string'. 224 """ 225 226 text = wikiutil.escape(text) 227 return page.link_to_raw(request, text, query_string) 228 229 def linkToResource(url, request, text, query_string=None): 230 231 """ 232 Using 'request', return a link to 'url' with the given link 'text' and 233 optional 'query_string'. 234 """ 235 236 if query_string: 237 query_string = wikiutil.makeQueryString(query_string) 238 url = "%s?%s" % (url, query_string) 239 240 formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter 241 242 output = [] 243 output.append(formatter.url(1, url)) 244 output.append(formatter.text(text)) 245 output.append(formatter.url(0)) 246 return "".join(output) 247 248 def getFullPageName(parent, title): 249 250 """ 251 Return a full page name from the given 'parent' page (can be empty or None) 252 and 'title' (a simple page name). 253 """ 254 255 if parent: 256 return "%s/%s" % (parent.rstrip("/"), title) 257 else: 258 return title 259 260 # vim: tabstop=4 expandtab shiftwidth=4