hardware.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # $Id$
  2. from PyQt4 import QtCore
  3. from qt_utils import Signal
  4. class HardwareModel(QtCore.QAbstractTableModel):
  5. '''Base class for machine and extension models.
  6. '''
  7. _hardwareType = property() # abstract
  8. populating = Signal() # repopulation starts
  9. populated = Signal() # repopulation ends
  10. def __init__(self, bridge):
  11. QtCore.QAbstractTableModel.__init__(self)
  12. self._bridge = bridge
  13. self.__repliesLeft = None
  14. self._tempInfoDict = {} # for testing hardware
  15. self.__itemIter = None
  16. def __listReply(self, *items):
  17. self.__repliesLeft = len(items)
  18. self.__itemIter = iter(items)
  19. # start requesting info for all these hardware items
  20. self.__requestInfo()
  21. def __listFailed(self, message):
  22. print 'Failed to get list of %ss: %s' % (
  23. self._hardwareType, message
  24. )
  25. self.__allDone()
  26. def __allDone(self):
  27. self.__repliesLeft = None
  28. self.populated.emit()
  29. def __requestInfo(self):
  30. '''Requests information about the current machine.
  31. Current depends on the value of __itemIter
  32. '''
  33. item = self.__itemIter.next()
  34. self._bridge.command(
  35. 'openmsx_info', self._hardwareType + 's', item
  36. )(
  37. lambda *info: self.__infoReply(item, info),
  38. lambda message: self.__infoFailed(item, message)
  39. )
  40. def __machineIdReply(self, machineId, name):
  41. self._bridge.addMachineToIgnore(machineId)
  42. self._startHardwareTest(machineId, name)
  43. def _startHardwareTest(self, machineId, name):
  44. '''Implements the hardware specific test to check if it is working.
  45. '''
  46. raise NotImplementedError
  47. def __infoReply(self, name, info):
  48. infoDict = dict(info[i : i + 2] for i in xrange(0, len(info), 2))
  49. self._tempInfoDict = infoDict
  50. if self._testable:
  51. self._bridge.command('create_machine')(
  52. lambda machineId, name_ = name: self.__machineIdReply(machineId, name_),
  53. None
  54. )
  55. else:
  56. self.__testEnd(name)
  57. def __infoFailed(self, name, message):
  58. print 'Failed to get info about %s %s: %s' % (
  59. self._hardwareType, name, message
  60. )
  61. self.__infoDone()
  62. def _testDone(self, name, machineId, message, successful):
  63. # this is automatically done after the machine is deleted
  64. # in openMSX, see openmsx_control.py:
  65. #self._bridge.removeMachineToIgnore(machineId)
  66. print 'Test for: %s successful: %s' % (name, successful)
  67. if successful:
  68. self._tempInfoDict['working'] = 'Yes'
  69. else:
  70. self._tempInfoDict['working'] = 'No'
  71. self._tempInfoDict['brokenreason'] = message
  72. print 'Broken hardware found: %s %s: %s' % (
  73. self._hardwareType, name, message
  74. )
  75. self.__testEnd(name)
  76. def __testEnd(self, name):
  77. self._storeItem(name, self._tempInfoDict)
  78. self.__infoDone()
  79. def __infoDone(self):
  80. self.__repliesLeft -= 1
  81. if self.__repliesLeft == 0:
  82. self.__allDone()
  83. else:
  84. # process the next one!
  85. self.__requestInfo()
  86. def _clearItems(self):
  87. '''Clears the items stored in the model.
  88. Called at the start of repopulation.
  89. '''
  90. raise NotImplementedError
  91. def _storeItem(self, name, info):
  92. '''Stores the info about one particular hardware item.
  93. Called for each item when repopulating.
  94. '''
  95. raise NotImplementedError
  96. def repopulate(self):
  97. '''(Re)populate the model by querying openMSX.
  98. '''
  99. self.populating.emit()
  100. self.reset()
  101. self._clearItems()
  102. # Ask openMSX for list of hardware items.
  103. self._bridge.command(
  104. 'openmsx_info', self._hardwareType + 's'
  105. )(self.__listReply, self.__listFailed)