Changeset 15595 for main


Ignore:
Timestamp:
19 Sep 2019, 23:38:52 (5 years ago)
Author:
uli
Message:

Add a function to extract key/value pairs from

HTML forms.

Location:
main/waeup.kofa/trunk/src/waeup/kofa/utils
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.kofa/trunk/src/waeup/kofa/utils/helpers.py

    r14939 r15595  
    3030from cStringIO import StringIO
    3131from docutils.core import publish_string
     32from HTMLParser import HTMLParser
    3233from zope.component import getUtility
    3334from zope.component.interfaces import IFactory
     
    864865                contents=ReST2HTML(text))
    865866    return elements
     867
     868
     869
     870class FormVarParser(HTMLParser):
     871    """An HTML form parser that extracts keys and values.
     872
     873       Fed with an HTML document, we parse all starttags and check for each,
     874       whether it provides a `name` and a `value` attribute. If so, the
     875       values of the respective attributes are stored in instance var
     876       `form_vars` as a dict entry.
     877    """
     878
     879    def __init__(self):
     880        HTMLParser.__init__(self)  # old-style class - no super()
     881        self.form_vars = {}
     882
     883    def handle_starttag(self, tag, attrs):
     884        tag_attrs = {}
     885        for key, val in attrs:
     886            tag_attrs[key] = val
     887        if 'name' in tag_attrs and 'value' in tag_attrs:
     888            self.form_vars[tag_attrs['name']] = tag_attrs['value']
     889
     890
     891def extract_formvars(html_code):
     892    """Extract keys and values from an HTML form as dict.
     893
     894       No text, no values::
     895
     896         >>> extract_formvars("")
     897         {}
     898
     899       Simple input tags normally provide name and value::
     900
     901         >>> extract_formvars("<input type='text' name='foo' value='bar'>")
     902         {'foo': 'bar'}
     903
     904       The sample doc we stored in tests is a bit more difficult::
     905
     906         >>> html_path = os.path.join(os.path.dirname(__file__),
     907         ...                          'tests', 'sample_response.html')
     908         >>> html_code = open(html_path, 'r').read()
     909         >>> import pprint
     910         >>> pprint.pprint(extract_formvars(html_code))
     911         {'AMOUNT': '100',
     912         ...
     913          'TRANS_NUM': '01ESA20190916134824YA3YJ8'}
     914
     915    """
     916    result = {}
     917    parser = FormVarParser()
     918    parser.feed(html_code)
     919    return parser.form_vars
Note: See TracChangeset for help on using the changeset viewer.