00001
00002 import os, sys, csv, re
00003 from StringIO import StringIO
00004 from string import strip
00005
00006 class psv(csv.excel):
00007 delimiter = '|'
00008
00009 class Ret(list):
00010 pass
00011
00012 def ccsv_parse( ccsv , dialect=psv ):
00013 """
00014 Parse compound-csv strings into a list of dicts (one for each csv line)
00015
00016 Compound-csv format comprises standard csv (with 1 line fieldname header)
00017 separated by lines containing only ";"
00018
00019 * whitespace at front and back of keys and values is stripped
00020 * lines starting with # are regarded as ignored
00021
00022 TODO : improve robustness ...
00023
00024 * trailing spaces after the ";" cause parse errors, as those
00025 spaces are taken to be the fieldspec yielding::
00026
00027 AttributeError: 'NoneType' object has no attribute 'strip'
00028
00029 """
00030 docp = re.compile('"""([^"""]*)"""',re.DOTALL)
00031 docstrings = lambda t:docp.findall(t)
00032 undoc = lambda t:docp.sub("", t)
00033
00034 uncomment = lambda lines:re.compile("^#.*$\n",re.MULTILINE).sub("",lines)
00035 unblank_line = lambda line:len(line)>0
00036 unblank_segm = lambda segm:"\n".join( filter(unblank_line, segm.split("\n") ))
00037 stripr = lambda s:map(strip,s)
00038
00039 docs = "".join( docstrings( ccsv ) )
00040 bits = uncomment( undoc(ccsv) ).split(";")
00041 regs = filter( unblank_line , bits )
00042 segs = map( unblank_segm, regs )
00043
00044 ret = Ret()
00045 for segm in segs:
00046 rdr = csv.DictReader( StringIO(segm) , dialect = dialect )
00047 for r in rdr:
00048 d = dict( map(stripr, r.items()) )
00049 ret.append( d )
00050 ret.docs = docs
00051 return ret
00052
00053
00054
00055 if __name__ == '__main__':
00056
00057 import os
00058 spec = open( os.path.expandvars( "$DYBDBIROOT/spec/GSimPmtSpec.spec" ), "r").read()
00059
00060 p = ccsv_parse( spec )
00061 print p.docs
00062 for d in p:
00063 print d
00064