~sircmpwn/pass-rotate

ref: 837d726c037a6d52bd5cbf159a75386a39e4dea0 pass-rotate/passrotate/forms.py -rw-r--r-- 1.6 KiB
837d726cDrew DeVault Merge pull request #29 from gabm/AddPassName 4 years ago
                                                                                
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
from typing import Dict, Callable
from bs4 import BeautifulSoup
from bs4.element import ResultSet

FormData = Dict[str, str]


def get_form_data(inputs: ResultSet) -> FormData:
    """Returns data dictionary from list of BeautifulSoup input elements.

    Parameters:
        inputs: The list of BeautifulSoup input elements.

    Returns dictionary with (name, value) pairs from inputs.
    """
    return {
        i.get("name", ""): i.get("value", "") or "" for i in inputs if i.get("name", "")
    }


def get_form(text: str, type: str = "form", **kwargs) -> FormData:
    """Helper method to get the data from a form.

    Parameters:
        text: HTML text to be processed.
        type: HTML element type to find in page.
        **kwargs: Additional parameters to pass to `soup.find()`

    Returns dictionary with (name, value) pairs from inputs from first match.
    """
    soup = BeautifulSoup(text, "html5lib")
    form = soup.find(type, attrs=kwargs)
    inputs = form.find_all("input")
    return get_form_data(inputs)


def custom_get_form(text: str,
                    func: Callable[[BeautifulSoup], ResultSet]) -> FormData:
    """Helper method to get data from a form using a custom function.

    Parameters:
        text: HTML text to be processed.
        func: A function that takes in a BeautifulSoup object and outputs a
              list of input objects from the form desired.

    Returns dictionary with (name, value) pairs from inputs returned by func.
    """
    soup = BeautifulSoup(text, "html5lib")
    inputs = func(soup)
    return get_form_data(inputs)