def __save_config(self, filename, fields, suppress=False): """ This saves a dictionary to json """ f = '' with open(self.scrib_dir + filename, 'w') as out: f = f + str(json.dump(fields, out, indent=4)) if suppress is False: barf.Barf('SAV', self.scrib_dir + filename + ' successfully saved.')
def __load_config(self, filename, suppress=False): try: f = open(self.scrib_dir + filename, 'r') except IOError: return None try: config = json.load(f) f.close() except: barf.Barf('ERR', '%s%s could not be loaded.' % (self.scrib_dir, filename)) barf.Barf('ERR', 'Please make sure it is properly formatted.') sys.exit() if suppress is False: barf.Barf('MSG', self.scrib_dir + filename + ' successfully loaded.') return config
def test_barf_def_notime(): barf.Barf('DEF', 'Barf test', time=False)
def _unfilter(self, message): """ Also taken from Scrib 1.1.0 and needs to be tuned. """ # Had to write my own initial capitalizing code *sigh* message = "%s%s" % (message[:1].upper(), message[1:]) # Fixes punctuation message = message.replace(" ?", "?") message = message.replace(" !", "!") message = message.replace(" .", ".") message = message.replace(" ,", ",") message = message.replace(" : ", ": ") message = message.replace(" ; ", "; ") # Fixes I and I contractions message = message.replace(" i ", " I ") message = message.replace(" i'", " I'") message = message.replace("$C4", "||") message = message.replace("$b7", "|-:") message = message.replace("$b6", ";-|") message = message.replace("$b5", ":-|") message = message.replace("$b4", "|:") message = message.replace("$b3", ";|") message = message.replace("$b2", "=|") message = message.replace("$b1", ":|") # New emoticon filter that tries to catch almost all variations eyes, nose, mouth = r":;8BX=", r"-~'^O", r")(></\|CDPo39" # NOQA # Removed nose from the pattern for the sake of my sanity pattern1 = "[\s][%s][%s][\s]" % tuple(map(re.escape, [eyes, mouth])) pattern2 = "[\s][%s][%s][\s]" % tuple(map(re.escape, [mouth, eyes])) eye, horzmouth = r"^><vou*@#sxz~-=+", r"-_o.wv" pattern3 = "[\s][%s][%s][%s][\s]" % tuple( map(re.escape, [eye, horzmouth, eye])) # Add whitespace for less false positives; # sit will be stripped out of the string later if not message == "": message = " " + message + " " emoticon = re.search(pattern1, message, re.IGNORECASE) pattern = pattern1 if emoticon is None: emoticon = re.search(pattern2, message, re.IGNORECASE) pattern = pattern2 if emoticon is None: emoticon = re.search(pattern3, message, re.IGNORECASE) pattern = pattern3 # Init some strings so it does't barf later extra = "" emote = "" if emoticon is not None: emoticon = "%s" % emoticon.group() emotebeg = re.search(pattern, message, re.IGNORECASE).start() emoteend = re.search(pattern, message, re.IGNORECASE).end() # Remove the whitespace we added earlier message = message.strip() if not emotebeg == 0: emotebeg = emotebeg - 1 if emotebeg == 0: emoteend = emoteend - 2 emote = message[emotebeg:emoteend] if self.debug is 1: barf.Barf('DBG', "Clean._unfilter: Emote found: %s" % emote) new_message = message[:emotebeg] extra = message[emoteend:] message = new_message # Fixes the annoying XP capitalization in words... message = message.replace("XP", "xp") message = message.replace(" xp", " XP") message = message.replace("XX", "xx") if not message == "": # Remove whitespace if it wasn't removed earlier message = message.strip() if message.endswith(','): message = message[:-1] if not message.endswith(('.', '!', '?')): message = message + "." if not extra == "": extra = extra[1:] extra = "%s%s" % (extra[:1].upper(), extra[1:]) if not extra.endswith(('.', '!', '?')): extra = extra + "." extra = extra + " " if not emote == "": message = message + extra + emote return message
def test_barf_rol_24h(): barf.Barf('ROL', 'Barf test', time=True, hour=False)
def test_barf_err_notime(): barf.Barf('ERR', 'Barf test', time=False)
def test_barf_def_24h(): barf.Barf('DEF', 'Barf test', time=True, hour=False)
def test_barf_dbg_notime(): barf.Barf('DBG', 'Barf test', time=False)
def test_barf_msg_notime(): barf.Barf('MSG', 'Barf test', time=False)
def test_barf_msg_24h(): barf.Barf('MSG', 'Barf test', time=True, hour=False)
def test_barf_msg_12h(): barf.Barf('MSG', 'Barf test')
def test_barf_act_notime(): barf.Barf('ACT', 'Barf test', time=False)
def test_barf_act_24h(): barf.Barf('ACT', 'Barf test', time=True, hour=False)
def test_barf_act_12h(): barf.Barf('ACT', 'Barf test')
def test_barf_dbg_12h(): barf.Barf('DBG', 'Barf test')
def test_barf_dbg_24h(): barf.Barf('DBG', 'Barf test', time=True, hour=False)
def test_barf_def_12h(): barf.Barf('DEF', 'Barf test')
def test_barf_err_12h(): barf.Barf('ERR', 'Barf test')
def test_barf_sav_12h(): barf.Barf('SAV', 'Barf test')
def test_barf_err_24h(): barf.Barf('ERR', 'Barf test', time=True, hour=False)
def test_barf_sav_24h(): barf.Barf('SAV', 'Barf test', time=True, hour=False)
def test_barf_rol_12h(): barf.Barf('ROL', 'Barf test')
def test_barf_sav_notime(): barf.Barf('SAV', 'Barf test', time=False)
def test_barf_rol_notime(): barf.Barf('ROL', 'Barf test', time=False)
def test_barf_plg_12h(): barf.Barf('PLG', 'Barf test')
def test_barf_plg_24h(): barf.Barf('PLG', 'Barf test', time=True, hour=False)
def test_barf_plg_notime(): barf.Barf('PLG', 'Barf test', time=False)
#! /usr/bin/env python import datetime import os import sys from scrib import barf, cfg try: import redis except: barf.Barf('ERR', 'Please install redis module for python.') sys.exit() class Db: """ We need to support persistant connections instead of spamming the database with calls as we are doing now. """ def __init__(self): self.settings = cfg.set() # Don't set defaults here. self.settings.load('scrib.cfg', {}, suppress=True) self.barf = barf.Barf # Database vars self.host = self.settings.host self.port = self.settings.port self.tries = 0 self.max_tries = 2 self.prefix = 'scrib' # Connect to Scrib Collective
def test_barf_tab(): barf.Barf('TAB', 'Barf test')