
import sys,re


class changeFile:
    pth = ''
    fname = ''
    lines = []

    def __init__(self, pth):
        self.pth = pth
        self.fname = pth.split('/')[-1]

    def __enter__(self):
        with open(self.pth,'r') as file:
            for line in file:
                self.lines.append(line)
        return self

    def __exit__(self,exc_type, exc_val, exc_tb):
        with open(self.pth,'w') as file:
            file.writelines(self.lines)
        return False

if __name__ == '__main__':
    print('add console log to',sys.argv[1])

    with changeFile(sys.argv[1]) as chg:

        r = re.compile(r' *p.* (.*)\((.*)\): (.*) {')

        for i in range(len(chg.lines)-1):
            if ' get ' in chg.lines[i]:
                continue
            if 'console.log' in chg.lines[i+1]:
                continue
            if ': boolean {' in  chg.lines[i]:
                continue
            if r.match(chg.lines[i]):
                for name, eat, shit in r.findall(chg.lines[i]):
                    params = ', '.join([ param.split(': ')[0] for param in eat.split(', ') ])
                    chg.lines[i] += f"        console.log('{chg.fname}','{name}',{params});\n"


