From 82286ad884aeaf0538aeeccd8bdfe6fc34bb26ad 2013-05-09 21:22:46
From: Ahmet Bakan <lordnapi@gmail.com>
Date: 2013-05-09 21:22:46
Subject: [PATCH] Using a class varilable instead of temp files for keeping track of docs.

:meth:`IpythonDirective.setup` method was using a temporary file, such
as :file:`/tmp/seen_docRANDOM`, for storing visited source document
filenames. For a document that is not visited (name not in the temporary
file), it restarts counting line numbers.  Temporary file names are
selected randomly and they were not deleted from the disk upon
completion of a build. Since :meth:`.setup` picks a temp file
arbitrarily to visited status of a file is determined incorrectly.
Using a class variable resolves this problem.

Changed the directive setup function to use a class files for keeping
track of documents.

---

diff --git a/docs/sphinxext/ipython_directive.py b/docs/sphinxext/ipython_directive.py
index 79cd2ae..42131d1 100644
--- a/docs/sphinxext/ipython_directive.py
+++ b/docs/sphinxext/ipython_directive.py
@@ -77,7 +77,7 @@ from docutils.parsers.rst import directives
 from docutils import nodes
 from sphinx.util.compat import Directive
 
-matplotlib.use('Agg')
+#matplotlib.use('Agg')
 
 # Our own
 from IPython import Config, InteractiveShell
@@ -532,6 +532,8 @@ class IpythonDirective(Directive):
 
     shell = EmbeddedSphinxShell()
 
+    seen_docs = set()
+
     def get_config_options(self):
         # contains sphinx configuration variables
         config = self.state.document.settings.env.config
@@ -558,16 +560,13 @@ class IpythonDirective(Directive):
         # reset the execution count if we haven't processed this doc
         #NOTE: this may be borked if there are multiple seen_doc tmp files
         #check time stamp?
-        seen_docs = [i for i in os.listdir(tempfile.tempdir)
-            if i.startswith('seen_doc')]
-        if seen_docs:
-            fname = os.path.join(tempfile.tempdir, seen_docs[0])
-            docs = open(fname).read().split('\n')
-            if not self.state.document.current_source in docs:
+
+
+        if not self.state.document.current_source in self.seen_docs:
                 self.shell.IP.history_manager.reset()
                 self.shell.IP.execution_count = 1
-        else: # haven't processed any docs yet
-            docs = []
+                self.seen_docs.add(self.state.document.current_source)
+
 
 
         # get config values
@@ -588,13 +587,6 @@ class IpythonDirective(Directive):
                                       store_history=False)
         self.shell.clear_cout()
 
-        # write the filename to a tempfile because it's been "seen" now
-        if not self.state.document.current_source in docs:
-            fd, fname = tempfile.mkstemp(prefix="seen_doc", text=True)
-            fout = open(fname, 'a')
-            fout.write(self.state.document.current_source+'\n')
-            fout.close()
-
         return rgxin, rgxout, promptin, promptout