import datetime, re, time

class Patch:
    def __init__(self, name, author, timestamp, longdesc=None):
        self.name = name
        self.author = author
        self.timestamp = datetime.datetime(*time.strptime(timestamp, '%Y%m%d%H%M%S')[:6])
        self.longdesc = longdesc

    def __str__(self):
        return '%s at %s by %s' % (self.name, self.timestamp, self.author)

patch_re = re.compile(r'\[(?P<name>[^\n]+)\n(?P<author>[^\*]+)\*\*(?P<timestamp>\d{14})(?P<longdesc>\]|\n(?:^ [^\n]*\n)+)', re.M)
fix_longdesc_re = re.compile(r'^ ', re.M)

def parse_inventory(inventory):
    matches = patch_re.finditer(inventory)
    for match in matches:
        attrs = match.groupdict(None)
        if attrs['longdesc'] == ']':
            del(attrs['longdesc'])
        else:
            attrs['longdesc'] = fix_longdesc_re.sub('', attrs['longdesc']).strip()
        yield Patch(**attrs)

if __name__ == '__main__':
    inventory = open(r'C:\repos\darcsweb\_darcs\inventory', 'rb').read()
    for patch in parse_inventory(inventory):
        print patch