Revision: 6672
          http://matplotlib.svn.sourceforge.net/matplotlib/?rev=6672&view=rev
Author:   mdboom
Date:     2008-12-18 19:07:08 +0000 (Thu, 18 Dec 2008)

Log Message:
-----------
Fix how example files are added to the build.  Saves about 1MB in html output.

Modified Paths:
--------------
    branches/v0_98_5_maint/doc/conf.py
    branches/v0_98_5_maint/doc/make.py
    branches/v0_98_5_maint/doc/sphinxext/plot_directive.py
    branches/v0_98_5_maint/lib/matplotlib/pyplot.py

Added Paths:
-----------
    branches/v0_98_5_maint/doc/sphinxext/gen_rst.py

Modified: branches/v0_98_5_maint/doc/conf.py
===================================================================
--- branches/v0_98_5_maint/doc/conf.py  2008-12-18 17:41:11 UTC (rev 6671)
+++ branches/v0_98_5_maint/doc/conf.py  2008-12-18 19:07:08 UTC (rev 6672)
@@ -29,7 +29,7 @@
 # coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
 extensions = ['mathmpl', 'math_symbol_table', 'sphinx.ext.autodoc',
               'only_directives', 'plot_directive', 'inheritance_diagram',
-              'gen_gallery']
+              'gen_gallery', 'gen_rst']
 
 # Add any paths that contain templates here, relative to this directory.
 templates_path = ['_templates']

Modified: branches/v0_98_5_maint/doc/make.py
===================================================================
--- branches/v0_98_5_maint/doc/make.py  2008-12-18 17:41:11 UTC (rev 6671)
+++ branches/v0_98_5_maint/doc/make.py  2008-12-18 19:07:08 UTC (rev 6672)
@@ -34,15 +34,12 @@
 
 def html():
     check_build()
-    if not os.path.exists('examples/index.rst'):
-        examples()
     shutil.copy('../lib/matplotlib/mpl-data/matplotlibrc', 
'_static/matplotlibrc')
-    #figs()
     if small_docs:
         options = "-D plot_formats=\"['png']\""
     else:
         options = ''
-    if os.system('sphinx-build %s -b html -d build/doctrees . build/html' % 
options):
+    if os.system('sphinx-build %s -P -b html -d build/doctrees . build/html' % 
options):
         raise SystemExit("Building HTML failed.")
 
     figures_dest_path = 'build/html/pyplots'

Copied: branches/v0_98_5_maint/doc/sphinxext/gen_rst.py (from rev 6660, 
branches/v0_98_5_maint/doc/examples/gen_rst.py)
===================================================================
--- branches/v0_98_5_maint/doc/sphinxext/gen_rst.py                             
(rev 0)
+++ branches/v0_98_5_maint/doc/sphinxext/gen_rst.py     2008-12-18 19:07:08 UTC 
(rev 6672)
@@ -0,0 +1,155 @@
+"""
+generate the rst files for the examples by iterating over the pylab examples
+"""
+import os, glob
+
+import os
+import re
+import sys
+fileList = []
+
+def out_of_date(original, derived):
+    """
+    Returns True if derivative is out-of-date wrt original,
+    both of which are full file paths.
+
+    TODO: this check isn't adequate in some cases.  Eg, if we discover
+    a bug when building the examples, the original and derived will be
+    unchanged but we still want to force a rebuild.
+    """
+    return (not os.path.exists(derived) or
+            os.stat(derived).st_mtime < os.stat(original).st_mtime)
+
+noplot_regex = re.compile(r"#\s*-\*-\s*noplot\s*-\*-")
+
+def generate_example_rst(app):
+    rootdir = os.path.join(app.builder.srcdir, 'mpl_examples')
+    exampledir = os.path.join(app.builder.srcdir, 'examples')
+    if not os.path.exists(exampledir):
+        os.makedirs(exampledir)
+
+    datad = {}
+    for root, subFolders, files in os.walk(rootdir):
+        for fname in files:
+            if ( fname.startswith('.') or fname.startswith('#') or 
fname.startswith('_') or
+                 fname.find('.svn')>=0 or not fname.endswith('.py') ):
+                continue
+
+            fullpath = os.path.join(root,fname)
+            contents = file(fullpath).read()
+            # indent
+            relpath = os.path.split(root)[-1]
+            datad.setdefault(relpath, []).append((fullpath, fname, contents))
+
+    subdirs = datad.keys()
+    subdirs.sort()
+
+    fhindex = file(os.path.join(exampledir, 'index.rst'), 'w')
+    fhindex.write("""\
+.. _examples-index:
+
+####################
+Matplotlib Examples
+####################
+
+.. htmlonly::
+
+    :Release: |version|
+    :Date: |today|
+
+.. toctree::
+    :maxdepth: 2
+
+""")
+
+    for subdir in subdirs:
+        rstdir = os.path.join(exampledir, subdir)
+        if not os.path.exists(rstdir):
+            os.makedirs(rstdir)
+
+        outputdir = os.path.join(app.builder.outdir, 'examples')
+        if not os.path.exists(outputdir):
+            os.makedirs(outputdir)
+
+        outputdir = os.path.join(outputdir, subdir)
+        if not os.path.exists(outputdir):
+            os.makedirs(outputdir)
+
+        subdirIndexFile = os.path.join(rstdir, 'index.rst')
+        fhsubdirIndex = file(subdirIndexFile, 'w')
+        fhindex.write('    %s/index.rst\n\n'%subdir)
+
+        fhsubdirIndex.write("""\
+.. _%s-examples-index:
+
+##############################################
+%s Examples
+##############################################
+
+.. htmlonly::
+
+    :Release: |version|
+    :Date: |today|
+
+.. toctree::
+    :maxdepth: 1
+
+"""%(subdir, subdir))
+
+        print subdir
+
+        data = datad[subdir]
+        data.sort()
+
+        for fullpath, fname, contents in data:
+            basename, ext = os.path.splitext(fname)
+            outputfile = os.path.join(outputdir, fname)
+            #thumbfile = os.path.join(thumb_dir, '%s.png'%basename)
+            #print '    static_dir=%s, basename=%s, fullpath=%s, fname=%s, 
thumb_dir=%s, thumbfile=%s'%(static_dir, basename, fullpath, fname, thumb_dir, 
thumbfile)
+
+            rstfile = '%s.rst'%basename
+            outrstfile = os.path.join(rstdir, rstfile)
+
+            fhsubdirIndex.write('    %s\n'%rstfile)
+
+            if (not out_of_date(fullpath, outputfile) and
+                not out_of_date(fullpath, outrstfile)):
+                continue
+
+            print '    %s'%fname
+
+            fh = file(outrstfile, 'w')
+            fh.write('.. _%s-%s:\n\n'%(subdir, basename))
+            title = '%s example code: %s'%(subdir, fname)
+            #title = '<img src=%s> %s example code: %s'%(thumbfile, subdir, 
fname)
+
+
+            fh.write(title + '\n')
+            fh.write('='*len(title) + '\n\n')
+
+            do_plot = (subdir in ('api',
+                                  'pylab_examples',
+                                  'units') and
+                       not noplot_regex.search(contents))
+
+            if do_plot:
+                fh.write("\n\n.. plot:: %s\n\n::\n\n" % fullpath)
+            else:
+                fh.write("[`source code <%s>`_]\n\n::\n\n" % fname)
+                fhstatic = file(outputfile, 'w')
+                fhstatic.write(contents)
+                fhstatic.close()
+
+            # indent the contents
+            contents = '\n'.join(['    %s'%row.rstrip() for row in 
contents.split('\n')])
+            fh.write(contents)
+
+            fh.write('\n\nKeywords: python, matplotlib, pylab, example, codex 
(see :ref:`how-to-search-examples`)')
+            fh.close()
+
+        fhsubdirIndex.close()
+
+    fhindex.close()
+
+def setup(app):
+    app.connect('builder-inited', generate_example_rst)


Property changes on: branches/v0_98_5_maint/doc/sphinxext/gen_rst.py
___________________________________________________________________
Added: svn:mergeinfo
   + /branches/v0_91_maint/doc/examples/gen_rst.py:5753-5771

Modified: branches/v0_98_5_maint/doc/sphinxext/plot_directive.py
===================================================================
--- branches/v0_98_5_maint/doc/sphinxext/plot_directive.py      2008-12-18 
17:41:11 UTC (rev 6671)
+++ branches/v0_98_5_maint/doc/sphinxext/plot_directive.py      2008-12-18 
19:07:08 UTC (rev 6672)
@@ -34,6 +34,41 @@
 import matplotlib.image as image
 from matplotlib import _pylab_helpers
 
+if hasattr(os.path, 'relpath'):
+    relpath = os.path.relpath
+else:
+    def relpath(target, base=os.curdir):
+        """
+        Return a relative path to the target from either the current dir or an 
optional base dir.
+        Base can be a directory specified either as absolute or relative to 
current dir.
+        """
+
+        if not os.path.exists(target):
+            raise OSError, 'Target does not exist: '+target
+
+        if not os.path.isdir(base):
+            raise OSError, 'Base is not a directory or does not exist: '+base
+
+        base_list = (os.path.abspath(base)).split(os.sep)
+        target_list = (os.path.abspath(target)).split(os.sep)
+
+        # On the windows platform the target may be on a completely different 
drive from the base.
+        if os.name in ['nt','dos','os2'] and base_list[0] <> target_list[0]:
+            raise OSError, 'Target is on a different drive to base. Target: 
'+target_list[0].upper()+', base: '+base_list[0].upper()
+
+        # Starting from the filepath root, work out how much of the filepath is
+        # shared by base and target.
+        for i in range(min(len(base_list), len(target_list))):
+            if base_list[i] <> target_list[i]: break
+        else:
+            # If we broke out of the loop, i is pointing to the first 
differing path elements.
+            # If we didn't break out of the loop, i is pointing to identical 
path elements.
+            # Increment i so that in all cases it points to the first 
differing path elements.
+            i+=1
+
+        rel_list = [os.pardir] * (len(base_list)-i) + target_list[i:]
+        return os.path.join(*rel_list)
+
 def write_char(s):
     sys.stdout.write(s)
     sys.stdout.flush()
@@ -186,6 +221,7 @@
     reference = directives.uri(arguments[0])
     basedir, fname = os.path.split(reference)
     basename, ext = os.path.splitext(fname)
+    basedir = relpath(basedir, setup.app.builder.srcdir)
 
     # Get the directory of the rst file, and determine the relative
     # path from the resulting html file to the plot_directive links

Modified: branches/v0_98_5_maint/lib/matplotlib/pyplot.py
===================================================================
--- branches/v0_98_5_maint/lib/matplotlib/pyplot.py     2008-12-18 17:41:11 UTC 
(rev 6671)
+++ branches/v0_98_5_maint/lib/matplotlib/pyplot.py     2008-12-18 19:07:08 UTC 
(rev 6672)
@@ -1138,9 +1138,9 @@
 def plotting():
     """
     Plotting commands
-    ============    =================================================
+    =============== =========================================================
     Command         Description
-    =========       =================================================
+    =============== =========================================================
     axes            Create a new axes
     axis            Set or return the current axis limits
     bar             make a bar chart
@@ -1193,7 +1193,7 @@
     title           add a title to the current axes
     xlabel          add an xlabel to the current axes
     ylabel          add a ylabel to the current axes
-    ============    =================================================
+    =============== =========================================================
 
     The following commands will set the default colormap accordingly:
 


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/matplotlib-checkins

Reply via email to