set.download_file_name()增加suffix参数

This commit is contained in:
g1879 2023-11-05 16:57:10 +08:00
parent 2ddb7aff95
commit 897a9cd9c8
5 changed files with 57 additions and 27 deletions

View File

@ -26,7 +26,8 @@ class OptionsManager(object):
self.ini_path = str(path)
if not Path(self.ini_path).exists():
raise FileNotFoundError('ini文件不存在。')
raise FileNotFoundError('\nini文件不存在。\n如果是打包使用,请查看打包注意事项\nhttps://g1879.gitee.io/drission'
'pagedocs/advance/packaging/')
self._conf = RawConfigParser()
self._conf.read(self.ini_path, encoding='utf-8')

View File

@ -48,13 +48,16 @@ class BrowserDownloadManager(object):
self._browser.run_cdp('Browser.setDownloadBehavior', downloadPath=str(Path(path).absolute()),
behavior='allowAndName', eventsEnabled=True)
def set_rename(self, tab_id, rename):
def set_rename(self, tab_id, rename=None, suffix=None):
"""设置某个tab的重命名文件名
:param tab_id: tab id
:param rename: 文件名
:param rename: 文件名可不含后缀会自动使用远程文件后缀
:param suffix: 后缀名显式设置后缀名不使用远程文件后缀
:return: None
"""
TabDownloadSettings(tab_id).rename = rename
ts = TabDownloadSettings(tab_id)
ts.rename = rename
ts.suffix = suffix
def set_file_exists(self, tab_id, mode):
"""设置某个tab下载文件重名时执行的策略
@ -135,12 +138,22 @@ class BrowserDownloadManager(object):
settings = TabDownloadSettings(tab_id if tab_id in TabDownloadSettings.TABS else self._page.tab_id)
if settings.rename:
tmp = kwargs['suggestedFilename'].rsplit('.', 1)
ext_name = tmp[-1] if len(tmp) > 1 else ''
tmp = settings.rename.rsplit('.', 1)
ext_rename = tmp[-1] if len(tmp) > 1 else ''
name = settings.rename if ext_rename == ext_name else f'{settings.rename}.{ext_name}'
if settings.suffix is not None:
name = f'{settings.rename}.{settings.suffix}'
else:
tmp = kwargs['suggestedFilename'].rsplit('.', 1)
ext_name = tmp[-1] if len(tmp) > 1 else ''
tmp = settings.rename.rsplit('.', 1)
ext_rename = tmp[-1] if len(tmp) > 1 else ''
name = settings.rename if ext_rename == ext_name else f'{settings.rename}.{ext_name}'
settings.rename = None
settings.suffix = None
elif settings.suffix is not None:
name = f'{kwargs["suggestedFilename"].rsplit(".", 1)[0]}.{settings.suffix}'
settings.suffix = None
else:
name = kwargs['suggestedFilename']
@ -210,6 +223,7 @@ class TabDownloadSettings(object):
self._created = True
self.tab_id = tab_id
self.rename = None
self.suffix = None
self.path = ''
self.when_file_exists = 'rename'

View File

@ -19,7 +19,7 @@ class BrowserDownloadManager(object):
def set_path(self, tab_id: str, path: Union[Path, str]) -> None: ...
def set_rename(self, tab_id: str, rename: str) -> None: ...
def set_rename(self, tab_id: str, rename: str = None, suffix: str = None) -> None: ...
def set_file_exists(self, tab_id: str, mode: Literal['rename', 'skip', 'overwrite']) -> None: ...
@ -47,6 +47,7 @@ class TabDownloadSettings(object):
tab_id: str = ...
waiting_flag: Optional[bool, dict] = ...
rename: Optional[str] = ...
suffix: Optional[str] = ...
path: Optional[str] = ...
when_file_exists: str = ...

View File

@ -137,12 +137,13 @@ class TabSetter(ChromiumBaseSetter):
if self._page._DownloadKit:
self._page._DownloadKit.set.goal_path(path)
def download_file_name(self, name):
def download_file_name(self, name=None, suffix=None):
"""设置下一个被下载文件的名称
:param name: 文件名可不含后缀
:param name: 文件名可不含后缀会自动使用远程文件后缀
:param suffix: 后缀名显式设置后缀名不使用远程文件后缀
:return: None
"""
self._page.browser._dl_mgr.set_rename(self._page.tab_id, name)
self._page.browser._dl_mgr.set_rename(self._page.tab_id, name, suffix)
def when_download_file_exists(self, mode):
"""设置当存在同名文件时的处理方式
@ -181,6 +182,11 @@ class ChromiumPageSetter(TabSetter):
tab_or_id = tab_or_id.tab_id
self._page.browser.activate_tab(tab_or_id)
@property
def window(self):
"""返回用于设置浏览器窗口的对象"""
return PageWindowSetter(self._page)
class SessionPageSetter(object):
def __init__(self, page):
@ -558,14 +564,6 @@ class WindowSetter(object):
y = y if y is not None else info['top']
self._perform({'left': x - 8, 'top': y})
def hide(self):
"""隐藏浏览器窗口只在Windows系统可用"""
show_or_hide_browser(self._page, hide=True)
def show(self):
"""显示浏览器窗口只在Windows系统可用"""
show_or_hide_browser(self._page, hide=False)
def _get_info(self):
"""获取窗口位置及大小信息"""
return self._page.run_cdp('Browser.getWindowForTarget')
@ -576,3 +574,13 @@ class WindowSetter(object):
:return: None
"""
self._page.run_cdp('Browser.setWindowBounds', windowId=self._window_id, bounds=bounds)
class PageWindowSetter(WindowSetter):
def hide(self):
"""隐藏浏览器窗口只在Windows系统可用"""
show_or_hide_browser(self._page, hide=True)
def show(self):
"""显示浏览器窗口只在Windows系统可用"""
show_or_hide_browser(self._page, hide=False)

View File

@ -61,7 +61,7 @@ class TabSetter(ChromiumBaseSetter):
def download_path(self, path: Union[str, Path]) -> None: ...
def download_file_name(self, name: str) -> None: ...
def download_file_name(self, name: str = None, suffix: str = None) -> None: ...
def when_download_file_exists(self, mode: FILE_EXISTS) -> None: ...
@ -71,6 +71,9 @@ class TabSetter(ChromiumBaseSetter):
class ChromiumPageSetter(TabSetter):
_page: ChromiumPage = ...
@property
def window(self) -> PageWindowSetter: ...
def main_tab(self, tab_id: str = None) -> None: ...
def tab_to_front(self, tab_or_id: Union[str, ChromiumTab] = None) -> None: ...
@ -183,7 +186,6 @@ class PageScrollSetter(object):
class WindowSetter(object):
def __init__(self, page: ChromiumBase):
self._page: ChromiumBase = ...
self._window_id: str = ...
@ -200,10 +202,14 @@ class WindowSetter(object):
def location(self, x: int = None, y: int = None) -> None: ...
def hide(self) -> None: ...
def show(self) -> None: ...
def _get_info(self) -> dict: ...
def _perform(self, bounds: dict) -> None: ...
class PageWindowSetter(WindowSetter):
_page: ChromiumPage = ...
def hide(self) -> None: ...
def show(self) -> None: ...