cd.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright © 2024 Noah Vogt <noah@noahvogt.com>
  2. # This program is free software: you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation, either version 3 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License
  11. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. import os
  13. from utils import log
  14. if os.name == "nt":
  15. # pylint: disable=import-error
  16. import wmi # pyright: ignore
  17. else:
  18. from pycdio import DRIVER_DEVICE
  19. from cdio import (
  20. get_devices,
  21. Device,
  22. DriverUnsupportedError,
  23. DeviceException,
  24. )
  25. def get_cd_drives() -> list:
  26. if os.name == "nt":
  27. c = wmi.WMI()
  28. cd_drives = []
  29. for cd in c.Win32_CDROMDrive():
  30. cd_drives.append(cd.Drive)
  31. # pylint: disable=possibly-used-before-assignment
  32. else:
  33. cd_drives = get_devices(DRIVER_DEVICE)
  34. return cd_drives
  35. # pylint: disable=possibly-used-before-assignment
  36. def eject_drive(drive: Device) -> None: # pyright: ignore
  37. try:
  38. drive.eject_media() # pyright: ignore
  39. # pylint: disable=possibly-used-before-assignment
  40. except (DriverUnsupportedError, DeviceException):
  41. log(f"Eject of CD-ROM drive {drive} failed") # pyright: ignore