regex is not working in top level app #12

Open
akress opened this Issue May 1, 2010 · 1 comment

Comments

Projects
None yet
2 participants
@akress

akress commented May 1, 2010

When using subapplications regexes, the top level url definitions behave differently then regexes in subapplications. This is an example based on code in http://webpy.org/cookbook/subapp.

blog.py:

import web

urls = (
  "", "reblog",
  "/(blog|Blog)", "blog"  #change from original
)

class reblog:
    def GET(self): raise web.seeother('/')

class blog:
    def GET(self, path):
        return "blog " + path

app_blog = web.application(urls, locals())

code.py

import web
import blog
urls = (
  "/(blog|Blog)", blog.app_blog,  #change from original
   #  "/blog", blog.app_blog,
  "/(.*)", "index"
)

class index:
    def GET(self, path):
        return "hello " + path

app = web.application(urls, locals())

if __name__ == "__main__":
    app.run()

The regex /(blog|Blog) is the same in blog.py and code.py however request GET http://localhost:1111/blog/Blog does not return the expected result. If the regex is commented out in code.py and replaced with the line below it GET request returns an expected result.

@DaGoodBoy

This comment has been minimized.

Show comment Hide comment
@DaGoodBoy

DaGoodBoy Oct 10, 2011

I stumbled onto this as well. It appears that while local classes can use regex for URL handling, subapplication URL handling works only with literal strings. Looking in web/application.py around line 421 in the _match() function, the match test uses a startswith() rather than a regex function. This torpedos the design I've been working on, because I assumed (ass == me) that webpy would treat them consistently. So instead of doing URI rest calls that make sense to me:

  • /container/{item-hash}
  • /container/{item-hash}/summary
  • /container/{item-hash}/detail

I have to put static discriminators first so they can get passed off to subapplication classes:

  • /container/{item-hash}
  • /container/summary/{item-hash}
  • /container/detail/{item-hash}

I'm not sure what the purpose was in the inconsistency. When this was implemented, did something prevent treating local classes and subapplications differently? I don't see any comments that might shed light on the code.

I stumbled onto this as well. It appears that while local classes can use regex for URL handling, subapplication URL handling works only with literal strings. Looking in web/application.py around line 421 in the _match() function, the match test uses a startswith() rather than a regex function. This torpedos the design I've been working on, because I assumed (ass == me) that webpy would treat them consistently. So instead of doing URI rest calls that make sense to me:

  • /container/{item-hash}
  • /container/{item-hash}/summary
  • /container/{item-hash}/detail

I have to put static discriminators first so they can get passed off to subapplication classes:

  • /container/{item-hash}
  • /container/summary/{item-hash}
  • /container/detail/{item-hash}

I'm not sure what the purpose was in the inconsistency. When this was implemented, did something prevent treating local classes and subapplications differently? I don't see any comments that might shed light on the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment