def current_or_redirect_to_login(dest_url): # String -> Task (HTTPRedirection | Exception) User muser = current() return with_default( fold(reject, reject, login_url(dest_url).fmap(redirect_response)), muser.fmap(resolve))
def _format(chunks,req,fn): parsed = parser(chunks, req, formatter) return fold( lambda err: Left(err), lambda (newchunks,r,v): Right((newchunks, r, fn(v))), parsed )
def current_or_redirect_to_login(dest_url): # String -> Task (HTTPRedirection | Exception) User muser = current() return with_default( fold(reject, reject, login_url(dest_url).fmap(redirect_response)), muser.fmap(resolve) )
def render_home(req, config): secret = (Right(config['secret']) if config.has_key('secret') else Left( KeyError('No secret found in config'))) contents = (secret.fmap( lambda s: ("<h1><a href=\"%s\">Your secret is: %s!</a></h1>" % (encode_path(HomeR()), s)))) return (fold(reject, lambda c: resolve(c).fmap(from_html), contents))
def assert_fail(self, parser, req): def _fail(msg): self.assertTrue(True) def _success(a): self.assertFalse( True, "Expected failure, was success (value = %s)" % (a,)) act = parser(req) return either.fold( _fail, _success, act )
def assert_success(self, exp, parser, req): def _fail(msg): self.assertTrue( False, "Expected success, was failure (message = %s)" % act.value ) def _success(a): self.assertEqual(a,exp, "Expected %s, was %s" % (a, exp) ) act = parser(req) return either.fold( _fail, _success, act )
def custom(label,fn): # String -> (String -> Either String a) -> ParseState -> Either String ParseState def _custom((seen,rest), req, result): if len(rest) == 0: return Left("Got to the end of the URL but wanted %s" % label) else: chunk = rest[0] return fold( lambda msg: Left("Parsing '%s' failed: %s" % (chunk, msg)) , lambda x: Right( ((seen + [chunk], rest[1:]), req, result(x)) ), fn(chunk) )
def _one_of(choices, chunks, req, formatter): if len(choices) == 0: return Left("No parser worked") else: parser = choices[0] parsed = parser(chunks,req,formatter) return fold( lambda err: _one_of(choices[1:], chunks, req, formatter), lambda ((seen,rest),_,result): ( Right(((seen,rest),_,result)) if len(rest) == 0 or rest == [""] else \ _one_of(choices[1:], chunks, req, formatter) ), parsed )
def render_home(req,config): secret = ( Right(config['secret']) if config.has_key('secret') else Left(KeyError('No secret found in config')) ) contents = ( secret.fmap( lambda s: ( "<h1><a href=\"%s\">Your secret is: %s!</a></h1>" % (encode_path(HomeR()), s) )) ) return ( fold( reject, lambda c: resolve(c).fmap(from_html), contents ) )
def parse(formatter, parser, req): # (a -> b) -> Parser -> Request -> Either String b """ Note: formatter MUST be curried if > 1 argument. """ url = req.path[1:] parsed = parser( ([], url.split('/')), req, formatter ) return fold( lambda msg: debug_log("fail", Left(msg)), lambda ((_,rest),req,result): ( debug_log("success", Right(result() if callable(result) else result) ) if len(rest) == 0 or rest == [""] else \ debug_log("fail", Left("Parsed URL, but '%s' was left over" % "/".join(rest)) ) ), parsed )
def _dispatch(req): return fold( always( reject(exc.HTTPNotFound()) ), router(req,config), parser(req) )