def send(self): # TODO Add demandvalid() msg = EmailMessage() msg.set_content(self.text) msg['Subject'] = self.subject msg['From'] = self.from_.toaddress() msg['To'] = self.to.toaddresses() accts = configfile.getinstance().accounts accts = getattr(accts.smtpaccounts, self.type) for acct in accts: try: smtp = smtplib.SMTP(acct.host, acct.port) #smtp.set_debuglevel(1) smtp.login(acct.username, acct.password) smtp.send_message(msg) smtp.quit() except Exception: # Don't include username here because it can be the same # as password (e.g., smtp.postmarkapp.com). msg = 'Failed sending email using: ' + \ str(acct.host) + ':' + str(acct.port) self.log.exception(msg) continue break
def run(self, tu=None): testclass, testmethod, *_ = tu.split('.') + [None ] if tu else [None] * 2 cfg = configfile.getinstance() if cfg.isloaded and cfg.inproduction: raise Exception("Won't run in production environment.") for subcls in tester.__subclasses__(): if testclass and subcls.__name__ != testclass: continue inst = subcls() inst.testers = self self += inst for meth in subcls.__dict__.items(): if type(meth[1]) != FunctionType: continue if meth[0][0] == '_': continue if testmethod and testmethod != meth[0]: continue try: eargs = invoketesteventargs(meth) self.oninvoketest(self, eargs) getattr(inst, meth[0])() except Exception as ex: if self.breakonexception: print(ex) pdb.post_mortem(ex.__traceback__) inst._failures += failure(ex, assert_=meth[0]) print('')
def token(self): if self._token is None: d = {} for prop in 'exp', 'iss': v = getattr(self, '_' + prop) if v is not None: d[prop] = v secret = configfile.getinstance()['jwt-secret'] enc = pyjwt.encode(d, secret) self._token = enc.decode('utf-8') return self._token
def _getvalue(self, k): v = getattr(self, '_' + k) if v is None: if self.token: secret = configfile.getinstance()['jwt-secret'] d = pyjwt.decode(self.token, secret) try: return d[k] except KeyError: return None return None else: return v
def __call__(self, env, sres): statuscode = None log = None try: log = configfile.getinstance().logs.default except: pass try: self.clear() self._env = env self.demandvalid() reqdata = self.requestdata cls, meth = self.class_, self.method obj = cls(self) data = getattr(obj, meth)() data = [] if data == None else data try: br = data['__brokenrules'] if len(br): statuscode = '422 Unprocessable Entity' except KeyError: # If action return no __brokenrules data['__brokenrules'] = '' data['__exception'] = None except Exception as ex: if self.breakonexception: if not isinstance(ex, httperror): print(ex) pdb.post_mortem(ex.__traceback__) if log: log.exception('') if isinstance(ex, httperror): statuscode = ex.statuscode else: statuscode = '500 Internal Server Error' # Get the stack trace tb = traceback.format_exception(etype=None, value=None, tb=ex.__traceback__) # The top and bottom of the stack trace don't correspond to frames, so remove them tb.pop() tb.pop(0) tb = [re.split('\n +', f.strip()) for f in tb] data = { '__exception': type(ex).__name__ + ': ' + str(ex), '__traceback': tb, '__brokenrules': [] } else: if not statuscode: statuscode = '200 OK' finally: # Log try: d, env = self.requestdata, self.environment addr, cls, meth, = env['REMOTE_ADDR'], d['__class'], d[ '__method'] args, st, ua = str( d['__args']), statuscode[:3], env['HTTP_USER_AGENT'] log.info( application.Logformat.format(addr, cls, meth, '', st, ua)) log.debug( application.Logformat.format(addr, cls, meth, args, st, ua)) except: pass # Return data data = json.dumps(data) data = bytes(data, 'utf-8') resheads = [ ('Content-Length', str(len(data))), ('Content-Type', 'application/json'), ('Access-Control-Allow-Origin', '*'), ] sres(statuscode, resheads) return iter([data])
def __init__(self): super().__init__() cfg = configfile.getinstance() accts = cfg.accounts.mysqlaccounts for acct in accts: self += connection(acct)
def log(self): # Defer import to avoid circular dependency from configfile import configfile return configfile.getinstance().logs.default