Add more save file helpers
[tabakrolletjie.git] / tabakrolletjie / utils.py
index bc5aef2572cf42e3c7ba20146fe2bd80f45974ad..6f9e133cb999cd57b20dd64f65f251f4516415e3 100644 (file)
@@ -2,11 +2,16 @@
 
 import functools
 import time
+import pygame.surface
+import pygame.locals as pgl
+import os
+import sys
 
 from .constants import DEBUG
+from .loader import loader
 
 
-def debug_timer(label):
+def debug_timer(label, debug=False):
     """ A decorator for printing how long a function took if debug is true.
     """
     def debug_inner(f):
@@ -17,7 +22,73 @@ def debug_timer(label):
                 return f(*args, **kw)
             finally:
                 duration = time.time() - start_time
-                if DEBUG:
+                if DEBUG or debug:
                     print "%s [%g seconds]" % (label, duration)
         return wrapper
     return debug_inner
+
+
+class DetailedTimer(object):
+    """ A detailed timer for overly complex functions.
+    """
+    def __init__(self, title, debug=False):
+        if DEBUG or debug:
+            self.title = title
+            self.times = []
+        else:
+            # fast funtions with the correct signature
+            self.start = int
+            self.show = str
+            self.lap = str
+            self.end = int
+
+    def start(self):
+        print "---- %s ----" % self.title
+        self.times.append(time.time())
+
+    def lap(self, label):
+        now = time.time()
+        print "  %s: %s" % (label, now - self.times[-1])
+        self.times.append(now)
+
+    def show(self, text):
+        print "  %s" % text
+
+    def end(self):
+        print "  %s: %s" % ("total", time.time() - self.times[0])
+        print "---- ----"
+
+
+def shadowed_text(text, font_name, size, offset=4):
+    font_black = loader.load_font(font_name, size=size)
+    font_white = loader.load_font(font_name, size=size)
+    background = font_black.render(text, True, (0, 0, 0))
+    foreground = font_white.render(text, True, (255, 255, 255))
+    new_size = (background.get_width() + offset,
+                background.get_height() + offset)
+    base = pygame.surface.Surface(new_size, pgl.SWSURFACE).convert_alpha()
+    base.fill((0, 0, 0, 0))
+    base.blit(background, (offset, offset), None)
+    base.blit(foreground, (0, 0), None)
+    return base
+
+
+def save_location():
+    """Return the directory for the save location."""
+    app = "tabakrolletjie"
+    if sys.platform.startswith('win'):
+        if 'APPDATA' in os.environ:
+            return os.path.join(os.environ['APPDATA'], app)
+        return os.path.join(os.path.expanduser('~'), '.' + app)
+    elif 'XDG_DATA_HOME' in os.environ:
+        return os.path.join(os.environ['XDG_DATA_HOME'], app)
+    return os.path.join(os.path.expanduser('~'), '.local', 'share', app)
+
+
+def get_save_file_name():
+    return os.path.join(save_location(), 'savegame.json')
+
+
+def save_file_exists():
+    savefile = get_save_file_name()
+    return os.path.isfile(savefile)