xref: /openbmc/openbmc-test-automation/lib/wrap_utils.py (revision 20f38712b324e61a94e174017c487a0af4b373e1)
1e7e9171eSGeorge Keishing#!/usr/bin/env python3
2264bc147SMichael Walsh
3264bc147SMichael Walshr"""
4410b1787SMichael WalshThis module provides functions which are useful for writing python wrapper functions (i.e. in this context, a
5410b1787SMichael Walshwrapper function is one whose aim is to call some other function on the caller's behalf but to provide some
6410b1787SMichael Walshadditional functionality over and above what the base function provides).
7264bc147SMichael Walsh"""
8264bc147SMichael Walsh
9264bc147SMichael Walsh
10*20f38712SPatrick Williamsdef create_func_def_string(
11*20f38712SPatrick Williams    base_func_name, wrap_func_name, func_body_template, replace_dict
12*20f38712SPatrick Williams):
13264bc147SMichael Walsh    r"""
14410b1787SMichael Walsh    Create and return a complete function definition as a string.  The caller may run "exec" on the resulting
15410b1787SMichael Walsh    string to create the desired function.
16264bc147SMichael Walsh
17264bc147SMichael Walsh    Description of argument(s):
18410b1787SMichael Walsh    base_func_name                  The name of the base function around which a wrapper is being created.
19410b1787SMichael Walsh    wrap_func_name                  The name of the wrapper function being created.
20410b1787SMichael Walsh    func_body_template              A function body in the form of a list.  Each list element represents one
21410b1787SMichael Walsh                                    line of a function  This is a template in so far as text substitutions
22410b1787SMichael Walsh                                    will be done on it to arrive at a valid function definition.  This
23410b1787SMichael Walsh                                    template should NOT contain the function definition line (e.g. "def
24410b1787SMichael Walsh                                    func1():").  create_func_def_string will pre-pend the definition line.
25410b1787SMichael Walsh                                    The template should also contain the text "<call_line>" which is to be
26410b1787SMichael Walsh                                    replaced by text which will call the base function with appropriate
27410b1787SMichael Walsh                                    arguments.
28410b1787SMichael Walsh    replace_dict                    A dictionary indicating additional text replacements to be done.  For
29410b1787SMichael Walsh                                    example, if the template contains a "<sub1>" (be sure to include the
30410b1787SMichael Walsh                                    angle brackets), and the dictionary contains a key/value pair of
31410b1787SMichael Walsh                                    'sub1'/'replace1', then all instances of "<sub1>" will be replaced by
32410b1787SMichael Walsh                                    "replace1".
33264bc147SMichael Walsh    """
34264bc147SMichael Walsh
35264bc147SMichael Walsh    # Create the initial function definition list as a copy of the template.
36264bc147SMichael Walsh    func_def = list(func_body_template)
370f8b603cSMichael Walsh    func_def_line = "def " + wrap_func_name + "(*args, **kwargs):"
380f8b603cSMichael Walsh    call_line = base_func_name + "(*args, **kwargs)"
39410b1787SMichael Walsh    # Insert the func_def_line composed by create_wrapper_def_and_call is the first list entry.
40264bc147SMichael Walsh    func_def.insert(0, func_def_line)
41410b1787SMichael Walsh    # Make sure the replace_dict has a 'call_line'/call_line pair so that any '<call_line>' text gets
42410b1787SMichael Walsh    # replaced as intended.
43*20f38712SPatrick Williams    replace_dict["call_line"] = call_line
44264bc147SMichael Walsh
45264bc147SMichael Walsh    # Do the replacements.
46264bc147SMichael Walsh    for key, value in replace_dict.items():
47264bc147SMichael Walsh        func_def = [w.replace("<" + key + ">", value) for w in func_def]
48264bc147SMichael Walsh
49*20f38712SPatrick Williams    return "\n".join(func_def) + "\n"
50