taste-upgrade-IF-view 1.98 KB
Newer Older
Thanassis Tsiodras's avatar
Thanassis Tsiodras committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#!/usr/bin/env python
import os
import re
import sys


def panic(x):
    if not x.endswith("\n"):
        x += "\n"
    sys.stderr.write("\n"+chr(27)+"[32m" + x + chr(27) + "[0m\n")
    sys.exit(1)


class Matcher:
    def __init__(self, pattern, flags=0):
        self._pattern = re.compile(pattern, flags)
        self._lastOne = None

    def match(self, line):
        self._match=re.match(self._pattern, line)
        self._lastOne='Match'
        return self._match

    def search(self, line):
        self._search=re.search(self._pattern, line)
        self._lastOne='Search'
        return self._search

    def group(self, idx):
        if self._lastOne == 'Match':
            return self._match.group(idx)
        elif self._lastOne == 'Search':
            return self._search.group(idx)
        else:
            return panic("Matcher group called with index %d before match/search!\n" % idx)

    def groups(self):
        if self._lastOne == 'Match':
            return self._match.groups()
        elif self._lastOne == 'Search':
            return self._search.groups()
        else:
            return panic("Matcher groups called with match/search!\n")


def main():
    if len(sys.argv)!=3 or not os.path.isfile(sys.argv[1]):
        panic("Usage: " + os.path.basename(sys.argv[0]) + " inputInterfaceView outputInterfaceView")
    pattern = Matcher(r'^(\s*Taste::dataViewPath\s*=>\s*)(.*)\s*;\s*$')
    outputFile = open(sys.argv[2], 'w')
    for line in open(sys.argv[1], 'r').readlines():
        # Filter directives out of the lists in lines like this:
        # Taste::dataViewPath => ("TASTE-Directives.aadl","DataView.aadl");
        if pattern.search(line):
            l = eval(pattern.group(2))
            l = [x for x in l if 'taste-directives.aadl' not in x.lower()]
            outputFile.write(pattern.group(1) + '("' + '","'.join(l) + '");\n')
        else:
            outputFile.write(line)


if __name__ == "__main__":
    main()

# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4