We are using napolean style docstring for python modules. But there is a need to auto populate additional attributes in the docstring called Data Owner
and DAL Owner
so that the given function looks like this:
def func(self, arg1=None, arg2=None):
"""
Returns the timeseries for the specified arg1 and arg2.
Args:
arg1: argument 1
arg2: argument 2
Returns:
DataFrame containing timeseries of arg1 for arg2.
DAL Owner: Team IT
Data Owner: Team A
"""
These additional attributes and their values for a given function are provided in a separate csv file. The way I was thinking was to have a script (awk, sed?) that will
- extract all the function names in a given python file. Can easily do it in python
- for those function names, check if the owners exist in the csv file and if so create a mapping of the function name and owners. Doable
Now, this is the part which I havent figured out and dont know the best way forward. For a given function name and owners, I need to go back into the python file and add the owners to the docstring if it exists. I am thinking some sort of awk script but not quite sure
- Find the function that matches the pattern
- For that pattern, see if doctsring exists, triple quotation marks after closing parenthesis
- If docstring exists, add additional two lines for the owners before the closing triple quotation
- If docstring does not exists, then insert the two lines for owners between tripe quotations on the line after function declaration.
I know this is a lot of steps but can anyone provide insight with the previous 4 bullet points to insert the additional attributes to docstring given the function, attributes and the python file. Will a linux utility like sed, awk be more useful or should I go the python route. Is there some other option that's easier to implement.