Commit dd6907e3 authored by thomas.forbriger's avatar thomas.forbriger

[TASK] (csbusortdirlist): make program operational

parent bcf946a6
......@@ -93,11 +93,69 @@ class Basetable():
Base class for table objects.
"""
def __init__(self, pathname):
# ----------------------------------------------------------------------------
def __init__(self, pathline):
"""
Constructor: initialize from path name
"""
self.collection='Unknown'
self.collection=pathline.collection
self.datatype=pathline.datatype
self.years=dict()
self.channels=dict()
self.counts=dict()
# ----------------------------------------------------------------------------
def takepath(self, pathline):
"""
Take an additional path for the current collection
"""
if pathline.year in self.years.keys():
++self.years[pathline.year]
else:
self.years[pathline.year]=1
if pathline.channel in self.channels.keys():
++self.channels[pathline.channel]
else:
self.channels[pathline.channel]=1
count=(pathline.year, pathline.channel)
if count in self.counts.keys():
++self.counts[count]
else:
self.counts[count]=1
# ----------------------------------------------------------------------------
def write(self, os):
"""
Output a table
"""
os.write("Data stream %s (type %s):\n" %
(self.collection, self.datatype))
years=self.years.keys()
channels=self.channels.keys()
years.sort()
channels.sort()
os.write("|| ||")
for c in channels:
os.write(" %s ||" % c)
os.write("\n")
for y in years:
os.write("|| %s ||" % y)
for c in channels:
cellkey=(y, c)
if cellkey in self.counts.keys():
os.write(" %d ||" % self.counts[cellkey])
else:
os.write(" ||")
os.write("\n")
os.write("\n")
# ============================================================================
class SEEDlinktable(Basetable):
......@@ -105,55 +163,30 @@ class SEEDlinktable(Basetable):
Class to hold a seedlink data structure
"""
def __init__(self, pathname):
"""
Constructor: initialize from path name
"""
# ============================================================================
class UIPCtable(Basetable):
"""
Class to hold a UIPC data structure
"""
def __init__(self, pathname):
"""
Constructor: initialize from path name
"""
# ============================================================================
class DL1table(Basetable):
"""
Class to hold a DL1 data structure
"""
def __init__(self, pathname):
"""
Constructor: initialize from path name
"""
# ============================================================================
class HPMOtable(Basetable):
"""
Class to hold a HPMO data structure
"""
def __init__(self, pathname):
"""
Constructor: initialize from path name
"""
# ============================================================================
class Unknowntable(Basetable):
"""
Class to hold an unkown data structure
"""
def __init__(self, pathname):
"""
Constructor: initialize from path name
"""
# ============================================================================
class PathLine():
"""
......@@ -214,6 +247,14 @@ class PathLine():
else:
self.channel='std'
# ----------------------------------------------------------------------------
def tablekey(self):
"""
Return key to identify table in dictionary
"""
return self.datatype+':'+self.collection
# ----------------------------------------------------------------------------
def report(self):
......@@ -239,31 +280,49 @@ def readlist(filename):
for line in open(filename, 'r'):
inputline=line.rstrip('\n')
pathline=PathLine(inputline)
pathline.report()
if pathline.datatype == 'seedlink':
debugout("seedlink")
debugout(pathline.pathname)
table=SEEDlinktable(pathline.pathname)
elif pathline.datatype == 'DL1':
debugout("DL1")
debugout(pathline.pathname)
table=DL1table(pathline.pathname)
elif pathline.datatype == 'HPMO':
debugout("HPMO")
debugout(pathline.pathname)
table=HPMOtable(pathline.pathname)
elif pathline.datatype == 'UIPC':
debugout("UIPC")
debugout(pathline.pathname)
table=UIPCtable(pathline.pathname)
if VERBOSE: pathline.report()
if pathline.tablekey() in tables:
table=tables[pathline.tablekey()]
else:
verbose("input line of unknown data type:")
verbose(line)
table=Unknowntable(pathline.pathname)
# tables[table.collection]=table
if pathline.datatype == 'seedlink':
table=SEEDlinktable(pathline)
elif pathline.datatype == 'DL1':
table=DL1table(pathline)
elif pathline.datatype == 'HPMO':
table=HPMOtable(pathline)
elif pathline.datatype == 'UIPC':
table=UIPCtable(pathline)
else:
table=Unknowntable(pathline)
tables[pathline.tablekey()]=table
table.takepath(pathline)
return(tables)
debugout("finished function readlist")
# ----------------------------------------------------------------------------
def writeoutput(os, tables):
"""
Write tables to output file
"""
types=[]
for k in tables.keys():
types.append(tables[k].datatype)
keylist=tables.keys()
keylist.sort()
keylist.reverse()
for k in keylist:
table=tables[k]
if table.datatype != 'Unknown':
table.write(os)
# ============================================================================
def main(argv=None):
"""
......@@ -299,7 +358,7 @@ def main(argv=None):
outputfilename=args[1]
tabledata=readlist(inputfilename)
# check for existing file
# if an exception is raised here, this is just fine: we do not want to
# overwrite an existing file
......@@ -322,6 +381,8 @@ def main(argv=None):
print('ERROR: could not open file %s for output' % outputfilename)
raise err
writeoutput(output, tabledata)
# ----------------------------------------------------------------------------
if __name__ == '__main__':
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment