Emile Anclin wrote:
On Monday 15 November 2010 21:40:17 Okko Willeboordse wrote:
Hello,

hi,

We have a tool that runs scripts created by users.

Scripts are run using eval in some namespace with entities in it such
that users can use a convenient syntax.

Following illustrates this;
def function(i):
  print i

scriptglobals = {}
scriptglobals['okko'] = function

script_created_by_user = \
'''
i = 5
okko(i)
'''

eval(compile(script_created_by_user, 'file name', 'exec'),
scriptglobals)


Now we want to lint the user created script.

Running lint with the script obviously fails because the namespace is
missing.


Any ideas how we can pull this off?

I don't know if there is a "classical" way to do it;
but maybe you could overwrite lint.Pylinter.get_astng to return logilab.astng.builder.ASTNGBuilder.string_build(script_created_by_user)

But that's only half of the job; you will need to use the --init-hook
option to activate the scriptglobals

hope, that helps ;)

Another way could be to slightly rework the user script before passing it to pylint.
Temporarily adding

from foo import okko

or

def func(*args, **kwargs) :pass
okko = func

at the begining of the file.


There's also an option with pylint to delclare some names as global: additional-builtins. Adding it to your .pylintrc file is your best bet I guess.

JM
_______________________________________________
Python-Projects mailing list
[email protected]
http://lists.logilab.org/mailman/listinfo/python-projects

Reply via email to