_download.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # Copyright (c) Microsoft Corporation.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import pathlib
  15. from pathlib import Path
  16. from typing import TYPE_CHECKING, Optional, Union
  17. from playwright._impl._artifact import Artifact
  18. if TYPE_CHECKING: # pragma: no cover
  19. from playwright._impl._page import Page
  20. class Download:
  21. def __init__(
  22. self, page: "Page", url: str, suggested_filename: str, artifact: Artifact
  23. ) -> None:
  24. self._page = page
  25. self._loop = page._loop
  26. self._dispatcher_fiber = page._dispatcher_fiber
  27. self._url = url
  28. self._suggested_filename = suggested_filename
  29. self._artifact = artifact
  30. def __repr__(self) -> str:
  31. return f"<Download url={self.url!r} suggested_filename={self.suggested_filename!r}>"
  32. @property
  33. def page(self) -> "Page":
  34. return self._page
  35. @property
  36. def url(self) -> str:
  37. return self._url
  38. @property
  39. def suggested_filename(self) -> str:
  40. return self._suggested_filename
  41. async def delete(self) -> None:
  42. await self._artifact.delete()
  43. async def failure(self) -> Optional[str]:
  44. return await self._artifact.failure()
  45. async def path(self) -> pathlib.Path:
  46. return await self._artifact.path_after_finished()
  47. async def save_as(self, path: Union[str, Path]) -> None:
  48. await self._artifact.save_as(path)
  49. async def cancel(self) -> None:
  50. return await self._artifact.cancel()