42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
from os import makedirs
|
|
from os.path import exists, join
|
|
from sys import exc_info
|
|
from time import strftime
|
|
|
|
from pygame import image
|
|
|
|
from GameChild import *
|
|
from Input import *
|
|
|
|
class ScreenGrabber(GameChild):
|
|
|
|
def __init__(self, game):
|
|
GameChild.__init__(self, game)
|
|
self.delegate = self.get_delegate()
|
|
self.load_configuration()
|
|
self.subscribe(self.save_display)
|
|
|
|
def load_configuration(self):
|
|
config = self.get_configuration("screen-captures")
|
|
self.save_path = config["path"]
|
|
self.file_name_format = config["file-name-format"]
|
|
self.file_extension = config["file-extension"]
|
|
|
|
def save_display(self, event):
|
|
if self.delegate.compare(event, "capture-screen"):
|
|
directory = self.save_path
|
|
try:
|
|
if not exists(directory):
|
|
makedirs(directory)
|
|
name = self.build_name()
|
|
path = join(directory, name)
|
|
capture = image.save(self.get_screen(), path)
|
|
self.print_debug("Saved screen capture to %s" % (path))
|
|
except:
|
|
self.print_debug("Couldn't save screen capture to %s, %s" %\
|
|
(directory, exc_info()[1]))
|
|
|
|
def build_name(self):
|
|
return "{0}.{1}".format(strftime(self.file_name_format),
|
|
self.file_extension)
|