Blame common/scangobj-merge.py

Packit 133782
#!/usr/bin/python
Packit 133782
# -*- Mode: Python -*-
Packit 133782
# vi:si:et:sw=4:sts=4:ts=4
Packit 133782
Packit 133782
"""
Packit 133782
parse, merge and write gstdoc-scanobj files
Packit 133782
"""
Packit 133782
Packit 133782
import sys
Packit 133782
import os
Packit 133782
Packit 133782
def debug(*args):
Packit 133782
    pass
Packit 133782
Packit 133782
# OrderedDict class based on
Packit 133782
# http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/107747
Packit 133782
# Licensed under the Python License
Packit 133782
class OrderedDict(dict):
Packit 133782
    def __init__(self):
Packit 133782
        self._keys = []
Packit 133782
        dict.__init__(self)
Packit 133782
Packit 133782
    def __delitem__(self, key):
Packit 133782
        dict.__delitem__(self, key)
Packit 133782
        self._keys.remove(key)
Packit 133782
Packit 133782
    def __setitem__(self, key, item):
Packit 133782
        dict.__setitem__(self, key, item)
Packit 133782
        if key not in self._keys: self._keys.append(key)
Packit 133782
Packit 133782
    def clear(self):
Packit 133782
        dict.clear(self)
Packit 133782
        self._keys = []
Packit 133782
Packit 133782
    def copy(self):
Packit 133782
        dict = dict.copy(self)
Packit 133782
        dict._keys = self._keys[:]
Packit 133782
        return dict
Packit 133782
Packit 133782
    def items(self):
Packit 133782
        return zip(self._keys, self.values())
Packit 133782
Packit 133782
    def keys(self):
Packit 133782
        return self._keys
Packit 133782
Packit 133782
    def popitem(self):
Packit 133782
        try:
Packit 133782
            key = self._keys[-1]
Packit 133782
        except IndexError:
Packit 133782
            raise KeyError('dictionary is empty')
Packit 133782
Packit 133782
        val = self[key]
Packit 133782
        del self[key]
Packit 133782
Packit 133782
        return (key, val)
Packit 133782
Packit 133782
    def setdefault(self, key, failobj = None):
Packit 133782
        dict.setdefault(self, key, failobj)
Packit 133782
        if key not in self._keys: self._keys.append(key)
Packit 133782
Packit 133782
    def update(self, dict):
Packit 133782
        dict.update(self, dict)
Packit 133782
        for key in dict.keys():
Packit 133782
            if key not in self._keys: self._keys.append(key)
Packit 133782
Packit 133782
    def values(self):
Packit 133782
        return map(self.get, self._keys)
Packit 133782
Packit 133782
class Object:
Packit 133782
    def __init__(self, name):
Packit 133782
        self._signals = OrderedDict()
Packit 133782
        self._args = OrderedDict()
Packit 133782
        self.name = name
Packit 133782
Packit 133782
    def __repr__(self):
Packit 133782
        return "<Object %s>" % self.name
Packit 133782
Packit 133782
    def add_signal(self, signal, overwrite=True):
Packit 133782
        if not overwrite and self._signals.has_key(signal.name):
Packit 133782
            raise IndexError, "signal %s already in %r" % (signal.name, self)
Packit 133782
        self._signals[signal.name] = signal
Packit 133782
Packit 133782
    def add_arg(self, arg, overwrite=True):
Packit 133782
        if not overwrite and self._args.has_key(arg.name):
Packit 133782
            raise IndexError, "arg %s already in %r" % (arg.name, self)
Packit 133782
        self._args[arg.name] = arg
Packit 133782
Packit 133782
class Docable:
Packit 133782
    def __init__(self, **kwargs):
Packit 133782
        for key in self.attrs:
Packit 133782
            setattr(self, key, kwargs[key])
Packit 133782
        self.dict = kwargs
Packit 133782
Packit 133782
    def __repr__(self):
Packit 133782
        return "<%r %s>" % (str(self.__class__), self.name)
Packit 133782
Packit 133782
class Signal(Docable):
Packit 133782
    attrs = ['name', 'returns', 'args']
Packit 133782
Packit 133782
class Arg(Docable):
Packit 133782
    attrs = ['name', 'type', 'range', 'flags', 'nick', 'blurb', 'default']
Packit 133782
Packit 133782
class GDoc:
Packit 133782
    def load_file(self, filename):
Packit 133782
        try:
Packit 133782
            lines = open(filename).readlines()
Packit 133782
            self.load_data("".join(lines))
Packit 133782
        except IOError:
Packit 133782
            print "WARNING - could not read from %s" % filename
Packit 133782
Packit 133782
    def save_file(self, filename, backup=False):
Packit 133782
        """
Packit 133782
        Save the information to the given file if the file content changed.
Packit 133782
        """
Packit 133782
        olddata = None
Packit 133782
        try:
Packit 133782
            lines = open(filename).readlines()
Packit 133782
            olddata = "".join(lines)
Packit 133782
        except IOError:
Packit 133782
            print "WARNING - could not read from %s" % filename
Packit 133782
        newdata = self.get_data()
Packit 133782
        if olddata and olddata == newdata:
Packit 133782
            return
Packit 133782
Packit 133782
        if olddata:
Packit 133782
            if backup:
Packit 133782
                os.rename(filename, filename + '.bak')
Packit 133782
Packit 133782
        handle = open(filename, "w")
Packit 133782
        handle.write(newdata)
Packit 133782
        handle.close()
Packit 133782
Packit 133782
class Signals(GDoc):
Packit 133782
    def __init__(self):
Packit 133782
        self._objects = OrderedDict()
Packit 133782
Packit 133782
    def load_data(self, data):
Packit 133782
        """
Packit 133782
        Load the .signals lines, creating our list of objects and signals.
Packit 133782
        """
Packit 133782
        import re
Packit 133782
        smatcher = re.compile(
Packit 133782
            '(?s)'                                      # make . match \n
Packit 133782
            '<SIGNAL>\n(.*?)</SIGNAL>\n'
Packit 133782
            )
Packit 133782
        nmatcher = re.compile(
Packit 133782
            '<NAME>'
Packit 133782
            '(?P<object>\S*)'                           # store object
Packit 133782
            '::'
Packit 133782
            '(?P<signal>\S*)'                           # store signal
Packit 133782
            '</NAME>'
Packit 133782
        )
Packit 133782
        rmatcher = re.compile(
Packit 133782
            '(?s)'                                      # make . match \n
Packit 133782
            '<RETURNS>(?P<returns>\S*)</RETURNS>\n'     # store returns
Packit 133782
            '(?P<args>.*)'                              # store args
Packit 133782
        )
Packit 133782
        for block in smatcher.findall(data):
Packit 133782
            nmatch = nmatcher.search(block)
Packit 133782
            if nmatch:
Packit 133782
                o = nmatch.group('object')
Packit 133782
                debug("Found object", o)
Packit 133782
                debug("Found signal", nmatch.group('signal'))
Packit 133782
                if not self._objects.has_key(o):
Packit 133782
                    object = Object(o)
Packit 133782
                    self._objects[o] = object
Packit 133782
Packit 133782
                rmatch = rmatcher.search(block)
Packit 133782
                if rmatch:
Packit 133782
                    dict = rmatch.groupdict().copy()
Packit 133782
                    dict['name'] = nmatch.group('signal')
Packit 133782
                    signal = Signal(**dict)
Packit 133782
                    self._objects[o].add_signal(signal)
Packit 133782
Packit 133782
    def get_data(self):
Packit 133782
        lines = []
Packit 133782
        for o in self._objects.values():
Packit 133782
            for s in o._signals.values():
Packit 133782
                block = """<SIGNAL>
Packit 133782
<NAME>%(object)s::%(name)s</NAME>
Packit 133782
<RETURNS>%(returns)s</RETURNS>
Packit 133782
%(args)s</SIGNAL>
Packit 133782
"""
Packit 133782
                d = s.dict.copy()
Packit 133782
                d['object'] = o.name
Packit 133782
                lines.append(block % d)
Packit 133782
Packit 133782
        return "\n".join(lines) + '\n'
Packit 133782
Packit 133782
class Args(GDoc):
Packit 133782
    def __init__(self):
Packit 133782
        self._objects = OrderedDict()
Packit 133782
Packit 133782
    def load_data(self, data):
Packit 133782
        """
Packit 133782
        Load the .args lines, creating our list of objects and args.
Packit 133782
        """
Packit 133782
        import re
Packit 133782
        amatcher = re.compile(
Packit 133782
            '(?s)'                                      # make . match \n
Packit 133782
            '<ARG>\n(.*?)</ARG>\n'
Packit 133782
            )
Packit 133782
        nmatcher = re.compile(
Packit 133782
            '<NAME>'
Packit 133782
            '(?P<object>\S*)'                           # store object
Packit 133782
            '::'
Packit 133782
            '(?P<arg>\S*)'                              # store arg
Packit 133782
            '</NAME>'
Packit 133782
        )
Packit 133782
        rmatcher = re.compile(
Packit 133782
            '(?s)'                                      # make . match \n
Packit 133782
            '<TYPE>(?P<type>\S*)</TYPE>\n'              # store type
Packit 133782
            '<RANGE>(?P<range>.*?)</RANGE>\n'           # store range
Packit 133782
            '<FLAGS>(?P<flags>\S*)</FLAGS>\n'           # store flags
Packit 133782
            '<NICK>(?P<nick>.*?)</NICK>\n'              # store nick
Packit 133782
            '<BLURB>(?P<blurb>.*?)</BLURB>\n'           # store blurb
Packit 133782
            '<DEFAULT>(?P<default>.*?)</DEFAULT>\n'     # store default
Packit 133782
        )
Packit 133782
        for block in amatcher.findall(data):
Packit 133782
            nmatch = nmatcher.search(block)
Packit 133782
            if nmatch:
Packit 133782
                o = nmatch.group('object')
Packit 133782
                debug("Found object", o)
Packit 133782
                debug("Found arg", nmatch.group('arg'))
Packit 133782
                if not self._objects.has_key(o):
Packit 133782
                    object = Object(o)
Packit 133782
                    self._objects[o] = object
Packit 133782
Packit 133782
                rmatch = rmatcher.search(block)
Packit 133782
                if rmatch:
Packit 133782
                    dict = rmatch.groupdict().copy()
Packit 133782
                    dict['name'] = nmatch.group('arg')
Packit 133782
                    arg = Arg(**dict)
Packit 133782
                    self._objects[o].add_arg(arg)
Packit 133782
                else:
Packit 133782
                    print "ERROR: could not match arg from block %s" % block
Packit 133782
Packit 133782
    def get_data(self):
Packit 133782
        lines = []
Packit 133782
        for o in self._objects.values():
Packit 133782
            for a in o._args.values():
Packit 133782
                block = """<ARG>
Packit 133782
<NAME>%(object)s::%(name)s</NAME>
Packit 133782
<TYPE>%(type)s</TYPE>
Packit 133782
<RANGE>%(range)s</RANGE>
Packit 133782
<FLAGS>%(flags)s</FLAGS>
Packit 133782
<NICK>%(nick)s</NICK>
Packit 133782
<BLURB>%(blurb)s</BLURB>
Packit 133782
<DEFAULT>%(default)s</DEFAULT>
Packit 133782
</ARG>
Packit 133782
"""
Packit 133782
                d = a.dict.copy()
Packit 133782
                d['object'] = o.name
Packit 133782
                lines.append(block % d)
Packit 133782
Packit 133782
        return "\n".join(lines) + '\n'
Packit 133782
Packit 133782
class SingleLine(GDoc):
Packit 133782
    def __init__(self):
Packit 133782
        self._objects = []
Packit 133782
Packit 133782
    def load_data(self, data):
Packit 133782
        """
Packit 133782
        Load the .interfaces/.prerequisites lines, merge duplicates
Packit 133782
        """
Packit 133782
        # split data on '\n'
Packit 133782
        lines = data.splitlines();
Packit 133782
        # merge them into self._objects
Packit 133782
        for line in lines:
Packit 133782
            if line not in self._objects:
Packit 133782
                self._objects.append(line)
Packit 133782
Packit 133782
    def get_data(self):
Packit 133782
        lines = sorted(self._objects)
Packit 133782
        return "\n".join(lines) + '\n'
Packit 133782
Packit 133782
def main(argv):
Packit 133782
    modulename = None
Packit 133782
    try:
Packit 133782
        modulename = argv[1]
Packit 133782
    except IndexError:
Packit 133782
        sys.stderr.write('Please provide a documentation module name\n')
Packit 133782
        sys.exit(1)
Packit 133782
Packit 133782
    signals = Signals()
Packit 133782
    signals.load_file(modulename + '.signals')
Packit 133782
    signals.load_file(modulename + '.signals.new')
Packit 133782
    signals.save_file(modulename + '.signals', backup=True)
Packit 133782
    os.unlink(modulename + '.signals.new')
Packit 133782
Packit 133782
    args = Args()
Packit 133782
    args.load_file(modulename + '.args')
Packit 133782
    args.load_file(modulename + '.args.new')
Packit 133782
    args.save_file(modulename + '.args', backup=True)
Packit 133782
    os.unlink(modulename + '.args.new')
Packit 133782
Packit 133782
    ifaces = SingleLine()
Packit 133782
    ifaces.load_file(modulename + '.interfaces')
Packit 133782
    ifaces.load_file(modulename + '.interfaces.new')
Packit 133782
    ifaces.save_file(modulename + '.interfaces', backup=True)
Packit 133782
    os.unlink(modulename + '.interfaces.new')
Packit 133782
Packit 133782
    prereq = SingleLine()
Packit 133782
    prereq.load_file(modulename + '.prerequisites')
Packit 133782
    prereq.load_file(modulename + '.prerequisites.new')
Packit 133782
    prereq.save_file(modulename + '.prerequisites', backup=True)
Packit 133782
    os.unlink(modulename + '.prerequisites.new')
Packit 133782
Packit 133782
main(sys.argv)