diff --git a/NEWS.txt b/NEWS.txt index 404a0549a..49046babb 100644 --- a/NEWS.txt +++ b/NEWS.txt @@ -16,6 +16,7 @@ * Main window is now emitting a "selection_updated" signal, plugin api version bumps to 1.3.0 * Append system information to user-agent string * Compilation tag/variable aligned with iTunes, set only for Various Artists type compilations. + * autodetect the CD drive on Mac OS X (PICARD-123) * Ignore directories and files while indexing when show_hidden_files option is set to False (PICARD-528) * Add ignore_regex option which allows one to ignore matching paths, can be set in Options > Advanced (PICARD-528) * Added an "artists" tag to track metadata, based on the one in Jaikoz, which diff --git a/picard/util/cdrom.py b/picard/util/cdrom.py index 4b0cef41b..d506c6633 100644 --- a/picard/util/cdrom.py +++ b/picard/util/cdrom.py @@ -19,53 +19,66 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. import sys -from PyQt4.QtCore import QFile, QRegExp +if sys.platform == 'win32': + from ctypes import windll + +from PyQt4.QtCore import QFile, QRegExp, QIODevice, QString + +from picard import config from picard.util import uniqify -DEFAULT_DRIVES = [] try: + from libdiscid.compat import discid +except ImportError: try: - from libdiscid.compat import discid + import discid except ImportError: - try: - import discid - except ImportError: - discid = None - if discid is not None: - device = discid.get_default_device() - if device: - DEFAULT_DRIVES = [device] -except: - import traceback - print(traceback.format_exc()) + discid = None +DEFAULT_DRIVES = [] +if discid is not None: + device = discid.get_default_device() + if device: + DEFAULT_DRIVES.append(device) + LINUX_CDROM_INFO = '/proc/sys/dev/cdrom/info' +# if get_cdrom_drives() lists ALL drives available on the machine if sys.platform == 'win32': AUTO_DETECT_DRIVES = True - from ctypes import windll - GetLogicalDrives = windll.kernel32.GetLogicalDrives - GetDriveType = windll.kernel32.GetDriveTypeA - DRIVE_CDROM = 5 +elif sys.platform == 'linux2' and QFile.exists(LINUX_CDROM_INFO): + AUTO_DETECT_DRIVES = True +else: + # There might be more drives we couldn't detect + # setting uses a text field instead of a drop-down + AUTO_DETECT_DRIVES = False - def get_cdrom_drives(): - drives = list(DEFAULT_DRIVES) + +def _split_values(s): + """split a space separated list""" + return QString(s).trimmed().split(QRegExp("\\s+"), QString.SkipEmptyParts) + + +def get_cdrom_drives(): + """List available disc drives on the machine + """ + # add default drive from libdiscid to the list + drives = list(DEFAULT_DRIVES) + + if sys.platform == 'win32': + GetLogicalDrives = windll.kernel32.GetLogicalDrives + GetDriveType = windll.kernel32.GetDriveTypeA + DRIVE_CDROM = 5 mask = GetLogicalDrives() for i in range(26): if mask >> i & 1: drive = chr(i + ord("A")) + ":" if GetDriveType(drive) == DRIVE_CDROM: drives.append(drive) - return sorted(uniqify(drives)) -elif sys.platform == 'linux2' and QFile.exists(LINUX_CDROM_INFO): - AUTO_DETECT_DRIVES = True - from PyQt4.QtCore import QIODevice, QString - - # Read info from /proc/sys/dev/cdrom/info - def get_cdrom_drives(): - drives = list(DEFAULT_DRIVES) + elif sys.platform == 'linux2' and QFile.exists(LINUX_CDROM_INFO): + # Read info from /proc/sys/dev/cdrom/info cdinfo = QFile(LINUX_CDROM_INFO) if cdinfo.open(QIODevice.ReadOnly | QIODevice.Text): drive_names = [] @@ -75,11 +88,11 @@ elif sys.platform == 'linux2' and QFile.exists(LINUX_CDROM_INFO): if line.indexOf(':') != -1: key, values = line.split(':') if key == 'drive name': - drive_names = QString(values).trimmed().split(QRegExp("\\s+"), QString.SkipEmptyParts) + drive_names = _split_values(values) elif key == 'Can play audio': - drive_audio_caps = [v == '1' for v in - QString(values).trimmed().split(QRegExp("\\s+"), QString.SkipEmptyParts)] - break # no need to continue passed this line + drive_audio_caps = [v == '1' + for v in _split_values(values)] + break # no need to continue past this line line = cdinfo.readLine() # Show only drives that are capable of playing audio for index, drive in enumerate(drive_names): @@ -89,13 +102,13 @@ elif sys.platform == 'linux2' and QFile.exists(LINUX_CDROM_INFO): if symlink_target != '': device = symlink_target drives.append(device) - return sorted(uniqify(drives)) -else: - AUTO_DETECT_DRIVES = False + else: + for device in config.setting["cd_lookup_device"].split(","): + # Need to filter out empty strings, + # particularly if the device list is empty + if device.strip() != u'': + drives.append(device.strip()) - def get_cdrom_drives(): - from picard import config - # Need to filter out empty strings, particularly if the device list is empty - return filter(lambda string: (string != u''), - [d.strip() for d in config.setting["cd_lookup_device"].split(",")]) + # make sure no drive is listed twice (given by multiple sources) + return sorted(uniqify(drives))