pgfw/pgfw/ScreenGrabber.py

36 lines
1.2 KiB
Python
Raw Normal View History

2012-07-07 08:34:49 -04:00
from os import makedirs
2012-07-05 04:21:49 -04:00
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.subscribe_to(self.get_custom_event_id(), self.save_display)
2012-07-05 04:21:49 -04:00
def save_display(self, event):
2012-07-07 08:34:49 -04:00
if self.is_command(event, "capture-screen"):
directory = self.get_configuration().get("screen-captures", "path")
2012-07-05 04:21:49 -04:00
try:
if not exists(directory):
2012-07-07 08:34:49 -04:00
makedirs(directory)
2012-07-05 04:21:49 -04:00
name = self.build_name()
path = join(directory, name)
capture = image.save(self.get_screen(), path)
2012-12-23 05:20:36 -05:00
self.print_debug("Saved screen capture to %s" % (path))
2012-07-05 04:21:49 -04:00
except:
2012-12-23 05:20:36 -05:00
self.print_debug("Couldn't save screen capture to %s, %s" %\
(directory, exc_info()[1]))
2012-07-05 04:21:49 -04:00
def build_name(self):
2012-07-07 08:34:49 -04:00
config = self.get_configuration().get_section("screen-captures")
prefix = config["file-name-format"]
extension = config["file-extension"]
return "{0}.{1}".format(strftime(prefix), extension)