Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Moved test documentation builder into build_documentation script

git-svn-id: http://code.djangoproject.com/svn/djangoproject.com@372 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5e9f3153cbbce408939121c54b3e6e518e3f6eab 1 parent 26393a7
authored August 01, 2005
134  django_website/apps/docs/parts/build_documentation.py
@@ -12,28 +12,124 @@
12 12
 from docutils import nodes, utils
13 13
 from docutils.core import publish_parts
14 14
 from docutils.writers import html4css1
15  
-from django.conf.settings import DJANGO_DOCUMENT_ROOT_PATH
  15
+from django.conf import settings
  16
+from django.core import template
16 17
 
17 18
 SETTINGS = {
18 19
     'initial_header_level': 2
19 20
 }
20 21
 
21  
-def build(dirs):
  22
+MODEL_DOC_TEMPLATE = """
  23
+<div class="document" id="model-{{ model_name }}">
  24
+
  25
+<h1 class="title">{{ title }}</h1>
  26
+{{ blurb }}
  27
+
  28
+<h2 id='model-source-code'>Model source code</h2>
  29
+<pre class="literal-block">{{ model_source }}</pre>
  30
+
  31
+<h2 id='api-reference'>API reference</h2>
  32
+
  33
+{% for model in models %}
  34
+<h3>{{ model.name }} objects have the following methods:</h3>
  35
+<ul>
  36
+{% for method in model.methods %}<li><tt class="docutils literal"><span class="pre">{{ method }}()</span></tt></li>
  37
+{% endfor %}</ul>
  38
+{% endfor %}
  39
+
  40
+<h2 id='sample-usage'>Sample API usage</h2>
  41
+<pre class="literal-block">{{ api_usage }}</pre>
  42
+</div>
  43
+"""
  44
+
  45
+MODEL_TOC = """
  46
+<ul>
  47
+<li><a href="#model-source-code">Model source code</a></li>
  48
+<li><a href="api-reference">API reference</a></li>
  49
+<li><a href="sample-usage">Sample API usage</a></li>
  50
+</ul>
  51
+"""
  52
+
  53
+def build_documents():
  54
+    writer = DjangoHTMLWriter()
  55
+    for fname in glob.glob1(settings.DJANGO_DOCUMENT_ROOT_PATH, "*.txt"):
  56
+        in_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, fname)
  57
+        out_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, os.path.splitext(fname)[0] + ".html")
  58
+        toc_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, os.path.splitext(fname)[0] + "_toc.html")
  59
+        parts = publish_parts(
  60
+            open(in_file).read(),
  61
+            source_path=in_file,
  62
+            destination_path=out_file,
  63
+            writer=writer,
  64
+            settings_overrides=SETTINGS,
  65
+        )
  66
+        open(out_file, 'w').write(parts['html_body'])
  67
+        open(toc_file, 'w').write(parts['toc'])
  68
+            
  69
+def build_test_documents():
  70
+    sys.path.append(settings.DJANGO_TESTS_PATH)
22 71
     writer = DjangoHTMLWriter()
23  
-    for dir in dirs:
24  
-        for fname in glob.glob1(dir, "*.txt"):
25  
-            in_file = os.path.join(dir, fname)
26  
-            out_file = os.path.join(dir, os.path.splitext(fname)[0] + ".html")
27  
-            toc_file = os.path.join(dir, os.path.splitext(fname)[0] + "_toc.html")
28  
-            parts = publish_parts(
29  
-                open(in_file).read(),
30  
-                source_path=in_file,
31  
-                destination_path=out_file,
32  
-                writer=writer,
33  
-                settings_overrides=SETTINGS,
34  
-            )
35  
-            open(out_file, 'w').write(parts['html_body'])
36  
-            open(toc_file, 'w').write(parts['toc'])
  72
+    import runtests
  73
+    
  74
+    # Manually set INSTALLED_APPS to point to the test app.
  75
+    settings.INSTALLED_APPS = (runtests.APP_NAME,)
  76
+
  77
+    for model_name in runtests.get_test_models():
  78
+        mod = meta.get_app(model_name)
  79
+
  80
+        out_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, 'model_' + model_name + '.html')
  81
+        toc_file = os.path.join(settings.DJANGO_DOCUMENT_ROOT_PATH, 'model_' + model_name + '_toc.html')
  82
+
  83
+        # Clean up the title and blurb.
  84
+        title, blurb = mod.__doc__.strip().split('\n', 1)
  85
+        parts = publish_parts(
  86
+            blurb,
  87
+            source_path=mod.__file__,
  88
+            destination=out_file,
  89
+            writer=writer,
  90
+            settings_overrides=SETTINGS,
  91
+        )
  92
+        blurb = parts["html_body"]
  93
+        api_usage = mod.API_TESTS
  94
+
  95
+        # Get the source code of the model, without the docstring or the
  96
+        # API_TESTS variable.
  97
+        model_source = inspect.getsource(mod)
  98
+        model_source = model_source.replace(mod.__doc__, '')
  99
+        model_source = model_source.replace(mod.API_TESTS, '')
  100
+        model_source = model_source.replace('""""""\n', '\n')
  101
+        model_source = model_source.replace('API_TESTS = ', '')
  102
+        model_source = model_source.strip()
  103
+
  104
+        models = []
  105
+        for m in mod._MODELS:
  106
+            models.append({
  107
+                'name': m._meta.object_name,
  108
+                'methods': [method for method in dir(m) if not method.startswith('_')],
  109
+            })
  110
+
  111
+        # Run this through the template system.
  112
+        t = template.Template(MODEL_DOC_TEMPLATE)
  113
+        c = template.Context(locals())
  114
+        html = t.render(c)
  115
+
  116
+        try:
  117
+            fp = open(out_file, 'w')
  118
+        except IOError:
  119
+            sys.stderr.write("Couldn't write to %s.\n" % file_name)
  120
+            continue
  121
+        else:
  122
+            fp.write(html)
  123
+            fp.close()
  124
+            
  125
+        try:
  126
+            fp = open(toc_file, 'w')
  127
+        except IOError:
  128
+            sys.stderr.write("Couldn't write to %s.\n" % file_name)
  129
+            continue
  130
+        else:
  131
+            fp.write(MODEL_TOC)
  132
+            fp.close()    
37 133
 
38 134
 class DjangoHTMLWriter(html4css1.Writer):
39 135
     def __init__(self):
@@ -133,7 +229,5 @@ def visit_title(self, node):
133 229
 
134 230
     
135 231
 if __name__ == "__main__":
136  
-    if len(sys.argv) > 1:
137  
-        build(sys.argv[1:])
138  
-    else:
139  
-        build([DJANGO_DOCUMENT_ROOT_PATH])
  232
+    build_documents()
  233
+    build_test_documents()
1  django_website/settings/main.py
@@ -29,6 +29,7 @@
29 29
 
30 30
 # setting for documentation root path
31 31
 DJANGO_DOCUMENT_ROOT_PATH = "/home/html/djangoproject.com/docs/"
  32
+DJANGO_TESTS_PATH = "/home/html/djangoproject.com/docs/tests/"
32 33
 
33 34
 CACHE_MIDDLEWARE_SECONDS = 60 * 60 * 1 # 1 hour
34 35
 CACHE_MIDDLEWARE_KEY_PREFIX = 'djangoproject'

0 notes on commit 5e9f315

Please sign in to comment.
Something went wrong with that request. Please try again.