changeset 594:a9e9a7fbdbcf pyntnclick

Make debug a env variable
author Neil Muller <neil@dip.sun.ac.za>
date Sat, 11 Feb 2012 18:04:23 +0200
parents 1eb1537173ef
children 59f1ee3f5632
files pyntnclick/constants.py pyntnclick/main.py
diffstat 2 files changed, 28 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/pyntnclick/constants.py	Sat Feb 11 17:41:35 2012 +0200
+++ b/pyntnclick/constants.py	Sat Feb 11 18:04:23 2012 +0200
@@ -1,6 +1,17 @@
 # Useful constants
 # copyright boomslang team (see COPYRIGHT file for details)
 
+import os
+
+DEBUG_ENVVAR = 'PYNTNCLICK_DEBUG'
+
+
+def _get_debug():
+    debug = os.getenv(DEBUG_ENVVAR, default=False)
+    if debug in [False, 'False', '0']:
+        return False
+    return True
+
 
 class GameConstants(object):
     screen = (800, 600)
@@ -12,7 +23,7 @@
     button_size = 50
     scene_size = (screen[0], screen[1] - button_size)
     frame_rate = 25
-    debug = False
+    debug = _get_debug()
 
     # User event IDs:
     enter = 1
--- a/pyntnclick/main.py	Sat Feb 11 17:41:35 2012 +0200
+++ b/pyntnclick/main.py	Sat Feb 11 18:04:23 2012 +0200
@@ -19,7 +19,7 @@
 from pyntnclick.menu import MenuScreen
 from pyntnclick.gamescreen import GameScreen
 from pyntnclick.endscreen import EndScreen
-from pyntnclick.constants import GameConstants
+from pyntnclick.constants import GameConstants, DEBUG_ENVVAR
 from pyntnclick.resources import Resources
 from pyntnclick.sound import Sound
 from pyntnclick import state
@@ -66,6 +66,7 @@
         self.resource = Resources(self._resource_module)
         self.sound = Sound(self.resource)
         self.constants = self.game_constants()
+        self.debug_options = []
 
     def initial_state(self):
         """Create a copy of the initial game state."""
@@ -84,6 +85,9 @@
         parser = OptionParser()
         parser.add_option("--no-sound", action="store_false", default=True,
                 dest="sound", help="disable sound")
+        # We flag these, so we can warn the user that these require debug mode
+        self.debug_options = ['--scene', '--no-rects', '--rect-drawer',
+                '--list-scenes', '--details']
         if self.constants.debug:
             parser.add_option("--scene", type="str", default=None,
                 dest="scene", help="initial scene")
@@ -100,8 +104,19 @@
                     dest="detail", help="Detailed view for rect_drawer")
         return parser
 
+    def warn_debug(self, option):
+        """Warn the user that he needs debug mode"""
+        print '%s is only valid in debug mode' % option
+        print 'set %s to enable debug mode' % DEBUG_ENVVAR
+        print
+
     def main(self):
         parser = self.option_parser()
+        # This is a bit hack'ish, but works
+        if not self.constants.debug:
+            for option in self.debug_options:
+                if option in sys.argv:
+                    self.warn_debug(option)
         opts, _ = parser.parse_args(sys.argv)
         pygame.display.init()
         pygame.font.init()