CAT-SOOP is a tool for automatic collection and assessment of online exercises https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

content.py 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import ast
  2. import inspect
  3. module = cs_form.get('p', 'catsoop')
  4. cs_problem_spec = []
  5. if not (module.startswith('catsoop.') or module == 'catsoop'):
  6. module = 'catsoop'
  7. def _print(*args):
  8. cs_problem_spec.extend(str(i) for i in args)
  9. cs_problem_spec.append('\n')
  10. broke = False
  11. try:
  12. module = __import__(module, fromlist='dummy')
  13. except:
  14. broke = True
  15. if not broke:
  16. if module != 'catsoop':
  17. cs_content_header += ': <code>%s</code>' % module.__name__
  18. if module.__file__.endswith('__init__.py'):
  19. # this is a package; list its contents
  20. if module.__doc__ is not None:
  21. _print(module.__doc__)
  22. _print()
  23. _print('## Members:')
  24. for child in sorted(dir(module)):
  25. if child.startswith('_'):
  26. continue
  27. child_mod = getattr(module, child)
  28. if not isinstance(child_mod, type(module)):
  29. continue
  30. docline = ((child_mod.__doc__ or '').strip() or '\n').splitlines()[0]
  31. _print('* [%s](CURRENT?p=%s): %s' % (child, child_mod.__name__, docline))
  32. else:
  33. with open(module.__file__) as f:
  34. tree = ast.parse(f.read())
  35. _docs = {}
  36. for ix, node in enumerate(tree.body[:-1]):
  37. nextnode = tree.body[ix+1]
  38. if (isinstance(node, ast.Assign)
  39. and ix < len(tree.body)-1
  40. and isinstance(nextnode, ast.Expr)
  41. and isinstance(nextnode.value, ast.Str)
  42. and len(node.targets) == 1
  43. and isinstance(node.targets[0], ast.Name)):
  44. _docs[node.targets[0].id] = nextnode.value.s
  45. s = '<a href="%s/%s.py" target="_blank">View Source</a>' % (source_view_url_root,
  46. module.__name__.replace('.','/'))
  47. if module.__doc__ is not None:
  48. _print(module.__doc__)
  49. _print()
  50. _print(s)
  51. _print('## Members')
  52. for vname in sorted(dir(module)):
  53. if vname.startswith('_') or vname in getattr(module, '_nodoc', set()):
  54. continue
  55. x = getattr(module, vname)
  56. if type(x) == type(module):
  57. # this is a module, skip
  58. continue
  59. doc = x.__doc__
  60. if x.__doc__ == type(x).__doc__:
  61. doc = None
  62. if vname in _docs:
  63. doc = _docs[vname]
  64. try:
  65. lines, start = inspect.getsourcelines(x)
  66. l = ', <a href="%s/%s.py#L%s" target="_blank">lines %s-%s</a>' % (source_view_url_root,
  67. module.__name__.replace('.','/'),
  68. start, start, start+len(lines)-1)
  69. except:
  70. l = ''
  71. _print('<a name="%s"></a>' % vname)
  72. _print()
  73. _print('* <font size="+2">**%s**</font> (`%s`%s)' % (module.__name__ + '.' + vname, type(x).__name__, l))
  74. if doc is not None:
  75. _print()
  76. _print()
  77. _print('\n'.join(' %s' % (i[4:] if i.startswith(' ') else i) for i in doc.strip().splitlines()))
  78. _print('<p>&nbsp;</p>')
  79. _print()
  80. cs_problem_spec = [csm_language._md_format_string(globals(), ''.join(cs_problem_spec), False)]