Created
January 27, 2015 20:47
-
-
Save datamafia/ea8390685a8d74092514 to your computer and use it in GitHub Desktop.
Global Notification Object -work in progress-
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# http://datamafia.com | |
from time import gmtime, strftime | |
class notification: | |
notif = {} # container for all messages | |
summary_order = ( # list of notif types + order. Override as needed. | |
'log', | |
'user_good', | |
'user_bad', | |
'fatal', | |
'user_warning', | |
'error', | |
'exception', | |
) | |
def __init__(self): | |
pass | |
############################################################### | |
# Types of notifications @todo compact into lambda or dynamic # | |
############################################################### | |
def log(self, msg): | |
self.write_to_notif('log', msg) | |
return | |
def user_good(self, msg): | |
self.write_to_notif('user_good', msg) | |
return | |
def user_bad(self, msg): | |
self.write_to_notif('user_bad', msg) | |
return | |
def fatal(self, msg): | |
self.write_to_notif('fatal', msg) | |
return | |
def user_warning(self, msg): | |
self.write_to_notif('user_warning', msg) | |
return | |
def error(self, msg): | |
self.write_to_notif('error', msg) | |
return | |
def exception(self, msg, trace): | |
''' | |
Exception handler with optional (preferred) trace | |
Args: | |
msg : required, exception obj | |
trace : required, unique string to be appended to the | |
final output to search for exception in source | |
''' | |
msg.args += ('trace', trace ) | |
self.write_to_notif('exception', msg) | |
return | |
############################### | |
# Other methods and utilities # | |
############################### | |
def write_to_notif(self, key, value): | |
''' | |
Generic write for all notif | |
Args : | |
key : string, required, the key (type) of the message | |
value : string, required, the message associated with the instance | |
of the provided key | |
# dict of list w/tuples | |
''' | |
t = strftime("%Y-%m-%d_%H:%M%S", gmtime()) | |
try : | |
num_key = len(self.notif[key]) | |
except : | |
num_key = 0 # no zero offset, increment next | |
self.notif[key] = [] | |
num_key += 1 | |
self.notif[key].append( | |
(num_key, t, value) | |
) | |
def p(self, s, ref=''): | |
"""safe print to terminal""" | |
''' | |
* intended to be used in class, remove self for procedural style | |
* Used for notif lambda | |
Args : | |
s : string or whatever to print - required | |
ref : string, optional, used to reference where print occurs | |
Returns : | |
void | |
''' | |
try : | |
# obj check aka disqulaify by type. Different in 3.0 | |
if not isinstance(s, basestring) and not isinstance(s, int) \ | |
and not isinstance(s, dict) and not isinstance(s, tuple): | |
try: | |
s = s.__name__ | |
except : | |
pass | |
try : | |
s = s.encode('utf-8') | |
except : | |
pass | |
print '['+ref+'] '+str(s) | |
return | |
except Exception as e : | |
print e | |
pass # silent death | |
return | |
def summary(self, only=[]): | |
"""summary output of all notif data in a readable format""" | |
''' | |
Only prints when there is a notification, empty / None is skipped | |
Args : | |
only : list, optional, to ONLY use methods in list | |
Return : | |
void (prints to terminal) | |
''' | |
def sub_loop(typee, notif): | |
"""sub loop, looping on the key/type of notif""" | |
print '\r\nResults for '+typee+' notification : -----------------' | |
template = '%d.) %s [time:%s]' | |
for n in notif : | |
print template % (n[0], n[2], n[1]) | |
return | |
# print using predetermined order | |
for n in self.summary_order : | |
if only : | |
if n not in only : | |
continue | |
try : | |
if hasattr(self, n) and callable(getattr(self, n)) : | |
method = getattr(self, n) # is method | |
if n in self.notif : | |
sub_loop(n,self.notif[n]) | |
except : # Exception as e: | |
print 'method '+n+'() does not exists' | |
pass # be gentle |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment