예제 #1
0
파일: user.py 프로젝트: ericgj/fungi
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))
예제 #2
0
파일: parse.py 프로젝트: ericgj/fungi
 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
   )
예제 #3
0
파일: user.py 프로젝트: ericgj/fungi
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)
  )
예제 #4
0
파일: config.py 프로젝트: ericgj/fungi
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))
예제 #5
0
파일: test_parse.py 프로젝트: ericgj/fungi
  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 )
예제 #6
0
파일: test_parse.py 프로젝트: ericgj/fungi
  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 )
예제 #7
0
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)
      )
예제 #8
0
파일: parse.py 프로젝트: ericgj/fungi
 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
     )
예제 #9
0
파일: config.py 프로젝트: ericgj/fungi
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
    )
  )
예제 #10
0
파일: parse.py 프로젝트: ericgj/fungi
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
  )
예제 #11
0
파일: core.py 프로젝트: ericgj/fungi
 def _dispatch(req):
   return fold(
     always( reject(exc.HTTPNotFound()) ),
     router(req,config),
     parser(req)
   )