paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinForms library |
paul@0 | 4 | |
paul@21 | 5 | @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 7 | """ |
paul@0 | 8 | |
paul@3 | 9 | from MoinMoin.action import do_show |
paul@8 | 10 | from MoinMoin.Page import Page |
paul@0 | 11 | from MoinMoin import wikiutil |
paul@0 | 12 | from MoinSupport import * |
paul@0 | 13 | import re |
paul@0 | 14 | |
paul@0 | 15 | __version__ = "0.1" |
paul@0 | 16 | |
paul@4 | 17 | form_field_regexp_str = r"<<Form(Field|Message)\((.*?)\)>>" |
paul@1 | 18 | form_field_regexp = re.compile(form_field_regexp_str, re.DOTALL) |
paul@1 | 19 | |
paul@3 | 20 | # Common action functionality. |
paul@3 | 21 | |
paul@3 | 22 | class MoinFormHandlerAction: |
paul@3 | 23 | |
paul@3 | 24 | "A handler action that can be specialised for individual forms." |
paul@3 | 25 | |
paul@3 | 26 | def __init__(self, pagename, request): |
paul@3 | 27 | self.pagename = pagename |
paul@3 | 28 | self.request = request |
paul@3 | 29 | |
paul@3 | 30 | def processForm(self): |
paul@3 | 31 | |
paul@3 | 32 | """ |
paul@3 | 33 | Interpret the request details and modify them according to the structure |
paul@3 | 34 | of the interpreted information. |
paul@3 | 35 | """ |
paul@3 | 36 | |
paul@13 | 37 | _ = self.request.getText |
paul@13 | 38 | |
paul@3 | 39 | # Get the form fields and obtain the hierarchical field structure. |
paul@3 | 40 | |
paul@3 | 41 | form = get_form(self.request) |
paul@3 | 42 | fields = getFields(form, remove=True) |
paul@3 | 43 | |
paul@8 | 44 | # Modify and validate the form. |
paul@3 | 45 | |
paul@3 | 46 | self.modifyFields(fields) |
paul@8 | 47 | |
paul@13 | 48 | # Get the form definition. |
paul@13 | 49 | |
paul@17 | 50 | structure = self.getFormStructure(fields) |
paul@13 | 51 | |
paul@13 | 52 | # Without any form definition, the page is probably the wrong one. |
paul@13 | 53 | |
paul@13 | 54 | if not structure: |
paul@13 | 55 | self.request.theme.add_msg(_("This page does not provide a form."), "error") |
paul@13 | 56 | do_show(self.pagename, self.request) |
paul@13 | 57 | return |
paul@13 | 58 | |
paul@13 | 59 | # With a form definition, attempt to validate the fields. |
paul@13 | 60 | |
paul@13 | 61 | if self.validateFields(fields, structure): |
paul@8 | 62 | self.finished(fields, form) |
paul@8 | 63 | else: |
paul@8 | 64 | self.unfinished(fields, form) |
paul@8 | 65 | |
paul@8 | 66 | def finished(self, fields, form): |
paul@8 | 67 | |
paul@8 | 68 | "Handle the finished 'fields' and 'form'." |
paul@8 | 69 | |
paul@8 | 70 | self.unfinished(fields, form) |
paul@8 | 71 | |
paul@8 | 72 | def unfinished(self, fields, form): |
paul@8 | 73 | |
paul@8 | 74 | "Handle the unfinished 'fields' and 'form'." |
paul@8 | 75 | |
paul@8 | 76 | # Serialise and show the form. |
paul@8 | 77 | |
paul@3 | 78 | self.serialiseFields(fields, form) |
paul@3 | 79 | do_show(self.pagename, self.request) |
paul@3 | 80 | |
paul@17 | 81 | def getFormStructure(self, fields): |
paul@8 | 82 | |
paul@13 | 83 | "Return the structure of the form being handled." |
paul@8 | 84 | |
paul@8 | 85 | text = Page(self.request, self.pagename).get_raw_body() |
paul@17 | 86 | text = getFormForFragment(text, fields.get("fragment", [None])[0]) |
paul@13 | 87 | return getFormStructure(text, self.request) |
paul@13 | 88 | |
paul@13 | 89 | def validateFields(self, fields, structure): |
paul@13 | 90 | |
paul@13 | 91 | """ |
paul@13 | 92 | Validate the given 'fields' using the given form 'structure', |
paul@13 | 93 | introducing error fields where the individual fields do not conform to |
paul@13 | 94 | their descriptions. |
paul@13 | 95 | """ |
paul@8 | 96 | |
paul@8 | 97 | return self.validateFieldsUsingStructure(fields, structure) |
paul@8 | 98 | |
paul@8 | 99 | def validateFieldsUsingStructure(self, fields, structure): |
paul@8 | 100 | |
paul@8 | 101 | "Validate the given 'fields' using the given 'structure'." |
paul@8 | 102 | |
paul@8 | 103 | _ = self.request.getText |
paul@8 | 104 | valid = True |
paul@8 | 105 | |
paul@8 | 106 | for key, definition in structure.items(): |
paul@8 | 107 | value = fields.get(key) |
paul@8 | 108 | |
paul@8 | 109 | # Enter form sections and validate them. |
paul@8 | 110 | |
paul@8 | 111 | if isinstance(definition, dict): |
paul@8 | 112 | if value: |
paul@8 | 113 | for element in getSectionElements(value): |
paul@11 | 114 | valid = self.validateFieldsUsingStructure(element, structure[key]) and valid |
paul@8 | 115 | |
paul@8 | 116 | # Validate individual fields. |
paul@8 | 117 | |
paul@8 | 118 | elif structure.has_key(key): |
paul@8 | 119 | path, dictpage, label, section, field_args, allowed_values = definition |
paul@8 | 120 | errors = [] |
paul@8 | 121 | |
paul@8 | 122 | # Test for obligatory values. |
paul@8 | 123 | |
paul@10 | 124 | if not value or not value[0]: |
paul@8 | 125 | if field_args.get("required"): |
paul@11 | 126 | |
paul@11 | 127 | # Detect new parts of the structure and avoid producing |
paul@11 | 128 | # premature error messages. |
paul@11 | 129 | |
paul@11 | 130 | if not fields.has_key("_new"): |
paul@11 | 131 | errors.append(_("This field must be filled out.")) |
paul@11 | 132 | else: |
paul@11 | 133 | valid = False |
paul@8 | 134 | else: |
paul@8 | 135 | # Test for unacceptable values. |
paul@8 | 136 | |
paul@8 | 137 | if allowed_values and set(value).difference(allowed_values): |
paul@8 | 138 | errors.append(_("At least one of the choices is not acceptable.")) |
paul@8 | 139 | |
paul@8 | 140 | # Test the number of values. |
paul@8 | 141 | |
paul@8 | 142 | if field_args.get("type") == "select": |
paul@8 | 143 | if field_args.has_key("maxselected"): |
paul@8 | 144 | if len(value) > int(field_args["maxselected"]): |
paul@8 | 145 | errors.append(_("Incorrect number of choices given: need %s.") % field_args["maxselected"]) |
paul@8 | 146 | |
paul@8 | 147 | if errors: |
paul@8 | 148 | fields["%s-error" % key] = errors |
paul@8 | 149 | valid = False |
paul@8 | 150 | |
paul@8 | 151 | return valid |
paul@4 | 152 | |
paul@3 | 153 | def serialiseFields(self, fields, form, path=None): |
paul@3 | 154 | |
paul@3 | 155 | """ |
paul@3 | 156 | Serialise the given 'fields' to the given 'form', using the given 'path' |
paul@3 | 157 | to name the entries. |
paul@3 | 158 | """ |
paul@3 | 159 | |
paul@3 | 160 | for key, value in fields.items(): |
paul@3 | 161 | |
paul@3 | 162 | # Serialise sections. |
paul@3 | 163 | |
paul@3 | 164 | if isinstance(value, dict): |
paul@3 | 165 | for index, element in enumerate(getSectionElements(value)): |
paul@3 | 166 | element_ref = "%s$%s" % (key, index) |
paul@3 | 167 | |
paul@3 | 168 | self.serialiseFields(element, form, |
paul@3 | 169 | path and ("%s/%s" % (path, element_ref)) or element_ref |
paul@3 | 170 | ) |
paul@3 | 171 | |
paul@3 | 172 | # Serialise fields. |
paul@3 | 173 | |
paul@3 | 174 | else: |
paul@3 | 175 | form[path and ("%s/%s" % (path, key)) or key] = value |
paul@3 | 176 | |
paul@3 | 177 | def modifyFields(self, fields): |
paul@3 | 178 | |
paul@3 | 179 | "Modify the given 'fields', removing and adding items." |
paul@3 | 180 | |
paul@3 | 181 | # First, remove fields. |
paul@3 | 182 | |
paul@3 | 183 | for key in fields.keys(): |
paul@3 | 184 | if key.startswith("_remove="): |
paul@3 | 185 | self.removeField(key[8:], fields) |
paul@3 | 186 | |
paul@3 | 187 | # Then, add fields. |
paul@3 | 188 | |
paul@3 | 189 | for key in fields.keys(): |
paul@3 | 190 | if key.startswith("_add="): |
paul@3 | 191 | self.addField(key[5:], fields) |
paul@3 | 192 | |
paul@3 | 193 | def removeField(self, path, fields): |
paul@3 | 194 | |
paul@3 | 195 | """ |
paul@3 | 196 | Remove the section element indicated by the given 'path' from the |
paul@3 | 197 | 'fields'. |
paul@3 | 198 | """ |
paul@3 | 199 | |
paul@3 | 200 | section, (name, index) = getSectionForPath(path, fields) |
paul@17 | 201 | try: |
paul@17 | 202 | del section[name][index] |
paul@17 | 203 | except KeyError: |
paul@17 | 204 | pass |
paul@3 | 205 | |
paul@3 | 206 | def addField(self, path, fields): |
paul@3 | 207 | |
paul@3 | 208 | """ |
paul@3 | 209 | Add a section element indicated by the given 'path' to the 'fields'. |
paul@3 | 210 | """ |
paul@3 | 211 | |
paul@3 | 212 | section, (name, index) = getSectionForPath(path, fields) |
paul@3 | 213 | placeholder = {"_new" : ""} |
paul@3 | 214 | |
paul@3 | 215 | if section.has_key(name): |
paul@3 | 216 | indexes = section[name].keys() |
paul@3 | 217 | max_index = max(map(int, indexes)) |
paul@3 | 218 | section[name][max_index + 1] = placeholder |
paul@3 | 219 | else: |
paul@3 | 220 | max_index = -1 |
paul@3 | 221 | section[name] = {0 : placeholder} |
paul@3 | 222 | |
paul@5 | 223 | # Form and field information. |
paul@5 | 224 | |
paul@8 | 225 | def getFormStructure(text, request, path=None, structure=None): |
paul@8 | 226 | |
paul@8 | 227 | """ |
paul@8 | 228 | For the given form 'text' and using the 'request', return details of the |
paul@8 | 229 | form for the section at the given 'path' (or the entire form if 'path' is |
paul@8 | 230 | omitted), populating the given 'structure' (or populating a new structure if |
paul@8 | 231 | 'structure' is omitted). |
paul@8 | 232 | """ |
paul@8 | 233 | |
paul@8 | 234 | if structure is None: |
paul@8 | 235 | structure = {} |
paul@8 | 236 | |
paul@8 | 237 | for format, attributes, body in getFragments(text, True): |
paul@8 | 238 | |
paul@8 | 239 | # Get field details at the current level. |
paul@8 | 240 | |
paul@8 | 241 | if format is None: |
paul@8 | 242 | structure.update(getFormFields(body, path, request)) |
paul@8 | 243 | |
paul@8 | 244 | # Where a section is found, get details from within the section. |
paul@8 | 245 | |
paul@11 | 246 | elif format == "form": |
paul@11 | 247 | if attributes.has_key("section"): |
paul@11 | 248 | section_name = attributes["section"] |
paul@11 | 249 | section = structure[section_name] = {} |
paul@11 | 250 | getFormStructure(body, request, path and ("%s/%s" % (path, section_name)) or section_name, section) |
paul@11 | 251 | elif attributes.has_key("message"): |
paul@11 | 252 | getFormStructure(body, request, path, structure) |
paul@12 | 253 | elif attributes.has_key("not-message"): |
paul@12 | 254 | getFormStructure(body, request, path, structure) |
paul@8 | 255 | |
paul@8 | 256 | # Get field details from other kinds of region. |
paul@8 | 257 | |
paul@8 | 258 | elif format != "form": |
paul@8 | 259 | getFormStructure(body, request, path, structure) |
paul@8 | 260 | |
paul@8 | 261 | return structure |
paul@8 | 262 | |
paul@8 | 263 | def getFormForFragment(text, fragment=None): |
paul@8 | 264 | |
paul@8 | 265 | """ |
paul@8 | 266 | Return the form region from the given 'text' for the specified 'fragment'. |
paul@8 | 267 | If no fragment is specified, the first form region is returned. |
paul@8 | 268 | """ |
paul@8 | 269 | |
paul@8 | 270 | for format, attributes, body in getFragments(text): |
paul@8 | 271 | if not fragment or attributes.get("fragment") == fragment: |
paul@8 | 272 | return body |
paul@8 | 273 | |
paul@8 | 274 | return None |
paul@8 | 275 | |
paul@5 | 276 | def getFieldArguments(field_definition): |
paul@5 | 277 | |
paul@5 | 278 | "Return the parsed arguments from the given 'field_definition' string." |
paul@5 | 279 | |
paul@5 | 280 | field_args = {} |
paul@5 | 281 | |
paul@5 | 282 | for field_arg in field_definition.split(): |
paul@8 | 283 | if field_arg == "required": |
paul@8 | 284 | field_args[field_arg] = True |
paul@8 | 285 | continue |
paul@5 | 286 | |
paul@5 | 287 | # Record the key-value details. |
paul@5 | 288 | |
paul@5 | 289 | try: |
paul@5 | 290 | argname, argvalue = field_arg.split("=", 1) |
paul@5 | 291 | field_args[argname] = argvalue |
paul@5 | 292 | |
paul@5 | 293 | # Single keywords are interpreted as type descriptions. |
paul@5 | 294 | |
paul@5 | 295 | except ValueError: |
paul@5 | 296 | if not field_args.has_key("type"): |
paul@5 | 297 | field_args["type"] = field_arg |
paul@5 | 298 | |
paul@5 | 299 | return field_args |
paul@5 | 300 | |
paul@0 | 301 | # Common formatting functions. |
paul@0 | 302 | |
paul@18 | 303 | def getFormOutput(text, fields, path=None, repeating=None, index=None): |
paul@0 | 304 | |
paul@0 | 305 | """ |
paul@0 | 306 | Combine regions found in the given 'text' and then return them as a single |
paul@0 | 307 | block. The reason for doing this, as opposed to just passing each region to |
paul@0 | 308 | a suitable parser for formatting, is that form sections may break up |
paul@0 | 309 | regions, and such sections may not define separate subregions but instead |
paul@0 | 310 | act as a means of conditional inclusion of text into an outer region. |
paul@0 | 311 | |
paul@0 | 312 | The given 'fields' are used to populate fields provided in forms and to |
paul@0 | 313 | control whether sections are populated or not. |
paul@18 | 314 | |
paul@18 | 315 | The optional 'path' is used to adjust form fields to refer to the correct |
paul@18 | 316 | part of the form hierarchy. |
paul@18 | 317 | |
paul@18 | 318 | The optional 'repeating' and 'index' is used to refer to individual values |
paul@18 | 319 | of a designated field. |
paul@0 | 320 | """ |
paul@0 | 321 | |
paul@0 | 322 | output = [] |
paul@0 | 323 | section = fields |
paul@0 | 324 | |
paul@0 | 325 | for region in getRegions(text, True): |
paul@0 | 326 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@0 | 327 | |
paul@6 | 328 | # Adjust any FormField macros to use hierarchical names. |
paul@0 | 329 | |
paul@0 | 330 | if format is None: |
paul@18 | 331 | output.append((path or repeating) and |
paul@18 | 332 | adjustFormFields(body, path, repeating, index) or body) |
paul@0 | 333 | |
paul@0 | 334 | # Include form sections only if fields exist for those sections. |
paul@0 | 335 | |
paul@0 | 336 | elif format == "form": |
paul@0 | 337 | section_name = attributes.get("section") |
paul@10 | 338 | message_name = attributes.get("message") |
paul@12 | 339 | absent_message_name = attributes.get("not-message") |
paul@10 | 340 | |
paul@10 | 341 | # Sections are groups of fields in their own namespace. |
paul@10 | 342 | |
paul@0 | 343 | if section_name and section.has_key(section_name): |
paul@1 | 344 | |
paul@1 | 345 | # Iterate over the section contents ignoring the given indexes. |
paul@1 | 346 | |
paul@1 | 347 | for index, element in enumerate(getSectionElements(section[section_name])): |
paul@6 | 348 | element_ref = "%s$%s" % (section_name, index) |
paul@1 | 349 | |
paul@6 | 350 | # Get the output for the section. |
paul@6 | 351 | |
paul@6 | 352 | output.append(getFormOutput(body, element, |
paul@6 | 353 | path and ("%s/%s" % (path, element_ref)) or element_ref)) |
paul@0 | 354 | |
paul@10 | 355 | # Message regions are conditional on a particular field and |
paul@10 | 356 | # reference the current namespace. |
paul@10 | 357 | |
paul@10 | 358 | elif message_name and section.has_key(message_name): |
paul@18 | 359 | |
paul@18 | 360 | if attributes.get("repeating"): |
paul@18 | 361 | for index in range(0, len(section[message_name])): |
paul@18 | 362 | output.append(getFormOutput(body, section, path, message_name, index)) |
paul@18 | 363 | else: |
paul@18 | 364 | output.append(getFormOutput(body, section, path)) |
paul@10 | 365 | |
paul@12 | 366 | # Not-message regions are conditional on a particular field being |
paul@12 | 367 | # absent. They reference the current namespace. |
paul@12 | 368 | |
paul@12 | 369 | elif absent_message_name and not section.has_key(absent_message_name): |
paul@12 | 370 | output.append(getFormOutput(body, section, path)) |
paul@12 | 371 | |
paul@0 | 372 | # Inspect and include other regions. |
paul@0 | 373 | |
paul@0 | 374 | else: |
paul@0 | 375 | output.append(header) |
paul@18 | 376 | output.append(getFormOutput(body, section, path, repeating, index)) |
paul@0 | 377 | output.append(close) |
paul@0 | 378 | |
paul@0 | 379 | return "".join(output) |
paul@0 | 380 | |
paul@8 | 381 | def getFormFields(body, path, request): |
paul@8 | 382 | |
paul@8 | 383 | "Return a dictionary of fields from the given 'body' at the given 'path'." |
paul@8 | 384 | |
paul@8 | 385 | fields = {} |
paul@8 | 386 | cache = {} |
paul@8 | 387 | type = None |
paul@8 | 388 | |
paul@8 | 389 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@8 | 390 | state = i % 3 |
paul@8 | 391 | |
paul@8 | 392 | if state == 1: |
paul@8 | 393 | type = match |
paul@8 | 394 | elif state == 2 and type == "Field": |
paul@8 | 395 | args = {} |
paul@8 | 396 | |
paul@8 | 397 | # Obtain the macro arguments, adjusted to consider the path. |
paul@8 | 398 | |
paul@8 | 399 | name, path, dictpage, label, section = \ |
paul@8 | 400 | getMacroArguments(adjustMacroArguments(parseMacroArguments(match), path)) |
paul@8 | 401 | |
paul@8 | 402 | # Obtain field information from the cache, if possible. |
paul@8 | 403 | |
paul@8 | 404 | cache_key = (name, dictpage) |
paul@8 | 405 | |
paul@8 | 406 | if cache.has_key(cache_key): |
paul@8 | 407 | field_args, allowed_values = cache[cache_key] |
paul@8 | 408 | |
paul@8 | 409 | # Otherwise, obtain field information from any WikiDict. |
paul@8 | 410 | |
paul@8 | 411 | else: |
paul@8 | 412 | field_args = {} |
paul@8 | 413 | allowed_values = None |
paul@8 | 414 | |
paul@8 | 415 | if dictpage: |
paul@8 | 416 | wikidict = getWikiDict(dictpage, request) |
paul@8 | 417 | if wikidict: |
paul@8 | 418 | field_definition = wikidict.get(name) |
paul@8 | 419 | if field_definition: |
paul@8 | 420 | field_args = getFieldArguments(field_definition) |
paul@8 | 421 | if field_args.has_key("source"): |
paul@8 | 422 | sourcedict = getWikiDict(field_args["source"], request) |
paul@8 | 423 | if sourcedict: |
paul@8 | 424 | allowed_values = sourcedict.keys() |
paul@8 | 425 | |
paul@8 | 426 | cache[cache_key] = field_args, allowed_values |
paul@8 | 427 | |
paul@8 | 428 | # Store the field information. |
paul@8 | 429 | |
paul@8 | 430 | fields[name] = path, dictpage, label, section, field_args, allowed_values |
paul@8 | 431 | |
paul@8 | 432 | return fields |
paul@8 | 433 | |
paul@18 | 434 | def adjustFormFields(body, path, repeating=None, index=None): |
paul@1 | 435 | |
paul@1 | 436 | """ |
paul@1 | 437 | Return a version of the 'body' with the names in FormField macros updated to |
paul@18 | 438 | incorporate the given 'path'. If 'repeating' is specified, any field with |
paul@18 | 439 | such a name will be adjusted to reference the value with the given 'index'. |
paul@1 | 440 | """ |
paul@1 | 441 | |
paul@1 | 442 | result = [] |
paul@4 | 443 | type = None |
paul@1 | 444 | |
paul@7 | 445 | for i, match in enumerate(form_field_regexp.split(body)): |
paul@7 | 446 | state = i % 3 |
paul@4 | 447 | |
paul@4 | 448 | # Reproduce normal text as is. |
paul@4 | 449 | |
paul@7 | 450 | if state == 0: |
paul@1 | 451 | result.append(match) |
paul@4 | 452 | |
paul@4 | 453 | # Capture the macro type. |
paul@4 | 454 | |
paul@7 | 455 | elif state == 1: |
paul@4 | 456 | type = match |
paul@4 | 457 | |
paul@4 | 458 | # Substitute the macro and modified arguments. |
paul@4 | 459 | |
paul@1 | 460 | else: |
paul@4 | 461 | result.append("<<Form%s(%s)>>" % (type, ",".join( |
paul@18 | 462 | adjustMacroArguments(parseMacroArguments(match), path, repeating, index) |
paul@4 | 463 | ))) |
paul@1 | 464 | |
paul@1 | 465 | return "".join(result) |
paul@1 | 466 | |
paul@18 | 467 | def adjustMacroArguments(args, path, repeating=None, index=None): |
paul@0 | 468 | |
paul@0 | 469 | """ |
paul@1 | 470 | Adjust the given 'args' so that the path incorporates the given |
paul@6 | 471 | 'path', returning a new list containing the revised path and remaining |
paul@18 | 472 | arguments. If 'repeating' is specified, any field with such a name will be |
paul@18 | 473 | adjusted to reference the value with the given 'index'. |
paul@1 | 474 | """ |
paul@1 | 475 | |
paul@18 | 476 | if not path and not repeating: |
paul@8 | 477 | return args |
paul@8 | 478 | |
paul@1 | 479 | result = [] |
paul@6 | 480 | old_path = None |
paul@18 | 481 | found_name = None |
paul@1 | 482 | |
paul@1 | 483 | for arg in args: |
paul@1 | 484 | if arg.startswith("path="): |
paul@6 | 485 | old_path = arg[5:] |
paul@1 | 486 | else: |
paul@1 | 487 | result.append(arg) |
paul@18 | 488 | if arg.startswith("name="): |
paul@18 | 489 | found_name = arg[5:] |
paul@18 | 490 | elif found_name is None: |
paul@18 | 491 | found_name = arg |
paul@1 | 492 | |
paul@18 | 493 | if path: |
paul@18 | 494 | qualified = old_path and ("%s/%s" % (old_path, path)) or path |
paul@18 | 495 | result.append("path=%s" % qualified) |
paul@18 | 496 | |
paul@18 | 497 | if repeating and repeating == found_name: |
paul@18 | 498 | result.append("index=%s" % index) |
paul@1 | 499 | |
paul@1 | 500 | return result |
paul@1 | 501 | |
paul@1 | 502 | def parseMacroArguments(args): |
paul@1 | 503 | |
paul@1 | 504 | """ |
paul@21 | 505 | Interpret the arguments. To support commas in labels, the label argument |
paul@21 | 506 | should be quoted. For example: |
paul@21 | 507 | |
paul@21 | 508 | "label=No, thanks!" |
paul@1 | 509 | """ |
paul@1 | 510 | |
paul@1 | 511 | try: |
paul@1 | 512 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@1 | 513 | except AttributeError: |
paul@1 | 514 | parsed_args = args.split(",") |
paul@1 | 515 | |
paul@1 | 516 | return [arg for arg in parsed_args if arg] |
paul@1 | 517 | |
paul@8 | 518 | def getMacroArguments(parsed_args): |
paul@8 | 519 | |
paul@8 | 520 | "Return the macro arguments decoded from 'parsed_args'." |
paul@8 | 521 | |
paul@8 | 522 | name = None |
paul@8 | 523 | path = None |
paul@8 | 524 | dictpage = None |
paul@8 | 525 | label = None |
paul@8 | 526 | section = None |
paul@8 | 527 | |
paul@8 | 528 | for arg in parsed_args: |
paul@8 | 529 | if arg.startswith("name="): |
paul@8 | 530 | name = arg[5:] |
paul@8 | 531 | |
paul@8 | 532 | elif arg.startswith("path="): |
paul@8 | 533 | path = arg[5:] |
paul@8 | 534 | |
paul@8 | 535 | elif arg.startswith("dict="): |
paul@8 | 536 | dictpage = arg[5:] |
paul@8 | 537 | |
paul@8 | 538 | elif arg.startswith("label="): |
paul@8 | 539 | label = arg[6:] |
paul@8 | 540 | |
paul@8 | 541 | elif arg.startswith("section="): |
paul@8 | 542 | section = arg[8:] |
paul@8 | 543 | |
paul@8 | 544 | elif name is None: |
paul@8 | 545 | name = arg |
paul@8 | 546 | |
paul@8 | 547 | elif dictpage is None: |
paul@8 | 548 | dictpage = arg |
paul@8 | 549 | |
paul@8 | 550 | return name, path, dictpage, label, section |
paul@8 | 551 | |
paul@1 | 552 | def getFields(d, remove=False): |
paul@1 | 553 | |
paul@1 | 554 | """ |
paul@1 | 555 | Return the form fields hierarchy for the given dictionary 'd'. If the |
paul@1 | 556 | optional 'remove' parameter is set to a true value, remove the entries for |
paul@1 | 557 | the fields from 'd'. |
paul@0 | 558 | """ |
paul@0 | 559 | |
paul@0 | 560 | fields = {} |
paul@0 | 561 | |
paul@0 | 562 | for key, value in d.items(): |
paul@0 | 563 | |
paul@1 | 564 | # Detect modifying fields. |
paul@1 | 565 | |
paul@1 | 566 | if key.find("=") != -1: |
paul@1 | 567 | fields[key] = value |
paul@1 | 568 | if remove: |
paul@1 | 569 | del d[key] |
paul@1 | 570 | continue |
paul@1 | 571 | |
paul@0 | 572 | # Reproduce the original hierarchy of the fields. |
paul@0 | 573 | |
paul@0 | 574 | section = fields |
paul@1 | 575 | parts = getPathDetails(key) |
paul@0 | 576 | |
paul@1 | 577 | for name, index in parts[:-1]: |
paul@1 | 578 | |
paul@1 | 579 | # Add an entry for instances of the section. |
paul@0 | 580 | |
paul@0 | 581 | if not section.has_key(name): |
paul@0 | 582 | section[name] = {} |
paul@0 | 583 | |
paul@1 | 584 | # Add an entry for the specific instance of the section. |
paul@1 | 585 | |
paul@0 | 586 | if not section[name].has_key(index): |
paul@0 | 587 | section[name][index] = {} |
paul@0 | 588 | |
paul@0 | 589 | section = section[name][index] |
paul@0 | 590 | |
paul@1 | 591 | section[parts[-1][0]] = value |
paul@1 | 592 | |
paul@1 | 593 | if remove: |
paul@1 | 594 | del d[key] |
paul@0 | 595 | |
paul@0 | 596 | return fields |
paul@0 | 597 | |
paul@1 | 598 | def getPathDetails(path): |
paul@4 | 599 | |
paul@4 | 600 | """ |
paul@4 | 601 | Return the given 'path' as a list of (name, index) tuples providing details |
paul@4 | 602 | of section instances, with any specific field appearing as the last element |
paul@4 | 603 | and having the form (name, None). |
paul@4 | 604 | """ |
paul@4 | 605 | |
paul@1 | 606 | parts = [] |
paul@1 | 607 | |
paul@1 | 608 | for part in path.split("/"): |
paul@1 | 609 | try: |
paul@1 | 610 | name, index = part.split("$", 1) |
paul@1 | 611 | index = int(index) |
paul@1 | 612 | except ValueError: |
paul@1 | 613 | name, index = part, None |
paul@1 | 614 | |
paul@1 | 615 | parts.append((name, index)) |
paul@1 | 616 | |
paul@1 | 617 | return parts |
paul@1 | 618 | |
paul@1 | 619 | def getSectionForPath(path, fields): |
paul@1 | 620 | |
paul@1 | 621 | """ |
paul@1 | 622 | Obtain the section indicated by the given 'path' from the 'fields', |
paul@1 | 623 | returning a tuple of the form (parent section, (name, index)), where the |
paul@1 | 624 | parent section contains the referenced section, where name is the name of |
paul@1 | 625 | the referenced section, and where index, if not None, is the index of a |
paul@1 | 626 | specific section instance within the named section. |
paul@1 | 627 | """ |
paul@1 | 628 | |
paul@1 | 629 | parts = getPathDetails(path) |
paul@1 | 630 | section = fields |
paul@1 | 631 | |
paul@1 | 632 | for name, index in parts[:-1]: |
paul@1 | 633 | section = fields[name][index] |
paul@1 | 634 | |
paul@1 | 635 | return section, parts[-1] |
paul@1 | 636 | |
paul@1 | 637 | def getSectionElements(section_elements): |
paul@1 | 638 | |
paul@1 | 639 | "Return the given 'section_elements' as an ordered collection." |
paul@1 | 640 | |
paul@1 | 641 | keys = map(int, section_elements.keys()) |
paul@1 | 642 | keys.sort() |
paul@1 | 643 | |
paul@1 | 644 | elements = [] |
paul@1 | 645 | |
paul@1 | 646 | for key in keys: |
paul@1 | 647 | elements.append(section_elements[key]) |
paul@1 | 648 | |
paul@1 | 649 | return elements |
paul@1 | 650 | |
paul@9 | 651 | # Parser-related formatting functions. |
paul@9 | 652 | |
paul@9 | 653 | def formatForm(text, request, fmt, attrs=None, write=None): |
paul@9 | 654 | |
paul@9 | 655 | """ |
paul@9 | 656 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@9 | 657 | The optional 'attrs' can be used to control the presentation of the form. |
paul@9 | 658 | |
paul@9 | 659 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@9 | 660 | write output using the request. |
paul@9 | 661 | """ |
paul@9 | 662 | |
paul@9 | 663 | write = write or request.write |
paul@9 | 664 | page = request.page |
paul@9 | 665 | |
paul@9 | 666 | fields = getFields(get_form(request)) |
paul@9 | 667 | |
paul@9 | 668 | queryparams = [] |
paul@9 | 669 | |
paul@16 | 670 | for argname, default in [("fragment", None), ("action", "MoinFormHandler")]: |
paul@9 | 671 | if attrs and attrs.has_key(argname): |
paul@9 | 672 | queryparams.append("%s=%s" % (argname, attrs[argname])) |
paul@14 | 673 | elif default: |
paul@14 | 674 | queryparams.append("%s=%s" % (argname, default)) |
paul@9 | 675 | |
paul@9 | 676 | querystr = "&".join(queryparams) |
paul@17 | 677 | fragment = attrs.get("fragment") |
paul@9 | 678 | |
paul@17 | 679 | write(fmt.rawHTML('<form method="post" action="%s%s"%s>' % ( |
paul@14 | 680 | escattr(page.url(request, querystr)), |
paul@17 | 681 | fragment and ("#%s" % escattr(fragment)) or "", |
paul@17 | 682 | fragment and (' id="%s"' % escattr(fragment)) or "" |
paul@14 | 683 | ))) |
paul@9 | 684 | |
paul@9 | 685 | # Obtain page text for the form, incorporating subregions and applicable |
paul@9 | 686 | # sections. |
paul@9 | 687 | |
paul@9 | 688 | output = getFormOutput(text, fields) |
paul@9 | 689 | write(formatText(output, request, fmt, inhibit_p=False)) |
paul@9 | 690 | |
paul@9 | 691 | write(fmt.rawHTML('</form>')) |
paul@9 | 692 | |
paul@0 | 693 | def formatFormForOutputType(text, request, mimetype, attrs=None, write=None): |
paul@0 | 694 | |
paul@0 | 695 | """ |
paul@0 | 696 | Format the given 'text' using the specified 'request' for the given output |
paul@0 | 697 | 'mimetype'. |
paul@0 | 698 | |
paul@0 | 699 | The optional 'attrs' can be used to control the presentation of the form. |
paul@0 | 700 | |
paul@0 | 701 | If the 'write' parameter is specified, use it to write output; otherwise, |
paul@0 | 702 | write output using the request. |
paul@0 | 703 | """ |
paul@0 | 704 | |
paul@0 | 705 | write = write or request.write |
paul@0 | 706 | |
paul@0 | 707 | if mimetype == "text/html": |
paul@0 | 708 | write('<html>') |
paul@0 | 709 | write('<body>') |
paul@0 | 710 | fmt = request.html_formatter |
paul@0 | 711 | fmt.setPage(request.page) |
paul@0 | 712 | formatForm(text, request, fmt, attrs, write) |
paul@0 | 713 | write('</body>') |
paul@0 | 714 | write('</html>') |
paul@0 | 715 | |
paul@0 | 716 | # vim: tabstop=4 expandtab shiftwidth=4 |