Compare commits
4 Commits
6f6d2192da
...
3001caf7ce
Author | SHA1 | Date | |
---|---|---|---|
3001caf7ce | |||
309a9b678e | |||
c6dbcda947 | |||
fe3dad39b5 |
@ -1,6 +1,6 @@
|
|||||||
[metadata]
|
[metadata]
|
||||||
name = yamlhttpforms
|
name = yamlhttpforms
|
||||||
version = 0.0.3
|
version = 0.0.6
|
||||||
author = Daniil F.
|
author = Daniil F.
|
||||||
author_email = mail@placeholder123.to
|
author_email = mail@placeholder123.to
|
||||||
description = HTTP forms defined in YAML
|
description = HTTP forms defined in YAML
|
||||||
|
@ -28,9 +28,13 @@ class FormField:
|
|||||||
for attr in function.split('.'):
|
for attr in function.split('.'):
|
||||||
obj = getattr(obj, attr)
|
obj = getattr(obj, attr)
|
||||||
self._default = obj
|
self._default = obj
|
||||||
|
elif default is None:
|
||||||
|
self._default = None
|
||||||
else:
|
else:
|
||||||
self._default = default
|
self._default = str(default)
|
||||||
self.options: Optional[OptionsT] = options
|
self.options: Optional[OptionsT] = None
|
||||||
|
if options is not None:
|
||||||
|
self.options = {str(k): str(v) for k, v in options.items()}
|
||||||
self.required: bool = required
|
self.required: bool = required
|
||||||
|
|
||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
|
import sys
|
||||||
from typing import Dict, TYPE_CHECKING
|
from typing import Dict, TYPE_CHECKING
|
||||||
|
|
||||||
from bs4.element import Tag
|
from bs4.element import Tag
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
@ -8,6 +10,8 @@ if TYPE_CHECKING:
|
|||||||
INPUT, SELECT, OPTION = 'input', 'select', 'option'
|
INPUT, SELECT, OPTION = 'input', 'select', 'option'
|
||||||
NAME, VALUE, SELECTED = 'name', 'value', 'selected'
|
NAME, VALUE, SELECTED = 'name', 'value', 'selected'
|
||||||
|
|
||||||
|
NON_PRINTABLE_TO_NONE = {code: None for code in range(sys.maxunicode + 1) if not chr(code).isprintable()}
|
||||||
|
|
||||||
|
|
||||||
class WrongInterface(Exception):
|
class WrongInterface(Exception):
|
||||||
pass
|
pass
|
||||||
@ -29,6 +33,10 @@ class UnknownField(WrongInterface):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def printable_only(string: str) -> str:
|
||||||
|
return string.translate(NON_PRINTABLE_TO_NONE)
|
||||||
|
|
||||||
|
|
||||||
def check_select_field_options(field_tag: Tag, field_interface: 'FormField', check_defaults: bool = True) -> None:
|
def check_select_field_options(field_tag: Tag, field_interface: 'FormField', check_defaults: bool = True) -> None:
|
||||||
"""
|
"""
|
||||||
Compares the `options` and `default` attributes of a `'FormField'` object with the options of its HTML counterpart.
|
Compares the `options` and `default` attributes of a `'FormField'` object with the options of its HTML counterpart.
|
||||||
@ -45,13 +53,23 @@ def check_select_field_options(field_tag: Tag, field_interface: 'FormField', che
|
|||||||
`FieldInterfaceWrong`
|
`FieldInterfaceWrong`
|
||||||
if the `default` is not equal to the value of the <option> tag which has the `selected` attribute
|
if the `default` is not equal to the value of the <option> tag which has the `selected` attribute
|
||||||
"""
|
"""
|
||||||
options = {tag[VALUE]: tag.get_text(strip=True) for tag in field_tag.find_all(OPTION)}
|
html_options = {(tag[VALUE], printable_only(tag.get_text(strip=True))) for tag in field_tag.find_all(OPTION)}
|
||||||
if options != field_interface.options:
|
interface_options = set(field_interface.options.items())
|
||||||
raise SelectOptionsWrong(f"Wrong options in {field_interface}")
|
missing_in_interface = html_options - interface_options
|
||||||
|
not_in_html = interface_options - html_options
|
||||||
|
s = ""
|
||||||
|
if missing_in_interface:
|
||||||
|
s += "\nThe following <options> HTML tags were not found in the interface:\n"
|
||||||
|
s += "\n".join(str(tup) for tup in missing_in_interface)
|
||||||
|
if not_in_html:
|
||||||
|
s += "\nThe following options were defined, but are not present in the HTML:\n"
|
||||||
|
s += "\n".join(str(tup) for tup in not_in_html)
|
||||||
|
if s:
|
||||||
|
raise SelectOptionsWrong(f"Wrong options in field '{field_interface.name}'." + s)
|
||||||
if check_defaults:
|
if check_defaults:
|
||||||
default = field_tag.find(lambda tag: tag.name == OPTION and tag.has_attr(SELECTED))[VALUE]
|
default_option = field_tag.find(lambda tag: tag.name == OPTION and tag.has_attr(SELECTED))
|
||||||
if default != field_interface.default:
|
if default_option is not None and default_option[VALUE] != field_interface.default:
|
||||||
raise FieldInterfaceWrong(f"Default option '{default}' missing for {field_interface}")
|
raise FieldInterfaceWrong(f"Default option '{default_option[VALUE]}' missing for {field_interface}")
|
||||||
|
|
||||||
|
|
||||||
def check_field_interface(field_tag: Tag, field_interface: 'FormField', check_defaults: bool = True) -> None:
|
def check_field_interface(field_tag: Tag, field_interface: 'FormField', check_defaults: bool = True) -> None:
|
||||||
@ -99,7 +117,7 @@ def check_form_interface(form_tag: Tag, form_interface: 'Form', check_defaults:
|
|||||||
"""
|
"""
|
||||||
field_tags: Dict[str, Tag] = {tag[NAME]: tag for tag in form_tag.find_all(INPUT) + form_tag.find_all(SELECT)}
|
field_tags: Dict[str, Tag] = {tag[NAME]: tag for tag in form_tag.find_all(INPUT) + form_tag.find_all(SELECT)}
|
||||||
for field in form_interface.fields.values():
|
for field in form_interface.fields.values():
|
||||||
tag = field_tags.pop(field.name)
|
tag = field_tags.pop(field.name, None)
|
||||||
if tag is None:
|
if tag is None:
|
||||||
raise UnknownField(f"The defined field does not exist in the form: {field}")
|
raise UnknownField(f"The defined field does not exist in the form: {field}")
|
||||||
check_field_interface(tag, field, check_defaults=check_defaults)
|
check_field_interface(tag, field, check_defaults=check_defaults)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user