From 7d825e53d9e79f140b6fbae1c0be8e9de81d8c11 Mon Sep 17 00:00:00 2001 From: daijro Date: Sun, 29 Sep 2024 17:24:13 -0500 Subject: [PATCH] pythonlib: Typing & environ var fixes 0.2.1 - Fixes typing with OS_NAME - Fix exceeding environment variable length on Windows (caused by typing issue) - Fix not passing fontconfig on Linux (caused by typing issue) --- pythonlib/camoufox/pkgman.py | 8 ++++---- pythonlib/camoufox/utils.py | 12 ++++++------ pythonlib/pyproject.toml | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/pythonlib/camoufox/pkgman.py b/pythonlib/camoufox/pkgman.py index 258ab17..1ccf353 100644 --- a/pythonlib/camoufox/pkgman.py +++ b/pythonlib/camoufox/pkgman.py @@ -7,7 +7,7 @@ import sys import tempfile from io import BufferedWriter, BytesIO from pathlib import Path -from typing import List, Optional, Union +from typing import List, Literal, Optional, Union from zipfile import ZipFile import click @@ -34,12 +34,12 @@ ARCH_MAP: dict[str, str] = { 'armv6l': 'arm64', 'armv7l': 'arm64', } -OS_MAP: dict[str, str] = {'darwin': 'mac', 'linux': 'lin', 'win32': 'win'} +OS_MAP: dict[str, Literal['mac', 'win', 'lin']] = {'darwin': 'mac', 'linux': 'lin', 'win32': 'win'} if sys.platform not in OS_MAP: raise UnsupportedOS(f"OS {sys.platform} is not supported") -OS_NAME: str = OS_MAP[sys.platform] +OS_NAME: Literal['mac', 'win', 'lin'] = OS_MAP[sys.platform] INSTALL_DIR: Path = Path(user_cache_dir("camoufox")) @@ -179,7 +179,7 @@ class CamoufoxFetcher: # Set permissions on INSTALL_DIR if OS_NAME != 'win': - os.system(f'chmod -R 755 {shlex.quote(str(INSTALL_DIR))}') + os.system(f'chmod -R 755 {shlex.quote(str(INSTALL_DIR))}') # nosec rprint('\nCamoufox successfully installed.', fg="yellow") except Exception as e: diff --git a/pythonlib/camoufox/utils.py b/pythonlib/camoufox/utils.py index 113d811..d612201 100644 --- a/pythonlib/camoufox/utils.py +++ b/pythonlib/camoufox/utils.py @@ -2,7 +2,7 @@ import os import sys from os import environ from random import randrange -from typing import Any, Dict, List, Optional, Tuple, Union, cast +from typing import Any, Dict, List, Literal, Optional, Tuple, Union, cast import numpy as np import orjson @@ -38,7 +38,7 @@ def get_env_vars( """ Gets a dictionary of environment variables for Camoufox. """ - env_vars = {} + env_vars: Dict[str, Union[str, float, bool]] = {} try: updated_config_data = orjson.dumps(config_map) except orjson.JSONEncodeError as e: @@ -46,7 +46,7 @@ def get_env_vars( sys.exit(1) # Split the config into chunks - chunk_size = 2047 if OS_NAME == 'windows' else 32767 + chunk_size = 2047 if OS_NAME == 'win' else 32767 config_str = updated_config_data.decode('utf-8') for i in range(0, len(config_str), chunk_size): @@ -58,7 +58,7 @@ def get_env_vars( print(f"Error setting {env_name}: {e}") sys.exit(1) - if OS_NAME == 'linux': + if OS_NAME == 'lin': fontconfig_path = get_path(os.path.join("fontconfig", user_agent_os)) env_vars['FONTCONFIG_PATH'] = fontconfig_path @@ -115,7 +115,7 @@ def validate_type(value: Any, expected_type: str) -> bool: return False -def get_target_os(config: Dict[str, Any]) -> str: +def get_target_os(config: Dict[str, Any]) -> Literal['mac', 'win', 'lin']: """ Gets the OS from the config if the user agent is set, otherwise returns the OS of the current system. @@ -125,7 +125,7 @@ def get_target_os(config: Dict[str, Any]) -> str: return OS_NAME -def determine_ua_os(user_agent: str) -> str: +def determine_ua_os(user_agent: str) -> Literal['mac', 'win', 'lin']: """ Determines the OS from the user agent string. """ diff --git a/pythonlib/pyproject.toml b/pythonlib/pyproject.toml index 7f0091b..ace2fa4 100644 --- a/pythonlib/pyproject.toml +++ b/pythonlib/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "camoufox" -version = "0.2.0" +version = "0.2.1" description = "Wrapper around Playwright to help launch Camoufox" authors = ["daijro "] license = "MIT"