add utils.abspath

This commit is contained in:
sebthom
2022-02-17 12:50:52 +01:00
parent c844c2473c
commit b879e3d6db
2 changed files with 23 additions and 7 deletions

View File

@@ -15,7 +15,7 @@ from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from . import utils, resources
from .utils import apply_defaults, ensure, is_frozen, pause, pluralize, safe_get
from .utils import abspath, apply_defaults, ensure, is_frozen, pause, pluralize, safe_get
from .selenium_mixin import SeleniumMixin
LOG_ROOT:Final[logging.Logger] = logging.getLogger()
@@ -31,7 +31,7 @@ class KleinanzeigenBot(SeleniumMixin):
self.root_url = "https://www.ebay-kleinanzeigen.de"
self.config:dict[str, Any] = {}
self.config_file_path = os.path.join(os.getcwd(), "config.yaml")
self.config_file_path = abspath("config.yaml")
self.categories:dict[str, str] = {}
@@ -40,7 +40,7 @@ class KleinanzeigenBot(SeleniumMixin):
log_file_basename = os.path.splitext(os.path.basename(sys.executable))[0]
else:
log_file_basename = self.__module__
self.log_file_path = os.path.join(os.getcwd(), f"{log_file_basename}.log")
self.log_file_path = abspath(f"{log_file_basename}.log")
self.command = "help"
self.force_mode = False
@@ -121,10 +121,10 @@ class KleinanzeigenBot(SeleniumMixin):
self.show_help()
sys.exit(0)
case "--config":
self.config_file_path = os.path.abspath(value)
self.config_file_path = abspath(value)
case "--logfile":
if value:
self.log_file_path = os.path.abspath(value)
self.log_file_path = abspath(value)
else:
self.log_file_path = None
case "--force":
@@ -162,7 +162,7 @@ class KleinanzeigenBot(SeleniumMixin):
data_root_dir = os.path.dirname(self.config_file_path)
for file_pattern in self.config["ad_files"]:
for ad_file in glob.glob(file_pattern, root_dir = data_root_dir, recursive = True):
ad_files.add(os.path.join(data_root_dir, ad_file))
ad_files.add(abspath(ad_file, relative_to = data_root_dir))
LOG.info(" -> found %s", pluralize("ad file", ad_files))
if not ad_files:
return []
@@ -238,7 +238,7 @@ class KleinanzeigenBot(SeleniumMixin):
if os.path.isabs(image_file):
images.add(image_file)
else:
images.add(os.path.join(os.path.dirname(ad_file), image_file))
images.add(abspath(image_file, relative_to = ad_file))
ensure(images or not ad_cfg["images"], f"No images found for given file patterns {ad_cfg['images']} at {ad_dir}")
ad_cfg["images"] = sorted(images)

View File

@@ -15,6 +15,22 @@ LOG_ROOT:Final[logging.Logger] = logging.getLogger()
LOG:Final[logging.Logger] = logging.getLogger("kleinanzeigen_bot.utils")
def abspath(relative_path:str, relative_to:str = None):
"""
Makes a given relative path absolute based on another file/folder
"""
if os.path.isabs(relative_path):
return relative_path
if not relative_to:
return os.path.abspath(relative_path)
if os.path.isfile(relative_to):
relative_to = os.path.dirname(relative_to)
return os.path.normpath(os.path.join(relative_to, relative_path))
def ensure(condition:bool | Callable[[], bool], error_message:str, timeout:float = 5, poll_requency:float = 0.5) -> None:
"""
:param timeout: timespan in seconds until when the condition must become `True`, default is 5 seconds