Making a basic block -> tree reader

Sun, 01 Aug 2010 01:05:50 +0200

author
Andy Buckley <andy@insectnation.org>
date
Sun, 01 Aug 2010 01:05:50 +0200
changeset 1
dee5706ae812
parent 0
c88183197787
child 2
3eef12df64d5

Making a basic block -> tree reader

pyslha.py file | annotate | diff | comparison | revisions
     1.1 --- a/pyslha.py	Wed Jul 07 23:02:32 2010 +0100
     1.2 +++ b/pyslha.py	Sun Aug 01 01:05:50 2010 +0200
     1.3 @@ -0,0 +1,81 @@
     1.4 +#! /usr/bin/env python
     1.5 +
     1.6 +import re
     1.7 +
     1.8 +class Block:
     1.9 +    def __init__(self, name):
    1.10 +        self.name = name
    1.11 +        self.entries = {}
    1.12 +
    1.13 +    def add_entry(self, entry):
    1.14 +        #print entry
    1.15 +        nextparent = self.entries
    1.16 +        if len(entry) < 2:
    1.17 +            raise Exception("Block entries must be at least a 2-tuple")
    1.18 +        for e in entry[:-2]:
    1.19 +            if e is not entry[-1]:
    1.20 +                nextparent = nextparent.setdefault(e, {})
    1.21 +        nextparent[entry[-2]] = entry[-1]
    1.22 +        #print self.entries
    1.23 +
    1.24 +    def __cmp__(self, other):
    1.25 +        return self.name < other.name
    1.26 +
    1.27 +    def __str__(self):
    1.28 +        s = self.name
    1.29 +        s += "\n"
    1.30 +        s += str(self.entries)
    1.31 +        return s
    1.32 +
    1.33 +
    1.34 +class Decay:
    1.35 +    def __init__(self, pid, totalwidth):
    1.36 +        self.pid = totalwidth
    1.37 +
    1.38 +
    1.39 +def readSpcFile(filename):
    1.40 +    ## TODO: Use new file handling from future
    1.41 +    blocks = {}
    1.42 +    decays = {}
    1.43 +    f = open(filename, "r")
    1.44 +    currentblock = None
    1.45 +    for l in f:
    1.46 +        line = l.strip()[:-1]
    1.47 +        ## Handle (ignore) comment lines
    1.48 +        if line.startswith("#"):
    1.49 +            continue
    1.50 +        if "#" in line:
    1.51 +            line = line[:line.index("#")]
    1.52 +
    1.53 +        ## Handle BLOCK/DECAY start lines
    1.54 +        if line.startswith("BLOCK"):
    1.55 +            ##TODO: Deal with Q= entries
    1.56 +            blockname = line[5:].strip()
    1.57 +            currentblock = blockname
    1.58 +            blocks[blockname] = Block(blockname)
    1.59 +        elif line.startswith("DECAY"):
    1.60 +            currentblock = "DECAY"
    1.61 +            # TODO: particle details
    1.62 +        elif currentblock is not None:
    1.63 +            items = line.split()
    1.64 +            if len(items) < 2:
    1.65 +                continue
    1.66 +            # TODO: Sort out tuple item types: autoconvert integers and floats
    1.67 +            if currentblock != "DECAY":
    1.68 +                #print currentblock
    1.69 +                blocks[currentblock].add_entry(items)
    1.70 +            else:
    1.71 +                #print "NOT HANDLING DECAYS YET!"
    1.72 +                pass
    1.73 +
    1.74 +    f.close()
    1.75 +    return blocks, decays
    1.76 +
    1.77 +
    1.78 +if __name__ == "__main__":
    1.79 +    import sys
    1.80 +    for a in sys.argv[1:]:
    1.81 +        blocks, decays = readSpcFile(a)
    1.82 +        for bname, b in sorted(blocks.iteritems()):
    1.83 +            print b
    1.84 +            print

mercurial