bg
[sso] / shaken_and_spit_out / pgfw / ScreenGrabber.py
1 from os import makedirs
2 from os.path import exists, join
3 from sys import exc_info
4 from time import strftime
5
6 from pygame import image
7
8 from GameChild import *
9 from Input import *
10
11 class ScreenGrabber(GameChild):
12
13 def __init__(self, game):
14 GameChild.__init__(self, game)
15 self.delegate = self.get_delegate()
16 self.load_configuration()
17 self.subscribe(self.save_display)
18
19 def load_configuration(self):
20 config = self.get_configuration("screen-captures")
21 self.save_path = config["path"]
22 self.file_name_format = config["file-name-format"]
23 self.file_extension = config["file-extension"]
24
25 def save_display(self, event):
26 if self.delegate.compare(event, "capture-screen"):
27 directory = self.save_path
28 try:
29 if not exists(directory):
30 makedirs(directory)
31 name = self.build_name()
32 path = join(directory, name)
33 capture = image.save(self.get_screen(), path)
34 self.print_debug("Saved screen capture to %s" % (path))
35 except:
36 self.print_debug("Couldn't save screen capture to %s, %s" %\
37 (directory, exc_info()[1]))
38
39 def build_name(self):
40 return "{0}.{1}".format(strftime(self.file_name_format),
41 self.file_extension)