use CONFIG instead of Config()

This commit is contained in:
shenchucheng 2023-07-25 00:15:16 +08:00
parent d07b6ba540
commit fac1cb0f59
6 changed files with 24 additions and 31 deletions

View file

@ -18,6 +18,6 @@ class SearchEngineType(Enum):
class WebBrowserEngineType(Enum):
PLAYWRIGHT = auto()
SELENIUM = auto()
CUSTOM_ENGINE = auto()
PLAYWRIGHT = "playwright"
SELENIUM = "selenium"
CUSTOM = "custom"

View file

@ -6,7 +6,7 @@ import importlib
from typing import Any, Callable, Coroutine, overload
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.tools import WebBrowserEngineType
from bs4 import BeautifulSoup
@ -17,8 +17,7 @@ class WebBrowserEngine:
engine: WebBrowserEngineType | None = None,
run_func: Callable[..., Coroutine[Any, Any, str | list[str]]] | None = None,
):
self.config = Config()
engine = engine or self.config.web_browser_engine
engine = engine or CONFIG.web_browser_engine
if engine == WebBrowserEngineType.PLAYWRIGHT:
web_browser_engine = importlib.import_module("metagpt.tools.web_browser_engine_playwright")
@ -26,7 +25,7 @@ class WebBrowserEngine:
elif engine == WebBrowserEngineType.SELENIUM:
web_browser_engine = importlib.import_module("metagpt.tools.web_browser_engine_selenium")
run_func = web_browser_engine.SeleniumWrapper().run
elif engine == WebBrowserEngineType.CUSTOM_ENGINE:
elif engine == WebBrowserEngineType.CUSTOM:
run_func = run_func
else:
raise NotImplementedError

View file

@ -6,7 +6,7 @@ from pathlib import Path
import sys
from typing import Literal
from playwright.async_api import async_playwright
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.logs import logger
@ -24,16 +24,14 @@ class PlaywrightWrapper:
launch_kwargs: dict | None = None,
**kwargs,
) -> None:
config = Config()
self.config = config
if browser_type is None:
browser_type = config.playwright_browser_type
browser_type = CONFIG.playwright_browser_type
self.browser_type = browser_type
launch_kwargs = launch_kwargs or {}
if config.global_proxy and "proxy" not in launch_kwargs:
if CONFIG.global_proxy and "proxy" not in launch_kwargs:
args = launch_kwargs.get("args", [])
if not any(str.startswith(i, "--proxy-server=") for i in args):
launch_kwargs["proxy"] = {"server": config.global_proxy}
launch_kwargs["proxy"] = {"server": CONFIG.global_proxy}
self.launch_kwargs = launch_kwargs
context_kwargs = {}
if "ignore_https_errors" in kwargs:
@ -70,8 +68,8 @@ class PlaywrightWrapper:
executable_path = Path(browser_type.executable_path)
if not executable_path.exists() and "executable_path" not in self.launch_kwargs:
kwargs = {}
if self.config.global_proxy:
kwargs["env"] = {"ALL_PROXY": self.config.global_proxy}
if CONFIG.global_proxy:
kwargs["env"] = {"ALL_PROXY": CONFIG.global_proxy}
await _install_browsers(self.browser_type, **kwargs)
if not executable_path.exists():
parts = executable_path.parts

View file

@ -5,7 +5,7 @@ import asyncio
from copy import deepcopy
import importlib
from typing import Literal
from metagpt.config import Config
from metagpt.config import CONFIG
import asyncio
from selenium.webdriver.common.by import By
@ -29,14 +29,12 @@ class SeleniumWrapper:
loop: asyncio.AbstractEventLoop | None = None,
executor: futures.Executor | None = None,
) -> None:
config = Config()
self.config = config
if browser_type is None:
browser_type = config.selenium_browser_type
browser_type = CONFIG.selenium_browser_type
self.browser_type = browser_type
launch_kwargs = launch_kwargs or {}
if config.global_proxy and "proxy-server" not in launch_kwargs:
launch_kwargs["proxy-server"] = config.global_proxy
if CONFIG.global_proxy and "proxy-server" not in launch_kwargs:
launch_kwargs["proxy-server"] = CONFIG.global_proxy
self.executable_path = launch_kwargs.pop("executable_path", None)
self.launch_args = [f"--{k}={v}" for k, v in launch_kwargs.items()]

View file

@ -1,5 +1,5 @@
import pytest
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_playwright
@ -15,10 +15,9 @@ from metagpt.tools import web_browser_engine_playwright
)
async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy, capfd):
try:
config = Config()
global_proxy = config.global_proxy
global_proxy = CONFIG.global_proxy
if use_proxy:
config.global_proxy = proxy
CONFIG.global_proxy = proxy
browser = web_browser_engine_playwright.PlaywrightWrapper(browser_type, **kwagrs)
result = await browser.run(url)
assert isinstance(result, str)
@ -32,4 +31,4 @@ async def test_scrape_web_page(browser_type, use_proxy, kwagrs, url, urls, proxy
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
config.global_proxy = global_proxy
CONFIG.global_proxy = global_proxy

View file

@ -1,5 +1,5 @@
import pytest
from metagpt.config import Config
from metagpt.config import CONFIG
from metagpt.tools import web_browser_engine_selenium
@ -15,10 +15,9 @@ from metagpt.tools import web_browser_engine_selenium
)
async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd):
try:
config = Config()
global_proxy = config.global_proxy
global_proxy = CONFIG.global_proxy
if use_proxy:
Config().global_proxy = proxy
CONFIG.global_proxy = proxy
browser = web_browser_engine_selenium.SeleniumWrapper(browser_type)
result = await browser.run(url)
assert isinstance(result, str)
@ -32,4 +31,4 @@ async def test_scrape_web_page(browser_type, use_proxy, url, urls, proxy, capfd)
if use_proxy:
assert "Proxy:" in capfd.readouterr().out
finally:
config.global_proxy = global_proxy
CONFIG.global_proxy = global_proxy