/search.css" rel="stylesheet" type="text/css"/> /search.js">
00001 import os, logging 00002 log = logging.getLogger(__name__) 00003 00004 __all__ = ('Toc','TabularData','rst_title',) 00005 00006 try: 00007 from converter.tabular import TabularData 00008 except ImportError: 00009 class TabularData(list): 00010 """ 00011 pale imitation of the real TabularData 00012 """ 00013 def as_rst(self, cols ): 00014 def cell(v): 00015 return "/".join(map(lambda _:str(int(_)),v)) if type(v) in (list,tuple) else str(v) 00016 def fallback(d): 00017 return " ".join( map(lambda k:"%-10s" % cell(d[k]), cols ) ) 00018 return "\n".join(fallback(d) for d in self) + "\n\n" 00019 00020 00021 00022 00023 def rst_title( msg , cha="~" ): 00024 return "%s\n%s\n\n" % ( msg, cha * len(msg) ) 00025 00026 00027 class Toc(list): 00028 """ 00029 Writes index document (typically `index.rst`) containing :rst:directive:`toctree` 00030 with the relative paths to the content names provided 00031 00032 For understanding what sphinx comes up with bear in mind the impact of 00033 three tree hierarchies that all play a role: 00034 00035 #. rst file tree, controlled by toctree 00036 #. heading tree, presented content hierarchy depends on rst content usage of section headings 00037 00038 00039 """ 00040 00041 head = r""" 00042 00043 .. toctree:: 00044 00045 """ 00046 tail = r""" 00047 00048 00049 """ 00050 00051 def __init__(self, base=None , title=None): 00052 """ 00053 :param base: path of the index document to be written 00054 """ 00055 list.__init__(self) 00056 self.base = base 00057 self.title = title 00058 self.notes = [] 00059 00060 def __repr__(self): 00061 return "Toc(base='%s',title='%s')" % ( self.base , self.title ) 00062 00063 def __str__(self): 00064 titl = [] 00065 if self.title: 00066 titl = rst_title(self.title,"-").split("\n") 00067 00068 unrst = lambda _:_[:-4] 00069 def prep(path): 00070 assert path[-4:] == '.rst', path 00071 return os.path.relpath( unrst(path) , os.path.dirname(self.base) ) 00072 return "\n".join(titl + [_ for _ in self.notes]) + "\n" + self.head + "\n".join([" %s" % (_) for _ in map(prep,self)]) + self.tail 00073 00074 def write(self): 00075 log.info("writing Toc %s " % self.base ) 00076 with open(self.base,"w") as fp: 00077 fp.write(str(self)) 00078 00079 00080 00081 if __name__ == '__main__': 00082 td = TabularData([dict(a=1,b=2,c=3),dict(a=10,b=20,c=30)]) 00083 print td 00084 print str(td) 00085