Switchboard.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. """Switchboard class.
  2. This class is used to coordinate updates among all Viewers. Every Viewer must
  3. conform to the following interface:
  4. - it must include a method called update_yourself() which takes three
  5. arguments; the red, green, and blue values of the selected color.
  6. - When a Viewer selects a color and wishes to update all other Views, it
  7. should call update_views() on the Switchboard object. Note that the
  8. Viewer typically does *not* update itself before calling update_views(),
  9. since this would cause it to get updated twice.
  10. Optionally, Viewers can also implement:
  11. - save_options() which takes an optiondb (a dictionary). Store into this
  12. dictionary any values the Viewer wants to save in the persistent
  13. ~/.pynche file. This dictionary is saved using marshal. The namespace
  14. for the keys is ad-hoc; make sure you don't clobber some other Viewer's
  15. keys!
  16. - withdraw() which takes no arguments. This is called when Pynche is
  17. unmapped. All Viewers should implement this.
  18. - colordb_changed() which takes a single argument, an instance of
  19. ColorDB. This is called whenever the color name database is changed and
  20. gives a chance for the Viewers to do something on those events. See
  21. ListViewer for details.
  22. External Viewers are found dynamically. Viewer modules should have names such
  23. as FooViewer.py. If such a named module has a module global variable called
  24. ADDTOVIEW and this variable is true, the Viewer will be added dynamically to
  25. the `View' menu. ADDTOVIEW contains a string which is used as the menu item
  26. to display the Viewer (one kludge: if the string contains a `%', this is used
  27. to indicate that the next character will get an underline in the menu,
  28. otherwise the first character is underlined).
  29. FooViewer.py should contain a class called FooViewer, and its constructor
  30. should take two arguments, an instance of Switchboard, and optionally a Tk
  31. master window.
  32. """
  33. import sys
  34. import marshal
  35. class Switchboard:
  36. def __init__(self, initfile):
  37. self.__initfile = initfile
  38. self.__colordb = None
  39. self.__optiondb = {}
  40. self.__views = []
  41. self.__red = 0
  42. self.__green = 0
  43. self.__blue = 0
  44. self.__canceled = 0
  45. # read the initialization file
  46. fp = None
  47. if initfile:
  48. try:
  49. try:
  50. fp = open(initfile, 'rb')
  51. self.__optiondb = marshal.load(fp)
  52. if not isinstance(self.__optiondb, dict):
  53. print('Problem reading options from file:', initfile,
  54. file=sys.stderr)
  55. self.__optiondb = {}
  56. except (IOError, EOFError, ValueError):
  57. pass
  58. finally:
  59. if fp:
  60. fp.close()
  61. def add_view(self, view):
  62. self.__views.append(view)
  63. def update_views(self, red, green, blue):
  64. self.__red = red
  65. self.__green = green
  66. self.__blue = blue
  67. for v in self.__views:
  68. v.update_yourself(red, green, blue)
  69. def update_views_current(self):
  70. self.update_views(self.__red, self.__green, self.__blue)
  71. def current_rgb(self):
  72. return self.__red, self.__green, self.__blue
  73. def colordb(self):
  74. return self.__colordb
  75. def set_colordb(self, colordb):
  76. self.__colordb = colordb
  77. for v in self.__views:
  78. if hasattr(v, 'colordb_changed'):
  79. v.colordb_changed(colordb)
  80. self.update_views_current()
  81. def optiondb(self):
  82. return self.__optiondb
  83. def save_views(self):
  84. # save the current color
  85. self.__optiondb['RED'] = self.__red
  86. self.__optiondb['GREEN'] = self.__green
  87. self.__optiondb['BLUE'] = self.__blue
  88. for v in self.__views:
  89. if hasattr(v, 'save_options'):
  90. v.save_options(self.__optiondb)
  91. # save the name of the file used for the color database. we'll try to
  92. # load this first.
  93. self.__optiondb['DBFILE'] = self.__colordb.filename()
  94. fp = None
  95. try:
  96. try:
  97. fp = open(self.__initfile, 'wb')
  98. except IOError:
  99. print('Cannot write options to file:', \
  100. self.__initfile, file=sys.stderr)
  101. else:
  102. marshal.dump(self.__optiondb, fp)
  103. finally:
  104. if fp:
  105. fp.close()
  106. def withdraw_views(self):
  107. for v in self.__views:
  108. if hasattr(v, 'withdraw'):
  109. v.withdraw()
  110. def canceled(self, flag=1):
  111. self.__canceled = flag
  112. def canceled_p(self):
  113. return self.__canceled