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

mandos.model.scrape   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 45
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A Scraper.find_elements() 0 3 1
A Scraper.click_element() 0 4 1
A Scraper.go() 0 4 1
A Scraper.find_element() 0 3 1
A Scraper.create() 0 5 2
1
from __future__ import annotations
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
from dataclasses import dataclass
3
from typing import Sequence
4
5
from pocketutils.core.query_utils import QueryExecutor
0 ignored issues
show
introduced by
Unable to import 'pocketutils.core.query_utils'
Loading history...
6
7
from mandos.model.settings import MANDOS_SETTINGS
8
9
from mandos import logger
10
11
# noinspection PyBroadException
12
try:
13
    from selenium import webdriver
0 ignored issues
show
introduced by
Unable to import 'selenium'
Loading history...
14
    from selenium.webdriver.common.by import By
0 ignored issues
show
introduced by
Unable to import 'selenium.webdriver.common.by'
Loading history...
15
    from selenium.webdriver.remote.webdriver import WebDriver
0 ignored issues
show
introduced by
Unable to import 'selenium.webdriver.remote.webdriver'
Loading history...
16
    from selenium.webdriver.remote.webelement import WebElement
0 ignored issues
show
introduced by
Unable to import 'selenium.webdriver.remote.webelement'
Loading history...
17
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...
18
    webdriver = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "webdriver" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\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...
19
    WebDriver = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "WebDriver" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\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...
20
    By = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "By" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\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...
21
22
if webdriver is not None:
23
    # noinspection PyBroadException
24
    try:
25
        driver_fn = getattr(webdriver, MANDOS_SETTINGS.selenium_driver)
26
    except AttributeError:
27
        driver_fn = None
0 ignored issues
show
Coding Style Naming introduced by
Constant name "driver_fn" doesn't conform to UPPER_CASE naming style ('([^\\W\\da-z][^\\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...
28
        logger.warning(f"Selenium driver {MANDOS_SETTINGS.selenium_driver} not found")
29
30
31
@dataclass(frozen=True)
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
32
class Scraper:
33
    driver: WebDriver
34
    executor: QueryExecutor
35
36
    @classmethod
37
    def create(cls, executor: QueryExecutor) -> Scraper:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
38
        if driver_fn is None:
0 ignored issues
show
introduced by
The variable driver_fn does not seem to be defined in case webdriver is not None on line 22 is False. Are you sure this can never be the case?
Loading history...
39
            raise ValueError(f"Selenium driver {MANDOS_SETTINGS.selenium_driver} not found")
40
        return Scraper(driver_fn(), executor)
41
42
    def go(self, url: str) -> Scraper:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Coding Style Naming introduced by
Method name "go" 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...
43
        self.driver.get(url)
44
        # self.driver.find_elements_by_link_text("1")
45
        return self
46
47
    def find_element(self, thing: str, by: str) -> WebElement:
0 ignored issues
show
Coding Style Naming introduced by
Argument name "by" 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...
introduced by
Missing function or method docstring
Loading history...
48
        by = by.upper()
49
        return self.driver.find_element(thing, by)
50
51
    def find_elements(self, thing: str, by: str) -> Sequence[WebElement]:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Coding Style Naming introduced by
Argument name "by" 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...
52
        by = by.upper()
53
        return self.driver.find_elements(thing, by)
54
55
    def click_element(self, thing: str, by: str) -> None:
0 ignored issues
show
introduced by
Missing function or method docstring
Loading history...
Coding Style Naming introduced by
Argument name "by" 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...
56
        by = by.upper()
57
        element = self.driver.find_element(thing, by)
58
        element.click()
59
60
61
__all__ = ["Scraper", "By"]
62