Skip to content

Commit

Permalink
Added different log types
Browse files Browse the repository at this point in the history
Optimize the parsing logic to avoid unnecessary operations
  • Loading branch information
dmy.berezovskyi committed Jan 15, 2025
1 parent c1c8305 commit 479fdb8
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 190 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ setuptools="70.0.0"
ruff="0.6.8"
secure-test-automation="^1.3.1"
colorama="==0.4.6"
rich="==13.9.4"
typer="==0.15.1"


[tool.pytest.ini_options]
Expand Down
121 changes: 0 additions & 121 deletions scraper/chrome_scraper.py

This file was deleted.

6 changes: 2 additions & 4 deletions src/pageobjects/base_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.common.exceptions import (
TimeoutException, ElementNotVisibleException
)
from selenium.common.exceptions import TimeoutException, ElementNotVisibleException

from utils.helpers import timing
from utils.logger import log
Expand Down Expand Up @@ -126,7 +124,7 @@ def get_current_url(self):
def refresh(self):
"""Refresh the current page."""
self.driver.refresh()

def scroll_to_element(self, element):
"""Sroll to element"""
self.driver.execute_script("arguments[0].scrollIntoView();", element)
File renamed without changes.
11 changes: 11 additions & 0 deletions utils/cli/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""Top-level package for RP To-Do."""
# rptodo/__init__.py

__app_name__, __version__ = "Scrap", "1.0"


SUCCESS, DIR_ERROR = range(2)

ERRORS = {
DIR_ERROR: "config directory error"
}
70 changes: 12 additions & 58 deletions utils/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,12 @@
import argparse
from pathlib import Path

from pyfiglet import Figlet
from rich.console import Console
from colorama import Fore, Style

from scraper.chrome_scraper import ChromePageScraper


def create_cli():
# Initialize ArgumentParser
parser = argparse.ArgumentParser(prog="sstf", description="SSTF Command Line Tool")

# Add the 'get' subcommand
subparsers = parser.add_subparsers(dest="command")

# Add subcommand for 'get'
get_parser = subparsers.add_parser("get", help="Download and manage Chromedriver")
get_subparsers = get_parser.add_subparsers(dest="subcommand")

# Add subcommand for 'chromedriver'
chromedriver_parser = get_subparsers.add_parser("chromedriver",
help="Download chromedriver for a specified version and platform")
chromedriver_parser.add_argument('--milestone', type=str,
help=f"{Fore.CYAN}Chromium milestone version (e.g., 131).{Style.RESET_ALL}")
chromedriver_parser.add_argument('--version', type=str,
help=f"{Fore.CYAN}Chromium browser version.{Style.RESET_ALL}")
chromedriver_parser.add_argument('--platform', type=str, choices=["windows", "mac", "linux"],
help=f"{Fore.CYAN}Operating system platform.{Style.RESET_ALL}")
chromedriver_parser.add_argument('--output-dir', type=str, default=None,
help=f"{Fore.CYAN}Directory to save the downloaded Chromedriver.{Style.RESET_ALL}")
chromedriver_parser.add_argument('--extract', action='store_true',
help=f"{Fore.CYAN}Extract the Chromedriver after download.{Style.RESET_ALL}")

# Parse arguments
args = parser.parse_args()

# Handle 'get chromedriver' logic
if args.command == "get" and args.subcommand == "chromedriver":
console = Console()

# ASCII Art Header with Figlet (using Rich)
fig = Figlet(font="slant") # You can use different fonts like 'slant', 'block', etc.
console.print(fig.renderText("Chromedriver Download"), style="bold green")

# Run the actual logic for downloading chromedriver
ChromePageScraper.get_chromedriver(
platform=args.platform,
version=args.version,
milestone=args.milestone,
d_dir=Path(args.output_dir) if args.output_dir else None,
is_extracted=args.extract
)


if __name__ == "__main__":
create_cli()
@click.command()
@click.option("--platform", default=None, help="OS and architecture (e.g., 'win64', 'mac64')")
@click.option("--version", default=None, help="The version of Chrome (e.g., '89.0.4389.82')")
@click.option("--milestone", default=None, help="Milestone version (e.g., '129')")
@click.option("--dir", default=None, type=pathlib.Path, help="Directory to save the chromedriver")
@click.option("--extract", is_flag=True, help="Extract the chromedriver after downloading")
def cli(platform, version, milestone, dir, extract):
"""
CLI command to fetch the latest Chrome driver
"""
print(f"Fetching ChromeDriver for platform: {platform}, version: {version}, milestone: {milestone}")
ChromePageScraper.get_chromedriver(platform=platform, version=version, milestone=milestone, d_dir=dir, is_extracted=extract)
28 changes: 22 additions & 6 deletions utils/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ def __call__(cls, *args, **kwargs) -> Any:


class Logger(metaclass=Singleton):
def __init__(self, log_lvl: LogLevel = LogLevel.INFO) -> None:
def __init__(self,
log_lvl: LogLevel = LogLevel.INFO,
log_target: Literal["console", "file", "both"] = "console") -> None:
"""Logger
:param log_lvl: LogLevel
:param log_target: Target for logs (store to file, display to console or both(file and console))
"""
self._log = logging.getLogger("selenium")
self._log.setLevel(LogLevel.DEBUG.value)
self._log.setLevel(LogLevel.INFO.value)
self.log_file = self._create_log_file()
self.log_target = log_target
self._initialize_logging(log_lvl)

def _create_log_file(self) -> str:
Expand All @@ -49,10 +56,19 @@ def _initialize_logging(self, log_lvl: LogLevel) -> None:
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
fh = logging.FileHandler(self.log_file, mode="w")
fh.setFormatter(formatter)
fh.setLevel(log_lvl.value)
self._log.addHandler(fh)
# Lot to file or both file and console
if self.log_target in ("file", "both"):
fh = logging.FileHandler(self.log_file, mode="w")
fh.setFormatter(formatter)
fh.setLevel(log_lvl.value)
self._log.addHandler(fh)

# Log to console or both file and console
if self.log_target in ("console", "both"):
ch = logging.StreamHandler()
ch.setFormatter(formatter)
ch.setLevel(log_lvl.value)
self._log.addHandler(ch)

def get_instance(self) -> logging.Logger:
return self._log
Expand Down
Empty file added utils/scraper/__init__.py
Empty file.
Loading

0 comments on commit 479fdb8

Please sign in to comment.