diff --git a/src/calibre/web/jsbrowser/forms.py b/src/calibre/web/jsbrowser/forms.py index 98ee7f0b84..e415fef05d 100644 --- a/src/calibre/web/jsbrowser/forms.py +++ b/src/calibre/web/jsbrowser/forms.py @@ -85,6 +85,15 @@ class SelectControl(RadioControl): class Form(object): + ''' + Provides dictionary like access to all the controls in a form. + For example:: + form['username'] = 'some name' + form['password'] = 'password' + + See also the :attr:`controls` property and the :meth:`submit_control` method. + ''' + def __init__(self, qwe): self.qwe = qwe self.attributes = {unicode(x):unicode(qwe.attribute(x)) for x in @@ -97,6 +106,16 @@ class Form(object): selects = list(map(SelectControl, qwe.findAll('select'))) self.select_controls = {x.name:x for x in selects} + @property + def controls(self): + for x in self.input_controls: + if x.name: + yield x.name + for x in (self.radio_controls, self.select_controls): + for n in x.iterkeys(): + if n: + yield n + def __getitem__(self, key): for x in self.input_controls: if key == x.name: @@ -108,6 +127,21 @@ class Form(object): continue raise KeyError('No control with the name %s in this form'%key) + def __setitem__(self, key, val): + control = None + for x in self.input_controls: + if key == x.name: + control = x + break + if control is None: + for x in (self.radio_controls, self.select_controls): + control = x.get(key, None) + if control is not None: + break + if control is None: + raise KeyError('No control with the name %s in this form'%key) + control.value = val + def __repr__(self): attrs = ['%s=%s'%(k, v) for k, v in self.attributes.iteritems()] return '
'%(' '.join(attrs)) diff --git a/src/calibre/web/jsbrowser/test.py b/src/calibre/web/jsbrowser/test.py index a421508926..fbafad3f0c 100644 --- a/src/calibre/web/jsbrowser/test.py +++ b/src/calibre/web/jsbrowser/test.py @@ -106,7 +106,7 @@ class Test(unittest.TestCase): } f = self.browser.select_form('#controls_test') for k, vals in values.iteritems(): - f[k].value = vals[0] + f[k] = vals[0] self.browser.submit() dat = self.server.form_data for k, vals in values.iteritems():