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)
This commit is contained in:
daijro 2024-09-29 17:24:13 -05:00
parent f6396c1e81
commit 7d825e53d9
3 changed files with 11 additions and 11 deletions

View file

@ -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:

View file

@ -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.
"""

View file

@ -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 <daijro.dev@gmail.com>"]
license = "MIT"