/Tools/pynche/Switchboard.py

http://unladen-swallow.googlecode.com/ · Python · 139 lines · 117 code · 3 blank · 19 comment · 10 complexity · 3cf5e760888771b90df9c7cc31b3efe7 MD5 · raw file

  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. from types import DictType
  35. import marshal
  36. class Switchboard:
  37. def __init__(self, initfile):
  38. self.__initfile = initfile
  39. self.__colordb = None
  40. self.__optiondb = {}
  41. self.__views = []
  42. self.__red = 0
  43. self.__green = 0
  44. self.__blue = 0
  45. self.__canceled = 0
  46. # read the initialization file
  47. fp = None
  48. if initfile:
  49. try:
  50. try:
  51. fp = open(initfile)
  52. self.__optiondb = marshal.load(fp)
  53. if not isinstance(self.__optiondb, DictType):
  54. print >> sys.stderr, \
  55. 'Problem reading options from file:', initfile
  56. self.__optiondb = {}
  57. except (IOError, EOFError, ValueError):
  58. pass
  59. finally:
  60. if fp:
  61. fp.close()
  62. def add_view(self, view):
  63. self.__views.append(view)
  64. def update_views(self, red, green, blue):
  65. self.__red = red
  66. self.__green = green
  67. self.__blue = blue
  68. for v in self.__views:
  69. v.update_yourself(red, green, blue)
  70. def update_views_current(self):
  71. self.update_views(self.__red, self.__green, self.__blue)
  72. def current_rgb(self):
  73. return self.__red, self.__green, self.__blue
  74. def colordb(self):
  75. return self.__colordb
  76. def set_colordb(self, colordb):
  77. self.__colordb = colordb
  78. for v in self.__views:
  79. if hasattr(v, 'colordb_changed'):
  80. v.colordb_changed(colordb)
  81. self.update_views_current()
  82. def optiondb(self):
  83. return self.__optiondb
  84. def save_views(self):
  85. # save the current color
  86. self.__optiondb['RED'] = self.__red
  87. self.__optiondb['GREEN'] = self.__green
  88. self.__optiondb['BLUE'] = self.__blue
  89. for v in self.__views:
  90. if hasattr(v, 'save_options'):
  91. v.save_options(self.__optiondb)
  92. # save the name of the file used for the color database. we'll try to
  93. # load this first.
  94. self.__optiondb['DBFILE'] = self.__colordb.filename()
  95. fp = None
  96. try:
  97. try:
  98. fp = open(self.__initfile, 'w')
  99. except IOError:
  100. print >> sys.stderr, 'Cannot write options to file:', \
  101. self.__initfile
  102. else:
  103. marshal.dump(self.__optiondb, fp)
  104. finally:
  105. if fp:
  106. fp.close()
  107. def withdraw_views(self):
  108. for v in self.__views:
  109. if hasattr(v, 'withdraw'):
  110. v.withdraw()
  111. def canceled(self, flag=1):
  112. self.__canceled = flag
  113. def canceled_p(self):
  114. return self.__canceled