paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinSupport library (derived from EventAggregatorSupport) |
paul@0 | 4 | |
paul@36 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@46 | 7 | 2004 by Florian Festi, |
paul@46 | 8 | 2006 by Mikko Virkkil, |
paul@46 | 9 | 2005-2008 MoinMoin:ThomasWaldmann, |
paul@46 | 10 | 2007 MoinMoin:ReimarBauer. |
paul@0 | 11 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 12 | """ |
paul@0 | 13 | |
paul@0 | 14 | from DateSupport import * |
paul@41 | 15 | from ItemSupport import ItemDirectoryStore |
paul@58 | 16 | from MoinMoin.parser import text_moin_wiki |
paul@1 | 17 | from MoinMoin.Page import Page |
paul@37 | 18 | from MoinMoin.util import lock |
paul@25 | 19 | from MoinMoin import config, search, wikiutil |
paul@10 | 20 | from StringIO import StringIO |
paul@10 | 21 | from shlex import shlex |
paul@0 | 22 | import re |
paul@0 | 23 | import time |
paul@37 | 24 | import os |
paul@0 | 25 | |
paul@35 | 26 | # Moin 1.9 request parameters. |
paul@35 | 27 | |
paul@35 | 28 | try: |
paul@35 | 29 | from MoinMoin.support.werkzeug.datastructures import MultiDict |
paul@35 | 30 | except ImportError: |
paul@35 | 31 | pass |
paul@35 | 32 | |
paul@46 | 33 | __version__ = "0.3" |
paul@0 | 34 | |
paul@25 | 35 | # Extraction of shared fragments. |
paul@25 | 36 | |
paul@25 | 37 | marker_regexp_str = r"([{]{3,}|[}]{3,})" |
paul@25 | 38 | marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... |
paul@25 | 39 | |
paul@29 | 40 | # Extraction of headings. |
paul@29 | 41 | |
paul@29 | 42 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@29 | 43 | |
paul@25 | 44 | # Category extraction from pages. |
paul@25 | 45 | |
paul@25 | 46 | category_regexp = None |
paul@25 | 47 | |
paul@25 | 48 | # Simple content parsing. |
paul@25 | 49 | |
paul@25 | 50 | verbatim_regexp = re.compile(ur'(?:' |
paul@25 | 51 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@25 | 52 | ur'|' |
paul@25 | 53 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@25 | 54 | ur'|' |
paul@25 | 55 | ur'!(?P<verbatim3>.*?)(\s|$)?' |
paul@25 | 56 | ur'|' |
paul@25 | 57 | ur'`(?P<monospace>.*?)`' |
paul@25 | 58 | ur'|' |
paul@25 | 59 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@25 | 60 | ur')', re.UNICODE) |
paul@25 | 61 | |
paul@25 | 62 | # Category discovery. |
paul@0 | 63 | |
paul@25 | 64 | def getCategoryPattern(request): |
paul@25 | 65 | global category_regexp |
paul@25 | 66 | |
paul@25 | 67 | try: |
paul@25 | 68 | return request.cfg.cache.page_category_regexact |
paul@25 | 69 | except AttributeError: |
paul@25 | 70 | |
paul@25 | 71 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@25 | 72 | |
paul@25 | 73 | if category_regexp is None: |
paul@25 | 74 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@25 | 75 | return category_regexp |
paul@25 | 76 | |
paul@25 | 77 | def getCategories(request): |
paul@25 | 78 | |
paul@25 | 79 | """ |
paul@25 | 80 | From the AdvancedSearch macro, return a list of category page names using |
paul@25 | 81 | the given 'request'. |
paul@25 | 82 | """ |
paul@25 | 83 | |
paul@25 | 84 | # This will return all pages with "Category" in the title. |
paul@25 | 85 | |
paul@25 | 86 | cat_filter = getCategoryPattern(request).search |
paul@25 | 87 | return request.rootpage.getPageList(filter=cat_filter) |
paul@25 | 88 | |
paul@25 | 89 | def getCategoryMapping(category_pagenames, request): |
paul@2 | 90 | |
paul@2 | 91 | """ |
paul@25 | 92 | For the given 'category_pagenames' return a list of tuples of the form |
paul@25 | 93 | (category name, category page name) using the given 'request'. |
paul@25 | 94 | """ |
paul@25 | 95 | |
paul@25 | 96 | cat_pattern = getCategoryPattern(request) |
paul@25 | 97 | mapping = [] |
paul@25 | 98 | for pagename in category_pagenames: |
paul@25 | 99 | name = cat_pattern.match(pagename).group("key") |
paul@25 | 100 | if name != "Category": |
paul@25 | 101 | mapping.append((name, pagename)) |
paul@25 | 102 | mapping.sort() |
paul@25 | 103 | return mapping |
paul@25 | 104 | |
paul@25 | 105 | def getCategoryPages(pagename, request): |
paul@25 | 106 | |
paul@25 | 107 | """ |
paul@25 | 108 | Return the pages associated with the given category 'pagename' using the |
paul@25 | 109 | 'request'. |
paul@25 | 110 | """ |
paul@25 | 111 | |
paul@25 | 112 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@25 | 113 | results = search.searchPages(request, query, "page_name") |
paul@27 | 114 | return filterCategoryPages(results, request) |
paul@27 | 115 | |
paul@27 | 116 | def filterCategoryPages(results, request): |
paul@27 | 117 | |
paul@27 | 118 | "Filter category pages from the given 'results' using the 'request'." |
paul@25 | 119 | |
paul@25 | 120 | cat_pattern = getCategoryPattern(request) |
paul@25 | 121 | pages = [] |
paul@25 | 122 | for page in results.hits: |
paul@25 | 123 | if not cat_pattern.match(page.page_name): |
paul@25 | 124 | pages.append(page) |
paul@25 | 125 | return pages |
paul@25 | 126 | |
paul@25 | 127 | def getAllCategoryPages(category_names, request): |
paul@25 | 128 | |
paul@25 | 129 | """ |
paul@25 | 130 | Return all pages belonging to the categories having the given |
paul@25 | 131 | 'category_names', using the given 'request'. |
paul@25 | 132 | """ |
paul@25 | 133 | |
paul@25 | 134 | pages = [] |
paul@25 | 135 | pagenames = set() |
paul@25 | 136 | |
paul@25 | 137 | for category_name in category_names: |
paul@25 | 138 | |
paul@25 | 139 | # Get the pages and page names in the category. |
paul@25 | 140 | |
paul@25 | 141 | pages_in_category = getCategoryPages(category_name, request) |
paul@25 | 142 | |
paul@25 | 143 | # Visit each page in the category. |
paul@25 | 144 | |
paul@25 | 145 | for page_in_category in pages_in_category: |
paul@25 | 146 | pagename = page_in_category.page_name |
paul@25 | 147 | |
paul@25 | 148 | # Only process each page once. |
paul@25 | 149 | |
paul@25 | 150 | if pagename in pagenames: |
paul@25 | 151 | continue |
paul@25 | 152 | else: |
paul@25 | 153 | pagenames.add(pagename) |
paul@25 | 154 | |
paul@25 | 155 | pages.append(page_in_category) |
paul@25 | 156 | |
paul@25 | 157 | return pages |
paul@25 | 158 | |
paul@28 | 159 | def getPagesForSearch(search_pattern, request): |
paul@27 | 160 | |
paul@27 | 161 | """ |
paul@27 | 162 | Return result pages for a search employing the given 'search_pattern' and |
paul@28 | 163 | using the given 'request'. |
paul@27 | 164 | """ |
paul@27 | 165 | |
paul@27 | 166 | query = search.QueryParser().parse_query(search_pattern) |
paul@27 | 167 | results = search.searchPages(request, query, "page_name") |
paul@27 | 168 | return filterCategoryPages(results, request) |
paul@27 | 169 | |
paul@25 | 170 | # WikiDict functions. |
paul@25 | 171 | |
paul@25 | 172 | def getWikiDict(pagename, request): |
paul@25 | 173 | |
paul@25 | 174 | """ |
paul@25 | 175 | Return the WikiDict provided by the given 'pagename' using the given |
paul@25 | 176 | 'request'. |
paul@2 | 177 | """ |
paul@2 | 178 | |
paul@25 | 179 | if pagename and Page(request, pagename).exists() and request.user.may.read(pagename): |
paul@25 | 180 | if hasattr(request.dicts, "dict"): |
paul@25 | 181 | return request.dicts.dict(pagename) |
paul@25 | 182 | else: |
paul@25 | 183 | return request.dicts[pagename] |
paul@0 | 184 | else: |
paul@25 | 185 | return None |
paul@25 | 186 | |
paul@25 | 187 | # Searching-related functions. |
paul@25 | 188 | |
paul@25 | 189 | def getPagesFromResults(result_pages, request): |
paul@25 | 190 | |
paul@25 | 191 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@25 | 192 | |
paul@25 | 193 | return [Page(request, page.page_name) for page in result_pages] |
paul@25 | 194 | |
paul@25 | 195 | # Region/section parsing. |
paul@25 | 196 | |
paul@25 | 197 | def getRegions(s, include_non_regions=False): |
paul@25 | 198 | |
paul@25 | 199 | """ |
paul@25 | 200 | Parse the string 's', returning a list of explicitly declared regions. |
paul@25 | 201 | |
paul@25 | 202 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 203 | included for text between explicitly declared regions. |
paul@25 | 204 | """ |
paul@25 | 205 | |
paul@25 | 206 | regions = [] |
paul@25 | 207 | marker = None |
paul@25 | 208 | is_block = True |
paul@25 | 209 | |
paul@25 | 210 | # Start a region for exposed text, if appropriate. |
paul@25 | 211 | |
paul@25 | 212 | if include_non_regions: |
paul@25 | 213 | regions.append("") |
paul@25 | 214 | |
paul@25 | 215 | for match_text in marker_regexp.split(s): |
paul@25 | 216 | |
paul@25 | 217 | # Capture section text. |
paul@25 | 218 | |
paul@25 | 219 | if is_block: |
paul@25 | 220 | if marker or include_non_regions: |
paul@25 | 221 | regions[-1] += match_text |
paul@25 | 222 | |
paul@25 | 223 | # Handle section markers. |
paul@25 | 224 | |
paul@31 | 225 | else: |
paul@25 | 226 | |
paul@25 | 227 | # Close any open sections, returning to exposed text regions. |
paul@25 | 228 | |
paul@25 | 229 | if marker: |
paul@31 | 230 | |
paul@31 | 231 | # Add any marker to the current region, regardless of whether it |
paul@31 | 232 | # successfully closes a section. |
paul@31 | 233 | |
paul@31 | 234 | regions[-1] += match_text |
paul@31 | 235 | |
paul@25 | 236 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@25 | 237 | marker = None |
paul@25 | 238 | |
paul@25 | 239 | # Start a region for exposed text, if appropriate. |
paul@25 | 240 | |
paul@25 | 241 | if include_non_regions: |
paul@25 | 242 | regions.append("") |
paul@25 | 243 | |
paul@31 | 244 | # Without a current marker, start a new section. |
paul@25 | 245 | |
paul@31 | 246 | else: |
paul@25 | 247 | marker = match_text |
paul@25 | 248 | regions.append("") |
paul@25 | 249 | |
paul@31 | 250 | # Add the marker to the new region. |
paul@25 | 251 | |
paul@31 | 252 | regions[-1] += match_text |
paul@0 | 253 | |
paul@25 | 254 | # The match text alternates between text between markers and the markers |
paul@25 | 255 | # themselves. |
paul@25 | 256 | |
paul@25 | 257 | is_block = not is_block |
paul@25 | 258 | |
paul@25 | 259 | return regions |
paul@25 | 260 | |
paul@25 | 261 | def getFragmentsFromRegions(regions): |
paul@25 | 262 | |
paul@25 | 263 | """ |
paul@25 | 264 | Return fragments from the given 'regions', each having the form |
paul@32 | 265 | (format, attributes, body text). |
paul@25 | 266 | """ |
paul@25 | 267 | |
paul@25 | 268 | fragments = [] |
paul@25 | 269 | |
paul@25 | 270 | for region in regions: |
paul@32 | 271 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@32 | 272 | fragments.append((format, attributes, body)) |
paul@25 | 273 | |
paul@32 | 274 | return fragments |
paul@25 | 275 | |
paul@32 | 276 | def getFragmentFromRegion(region): |
paul@25 | 277 | |
paul@32 | 278 | """ |
paul@32 | 279 | Return a fragment for the given 'region' having the form (format, |
paul@32 | 280 | attributes, body text, header, close), where the 'header' is the original |
paul@32 | 281 | declaration of the 'region' or None if no explicit region is defined, and |
paul@32 | 282 | 'close' is the closing marker of the 'region' or None if no explicit region |
paul@32 | 283 | is defined. |
paul@32 | 284 | """ |
paul@25 | 285 | |
paul@32 | 286 | if region.startswith("{{{"): |
paul@25 | 287 | |
paul@32 | 288 | body = region.lstrip("{") |
paul@32 | 289 | level = len(region) - len(body) |
paul@32 | 290 | body = body.rstrip("}").lstrip() |
paul@32 | 291 | |
paul@32 | 292 | # Remove any prelude and process metadata. |
paul@32 | 293 | |
paul@32 | 294 | if body.startswith("#!"): |
paul@25 | 295 | |
paul@32 | 296 | try: |
paul@32 | 297 | declaration, body = body.split("\n", 1) |
paul@32 | 298 | except ValueError: |
paul@32 | 299 | declaration = body |
paul@32 | 300 | body = "" |
paul@25 | 301 | |
paul@32 | 302 | arguments = declaration[2:] |
paul@32 | 303 | |
paul@32 | 304 | # Get any parser/format declaration. |
paul@25 | 305 | |
paul@32 | 306 | if arguments and not arguments[0].isspace(): |
paul@32 | 307 | details = arguments.split(None, 1) |
paul@32 | 308 | if len(details) == 2: |
paul@32 | 309 | format, arguments = details |
paul@32 | 310 | else: |
paul@32 | 311 | format = details[0] |
paul@32 | 312 | arguments = "" |
paul@32 | 313 | else: |
paul@32 | 314 | format = None |
paul@25 | 315 | |
paul@32 | 316 | # Get the attributes/arguments for the region. |
paul@32 | 317 | |
paul@32 | 318 | attributes = parseAttributes(arguments, False) |
paul@25 | 319 | |
paul@32 | 320 | # Add an entry for the format in the attribute dictionary. |
paul@25 | 321 | |
paul@32 | 322 | if format and not attributes.has_key(format): |
paul@32 | 323 | attributes[format] = True |
paul@32 | 324 | |
paul@32 | 325 | return format, attributes, body, level * "{" + declaration + "\n", level * "}" |
paul@25 | 326 | |
paul@25 | 327 | else: |
paul@32 | 328 | return None, {}, body, level * "{" + "\n", level * "}" |
paul@25 | 329 | |
paul@32 | 330 | else: |
paul@32 | 331 | return None, {}, region, None, None |
paul@25 | 332 | |
paul@25 | 333 | def getFragments(s, include_non_regions=False): |
paul@25 | 334 | |
paul@25 | 335 | """ |
paul@25 | 336 | Return fragments for the given string 's', each having the form |
paul@25 | 337 | (format, arguments, body text). |
paul@25 | 338 | |
paul@25 | 339 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 340 | included for text between explicitly declared regions. |
paul@25 | 341 | """ |
paul@25 | 342 | |
paul@25 | 343 | return getFragmentsFromRegions(getRegions(s, include_non_regions)) |
paul@25 | 344 | |
paul@29 | 345 | # Heading extraction. |
paul@29 | 346 | |
paul@29 | 347 | def getHeadings(s): |
paul@29 | 348 | |
paul@29 | 349 | """ |
paul@29 | 350 | Return tuples of the form (level, title, span) for headings found within the |
paul@29 | 351 | given string 's'. The span is itself a (start, end) tuple indicating the |
paul@29 | 352 | matching region of 's' for a heading declaration. |
paul@29 | 353 | """ |
paul@29 | 354 | |
paul@29 | 355 | headings = [] |
paul@29 | 356 | |
paul@29 | 357 | for match in heading_regexp.finditer(s): |
paul@29 | 358 | headings.append( |
paul@29 | 359 | (len(match.group("level")), match.group("heading"), match.span()) |
paul@29 | 360 | ) |
paul@29 | 361 | |
paul@29 | 362 | return headings |
paul@29 | 363 | |
paul@25 | 364 | # Region/section attribute parsing. |
paul@0 | 365 | |
paul@10 | 366 | def parseAttributes(s, escape=True): |
paul@10 | 367 | |
paul@10 | 368 | """ |
paul@10 | 369 | Parse the section attributes string 's', returning a mapping of names to |
paul@10 | 370 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@10 | 371 | for use with the formatter API. If 'escape' is set to a false value, the |
paul@10 | 372 | attributes will have any quoting removed. |
paul@10 | 373 | """ |
paul@10 | 374 | |
paul@10 | 375 | attrs = {} |
paul@10 | 376 | f = StringIO(s) |
paul@10 | 377 | name = None |
paul@10 | 378 | need_value = False |
paul@34 | 379 | lex = shlex(f) |
paul@34 | 380 | lex.wordchars += "-" |
paul@10 | 381 | |
paul@34 | 382 | for token in lex: |
paul@10 | 383 | |
paul@10 | 384 | # Capture the name if needed. |
paul@10 | 385 | |
paul@10 | 386 | if name is None: |
paul@10 | 387 | name = escape and wikiutil.escape(token) or strip_token(token) |
paul@10 | 388 | |
paul@10 | 389 | # Detect either an equals sign or another name. |
paul@10 | 390 | |
paul@10 | 391 | elif not need_value: |
paul@10 | 392 | if token == "=": |
paul@10 | 393 | need_value = True |
paul@10 | 394 | else: |
paul@10 | 395 | attrs[name.lower()] = escape and "true" or True |
paul@10 | 396 | name = wikiutil.escape(token) |
paul@10 | 397 | |
paul@10 | 398 | # Otherwise, capture a value. |
paul@10 | 399 | |
paul@10 | 400 | else: |
paul@10 | 401 | # Quoting of attributes done similarly to wikiutil.parseAttributes. |
paul@10 | 402 | |
paul@10 | 403 | if token: |
paul@10 | 404 | if escape: |
paul@10 | 405 | if token[0] in ("'", '"'): |
paul@10 | 406 | token = wikiutil.escape(token) |
paul@10 | 407 | else: |
paul@10 | 408 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@10 | 409 | else: |
paul@10 | 410 | token = strip_token(token) |
paul@10 | 411 | |
paul@10 | 412 | attrs[name.lower()] = token |
paul@10 | 413 | name = None |
paul@10 | 414 | need_value = False |
paul@10 | 415 | |
paul@13 | 416 | # Handle any name-only attributes at the end of the collection. |
paul@13 | 417 | |
paul@13 | 418 | if name and not need_value: |
paul@13 | 419 | attrs[name.lower()] = escape and "true" or True |
paul@13 | 420 | |
paul@10 | 421 | return attrs |
paul@10 | 422 | |
paul@10 | 423 | def strip_token(token): |
paul@10 | 424 | |
paul@10 | 425 | "Return the given 'token' stripped of quoting." |
paul@10 | 426 | |
paul@10 | 427 | if token[0] in ("'", '"') and token[-1] == token[0]: |
paul@10 | 428 | return token[1:-1] |
paul@10 | 429 | else: |
paul@10 | 430 | return token |
paul@10 | 431 | |
paul@46 | 432 | # Macro argument parsing. |
paul@46 | 433 | |
paul@46 | 434 | def parseMacroArguments(args): |
paul@46 | 435 | |
paul@46 | 436 | """ |
paul@46 | 437 | Interpret the arguments. To support commas in labels, the label argument |
paul@46 | 438 | should be quoted. For example: |
paul@46 | 439 | |
paul@46 | 440 | "label=No, thanks!" |
paul@46 | 441 | """ |
paul@46 | 442 | |
paul@46 | 443 | try: |
paul@46 | 444 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@46 | 445 | except AttributeError: |
paul@46 | 446 | parsed_args = args.split(",") |
paul@46 | 447 | |
paul@46 | 448 | pairs = [] |
paul@46 | 449 | for arg in parsed_args: |
paul@46 | 450 | if arg: |
paul@46 | 451 | pair = arg.split("=", 1) |
paul@46 | 452 | if len(pair) < 2: |
paul@46 | 453 | pairs.append((None, arg)) |
paul@46 | 454 | else: |
paul@46 | 455 | pairs.append(tuple(pair)) |
paul@46 | 456 | |
paul@46 | 457 | return pairs |
paul@46 | 458 | |
paul@25 | 459 | # Request-related classes and associated functions. |
paul@0 | 460 | |
paul@0 | 461 | class Form: |
paul@0 | 462 | |
paul@0 | 463 | """ |
paul@0 | 464 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@0 | 465 | environment. |
paul@0 | 466 | """ |
paul@0 | 467 | |
paul@35 | 468 | def __init__(self, request): |
paul@35 | 469 | self.request = request |
paul@35 | 470 | self.form = request.values |
paul@0 | 471 | |
paul@0 | 472 | def has_key(self, name): |
paul@0 | 473 | return not not self.form.getlist(name) |
paul@0 | 474 | |
paul@0 | 475 | def get(self, name, default=None): |
paul@0 | 476 | values = self.form.getlist(name) |
paul@0 | 477 | if not values: |
paul@0 | 478 | return default |
paul@0 | 479 | else: |
paul@0 | 480 | return values |
paul@0 | 481 | |
paul@0 | 482 | def __getitem__(self, name): |
paul@0 | 483 | return self.form.getlist(name) |
paul@0 | 484 | |
paul@35 | 485 | def __setitem__(self, name, value): |
paul@35 | 486 | try: |
paul@35 | 487 | self.form.setlist(name, value) |
paul@35 | 488 | except TypeError: |
paul@35 | 489 | self._write_enable() |
paul@35 | 490 | self.form.setlist(name, value) |
paul@35 | 491 | |
paul@33 | 492 | def __delitem__(self, name): |
paul@35 | 493 | try: |
paul@35 | 494 | del self.form[name] |
paul@35 | 495 | except TypeError: |
paul@35 | 496 | self._write_enable() |
paul@35 | 497 | del self.form[name] |
paul@35 | 498 | |
paul@35 | 499 | def _write_enable(self): |
paul@35 | 500 | self.form = self.request.values = MultiDict(self.form) |
paul@33 | 501 | |
paul@33 | 502 | def keys(self): |
paul@33 | 503 | return self.form.keys() |
paul@33 | 504 | |
paul@32 | 505 | def items(self): |
paul@35 | 506 | return self.form.lists() |
paul@32 | 507 | |
paul@0 | 508 | class ActionSupport: |
paul@0 | 509 | |
paul@0 | 510 | """ |
paul@0 | 511 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@0 | 512 | convenience methods. |
paul@0 | 513 | """ |
paul@0 | 514 | |
paul@0 | 515 | def get_form(self): |
paul@0 | 516 | return get_form(self.request) |
paul@0 | 517 | |
paul@0 | 518 | def _get_selected(self, value, input_value): |
paul@0 | 519 | |
paul@0 | 520 | """ |
paul@0 | 521 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 522 | otherwise) if 'value' matches 'input_value'. |
paul@0 | 523 | """ |
paul@0 | 524 | |
paul@0 | 525 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@0 | 526 | |
paul@0 | 527 | def _get_selected_for_list(self, value, input_values): |
paul@0 | 528 | |
paul@0 | 529 | """ |
paul@0 | 530 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 531 | otherwise) if 'value' matches one of the 'input_values'. |
paul@0 | 532 | """ |
paul@0 | 533 | |
paul@0 | 534 | return value in input_values and 'selected="selected"' or '' |
paul@0 | 535 | |
paul@26 | 536 | def get_option_list(self, value, values): |
paul@26 | 537 | |
paul@26 | 538 | """ |
paul@26 | 539 | Return a list of HTML element definitions for options describing the |
paul@26 | 540 | given 'values', selecting the option with the specified 'value' if |
paul@26 | 541 | present. |
paul@26 | 542 | """ |
paul@26 | 543 | |
paul@26 | 544 | options = [] |
paul@26 | 545 | for available_value in values: |
paul@26 | 546 | selected = self._get_selected(available_value, value) |
paul@26 | 547 | options.append('<option value="%s" %s>%s</option>' % ( |
paul@26 | 548 | escattr(available_value), selected, wikiutil.escape(available_value))) |
paul@26 | 549 | return options |
paul@26 | 550 | |
paul@0 | 551 | def _get_input(self, form, name, default=None): |
paul@0 | 552 | |
paul@0 | 553 | """ |
paul@0 | 554 | Return the input from 'form' having the given 'name', returning either |
paul@0 | 555 | the input converted to an integer or the given 'default' (optional, None |
paul@0 | 556 | if not specified). |
paul@0 | 557 | """ |
paul@0 | 558 | |
paul@0 | 559 | value = form.get(name, [None])[0] |
paul@0 | 560 | if not value: # true if 0 obtained |
paul@0 | 561 | return default |
paul@0 | 562 | else: |
paul@0 | 563 | return int(value) |
paul@0 | 564 | |
paul@0 | 565 | def get_form(request): |
paul@0 | 566 | |
paul@0 | 567 | "Work around disruptive MoinMoin changes in 1.9." |
paul@0 | 568 | |
paul@0 | 569 | if hasattr(request, "values"): |
paul@35 | 570 | return Form(request) |
paul@0 | 571 | else: |
paul@0 | 572 | return request.form |
paul@0 | 573 | |
paul@0 | 574 | class send_headers_cls: |
paul@0 | 575 | |
paul@0 | 576 | """ |
paul@0 | 577 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@0 | 578 | 1.9.x environment. |
paul@0 | 579 | """ |
paul@0 | 580 | |
paul@0 | 581 | def __init__(self, request): |
paul@0 | 582 | self.request = request |
paul@0 | 583 | |
paul@0 | 584 | def __call__(self, headers): |
paul@0 | 585 | for header in headers: |
paul@0 | 586 | parts = header.split(":") |
paul@0 | 587 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@0 | 588 | |
paul@0 | 589 | def get_send_headers(request): |
paul@0 | 590 | |
paul@0 | 591 | "Return a function that can send response headers." |
paul@0 | 592 | |
paul@0 | 593 | if hasattr(request, "http_headers"): |
paul@0 | 594 | return request.http_headers |
paul@0 | 595 | elif hasattr(request, "emit_http_headers"): |
paul@0 | 596 | return request.emit_http_headers |
paul@0 | 597 | else: |
paul@0 | 598 | return send_headers_cls(request) |
paul@0 | 599 | |
paul@0 | 600 | def escattr(s): |
paul@0 | 601 | return wikiutil.escape(s, 1) |
paul@0 | 602 | |
paul@0 | 603 | def getPathInfo(request): |
paul@0 | 604 | if hasattr(request, "getPathinfo"): |
paul@0 | 605 | return request.getPathinfo() |
paul@0 | 606 | else: |
paul@0 | 607 | return request.path |
paul@0 | 608 | |
paul@14 | 609 | def getHeader(request, header_name, prefix=None): |
paul@17 | 610 | |
paul@17 | 611 | """ |
paul@17 | 612 | Using the 'request', return the value of the header with the given |
paul@17 | 613 | 'header_name', using the optional 'prefix' to obtain protocol-specific |
paul@17 | 614 | headers if necessary. |
paul@17 | 615 | |
paul@17 | 616 | If no value is found for the given 'header_name', None is returned. |
paul@17 | 617 | """ |
paul@17 | 618 | |
paul@14 | 619 | if hasattr(request, "getHeader"): |
paul@14 | 620 | return request.getHeader(header_name) |
paul@14 | 621 | elif hasattr(request, "headers"): |
paul@17 | 622 | return request.headers.get(header_name) |
paul@14 | 623 | else: |
paul@17 | 624 | return request.env.get((prefix and prefix + "_" or "") + header_name.upper()) |
paul@14 | 625 | |
paul@23 | 626 | def writeHeaders(request, mimetype, metadata, status=None): |
paul@23 | 627 | |
paul@23 | 628 | """ |
paul@23 | 629 | Using the 'request', write resource headers using the given 'mimetype', |
paul@23 | 630 | based on the given 'metadata'. If the optional 'status' is specified, set |
paul@23 | 631 | the status header to the given value. |
paul@23 | 632 | """ |
paul@23 | 633 | |
paul@23 | 634 | send_headers = get_send_headers(request) |
paul@23 | 635 | |
paul@23 | 636 | # Define headers. |
paul@23 | 637 | |
paul@23 | 638 | headers = ["Content-Type: %s; charset=%s" % (mimetype, config.charset)] |
paul@23 | 639 | |
paul@23 | 640 | # Define the last modified time. |
paul@23 | 641 | # NOTE: Consider using request.httpDate. |
paul@23 | 642 | |
paul@23 | 643 | latest_timestamp = metadata.get("last-modified") |
paul@23 | 644 | if latest_timestamp: |
paul@23 | 645 | headers.append("Last-Modified: %s" % latest_timestamp.as_HTTP_datetime_string()) |
paul@23 | 646 | |
paul@23 | 647 | if status: |
paul@23 | 648 | headers.append("Status: %s" % status) |
paul@23 | 649 | |
paul@23 | 650 | send_headers(headers) |
paul@23 | 651 | |
paul@1 | 652 | # Page access functions. |
paul@1 | 653 | |
paul@1 | 654 | def getPageURL(page): |
paul@1 | 655 | |
paul@1 | 656 | "Return the URL of the given 'page'." |
paul@1 | 657 | |
paul@1 | 658 | request = page.request |
paul@1 | 659 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@1 | 660 | |
paul@1 | 661 | def getFormat(page): |
paul@1 | 662 | |
paul@1 | 663 | "Get the format used on the given 'page'." |
paul@1 | 664 | |
paul@1 | 665 | return page.pi["format"] |
paul@1 | 666 | |
paul@1 | 667 | def getMetadata(page): |
paul@1 | 668 | |
paul@1 | 669 | """ |
paul@1 | 670 | Return a dictionary containing items describing for the given 'page' the |
paul@1 | 671 | page's "created" time, "last-modified" time, "sequence" (or revision number) |
paul@1 | 672 | and the "last-comment" made about the last edit. |
paul@1 | 673 | """ |
paul@1 | 674 | |
paul@1 | 675 | request = page.request |
paul@1 | 676 | |
paul@1 | 677 | # Get the initial revision of the page. |
paul@1 | 678 | |
paul@1 | 679 | revisions = page.getRevList() |
paul@26 | 680 | |
paul@26 | 681 | if not revisions: |
paul@26 | 682 | return {} |
paul@26 | 683 | |
paul@1 | 684 | event_page_initial = Page(request, page.page_name, rev=revisions[-1]) |
paul@1 | 685 | |
paul@1 | 686 | # Get the created and last modified times. |
paul@1 | 687 | |
paul@1 | 688 | initial_revision = getPageRevision(event_page_initial) |
paul@1 | 689 | |
paul@1 | 690 | metadata = {} |
paul@1 | 691 | metadata["created"] = initial_revision["timestamp"] |
paul@1 | 692 | latest_revision = getPageRevision(page) |
paul@1 | 693 | metadata["last-modified"] = latest_revision["timestamp"] |
paul@1 | 694 | metadata["sequence"] = len(revisions) - 1 |
paul@1 | 695 | metadata["last-comment"] = latest_revision["comment"] |
paul@1 | 696 | |
paul@1 | 697 | return metadata |
paul@0 | 698 | |
paul@0 | 699 | def getPageRevision(page): |
paul@0 | 700 | |
paul@0 | 701 | "Return the revision details dictionary for the given 'page'." |
paul@0 | 702 | |
paul@0 | 703 | # From Page.edit_info... |
paul@0 | 704 | |
paul@0 | 705 | if hasattr(page, "editlog_entry"): |
paul@0 | 706 | line = page.editlog_entry() |
paul@0 | 707 | else: |
paul@0 | 708 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@0 | 709 | |
paul@0 | 710 | # Similar to Page.mtime_usecs behaviour... |
paul@0 | 711 | |
paul@0 | 712 | if line: |
paul@0 | 713 | timestamp = line.ed_time_usecs |
paul@0 | 714 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@0 | 715 | comment = line.comment |
paul@0 | 716 | else: |
paul@0 | 717 | mtime = 0 |
paul@0 | 718 | comment = "" |
paul@0 | 719 | |
paul@56 | 720 | # Give the time zone as UTC. |
paul@0 | 721 | |
paul@56 | 722 | return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + ("UTC",)), "comment" : comment} |
paul@0 | 723 | |
paul@11 | 724 | # Page parsing and formatting of embedded content. |
paul@11 | 725 | |
paul@15 | 726 | def getPageParserClass(request): |
paul@15 | 727 | |
paul@15 | 728 | "Using 'request', return a parser class for the current page's format." |
paul@15 | 729 | |
paul@16 | 730 | return getParserClass(request, getFormat(request.page)) |
paul@15 | 731 | |
paul@11 | 732 | def getParserClass(request, format): |
paul@11 | 733 | |
paul@11 | 734 | """ |
paul@11 | 735 | Return a parser class using the 'request' for the given 'format', returning |
paul@11 | 736 | a plain text parser if no parser can be found for the specified 'format'. |
paul@11 | 737 | """ |
paul@11 | 738 | |
paul@11 | 739 | try: |
paul@11 | 740 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@11 | 741 | except wikiutil.PluginMissingError: |
paul@11 | 742 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@11 | 743 | |
paul@15 | 744 | def getFormatterClass(request, format): |
paul@15 | 745 | |
paul@15 | 746 | """ |
paul@15 | 747 | Return a formatter class using the 'request' for the given output 'format', |
paul@15 | 748 | returning a plain text formatter if no formatter can be found for the |
paul@15 | 749 | specified 'format'. |
paul@15 | 750 | """ |
paul@15 | 751 | |
paul@15 | 752 | try: |
paul@15 | 753 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", format or "plain") |
paul@15 | 754 | except wikiutil.PluginMissingError: |
paul@15 | 755 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", "plain") |
paul@15 | 756 | |
paul@33 | 757 | def formatText(text, request, fmt, inhibit_p=True, parser_cls=None): |
paul@15 | 758 | |
paul@15 | 759 | """ |
paul@15 | 760 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@15 | 761 | Suppress line anchors in the output, and fix lists by indicating that a |
paul@15 | 762 | paragraph has already been started. |
paul@15 | 763 | """ |
paul@15 | 764 | |
paul@15 | 765 | if not parser_cls: |
paul@15 | 766 | parser_cls = getPageParserClass(request) |
paul@15 | 767 | parser = parser_cls(text, request, line_anchors=False) |
paul@15 | 768 | |
paul@15 | 769 | old_fmt = request.formatter |
paul@15 | 770 | request.formatter = fmt |
paul@15 | 771 | try: |
paul@58 | 772 | if isinstance(fmt, text_moin_wiki.Parser): |
paul@58 | 773 | return redirectedOutput(request, parser, fmt, inhibit_p=inhibit_p) |
paul@58 | 774 | else: |
paul@58 | 775 | return redirectedOutput(request, parser, fmt) |
paul@15 | 776 | finally: |
paul@15 | 777 | request.formatter = old_fmt |
paul@15 | 778 | |
paul@11 | 779 | def redirectedOutput(request, parser, fmt, **kw): |
paul@11 | 780 | |
paul@11 | 781 | "A fixed version of the request method of the same name." |
paul@11 | 782 | |
paul@11 | 783 | buf = StringIO() |
paul@11 | 784 | request.redirect(buf) |
paul@11 | 785 | try: |
paul@11 | 786 | parser.format(fmt, **kw) |
paul@11 | 787 | if hasattr(fmt, "flush"): |
paul@11 | 788 | buf.write(fmt.flush(True)) |
paul@11 | 789 | finally: |
paul@11 | 790 | request.redirect() |
paul@11 | 791 | text = buf.getvalue() |
paul@11 | 792 | buf.close() |
paul@11 | 793 | return text |
paul@11 | 794 | |
paul@46 | 795 | # Finding components for content types. |
paul@46 | 796 | |
paul@46 | 797 | def getParsersForContentType(cfg, mimetype): |
paul@46 | 798 | |
paul@46 | 799 | """ |
paul@46 | 800 | Find parsers that support the given 'mimetype', constructing a dictionary |
paul@46 | 801 | mapping content types to lists of parsers that is then cached in the 'cfg' |
paul@46 | 802 | object. A list of suitable parsers is returned for 'mimetype'. |
paul@46 | 803 | """ |
paul@46 | 804 | |
paul@46 | 805 | if not hasattr(cfg.cache, "MIMETYPE_TO_PARSER"): |
paul@46 | 806 | available = {} |
paul@46 | 807 | |
paul@46 | 808 | for name in wikiutil.getPlugins("parser", cfg): |
paul@46 | 809 | |
paul@46 | 810 | # Import each parser in order to inspect supported content types. |
paul@46 | 811 | |
paul@46 | 812 | try: |
paul@46 | 813 | parser_cls = wikiutil.importPlugin(cfg, "parser", name, "Parser") |
paul@46 | 814 | except wikiutil.PluginMissingError: |
paul@46 | 815 | continue |
paul@46 | 816 | |
paul@46 | 817 | # Attempt to determine supported content types. |
paul@46 | 818 | # NOTE: Extensions and /etc/mime.types (or equivalent) could also be |
paul@46 | 819 | # NOTE: used. |
paul@46 | 820 | |
paul@46 | 821 | if hasattr(parser_cls, "input_mimetypes"): |
paul@46 | 822 | for input_mimetype in parser_cls.input_mimetypes: |
paul@46 | 823 | if not available.has_key(input_mimetype): |
paul@46 | 824 | available[input_mimetype] = [] |
paul@46 | 825 | available[input_mimetype].append(parser_cls) |
paul@46 | 826 | |
paul@46 | 827 | # Support some basic parsers. |
paul@46 | 828 | |
paul@46 | 829 | elif name == "text_moin_wiki": |
paul@46 | 830 | available["text/moin-wiki"] = [parser_cls] |
paul@46 | 831 | available["text/moin"] = [parser_cls] |
paul@58 | 832 | elif name == "text_html": |
paul@58 | 833 | available["text/html"] = [parser_cls] |
paul@59 | 834 | available["application/xhtml+xml"] = [parser_cls] |
paul@46 | 835 | |
paul@46 | 836 | cfg.cache.MIMETYPE_TO_PARSER = available |
paul@46 | 837 | |
paul@46 | 838 | return cfg.cache.MIMETYPE_TO_PARSER.get(mimetype, []) |
paul@46 | 839 | |
paul@25 | 840 | # Textual representations. |
paul@25 | 841 | |
paul@25 | 842 | def getSimpleWikiText(text): |
paul@25 | 843 | |
paul@25 | 844 | """ |
paul@25 | 845 | Return the plain text representation of the given 'text' which may employ |
paul@25 | 846 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@25 | 847 | text. |
paul@25 | 848 | """ |
paul@25 | 849 | |
paul@25 | 850 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@25 | 851 | |
paul@25 | 852 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@25 | 853 | |
paul@25 | 854 | def getEncodedWikiText(text): |
paul@25 | 855 | |
paul@25 | 856 | "Encode the given 'text' in a verbatim representation." |
paul@25 | 857 | |
paul@25 | 858 | return "<<Verbatim(%s)>>" % text |
paul@25 | 859 | |
paul@25 | 860 | def getPrettyTitle(title): |
paul@25 | 861 | |
paul@25 | 862 | "Return a nicely formatted version of the given 'title'." |
paul@25 | 863 | |
paul@25 | 864 | return title.replace("_", " ").replace("/", u" ? ") |
paul@25 | 865 | |
paul@0 | 866 | # User interface functions. |
paul@0 | 867 | |
paul@0 | 868 | def getParameter(request, name, default=None): |
paul@0 | 869 | |
paul@0 | 870 | """ |
paul@0 | 871 | Using the given 'request', return the value of the parameter with the given |
paul@0 | 872 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@0 | 873 | in the 'request'. |
paul@0 | 874 | """ |
paul@0 | 875 | |
paul@0 | 876 | return get_form(request).get(name, [default])[0] |
paul@0 | 877 | |
paul@0 | 878 | def getQualifiedParameter(request, prefix, argname, default=None): |
paul@0 | 879 | |
paul@0 | 880 | """ |
paul@0 | 881 | Using the given 'request', 'prefix' and 'argname', retrieve the value of the |
paul@0 | 882 | qualified parameter, returning the optional 'default' (or None) if no value |
paul@0 | 883 | was supplied in the 'request'. |
paul@0 | 884 | """ |
paul@0 | 885 | |
paul@0 | 886 | argname = getQualifiedParameterName(prefix, argname) |
paul@0 | 887 | return getParameter(request, argname, default) |
paul@0 | 888 | |
paul@0 | 889 | def getQualifiedParameterName(prefix, argname): |
paul@0 | 890 | |
paul@0 | 891 | """ |
paul@0 | 892 | Return the qualified parameter name using the given 'prefix' and 'argname'. |
paul@0 | 893 | """ |
paul@0 | 894 | |
paul@27 | 895 | if not prefix: |
paul@0 | 896 | return argname |
paul@0 | 897 | else: |
paul@0 | 898 | return "%s-%s" % (prefix, argname) |
paul@0 | 899 | |
paul@0 | 900 | # Page-related functions. |
paul@0 | 901 | |
paul@0 | 902 | def getPrettyPageName(page): |
paul@0 | 903 | |
paul@0 | 904 | "Return a nicely formatted title/name for the given 'page'." |
paul@0 | 905 | |
paul@0 | 906 | title = page.split_title(force=1) |
paul@0 | 907 | return getPrettyTitle(title) |
paul@0 | 908 | |
paul@25 | 909 | def linkToPage(request, page, text, query_string=None, anchor=None, **kw): |
paul@0 | 910 | |
paul@0 | 911 | """ |
paul@0 | 912 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@25 | 913 | optional 'query_string' and 'anchor'. |
paul@0 | 914 | """ |
paul@0 | 915 | |
paul@0 | 916 | text = wikiutil.escape(text) |
paul@25 | 917 | return page.link_to_raw(request, text, query_string, anchor, **kw) |
paul@0 | 918 | |
paul@25 | 919 | def linkToResource(url, request, text, query_string=None, anchor=None): |
paul@0 | 920 | |
paul@0 | 921 | """ |
paul@0 | 922 | Using 'request', return a link to 'url' with the given link 'text' and |
paul@25 | 923 | optional 'query_string' and 'anchor'. |
paul@0 | 924 | """ |
paul@0 | 925 | |
paul@25 | 926 | if anchor: |
paul@25 | 927 | url += "#%s" % anchor |
paul@25 | 928 | |
paul@0 | 929 | if query_string: |
paul@0 | 930 | query_string = wikiutil.makeQueryString(query_string) |
paul@25 | 931 | url += "?%s" % query_string |
paul@0 | 932 | |
paul@0 | 933 | formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter |
paul@0 | 934 | |
paul@0 | 935 | output = [] |
paul@0 | 936 | output.append(formatter.url(1, url)) |
paul@0 | 937 | output.append(formatter.text(text)) |
paul@0 | 938 | output.append(formatter.url(0)) |
paul@0 | 939 | return "".join(output) |
paul@0 | 940 | |
paul@0 | 941 | def getFullPageName(parent, title): |
paul@0 | 942 | |
paul@0 | 943 | """ |
paul@0 | 944 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@0 | 945 | and 'title' (a simple page name). |
paul@0 | 946 | """ |
paul@0 | 947 | |
paul@0 | 948 | if parent: |
paul@0 | 949 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@0 | 950 | else: |
paul@0 | 951 | return title |
paul@0 | 952 | |
paul@37 | 953 | # Content storage support. |
paul@37 | 954 | |
paul@41 | 955 | class ItemStore(ItemDirectoryStore): |
paul@37 | 956 | |
paul@37 | 957 | "A page-specific item store." |
paul@37 | 958 | |
paul@47 | 959 | def __init__(self, page, item_dir="items", lock_dir="item_locks"): |
paul@37 | 960 | |
paul@37 | 961 | "Initialise an item store for the given 'page'." |
paul@37 | 962 | |
paul@47 | 963 | item_dir_path = tuple(item_dir.split("/")) |
paul@47 | 964 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@47 | 965 | ItemDirectoryStore.__init__(self, page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@40 | 966 | self.page = page |
paul@37 | 967 | |
paul@40 | 968 | def can_write(self): |
paul@40 | 969 | |
paul@40 | 970 | """ |
paul@40 | 971 | Return whether the user associated with the request can write to the |
paul@40 | 972 | page owning this store. |
paul@40 | 973 | """ |
paul@40 | 974 | |
paul@40 | 975 | user = self.page.request.user |
paul@40 | 976 | return user and user.may.write(self.page.page_name) |
paul@40 | 977 | |
paul@42 | 978 | def can_read(self): |
paul@42 | 979 | |
paul@42 | 980 | """ |
paul@42 | 981 | Return whether the user associated with the request can read from the |
paul@42 | 982 | page owning this store. |
paul@42 | 983 | """ |
paul@42 | 984 | |
paul@42 | 985 | user = self.page.request.user |
paul@42 | 986 | return user and user.may.read(self.page.page_name) |
paul@42 | 987 | |
paul@41 | 988 | # High-level methods. |
paul@41 | 989 | |
paul@41 | 990 | def append(self, item): |
paul@41 | 991 | |
paul@41 | 992 | "Append the given 'item' to the store." |
paul@41 | 993 | |
paul@41 | 994 | if not self.can_write(): |
paul@41 | 995 | return |
paul@41 | 996 | |
paul@41 | 997 | ItemDirectoryStore.append(self, item) |
paul@41 | 998 | |
paul@42 | 999 | def __len__(self): |
paul@42 | 1000 | |
paul@42 | 1001 | """ |
paul@42 | 1002 | Return the number of the next item (which should also be the number of |
paul@42 | 1003 | items). |
paul@42 | 1004 | """ |
paul@42 | 1005 | |
paul@42 | 1006 | if not self.can_read(): |
paul@42 | 1007 | return 0 |
paul@42 | 1008 | |
paul@42 | 1009 | return ItemDirectoryStore.__len__(self) |
paul@42 | 1010 | |
paul@42 | 1011 | def __getitem__(self, number): |
paul@42 | 1012 | |
paul@42 | 1013 | "Return the item with the given 'number'." |
paul@42 | 1014 | |
paul@42 | 1015 | if not self.can_read(): |
paul@42 | 1016 | raise IndexError, number |
paul@42 | 1017 | |
paul@42 | 1018 | return ItemDirectoryStore.__getitem__(self, number) |
paul@42 | 1019 | |
paul@0 | 1020 | # vim: tabstop=4 expandtab shiftwidth=4 |