how to use auth.wiki "extra" and "env"

62 views
Skip to first unread message

dhmorgan

unread,
Jul 16, 2013, 11:59:45 PM7/16/13
to
I'm experimenting with auth.wiki() using the book's instructions; using web2py 2.60; created new simple app ("authwiki") via the admin wizard


I'm attempting to utilize "@(join:a,b,c)" in the body of my wiki page, having first set the controller to return auth.wiki(env=dict(join=lambda a,b,c:"%s-%s-%s" % (a,b,c)))

"@(join:a,b,c)" goes unrendered, as does "@join:a,b,c"

"@{join:a,b,c}" and "@{join:(a,b,c)} both produce an error indicating:

    ERROR: <lambda>() takes exactly 3 arguments (1 given)


In controllers/default.py: 

def index():
   return auth.wiki(env=dict(join=lambda a,b,c:"%s-%s-%s" % (a,b,c)))


I also tried the same using "extra" instead of "env" in the auth.wiki() call and got the same or similar results.


This is the actual body text:

- brackets and colon

    @{join:'how','you','doin'} 

- brackets, colon and parenths

    @{join:('how','you','doin')}

- parentheses and colon

    @(join:'how','you','doin')

- colon only

    @join:'how','you','doin'

Massimo Di Pierro

unread,
Jul 17, 2013, 5:21:35 AM7/17/13
to web...@googlegroups.com
The correct syntax as far as the wiki is concerned is

@{join:a,b,c}

The problem is that MARKMIN identifies the call to "join" and the args "a,b,c" but it does not parse the args. You will have to do that:

def myjoin(vars):
     items = vars.split(',')
     if len(items)==3: return '-'.join(items)
     return 'Error!'

def index():
    return auth.wiki(env=dict(join=myjoin)

Alan Etkin

unread,
Jul 17, 2013, 7:47:07 AM7/17/13
to web...@googlegroups.com
The correct syntax as far as the wiki is concerned is

@{join:a,b,c}

The problem is that MARKMIN identifies the call to "join" and the args "a,b,c" but it does not parse the args. You will have to do that:

def myjoin(vars):
     items = vars.split(',')
     if len(items)==3: return '-'.join(items)
     return 'Error!'

def index():
    return auth.wiki(env=dict(join=myjoin)

Then the book example needs to be fixed because the arguments are not parsed by the custom join function.

Also, it is confusing the use of the extra word in "... This calls the join function passed as extra with parameters ..." since there's an extra wiki argument used for additional markmin render functions (to process custom blocks), but that feature is not related to env.

Massimo Di Pierro

unread,
Jul 17, 2013, 8:11:58 AM7/17/13
to web...@googlegroups.com
Tis gives us two options: fix the docs or change the code. I may be easier to fix the code. How about we do:

@{join:1,2,3} we first parse as c = {'a':'join','b':'1,2,3') then we do args = ast.literal_eval('(%s)' % c['b'] and then we call env[c['a']](*args)?

Alan Etkin

unread,
Jul 17, 2013, 8:16:03 AM7/17/13
to web...@googlegroups.com
I have a possible correction here

https://github.com/mdipierro/web2py-book/pull/86

dhmorgan

unread,
Jul 17, 2013, 9:24:58 AM7/17/13
to web...@googlegroups.com
Thank you both for helping me out here.

Changing the lambda, as indicated in the pending book correction, works.


in controller:

    def index(): 
        return auth.wiki(env=dict(join=lambda a:"-".join(a.split(","))))


in wiki page body:    

    @{join:'this',that','the other'}

    @{join:how,you,doin'} 

result:

    'this'-'that'-'the other'

    how-you-doin'


Danny

Massimo Di Pierro

unread,
Jul 17, 2013, 10:55:20 AM7/17/13
to web...@googlegroups.com
I am thinking that for security reasons it may be better to leave the book un-changed and make it work as described by doing the parsing properly and safely.

Alan Etkin

unread,
Jul 17, 2013, 11:05:18 AM7/17/13
to web...@googlegroups.com
I am thinking that for security reasons it may be better to leave the book un-changed and make it work as described by doing the parsing properly and safely.

Agree. Nevermind my patch then.

BTW: perhaps the new implementation could support also keyword arguments and basic types (ints, string, float)

Massimo Di Pierro

unread,
Jul 17, 2013, 11:14:25 AM7/17/13
to web...@googlegroups.com
I agree. It should  also support the use of variable already passed to the MARKMIN environment. Yet it should not support expressions and python code. All of this may not be easy since we do not want to user eval.

Alan Etkin

unread,
Jul 17, 2013, 1:07:27 PM7/17/13
to web...@googlegroups.com
El miércoles, 17 de julio de 2013 12:14:25 UTC-3, Massimo Di Pierro escribió:
I agree. It should  also support the use of variable already passed to the MARKMIN environment. Yet it should not support expressions and python code. All of this may not be easy since we do not want to user eval.

I have opened two issues (one for fixing the unparsed args and the other for the enhancements)

http://code.google.com/p/web2py/issues/detail?id=1589
http://code.google.com/p/web2py/issues/detail?id=1590

Reply all
Reply to author
Forward
0 new messages