Skip to content

Instantly share code, notes, and snippets.

@meggart
Created July 5, 2017 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meggart/e1d0e4b880b94e198665d675d7a8553d to your computer and use it in GitHub Desktop.
Save meggart/e1d0e4b880b94e198665d675d7a8553d to your computer and use it in GitHub Desktop.
import cablab
from cablab import cube_gen
from cablab import cube_config
from collections import OrderedDict
from textwrap import TextWrapper
class varInfo:
def __init__(self):
plist = cube_gen._load_source_providers()
plist.pop('test')
conf = cube_config.CubeConfig()
all_vars = {}
for pn,prov in plist.items():
m = eval(prov.__module__)
needsvar = hasattr(m,'all_vars_descr')
if needsvar:
all_vars_descr = m.all_vars_descr
varname = [k for k,v in all_vars_descr.items() if pn in v.keys()]
p = prov(conf,dir="./",var=varname[0])
else:
p = prov(conf,dir="./")
for k,v in p.variable_descriptors.items():
if 'project_name' in v:
pname = v['project_name']
if pname in all_vars:
all_vars[pname][k]=v
else:
all_vars[pname]={k:v}
self.all_vars = all_vars
self.td = varInfo.tabledescriptor(all_vars)
class tabledescriptor:
def __init__(self,all_vars):
self.colnames = OrderedDict([
('Project', 'project_name'),
('Name in ESDC', 'variable'),
('Description', 'comment'),
('URL', 'url'),
('References', 'references')
])
# Number of columns
self.ncol = len(self.colnames)
# Number of projects
self.nproj = len(all_vars)
# Number of variables per project
self.nvproj = [len(v) for v in all_vars.values()]
# Number of chars per columns
self.ncharcol = [30,40,100,80,510]
# Which columns are joint per project
self.isjoint = [True, False, False, True, True]
def genHeader(self):
l1 = '+'
l2 = '|'
l3 = '+'
for (i,cn) in enumerate(self.colnames.keys()):
l1 = l1 + '-'*(self.ncharcol[i]+2) + '+'
l2 = l2 + ' ' + cn.ljust(self.ncharcol[i]) + ' |'
l3 = l3 + '='*(self.ncharcol[i]+2) + '+'
return [l1,l2,l3]
def sepLine(self):
l = '+'
for (i,cn) in enumerate(self.colnames.keys()):
l = l + '-'*(self.ncharcol[i]+2) + '+'
return l
def getEntryValue(self, colname, varname, varentry):
if colname=='Name in ESDC':
return varname
elif colname=='Description':
return varentry.get(self.colnames[colname],varentry.get("long_name","missing"))
else:
return varentry.get(self.colnames[colname],'missing')
def genProject(self,projinfo, projname):
td = self
nprojrow = len(projinfo)
lines = ['|' for i in range(nprojrow*2-1)]
for icol,colname in enumerate(td.colnames):
if td.isjoint[icol]:
# We have to fill a cell that has only one entry per project, we fill with spaces if needed
firstkey,firstitem = next(iter(projinfo.items()))
wrapper = TextWrapper(width = td.ncharcol[icol])
text = wrapper.wrap(td.getEntryValue(colname,firstkey,firstitem))
nlines = 2*len(projinfo)-1
if nlines<len(text):
print(text)
raise Exception("Does not fit! %s %s" % (nlines,len(text)))
imiddle = nlines//2
r1 = imiddle - len(text)//2
#Fill empty lines first
itext = 0
for i in range(r1):
lines[i]=lines[i]+' '*(td.ncharcol[icol]+2)+'|'
#Then the actual text
for i in range(r1,r1+len(text)):
lines[i]=lines[i]+' '+text[itext].ljust(td.ncharcol[icol])+' |'
itext+=1
# And then trailing empty lines
for i in range(r1+len(text),nlines):
lines[i]=lines[i]+' '*(td.ncharcol[icol]+2)+'|'
else:
# Single entry per avriable separated by a ---- separator
iline = 0
for ivar,(varname,varentry) in enumerate(projinfo.items()):
lines[iline]=lines[iline]+' '+td.getEntryValue(colname,varname,varentry).ljust(td.ncharcol[icol])+' |'
if iline+1<nlines:
lines[iline+1]=lines[iline+1][:-1]+'+'+'-'*(td.ncharcol[icol]+2)+'+'
iline+=2
return lines
def genTable(self,outputfile = "esdctable.txt"):
td = self.td
h = td.genHeader()
projects=[td.genProject(projinfo, projname) for projname,projinfo in self.all_vars.items()]
sl = td.sepLine()
for proj in projects:
h.extend(proj)
h.append(sl)
with open(outputfile,"w") as f:
f.writelines("\n".join(h))
return None
if __name__ == '__main__':
vI=varInfo()
vI.genTable()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment