/mothership/tools/configtool.py

https://github.com/excid3/chromium · Python · 122 lines · 67 code · 20 blank · 35 comment · 8 complexity · a03287dbe9f9836bc944b3e0d86f92fa MD5 · raw file

  1. #!/usr/bin/env python
  2. # Copyright (c) 2001, Stanford University
  3. # All rights reserved.
  4. #
  5. # See the file LICENSE.txt for information on redistributing this software.
  6. #
  7. # Authors:
  8. # Brian Paul
  9. """This is the top-level application module for the graphical config tool."""
  10. import os, sys
  11. from wxPython.wx import *
  12. import graph, crutils
  13. #----------------------------------------------------------------------------
  14. class ConfigApp(wxApp):
  15. """ The main application object.
  16. """
  17. def OnInit(self):
  18. """ Initialise the application.
  19. """
  20. wxInitAllImageHandlers()
  21. assert self
  22. self.DocList = []
  23. if len(sys.argv) == 1:
  24. # No file name was specified on the command line -> start with a
  25. # blank document.
  26. frame = graph.GraphFrame(None, -1, graph.TitleString)
  27. #frame.Centre()
  28. frame.Show(TRUE)
  29. self.DocList.append(frame)
  30. else:
  31. # Load the file(s) specified on the command line.
  32. for arg in sys.argv[1:]:
  33. fileName = os.path.join(os.getcwd(), arg)
  34. if os.path.isfile(fileName):
  35. title = graph.TitleString + ": " + os.path.basename(fileName)
  36. frame = graph.GraphFrame(None, -1, title)
  37. if frame.loadConfig(fileName):
  38. frame.Show(TRUE)
  39. self.DocList.append(frame)
  40. else:
  41. frame.Destroy()
  42. return FALSE
  43. return TRUE
  44. #----------------------------------------------------------------------------
  45. class ExceptionHandler:
  46. """ A simple error-handling class to write exceptions to a text file.
  47. Under MS Windows, the standard DOS console window doesn't scroll and
  48. closes as soon as the application exits, making it hard to find and
  49. view Python exceptions. This utility class allows you to handle Python
  50. exceptions in a more friendly manner.
  51. """
  52. def __init__(self):
  53. """ Standard constructor.
  54. """
  55. self._buff = ""
  56. if os.path.exists("errors.txt"):
  57. os.remove("errors.txt") # Delete previous error log, if any.
  58. def write(self, s):
  59. """ Write the given error message to a text file.
  60. Note that if the error message doesn't end in a carriage return, we
  61. have to buffer up the inputs until a carriage return is received.
  62. """
  63. if (s[-1] != "\n") and (s[-1] != "\r"):
  64. self._buff = self._buff + s
  65. return
  66. try:
  67. s = self._buff + s
  68. self._buff = ""
  69. if s[:9] == "Traceback":
  70. # Tell the user than an exception occurred.
  71. wxMessageBox("An internal error has occurred.\nPlease " + \
  72. "refer to the 'errors.txt' file for details.",
  73. "Error", wxOK | wxCENTRE | wxICON_EXCLAMATION)
  74. f = open("errors.txt", "a")
  75. f.write(s)
  76. f.close()
  77. except:
  78. pass # Don't recursively crash on errors.
  79. #----------------------------------------------------------------------------
  80. def main():
  81. """ Start up the configuration tool.
  82. """
  83. # Redirect python exceptions to a log file.
  84. # XXX if we crash upon startup, try commenting-out this next line:
  85. # sys.stderr = ExceptionHandler()
  86. # Set the default site file
  87. #crutils.SetDefaultSiteFile("tungsten.crsite")
  88. # Find available SPU classes, print the list
  89. spuClasses = crutils.FindSPUNames()
  90. print "Found SPU classes: %s" % str(spuClasses)
  91. # Create and start the application.
  92. graph.App = ConfigApp(0)
  93. graph.App.MainLoop()
  94. if __name__ == "__main__":
  95. main()