docpage.html


001| ####################################################################################################
002| from sys import argv, path as syspath
003| from os import path
004| from xml.sax.saxutils import escape
005| from goodies import KWARG
006| from files import fileobj
007| from webpage import WebPage
008| ####################################################################################################
009| 
010| ##------------------------------------------------------------------------------------------------##
011| CSS = '''
011| body {
011|     background-color: #f7f9fc;
011|     color: #000000;
011|     font-family: "Verdana", "Arial", "Helvetica", sans-serif;
011|     font-size: 12px;
011|     font-weight: normal;
011|     font-style: normal;
011|     line-height: 1.33;
011|     text-decoration: none;
011|     text-align: left;
011|     vertical-align: top;
011|     padding: 0;
011|     border: 0;
011|     margin: 0;
011| }
011| h1, h2, h3, h4, h5, h6 {
011|     font-family: "Tahoma", "Verdana", "Arial", "Helvetica", sans-serif;
011|     font-weight: bold;
011|     font-style: normal;
011|     line-height: 1.0;
011|     text-decoration: none;
011|     text-align: left;
011|     vertical-align: bottom;
011|     padding: 0;
011|     border: 0;
011|     margin: 0;
011| }
011| h1 {
011|     color: #000000;
011|     font-size: 12px;
011|     font-weight: normal;
011|     font-style: italic;
011|     text-align: right;
011| }
011| h2 {
011|     color: #000000;
011|     font-size: 15px;
011|     text-align: right;
011| }
011| h3 {
011|     color: #603018;
011|     font-size: 14px;
011|     text-align: left;
011|     margin-bottom: 9px;
011| }
011| h4 {
011|     color: #804020;
011|     font-size: 14px;
011|     padding: 9px 9px 18px 9px;
011|     border: 1px solid black;
011|     margin-top: 15px;
011|     margin-bottom: 0;
011| }
011| pre {
011|     font-family: "Lucida Console", "Courier New", "Courier", monospace;
011|     font-size: 10px;
011|     font-weight: normal;
011|     font-style: normal;
011|     line-height: 1.20;
011|     padding: 5px 10px 20px 10px;
011|     margin: 0 0 15px 0;
011| }
011| #PageHead {
011|     background-color: #e0e7ef;
011|     font-size: medium;
011|     padding: 6px 20px 8px 6px;
011|     border-bottom: 2px solid black;
011|     margin: 0;
011| }
011| #PageBody {
011|     padding: 6px 24px 18px 12px;
011|     margin: 0;
011| }
011| #PageFoot {
011|     background-color: #c0c7cf;
011|     color: #808080;
011|     font-family: sans-serif;
011|     font-size: 9px;
011|     font-weight: normal;
011|     font-style: normal;
011|     line-height: 1.0;
011|     text-align: center;
011|     padding: 4px 12px 8px 12px;
011|     border-top: 1px solid black;
011|     margin: 0;
011| }
011| #PageFoot .Lt {
011|     float: left;
011|     text-align: left;
011| }
011| #PageFoot .Rt {
011|     float: right;
011|     text-align: right;
011| }
011| .ModuleDoc {
011|     background-color: #fffff7;
011|     padding: 8px 20px 16px 15px;
011|     border: 2px solid black;
011| }
011| .ObjectDoc {
011|     border-right: 1px solid black;
011|     border-bottom: 1px solid black;
011|     border-left: 1px solid black;
011|     background-color: #f7ffff;
011| }
011| .function {background-color:#ffffbf; color:#000000;}
011| .lambda {background-color:#ffdfbf; color:#000000;}
011| .class {background-color:#bfffff; color:#000000;}
011| .int, .float, .str {background-color:#dfffdf; color:#000000;}
011| .tuple, .list, .dict {background-color:#ffdfdf; color:#000000;}
011| 
011| '''

012| 
013| ##================================================================================================##
014| def module_docs (modname, fname='module-doc.html', fpath=None):
015| 
016|     m = __import__(modname)
017|     h = m.__doc__ if hasattr(m,'__doc__') else None
018|     h = escape(h.strip()) if h else '(no Module Doc)'
019| 
020|     # For each key in the Module's Dictionary...
021|     records = []
022|     for k in m.__dict__:
023|         # Use the key to get Module Object...
024|         obj = m.__dict__[k]
025|         print(obj)
026| 
027|         # If the Object has a Module (name) attribute, the Module name
028|         # must match this Module (otherwise it's an imported object)...
029|         mod_name = obj.__module__ if hasattr(obj,'__module__') else ''
030|         print(mod_name)
031|         if mod_name != m.__name__:
032|             if k.startswith('__') or (mod_name is None) or (0 < len(mod_name)):
033|                 print(f'skipping: {k} ({mod_name})')
034|                 continue
035| 
036|         # Get Object attributes (if they exist)...
037|         obj_name = obj.__name__ if hasattr(obj,'__name__') else ''
038|         obj_doc  = obj.__doc__ if hasattr(obj,'__doc__') else ''
039|         obj_type = type(obj).__name__
040|         if obj_type == "module":
041|             continue
042| 
043|         if obj_type in ['list','tuple','dict']:
044|             obj_doc = f'length={len(obj)}'
045| 
046|         elif obj_type in ['int','float']:
047|             obj_doc = str(obj)
048| 
049|         elif obj_type == 'str':
050|             s = str(obj).replace('\n',' ')
051|             if 80 < len(s):
052|                 s = f'{s[:38]} ... {s[-38:]} ({len(s)})'
053|             else:
054|                 s = f'{s} ({len(s)})'
055|             obj_doc = s
056| 
057|         elif obj_type == 'type':
058|             obj_type = 'class'
059| 
060|         if obj_name == '<lambda>':
061|             obj_type = 'lambda'
062| 
063|         print(f'object: {k}, {obj_type}, {obj_name}')
064| 
065|         # Add to collection...
066|         t = (k, obj_type, obj_name, obj_doc)
067|         records.append(t)
068| 
069|     # Create module HTML page...
070|     props = dict(
071|         h1='CJSonnack',
072|         h2='Python Apps',
073|         h3=f'{modname}.py',
074|         css=[], csstxt=CSS
075|     )
076|     pg = WebPage(modname.upper(), **props)
077|     pg.add()
078|     pg.add(f'<pre class="ModuleDoc">{h}</pre>')
079|     pg.add()
080| 
081|     def section (rcd_type):
082|         for rcd in sorted(r for r in records if r[1]==rcd_type):
083|             nam = rcd[0]
084|             typ = rcd[1]
085|             txt = rcd[3]
086|             pg.add(f'<h4 class="{typ}"><span style="font-weight:normal;">{typ}</span> {nam}</h4>')
087| 
088|             if (txt is not None) and (0 < len(txt)):
089|                 s = escape(txt.strip())
090|                 pg.add(f'<pre class="ObjectDoc">{s}</pre>')
091|             pg.add()
092| 
093|     section('class')
094|     section('function')
095|     section('lambda')
096|     section('dict')
097|     section('tuple')
098|     section('list')
099|     section('str')
100|     section('int')
101|     section('float')
102| 
103|     fn = path.join(fpath, fname)
104|     pg.save(fn)
105| 
106|     ix = h.find('.')
107|     return (h if ix<0 else h[:ix])
108| 
109| 
110| ####################################################################################################
111| if __name__ == '__main__':
112|     print(argv[0])
113|     print()
114|     for mname in argv[1:]:
115|         print(mname)
116|         fo = fileobj(mname)
117|         syspath.append(fo.pathname)
118|         module_docs(fo.name, fname=f'{fo.name}-docs.html', fpath=fo.pathname)
119|     print()
120| ####################################################################################################
121| '''eof'''
122|