/usr/share/games/singularity/code/safety.py is in singularity 0.30c-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #file: safety.py
#Copyright (C) 2008 FunnyMan3595
#This file is part of Endgame: Singularity.
#Endgame: Singularity is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#Endgame: Singularity is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with Endgame: Singularity; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#This file contains wrapper functions for making error-tolerant "safe" calls.
import logging
import time
import traceback
import sys
import os
import os.path
# We store error.log in .endgame on OSes with a HOME directory;
# otherwise, we store it in the CWD. As we're not importing any
# parts of E:S here, we have to reimplement this logic.
logpath = "error.log"
if os.environ.has_key("HOME"):
prefs_dir = os.path.expanduser("~/.endgame")
if not os.path.isdir(prefs_dir):
os.makedirs(prefs_dir)
logpath = os.path.join(prefs_dir, "error.log")
logging.getLogger().addHandler(logging.FileHandler(logpath))
class Buffer(object):
def __init__(self, prefix=""):
self.data = prefix
def write(self, unbuffered):
self.data += unbuffered
def get_timestamp(when=None):
if when == None:
when = time.time()
return time.ctime(when) + " " + time.tzname[time.daylight]
def log_error(error_message):
logging.getLogger().error(error_message)
sys.stderr.write(error_message + "\n")
def safe_call(func, args=(), kwargs={}, on_error=None):
try:
return func(*args, **kwargs)
except Exception, e:
if isinstance(e, SystemExit):
raise
buffer = Buffer("Exception in function %s at %s:\n"
% (func.__name__, get_timestamp()))
traceback.print_exc(file=buffer)
log_error(buffer.data)
# # ... --- ...
# import g
# g.play_sound("click")
# delays = (.15, .15, .8, .5, .5, .8, .15, .15)
# for delay in delays:
# time.sleep(delay)
# g.play_sound("click")
return on_error
# Catches any errors raised by a function, logs them, and returns the given
# value.
#
# Apply to a function like so:
# @safe(my_error_code)
# def my_function(...)
#
# And then:
# result = my_function(...)
# if result == my_error_code:
# # An error was raised.
def safe(on_error):
return lambda func: _safe(func, on_error)
def _safe(func, on_error):
return lambda *args, **kwargs: safe_call(func, args, kwargs, on_error)
|