user_interface.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """
  2. # TOP2049 Open Source programming suite
  3. #
  4. # Generic user interface support
  5. #
  6. # Copyright (c) 2009-2010 Michael Buesch <m@bues.ch>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License along
  19. # with this program; if not, write to the Free Software Foundation, Inc.,
  20. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. """
  22. import sys
  23. class AbstractUserInterface:
  24. "Abstract user interface class. Subclass it."
  25. # Progress meter IDs
  26. PROGRESSMETER_CHIPACCESS = 0 # Programming/reading progress meter
  27. PROGRESSMETER_USER = 100 # IDs equal or bigger than this are available to the app
  28. def progressMeterInit(self, meterId, message, nrSteps):
  29. pass # Do nothing by default
  30. def progressMeterFinish(self, meterId):
  31. pass # Do nothing by default
  32. def progressMeter(self, meterId, step):
  33. pass # Do nothing by default
  34. def warningMessage(self, message):
  35. pass # Do nothing by default
  36. def infoMessage(self, message):
  37. pass # Do nothing by default
  38. def debugMessage(self, message):
  39. pass # Do nothing by default
  40. class ConsoleUserInterface(AbstractUserInterface):
  41. "Console based user interface."
  42. def progressMeterInit(self, meterId, message, nrSteps):
  43. if meterId != self.PROGRESSMETER_CHIPACCESS:
  44. return
  45. self.progressNrSteps = nrSteps
  46. self.progress = list(range(0, 100))
  47. self.__genericConsoleMessage(message + " [0%", newline=False)
  48. def progressMeterFinish(self, meterId):
  49. if meterId != self.PROGRESSMETER_CHIPACCESS:
  50. return
  51. if not self.progressNrSteps:
  52. self.progressMeter(meterId, 0)
  53. self.__genericConsoleMessage("100%]")
  54. def progressMeter(self, meterId, step):
  55. if meterId != self.PROGRESSMETER_CHIPACCESS:
  56. return
  57. if self.progressNrSteps:
  58. percent = (step * 100 // self.progressNrSteps)
  59. else:
  60. percent = 100
  61. for progress in list(self.progress):
  62. if progress > percent:
  63. break
  64. if progress == 25:
  65. self.__genericConsoleMessage("25%", newline=False)
  66. elif progress == 50:
  67. self.__genericConsoleMessage("50%", newline=False)
  68. elif progress == 75:
  69. self.__genericConsoleMessage("75%", newline=False)
  70. elif progress % 2 == 0:
  71. self.__genericConsoleMessage(".", newline=False)
  72. self.progress.remove(progress)
  73. def __genericConsoleMessage(self, message, newline=True):
  74. if newline:
  75. print(message)
  76. else:
  77. sys.stdout.write(message)
  78. sys.stdout.flush()
  79. def warningMessage(self, message):
  80. self.__genericConsoleMessage(message)
  81. def infoMessage(self, message):
  82. self.__genericConsoleMessage(message)
  83. def debugMessage(self, message):
  84. self.__genericConsoleMessage(message)