Skip to content

Commit

Permalink
fix: error issues
Browse files Browse the repository at this point in the history
  • Loading branch information
joamag committed Oct 1, 2024
1 parent 8733d8c commit b61ecb9
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 22 deletions.
16 changes: 9 additions & 7 deletions src/weby_pilot/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.ui import WebDriverWait, Select

from .errors import WebyError

MAX_WAIT_TIME = 30.0

ImageFormat = Literal["png", "jpeg", "gif", "bmp", "tiff", "svg"]
Expand Down Expand Up @@ -156,7 +158,7 @@ def wait_download(
if overwrite:
remove(f"{self._downloads_dir}/{filename}")
else:
raise Exception(
raise WebyError(
f"File {filename} already exists in downloads folder"
)

Expand All @@ -181,7 +183,7 @@ def wait_download(
# we can return the destination file path
return dst

raise Exception(f"Download not completed after {timeout} seconds")
raise WebyError(f"Download not completed after {timeout} seconds")

def screenshot(self) -> bytes:
return self.driver.get_screenshot_as_png()
Expand All @@ -195,7 +197,7 @@ def screenshot_file(
filename = f"{name}.{image_format}"

if not self.driver.get_screenshot_as_file(filename):
raise Exception(f"Failed to save screenshot to {filename}")
raise WebyError(f"Failed to save screenshot to {filename}")

return filename

Expand Down Expand Up @@ -226,13 +228,13 @@ def download_ctx(
@property
def driver(self) -> Chrome:
if self._driver is None:
raise Exception("Driver is not started")
raise WebyError("Driver is not started")
return self._driver

@property
def wait(self) -> WebDriverWait[Chrome]:
if self._wait is None:
raise Exception("Wait is not started")
raise WebyError("Wait is not started")
return self._wait

def _cleanup_temp(self):
Expand All @@ -256,7 +258,7 @@ def _last_download_buffer(self) -> IO:
@property
def _last_download_path(self) -> str:
if self._last_path is None:
raise Exception("No file downloaded")
raise WebyError("No file downloaded")
if not exists(self._last_path):
raise Exception(f"File {self._last_download} does not exist")
raise WebyError(f"File {self._last_download} does not exist")
return self._last_path
7 changes: 4 additions & 3 deletions src/weby_pilot/big.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from os import environ

from .base import WebyAPI
from .errors import WebyError


class BigAPI(WebyAPI):
Expand All @@ -20,8 +21,8 @@ def build_login(self):
self.nif = environ.get("BIG_NIF", None)

if self.username is None:
raise Exception("BIG_USERNAME must be set")
raise WebyError("BIG_USERNAME must be set")
if self.password is None:
raise Exception("BIG_PASSWORD must be set")
raise WebyError("BIG_PASSWORD must be set")
if self.nif is None:
raise Exception("BIG_NIF must be set")
raise WebyError("BIG_NIF must be set")
25 changes: 13 additions & 12 deletions src/weby_pilot/bpi.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from .base import WebyAPI
from .common import FileType
from .errors import WebyError

BpiSections = Literal[
"Consultas",
Expand Down Expand Up @@ -76,9 +77,9 @@ def build_login(self):
self.password = environ.get("BPI_PASSWORD", None)

if self.username is None:
raise Exception("BPI_USERNAME must be set")
raise WebyError("BPI_USERNAME must be set")
if self.password is None:
raise Exception("BPI_PASSWORD must be set")
raise WebyError("BPI_PASSWORD must be set")

def get_balance(self) -> str:
with self.driver_ctx():
Expand Down Expand Up @@ -217,8 +218,8 @@ def select_filters(self, date_range: SelectDateRange, document_type: DocumentTyp
self.select_item(self.get_elements(By.XPATH, "//select")[2], date_range)
self.select_item(self.get_elements(By.XPATH, "//select")[3], document_type)

filter = self.get_element(By.XPATH, "//*[@value='Filtrar']")
filter.click()
_filter = self.get_element(By.XPATH, "//*[@value='Filtrar']")
_filter.click()

def select_account(self, index: int = 0, timeout=5.0):
account_element = self.get_element(
Expand Down Expand Up @@ -267,7 +268,7 @@ def __repr__(self):


class BpiDocument:
type: BpiDocumentType
doc_type: BpiDocumentType
name: str
buffer: IO[bytes]
file_type: FileType
Expand All @@ -276,43 +277,43 @@ class BpiDocument:

def __init__(
self,
type: BpiDocumentType,
doc_type: BpiDocumentType,
name: str,
buffer: IO[bytes],
file_type: FileType,
account: str | None = None,
date: datetime | None = None,
):
self.type = type
self.doc_type = doc_type
self.name = name
self.buffer = buffer
self.file_type = file_type
self.account = account
self.date = date

def __repr__(self):
return f"BpiDocument(type={self.type}, name={self.name}, account={self.account} date={self.date})"
return f"BpiDocument(doc_type={self.doc_type}, name={self.name}, account={self.account} date={self.date})"

@property
def year(self) -> int:
if self.date is None:
raise Exception("Date is not set")
raise WebyError("Date is not set")
return self.date.year

@property
def month(self) -> int:
if self.date is None:
raise Exception("Date is not set")
raise WebyError("Date is not set")
return self.date.month

@property
def day(self) -> int:
if self.date is None:
raise Exception("Date is not set")
raise WebyError("Date is not set")
return self.date.day

@property
def month_filename(self) -> str:
if self.date is None:
raise Exception("Date is not set")
raise WebyError("Date is not set")
return f"{self.date.strftime('%m')}.{self.file_type.extension}"
6 changes: 6 additions & 0 deletions src/weby_pilot/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-


class WebyError(Exception):
pass

0 comments on commit b61ecb9

Please sign in to comment.