/usr/lib/python2.7/dist-packages/formencode/htmlrename.py is in python-formencode 1.3.0-0ubuntu5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | """
Module to rename form fields
"""
from formencode.rewritingparser import RewritingParser
__all__ = ['rename', 'add_prefix']
def rename(form, rename_func):
"""
Rename all the form fields in the form (a string), using rename_func
rename_func will be called with one argument, the name of the
field, and should return a new name.
"""
p = RenamingParser(rename_func)
p.feed(form)
p.close()
return p.text()
def add_prefix(form, prefix, dotted=False):
"""
Add the given prefix to all the fields in the form.
If dotted is true, then add a dot between prefix and the previous
name. Empty fields will use the prefix as the name (with no dot).
"""
def rename_func(field_name):
if dotted:
if field_name:
return prefix + '.' + field_name
else:
return prefix
else:
return prefix + field_name
return rename(form, rename_func)
class RenamingParser(RewritingParser):
def __init__(self, rename_func):
RewritingParser.__init__(self)
self.rename_func = rename_func
def close(self):
self.handle_misc(None)
RewritingParser.close(self)
self._text = self._get_text()
def text(self):
try:
return self._text
except AttributeError:
raise Exception(
"You must .close() a parser instance before getting "
"the text from it")
def handle_starttag(self, tag, attrs, startend=False):
self.write_pos()
if tag in ('input', 'textarea', 'select'):
self.handle_field(tag, attrs, startend)
else:
return
def handle_startendtag(self, tag, attrs):
return self.handle_starttag(tag, attrs, True)
def handle_field(self, tag, attrs, startend):
name = self.get_attr(attrs, 'name', '')
new_name = self.rename_func(name)
if name is None:
self.del_attr(attrs, 'name')
else:
self.set_attr(attrs, 'name', new_name)
self.write_tag(tag, attrs)
self.skip_next = True
|