in Education by
In the context of a complex application, I need to import user-supplied 'scripts'. Ideally, a script would have def init(): blah def execute(): more blah def cleanup(): yadda so I'd just import imp fname, path, desc = imp.find_module(userscript) foo = imp.load_module(userscript, fname, path, desc) foo.init() However, as we all know, the user's script is executed as soon as load_module runs. Which means, a script can be something like this: def init(): blah yadda yielding to the yadda part being called as soon as I import the script. What I need is a way to: check first whether it has init(), execute() and cleanup() if they exist, all is well if they don't exist, complain don't run any other code, or at least not until I know there's no init() Normally I'd force the use the same old if __name__ == '__main__' trick, but I have little control on the user-supplied script, so I'm looking for a relatively painless solution. I have seen all sorts of complicated tricks, including parsing the script, but nothing really simple. I'm surprised it does not exist.. or maybe I'm not getting something. Thanks. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
My attempt using the ast module: import ast # which syntax elements are allowed at module level? whitelist = [ # docstring lambda x: isinstance(x, ast.Expr) \ and isinstance(x.value, ast.Str), # import lambda x: isinstance(x, ast.Import), # class lambda x: isinstance(x, ast.ClassDef), # function lambda x: isinstance(x, ast.FunctionDef), ] def validate(source, required_functions): tree = ast.parse(source) functions = set() required_functions = set(required_functions) for item in tree.body: if isinstance(item, ast.FunctionDef): functions.add(item.name) continue if all(not checker(item) for checker in whitelist): return False # at least the required functions must be there return len(required_functions - functions) == 0 if __name__ == "__main__": required_funcs = [ "init", "execute", "cleanup" ] with open("/tmp/test.py", "rb") as f: print("yay!" if validate(f.read(), required_funcs) else "d'oh!")

Related questions

0 votes
    In the context of a complex application, I need to import user-supplied 'scripts'. Ideally, a script ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    so with ng2 rc.6 I am getting an error of: Uncaught (in promise) Error: (SystemJS) SyntaxError: ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 8, 2022 in Education by JackTerrance
0 votes
    Trying to use ES6 but I'm stuck on something. To make it simple there is two problems : JS ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    How will you remove a file from Git without actually removing it from your local filesystem?...
asked Nov 4, 2020 in Technology by JackTerrance
0 votes
    The database system must take special actions to ensure that transactions operate properly without interference ... Concept topic in chapter Transactions of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    What is the best Azure solution for executing the code without a server?...
asked Jul 31, 2021 by JackTerrance
0 votes
    The awkward scenario I am developing a python package and would like to make use of an external package, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    Which module in the python standard library parses options received from the command line? a) getarg b) getopt c) main d) os...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    Which module in the python standard library parses options received from the command line? a) getarg b) getopt c) main d) os...
asked Jan 2, 2023 in Technology by JackTerrance
0 votes
    What is the default value used by sqoop when it encounters a missing value while importing form CSV file. A - NULL B - null C - space character D - No values...
asked Jan 13, 2021 in Technology by JackTerrance
0 votes
    An operation is said to be __________ if executing it several times in a row gives the same result as ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
0 votes
    It actually infects your device easily & makes it hard to _____________ (a) delete (b) identify (c) modify ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    In an application currently in production, one module of code is being modified. Is it necessary to re-test ... it enough to just test functionality associated with that module?...
asked Oct 17, 2020 in Technology by anonymous
0 votes
    I have an issue where i want to import value from a textfile into my excel-document which it does ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 20, 2022 in Education by JackTerrance
...