Passed
Push — main ( ec3fe3...82dd22 )
by Douglas
02:00
created

mandos.search.chembl.ChemblScrapeSearch._scrape()   A

Complexity

Conditions 4

Size

Total Lines 20
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nop 2
dl 0
loc 20
rs 9.5
c 0
b 0
f 0
1
import abc
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from typing import TypeVar
3
4
from typeddfs import TypedDfs
0 ignored issues
show
introduced by
Unable to import 'typeddfs'
Loading history...
5
6
from mandos.model.settings import QUERY_EXECUTORS
7
8
from mandos.model.scrape import Scraper, By
9
from mandos.model.apis.chembl_api import ChemblApi
10
from mandos.model.hits import AbstractHit
11
from mandos.model.searches import Search
12
13
H = TypeVar("H", bound=AbstractHit, covariant=True)
0 ignored issues
show
Coding Style Naming introduced by
Class name "H" doesn't conform to PascalCase naming style ('[^\\W\\da-z][^\\W_]+$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
14
15
16
class _ScraperSingleton:
17
    x = None
18
19
    @classmethod
20
    def get(cls):
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
21
        if cls.x is None:
22
            cls.x = Scraper.create(QUERY_EXECUTORS.chembl)
23
        return cls.x
24
25
26
ChemblTable = TypedDfs.typed("ChemblTable").build()
27
28
29
class ChemblScrapeSearch(Search[H], metaclass=abc.ABCMeta):
0 ignored issues
show
Documentation introduced by
Empty class docstring
Loading history...
30
    """"""
31
32
    @classmethod
33
    def _page_name(cls) -> str:
34
        raise NotImplementedError()
35
36
    def _scrape(self, chembl_id: int) -> ChemblTable:
37
        # e.g. target_predictions
38
        url = f"https://www.ebi.ac.uk/chembl/embed/#compound_report_card/{chembl_id}/{self._page_name}"
0 ignored issues
show
Coding Style introduced by
This line is too long as per the coding-style (103/100).

This check looks for lines that are too long. You can specify the maximum line length.

Loading history...
39
        scraper = _ScraperSingleton.get()
40
        scraper.go(url)
41
        rows = []
42
        i = 2
43
        while True:
44
            table = scraper.find_element("table", By.TAG_NAME)
45
            for tr in table.find_elements("tr"):
0 ignored issues
show
Coding Style Naming introduced by
Variable name "tr" doesn't conform to snake_case naming style ('([^\\W\\dA-Z][^\\WA-Z]2,|_[^\\WA-Z]*|__[^\\WA-Z\\d_][^\\WA-Z]+__)$' pattern)

This check looks for invalid names for a range of different identifiers.

You can set regular expressions to which the identifiers must conform if the defaults do not match your requirements.

If your project includes a Pylint configuration file, the settings contained in that file take precedence.

To find out more about Pylint, please refer to their site.

Loading history...
46
                rows += [td.text for td in tr.find_elements("td")]
47
            # noinspection PyBroadException
48
            try:
49
                scraper.find_elements(str(i), By.LINK_TEXT)
50
            except Exception:
0 ignored issues
show
Best Practice introduced by
Catching very general exceptions such as Exception is usually not recommended.

Generally, you would want to handle very specific errors in the exception handler. This ensure that you do not hide other types of errors which should be fixed.

So, unless you specifically plan to handle any error, consider adding a more specific exception.

Loading history...
51
                break
52
            i += 1
53
        header = rows[0]
54
        rows = rows[1:]
55
        return ChemblTable(rows, columns=header)
56
57
58
class ChemblSearch(Search[H], metaclass=abc.ABCMeta):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
59
    def __init__(self, key: str, api: ChemblApi):
60
        """
61
        Constructor.
62
63
        Args:
64
            api:
65
        """
66
        super().__init__(key)
67
        self.api = api
68
69
70
__all__ = ["ChemblSearch", "ChemblScrapeSearch", "ChemblTable"]
71