| 8 | | |
| 9 | | #-- Set up logging ------------------------------------------------------------- |
| 10 | | logging.basicConfig(level=logging.DEBUG, |
| 11 | | format='%(asctime)s %(levelname)-8s %(message)s', |
| 12 | | datefmt='%Y%m%d-%H%M%S', |
| 13 | | filename='wikidoc.log', |
| 14 | | filemode='w') |
| 15 | | # define a Handler which writes WARNING messages or higher to the sys.stderr |
| 16 | | console = logging.StreamHandler() |
| 17 | | console.setLevel(logging.WARNING) |
| 18 | | # set a format which is simpler for console use |
| 19 | | formatter = logging.Formatter('%(message)s') |
| 20 | | # tell the handler to use this format |
| 21 | | console.setFormatter(formatter) |
| 22 | | # add the handler to the root logger |
| 23 | | logging.getLogger('').addHandler(console) |
| | 9 | #import imp |
| | 10 | |
| | 11 | # Rather painful way of importing |
| | 12 | #this_file = os.path.abspath(__file__) |
| | 13 | #common_file = os.path.join(os.path.split(os.path.split(this_file)[0])[0],'common.py') |
| | 14 | #common = imp.load_source('common', common_file) |
| 38 | | |
| 39 | | classes = {} |
| 40 | | functions = [] |
| 41 | | data = [] |
| 42 | | |
| 43 | | #-- Process command line parameters -------------------------------------------- |
| 44 | | if len(sys.argv) > 1: |
| 45 | | output = sys.argv[1] |
| 46 | | else: |
| 47 | | output = 'wiki' |
| 48 | | logging.info('Generating API documentation in %s format' % output) |
| 49 | | |
| 50 | | #-- Define templates ----------------------------------------------------------- |
| 51 | | if output == 'wiki': |
| 52 | | default_arg_fmt = '%s<span style="color:grey;">=%s</span>' |
| 53 | | func_sig_fmt = '%s(<span style="font-weight:normal;">%s</span>)' |
| 54 | | function_fmt = '\n====<span style="color:#0066ff;">%s</span>====\n' |
| 55 | | method_fmt = '\n====<span style="color:#8888ff;">%s</span>====\n' |
| 56 | | staticmethod_fmt = '\n====<span style="color:#0066ff;">%s</span> (static)====\n' |
| 57 | | dict_fmt = "\n\n'''%s''' = {\n" |
| 58 | | dict_fmt_end = '}\n' |
| 59 | | data_element_fmt = "\n'''%s''' = %s\n" |
| 60 | | table_begin = "{|\n" |
| 61 | | table_end = "|}\n" |
| 62 | | table_row_fmt = "| || %s ||: %s\n|-\n" |
| 63 | | category_fmt = '\n==%s==\n' |
| 64 | | class_fmt = '\n===<span style="color:green">%s</span>===\n' |
| 65 | | horiz_line = '\n----\n' |
| 66 | | elif output == 'trac': |
| 67 | | default_arg_fmt = '%s=%s' |
| 68 | | func_sig_fmt = '%s(%s)' |
| 69 | | function_fmt = '\n=== %s ===\n' |
| 70 | | method_fmt = '\n=== %s ===\n' |
| 71 | | staticmethod_fmt = '\n=== %s ===\n' |
| 72 | | dict_fmt = "\n\n'''%s''' = {\n" |
| 73 | | dict_fmt_end = '}\n' |
| 74 | | data_element_fmt = "\n'''%s''' = %s\n" |
| 75 | | table_begin = "\n" |
| 76 | | table_end = "\n" |
| 77 | | table_row_fmt = "|| %s ||: %s ||\n" |
| 78 | | category_fmt = '\n= %s =\n' |
| 79 | | class_fmt = '\n== %s ==\n' |
| 80 | | horiz_line = '\n----\n' |
| 81 | | elif output == 'latex': |
| 82 | | default_arg_fmt = '%s{\\color{grey}=%s}' |
| 83 | | func_sig_fmt = '%s(\\mdseries %s)' |
| 84 | | function_fmt = '\n\\paragraph*{\\color{brightblue}{%s}}\n' |
| 85 | | method_fmt = '\n\\paragraph*{\\color{brightblue}{%s}}\n' |
| 86 | | staticmethod_fmt = '\n\\paragraph*{\\color{brightblue}{%s} (static)}\n' |
| 87 | | dict_fmt = '\n\\textbf{%s} = $\\lbrace$\n\n' |
| 88 | | dict_fmt_end = '$\\rbrace$\n' |
| 89 | | data_element_fmt = "\n\\textbf{%s} = %s\n" |
| 90 | | table_begin = "\\begin{tabular}{lll}\n" |
| 91 | | table_end = "\\end{tabular}\n" |
| 92 | | table_row_fmt = '& %s & :%s\\\\\n' |
| 93 | | category_fmt = '\n\\subsection{%s}\n' |
| 94 | | class_fmt = '\n\\subsubsection*{%s}\n' |
| 95 | | horiz_line = '' |
| 96 | | |
| | 29 | newline = "\n" |
| | 30 | |
| 161 | | # gather information from the common module |
| 162 | | logging.info("Gathering information from the common module.") |
| 163 | | for entry in dir(pyNN.common): |
| 164 | | if entry not in exclude: |
| 165 | | instance = eval('pyNN.common.%s' % entry) |
| 166 | | entry_type = type(instance) |
| 167 | | logging.info(' %-30s %s' % (entry,entry_type)) |
| 168 | | if entry_type in [types.ClassType, types.TypeType]: |
| 169 | | classes[entry] = { 'methods': [], 'data': [], 'staticmethods': [] } |
| 170 | | for classentry in dir(instance): |
| 171 | | if classentry not in exclude and (classentry[0] != '_' or classentry[0:2] == '__'): # don't include private methods |
| 172 | | classentry_type = type(eval('pyNN.common.%s.%s' % (entry,classentry))) |
| 173 | | logging.info(' %-28s %s' % (classentry,classentry_type)) |
| 174 | | if classentry_type == types.MethodType: |
| 175 | | classes[entry]['methods'].append(classentry) |
| 176 | | elif classentry_type == types.FunctionType: |
| 177 | | classes[entry]['staticmethods'].append(classentry) |
| | 96 | def apidoc(output): |
| | 97 | global newline |
| | 98 | classes = {} |
| | 99 | functions = [] |
| | 100 | data = [] |
| | 101 | |
| | 102 | #-- Define templates ----------------------------------------------------------- |
| | 103 | if output == 'wiki': |
| | 104 | default_arg_fmt = '%s<span style="color:grey;">=%s</span>' |
| | 105 | func_sig_fmt = '%s(<span style="font-weight:normal;">%s</span>)' |
| | 106 | function_fmt = '\n====<span style="color:#0066ff;">%s</span>====\n' |
| | 107 | method_fmt = '\n====<span style="color:#8888ff;">%s</span>====\n' |
| | 108 | staticmethod_fmt = '\n====<span style="color:#0066ff;">%s</span> (static)====\n' |
| | 109 | dict_fmt = "\n\n'''%s''' = {\n" |
| | 110 | dict_fmt_end = '}\n' |
| | 111 | data_element_fmt = "\n'''%s''' = %s\n" |
| | 112 | table_begin = "{|\n" |
| | 113 | table_end = "|}\n" |
| | 114 | table_row_fmt = "| || %s ||: %s\n|-\n" |
| | 115 | category_fmt = '\n==%s==\n' |
| | 116 | class_fmt = '\n===<span style="color:green">%s</span>===\n' |
| | 117 | horiz_line = '\n----\n' |
| | 118 | docstr_fmt = '%s' |
| | 119 | elif output == 'trac': |
| | 120 | default_arg_fmt = '%s=%s' |
| | 121 | func_sig_fmt = '%s(%s)' |
| | 122 | function_fmt = '\n=== %s ===\n' |
| | 123 | method_fmt = '\n=== %s ===\n' |
| | 124 | staticmethod_fmt = '\n=== %s ===\n' |
| | 125 | dict_fmt = "\n\n'''%s''' = {\n" |
| | 126 | dict_fmt_end = '}\n' |
| | 127 | data_element_fmt = "\n'''%s''' = %s\n" |
| | 128 | table_begin = "\n" |
| | 129 | table_end = "\n" |
| | 130 | table_row_fmt = "|| %s ||: %s ||\n" |
| | 131 | category_fmt = '\n= %s =\n' |
| | 132 | class_fmt = '\n== %s ==\n' |
| | 133 | horiz_line = '\n----\n' |
| | 134 | docstr_fmt = '%s' |
| | 135 | elif output == 'latex': |
| | 136 | default_arg_fmt = '%s{\\color{grey}=%s}' |
| | 137 | func_sig_fmt = '%s(\\mdseries %s)' |
| | 138 | function_fmt = '\n\\paragraph*{\\color{brightblue}{%s}}\n' |
| | 139 | method_fmt = '\n\\paragraph*{\\color{brightblue}{%s}}\n' |
| | 140 | staticmethod_fmt = '\n\\paragraph*{\\color{brightblue}{%s} (static)}\n' |
| | 141 | dict_fmt = '\n\\textbf{%s} = $\\lbrace$\n\n' |
| | 142 | dict_fmt_end = '$\\rbrace$\n' |
| | 143 | data_element_fmt = "\n\\textbf{%s} = %s\n" |
| | 144 | table_begin = "\\begin{tabular}{lll}\n" |
| | 145 | table_end = "\\end{tabular}\n" |
| | 146 | table_row_fmt = '& %s & :%s\\\\\n' |
| | 147 | category_fmt = '\n\\subsection{%s}\n' |
| | 148 | class_fmt = '\n\\subsubsection*{%s}\n' |
| | 149 | horiz_line = '' |
| | 150 | docstr_fmt = '%s' |
| | 151 | elif output == 'reportlab_xml': |
| | 152 | newline = "<br/>\n" |
| | 153 | default_arg_fmt = '%s<font color="gray">=%s</font>' |
| | 154 | func_sig_fmt = '<b>%s</b>(%s)' |
| | 155 | function_fmt = '\n<para style="FunctionDef"> %s </para>\n' |
| | 156 | method_fmt = function_fmt |
| | 157 | staticmethod_fmt = function_fmt |
| | 158 | dict_fmt = '\n\n<para><b>%s</b> = { ' |
| | 159 | dict_fmt_end = '}</para>\n' |
| | 160 | data_element_fmt = "<para><b>%s</b> = %s</para>" |
| | 161 | table_begin = ''#"<table>[" |
| | 162 | table_end = ''#"]</table>" |
| | 163 | #table_row_fmt = "[ '%s', ':', '%s'],\n" |
| | 164 | table_row_fmt = "'%s': %s, " |
| | 165 | category_fmt = '\n<para style="Category">%s</para>\n' |
| | 166 | class_fmt = '\n<para style="Class">%s</para>\n' |
| | 167 | horiz_line = '' |
| | 168 | docstr_fmt = '<para>%s</para>' |
| | 169 | |
| | 170 | # gather information from the common module |
| | 171 | logging.info("Gathering information from the common module.") |
| | 172 | for entry in dir(common): |
| | 173 | if entry not in exclude: |
| | 174 | instance = eval('common.%s' % entry) |
| | 175 | entry_type = type(instance) |
| | 176 | logging.info(' %-30s %s' % (entry,entry_type)) |
| | 177 | if entry_type in [types.ClassType, types.TypeType]: |
| | 178 | classes[entry] = { 'methods': [], 'data': [], 'staticmethods': [] } |
| | 179 | for classentry in dir(instance): |
| | 180 | if classentry not in exclude and (classentry[0] != '_' or classentry[0:2] == '__'): # don't include private methods |
| | 181 | classentry_type = type(eval('common.%s.%s' % (entry,classentry))) |
| | 182 | logging.info(' %-28s %s' % (classentry,classentry_type)) |
| | 183 | if classentry_type == types.MethodType: |
| | 184 | classes[entry]['methods'].append(classentry) |
| | 185 | elif classentry_type == types.FunctionType: |
| | 186 | classes[entry]['staticmethods'].append(classentry) |
| | 187 | else: |
| | 188 | classes[entry]['data'].append(classentry) |
| 179 | | classes[entry]['data'].append(classentry) |
| | 190 | logging.info(' %-28s excluded' % classentry) |
| | 191 | elif entry_type == types.FunctionType: |
| | 192 | functions.append(entry) |
| | 193 | elif entry_type == types.ModuleType: |
| | 194 | pass |
| | 195 | else: |
| | 196 | data.append(entry) |
| | 197 | else: |
| | 198 | logging.info(' %-30s excluded' % entry) |
| | 199 | |
| | 200 | # output starts here |
| | 201 | outputStr = '' |
| | 202 | if output == 'latex': |
| | 203 | outputStr += '\definecolor{brightblue}{rgb}{0.0,0.38,1.0}\n' |
| | 204 | outputStr += '\definecolor{paleblue}{rgb}{0.5,0.5,1.0}\n' |
| | 205 | outputStr += '\definecolor{grey}{rgb}{0.5,0.5,0.5}\n' |
| | 206 | |
| | 207 | logging.info("==== FUNCTIONS ====") |
| | 208 | outputStr += category_fmt % "Functions" |
| | 209 | for funcname in functions: |
| | 210 | funcinst = eval('common.%s' % funcname) |
| | 211 | outputStr += function_fmt % func_sig(funcinst, default_arg_fmt, func_sig_fmt) |
| | 212 | if funcinst.__doc__: |
| | 213 | outputStr += docstr_fmt % _(funcinst.__doc__.strip()) |
| | 214 | |
| | 215 | logging.info("==== CLASSES ====") |
| | 216 | # sort classes by type: |
| | 217 | error_classes = {} |
| | 218 | celltype_classes = {} |
| | 219 | other_classes = {} |
| | 220 | for classname in classes.keys(): |
| | 221 | if classname.find('Error') > -1: |
| | 222 | error_classes[classname] = classes[classname] |
| | 223 | elif issubclass(eval('common.%s' % classname),common.StandardCellType): |
| | 224 | celltype_classes[classname] = classes[classname] |
| | 225 | else: |
| | 226 | other_classes[classname] = classes[classname] |
| | 227 | |
| | 228 | logging.info('Sorting classes...') |
| | 229 | logging.info('Error classes: %s' % ', '.join(error_classes.keys())) |
| | 230 | logging.info('Celltype classes: %s' % ', '.join(celltype_classes.keys())) |
| | 231 | logging.info('Other classes: %s' % ', '.join(other_classes.keys())) |
| | 232 | |
| | 233 | # Now iterate through the classes |
| | 234 | outputStr += category_fmt % "Classes" |
| | 235 | for classes in [celltype_classes, other_classes, error_classes]: |
| | 236 | classlist = classes.keys() |
| | 237 | classlist.sort() |
| | 238 | for classname in classlist: |
| | 239 | outputStr += class_fmt % classname |
| | 240 | docstr = eval('common.%s.__doc__' % classname) |
| | 241 | if docstr: |
| | 242 | outputStr += docstr_fmt % _(docstr) |
| | 243 | for methodname in classes[classname]['methods']: |
| | 244 | methodinst = eval('common.%s.%s' % (classname,methodname)) |
| | 245 | fs = func_sig(methodinst, default_arg_fmt, func_sig_fmt) |
| | 246 | if fs: |
| | 247 | outputStr += method_fmt % fs |
| | 248 | if methodinst.__doc__: |
| | 249 | outputStr += docstr_fmt % _(methodinst.__doc__.strip()) |
| | 250 | for methodname in classes[classname]['staticmethods']: |
| | 251 | methodinst = eval('common.%s.%s' % (classname,methodname)) |
| | 252 | fs = func_sig(methodinst, default_arg_fmt, func_sig_fmt) |
| | 253 | if fs: |
| | 254 | outputStr += staticmethod_fmt % fs |
| | 255 | if methodinst.__doc__: |
| | 256 | outputStr += docstr_fmt % _(methodinst.__doc__.strip()) |
| | 257 | for element in classes[classname]['data']: |
| | 258 | instance = eval('common.%s.%s' % (classname,element)) |
| | 259 | if type(instance) == types.DictType: |
| | 260 | outputStr += dict_fmt % element |
| | 261 | if len(instance) > 0: |
| | 262 | outputStr += table_begin |
| | 263 | for k,v in instance.items(): |
| | 264 | if output == 'latex': |
| | 265 | v = str(v).replace('{',' $\\lbrace$').replace('}',' $\\rbrace$') |
| | 266 | outputStr += table_row_fmt % (k,v) |
| | 267 | elif output == 'wiki': |
| | 268 | outputStr += table_row_fmt % ('"%s"' % k,v) |
| | 269 | elif output == 'trac': |
| | 270 | outputStr += table_row_fmt % ("'%s'" % k,v) |
| | 271 | elif output == 'reportlab_xml': |
| | 272 | outputStr += table_row_fmt % ("'%s'" % k,v) |
| | 273 | outputStr += table_end |
| | 274 | outputStr += dict_fmt_end |
| 187 | | data.append(entry) |
| | 294 | outputStr += data_element_fmt % (element, instance) |
| | 295 | |
| | 296 | if output == 'latex': |
| | 297 | outputStr = outputStr.replace('_','\_') |
| | 298 | outputStr = outputStr.replace('>','$>$') |
| | 299 | outputStr = outputStr.replace('<','$<$') |
| | 300 | outputStr = leftquote.sub('`',outputStr) |
| | 301 | outputStr = leftdblquote.sub('``',outputStr) |
| | 302 | if output == 'trac': |
| | 303 | outputStr = outputStr.replace('__','!__') |
| | 304 | outputStr = camelcase.sub(r'!\1',outputStr) |
| | 305 | |
| | 306 | return outputStr |
| | 307 | |
| | 308 | # ============================================================================== |
| | 309 | if __name__ == "__main__": |
| | 310 | |
| | 311 | #-- Set up logging ------------------------------------------------------------- |
| | 312 | logging.basicConfig(level=logging.DEBUG, |
| | 313 | format='%(asctime)s %(levelname)-8s %(message)s', |
| | 314 | datefmt='%Y%m%d-%H%M%S', |
| | 315 | filename='wikidoc.log', |
| | 316 | filemode='w') |
| | 317 | # define a Handler which writes WARNING messages or higher to the sys.stderr |
| | 318 | console = logging.StreamHandler() |
| | 319 | console.setLevel(logging.WARNING) |
| | 320 | # set a format which is simpler for console use |
| | 321 | formatter = logging.Formatter('%(message)s') |
| | 322 | # tell the handler to use this format |
| | 323 | console.setFormatter(formatter) |
| | 324 | # add the handler to the root logger |
| | 325 | logging.getLogger('').addHandler(console) |
| | 326 | |
| | 327 | #-- Process command line parameters -------------------------------------------- |
| | 328 | if len(sys.argv) > 1: |
| | 329 | output = sys.argv[1] |
| 189 | | logging.info(' %-30s excluded' % entry) |
| 190 | | |
| 191 | | # output starts here |
| 192 | | outputStr = '' |
| 193 | | if output == 'latex': |
| 194 | | outputStr += '\definecolor{brightblue}{rgb}{0.0,0.38,1.0}\n' |
| 195 | | outputStr += '\definecolor{paleblue}{rgb}{0.5,0.5,1.0}\n' |
| 196 | | outputStr += '\definecolor{grey}{rgb}{0.5,0.5,0.5}\n' |
| 197 | | |
| 198 | | logging.info("==== DATA ====") |
| 199 | | outputStr += category_fmt % "Data" |
| 200 | | for element in data: |
| 201 | | instance = eval('pyNN.common.%s' % element) |
| 202 | | if type(instance) == types.DictType: |
| 203 | | outputStr += dict_fmt % element |
| 204 | | outputStr += table_begin |
| 205 | | for k,v in instance.items(): |
| 206 | | if output == 'latex': |
| 207 | | v = str(v).replace('{',' $\\lbrace$').replace('}',' $\\rbrace$') |
| 208 | | outputStr += table_row_fmt % (k,v) |
| 209 | | outputStr += table_end |
| 210 | | outputStr += dict_fmt_end |
| 211 | | else: |
| 212 | | outputStr += data_element_fmt % (element, instance) |
| 213 | | |
| 214 | | logging.info("==== FUNCTIONS ====") |
| 215 | | outputStr += category_fmt % "Functions" |
| 216 | | for funcname in functions: |
| 217 | | funcinst = eval('pyNN.common.%s' % funcname) |
| 218 | | outputStr += function_fmt % func_sig(funcinst) |
| 219 | | if funcinst.__doc__: |
| 220 | | outputStr += _(funcinst.__doc__.strip()) |
| 221 | | |
| 222 | | logging.info("==== CLASSES ====") |
| 223 | | # sort classes by type: |
| 224 | | error_classes = {} |
| 225 | | celltype_classes = {} |
| 226 | | other_classes = {} |
| 227 | | for classname in classes.keys(): |
| 228 | | if classname.find('Error') > -1: |
| 229 | | error_classes[classname] = classes[classname] |
| 230 | | elif issubclass(eval('pyNN.common.%s' % classname),pyNN.common.StandardCellType): |
| 231 | | celltype_classes[classname] = classes[classname] |
| 232 | | else: |
| 233 | | other_classes[classname] = classes[classname] |
| 234 | | |
| 235 | | logging.info('Sorting classes...') |
| 236 | | logging.info('Error classes: %s' % ', '.join(error_classes.keys())) |
| 237 | | logging.info('Celltype classes: %s' % ', '.join(celltype_classes.keys())) |
| 238 | | logging.info('Other classes: %s' % ', '.join(other_classes.keys())) |
| 239 | | |
| 240 | | # Now iterate through the classes |
| 241 | | outputStr += category_fmt % "Classes" |
| 242 | | for classes in [celltype_classes, other_classes, error_classes]: |
| 243 | | classlist = classes.keys() |
| 244 | | classlist.sort() |
| 245 | | for classname in classlist: |
| 246 | | outputStr += class_fmt % classname |
| 247 | | docstr = eval('pyNN.common.%s.__doc__' % classname) |
| 248 | | if docstr: |
| 249 | | outputStr += _(docstr) |
| 250 | | for methodname in classes[classname]['methods']: |
| 251 | | methodinst = eval('pyNN.common.%s.%s' % (classname,methodname)) |
| 252 | | fs = func_sig(methodinst) |
| 253 | | if fs: |
| 254 | | outputStr += method_fmt % fs |
| 255 | | if methodinst.__doc__: |
| 256 | | outputStr += _(methodinst.__doc__.strip()) |
| 257 | | for methodname in classes[classname]['staticmethods']: |
| 258 | | methodinst = eval('pyNN.common.%s.%s' % (classname,methodname)) |
| 259 | | fs = func_sig(methodinst) |
| 260 | | if fs: |
| 261 | | outputStr += staticmethod_fmt % fs |
| 262 | | if methodinst.__doc__: |
| 263 | | outputStr += _(methodinst.__doc__.strip()) |
| 264 | | for element in classes[classname]['data']: |
| 265 | | instance = eval('pyNN.common.%s.%s' % (classname,element)) |
| 266 | | if type(instance) == types.DictType: |
| 267 | | outputStr += dict_fmt % element |
| 268 | | if len(instance) > 0: |
| 269 | | outputStr += table_begin |
| 270 | | for k,v in instance.items(): |
| 271 | | if output == 'latex': |
| 272 | | v = str(v).replace('{',' $\\lbrace$').replace('}',' $\\rbrace$') |
| 273 | | outputStr += table_row_fmt % (k,v) |
| 274 | | elif output == 'wiki': |
| 275 | | outputStr += table_row_fmt % ('"%s"' % k,v) |
| 276 | | elif output == 'trac': |
| 277 | | outputStr += table_row_fmt % ("'%s'" % k,v) |
| 278 | | outputStr += table_end |
| 279 | | outputStr += dict_fmt_end |
| 280 | | else: |
| 281 | | outputStr += data_element_fmt % (element, instance) |
| 282 | | |
| 283 | | outputStr += horiz_line |
| 284 | | |
| 285 | | if output == 'latex': |
| 286 | | outputStr = outputStr.replace('_','\_') |
| 287 | | outputStr = outputStr.replace('>','$>$') |
| 288 | | outputStr = outputStr.replace('<','$<$') |
| 289 | | outputStr = leftquote.sub('`',outputStr) |
| 290 | | outputStr = leftdblquote.sub('``',outputStr) |
| 291 | | if output == 'trac': |
| 292 | | outputStr = outputStr.replace('__','!__') |
| 293 | | outputStr = camelcase.sub(r'!\1',outputStr) |
| 294 | | |
| 295 | | print outputStr |
| | 331 | output = 'wiki' |
| | 332 | logging.info('Generating API documentation in %s format' % output) |
| | 333 | print apidoc(output) |