Add more save file helpers
[tabakrolletjie.git] / tabakrolletjie / utils.py
1 """ Tabakutilities. """
2
3 import functools
4 import time
5 import pygame.surface
6 import pygame.locals as pgl
7 import os
8 import sys
9
10 from .constants import DEBUG
11 from .loader import loader
12
13
14 def debug_timer(label, debug=False):
15     """ A decorator for printing how long a function took if debug is true.
16     """
17     def debug_inner(f):
18         @functools.wraps(f)
19         def wrapper(*args, **kw):
20             start_time = time.time()
21             try:
22                 return f(*args, **kw)
23             finally:
24                 duration = time.time() - start_time
25                 if DEBUG or debug:
26                     print "%s [%g seconds]" % (label, duration)
27         return wrapper
28     return debug_inner
29
30
31 class DetailedTimer(object):
32     """ A detailed timer for overly complex functions.
33     """
34     def __init__(self, title, debug=False):
35         if DEBUG or debug:
36             self.title = title
37             self.times = []
38         else:
39             # fast funtions with the correct signature
40             self.start = int
41             self.show = str
42             self.lap = str
43             self.end = int
44
45     def start(self):
46         print "---- %s ----" % self.title
47         self.times.append(time.time())
48
49     def lap(self, label):
50         now = time.time()
51         print "  %s: %s" % (label, now - self.times[-1])
52         self.times.append(now)
53
54     def show(self, text):
55         print "  %s" % text
56
57     def end(self):
58         print "  %s: %s" % ("total", time.time() - self.times[0])
59         print "---- ----"
60
61
62 def shadowed_text(text, font_name, size, offset=4):
63     font_black = loader.load_font(font_name, size=size)
64     font_white = loader.load_font(font_name, size=size)
65     background = font_black.render(text, True, (0, 0, 0))
66     foreground = font_white.render(text, True, (255, 255, 255))
67     new_size = (background.get_width() + offset,
68                 background.get_height() + offset)
69     base = pygame.surface.Surface(new_size, pgl.SWSURFACE).convert_alpha()
70     base.fill((0, 0, 0, 0))
71     base.blit(background, (offset, offset), None)
72     base.blit(foreground, (0, 0), None)
73     return base
74
75
76 def save_location():
77     """Return the directory for the save location."""
78     app = "tabakrolletjie"
79     if sys.platform.startswith('win'):
80         if 'APPDATA' in os.environ:
81             return os.path.join(os.environ['APPDATA'], app)
82         return os.path.join(os.path.expanduser('~'), '.' + app)
83     elif 'XDG_DATA_HOME' in os.environ:
84         return os.path.join(os.environ['XDG_DATA_HOME'], app)
85     return os.path.join(os.path.expanduser('~'), '.local', 'share', app)
86
87
88 def get_save_file_name():
89     return os.path.join(save_location(), 'savegame.json')
90
91
92 def save_file_exists():
93     savefile = get_save_file_name()
94     return os.path.isfile(savefile)