[Happydoc-checkins] CVS: HappyDoc3/happydoclib/parseinfo __init__.py,1.4,1.5
Brought to you by:
doughellmann,
krlosaqp
|
From: Doug H. <dou...@us...> - 2003-03-15 14:23:24
|
Update of /cvsroot/happydoc/HappyDoc3/happydoclib/parseinfo
In directory sc8-pr-cvs1:/tmp/cvs-serv7784/happydoclib/parseinfo
Modified Files:
__init__.py
Log Message:
Add optional caching of parse results by pickling the information and
writing it to a file in the same directory as the input source.
Include command line options for setting the cache file name and
turning the cache on and off.
Index: __init__.py
===================================================================
RCS file: /cvsroot/happydoc/HappyDoc3/happydoclib/parseinfo/__init__.py,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** __init__.py 29 Dec 2002 18:36:21 -0000 1.4
--- __init__.py 15 Mar 2003 14:23:20 -0000 1.5
***************
*** 108,113 ****
--- 108,118 ----
# Import system modules
#
+ import md5
import os
import parser
+ try:
+ import cPickle as Pickle
+ except:
+ import Pickle
import types
import unittest
***************
*** 130,133 ****
--- 135,140 ----
'include_comments':1,
'docStringFormat':'StructuredText',
+ 'useCache':1,
+ 'cacheFilePrefix':'.happydoc.',
}
***************
*** 162,165 ****
--- 169,239 ----
def getDocs(parent, fileName):
+ global _parser_options
+ if _parser_options['useCache']:
+ return getDocsFromCache(parent, fileName)
+ else:
+ return getDocsFromFile(parent, fileName)
+
+ def getDocsFromCache(parent, fileName):
+ happydoclib.TRACE.into('parseinfo', 'getDocsFromCache',
+ parent=parent,
+ fileName=fileName,
+ outputLevel=TRACE_LEVEL,
+ )
+
+ #
+ # Check for cached parse results
+ #
+ # cache_file_name = '%s.happydoc' % name
+ dir_name = os.path.dirname(fileName)
+ base_name = os.path.basename(fileName)
+ cache_file_prefix = _parser_options['cacheFilePrefix']
+ cache_file_name = os.path.join(dir_name, '%s%s' % (cache_file_prefix,
+ base_name)
+ )
+ cached_md5 = None
+ cached_mod_info = None
+ if os.path.exists(cache_file_name):
+ try:
+ cache_file = open(cache_file_name, 'rb')
+ except IOError:
+ pass
+ else:
+ try:
+ cached_md5, cached_mod_info = Pickle.load(cache_file)
+ except EOFError:
+ pass
+ cache_file.close()
+
+ #
+ # Compute our md5 digest to determine
+ # whether or not we should
+ # use the cached results.
+ #
+ m = md5.new()
+ input_file = open(fileName, 'r')
+ m.update(input_file.read())
+ input_file.close()
+ current_md5 = m.hexdigest()
+
+ if current_md5 == cached_md5:
+ mod_info = cached_mod_info
+ else:
+ mod_info = getDocsFromFile(parent, fileName)
+ #
+ # Write back to the cache
+ #
+ cache_file = open(cache_file_name, 'wb')
+ Pickle.dump( (current_md5, mod_info),
+ cache_file )
+ cache_file.close()
+
+ happydoclib.TRACE.outof(mod_info,
+ outputLevel=TRACE_LEVEL,
+ )
+ return mod_info
+
+
+ def getDocsFromFile(parent, fileName):
"""Retrieve information from the parse tree of a source file.
***************
*** 170,174 ****
"""
! happydoclib.TRACE.into('parseinfo', 'getDocs',
parent=parent,
fileName=fileName,
--- 244,248 ----
"""
! happydoclib.TRACE.into('parseinfo', 'getDocsFromFile',
parent=parent,
fileName=fileName,
***************
*** 257,263 ****
--- 331,343 ----
self.filename = filename
setOption(include_comments=1)
+ setOption(useCache=0)
self.parsed_module = getDocs(None, filename)
return
+ def tearDown(self):
+ setOption(useCache=1)
+ unittest.TestCase.tearDown(self)
+ return
+
def _docStringsAreEqual(self, ds1, ds2):
if not ds1 and not ds2:
***************
*** 651,654 ****
--- 731,735 ----
def testIncludeCommentsOptionTrue(self):
happydoclib.parseinfo.setOption(include_comments=1)
+ happydoclib.parseinfo.setOption(useCache=0)
input_filename = os.path.join( 'TestCases',
***************
*** 667,670 ****
--- 748,752 ----
def testIncludeCommentsOptionFalse(self):
happydoclib.parseinfo.setOption(include_comments=0)
+ happydoclib.parseinfo.setOption(useCache=0)
input_filename = os.path.join( 'TestCases',
|