[Happydoc-checkins] CVS: HappyDoc/happydoclib path.py,1.8,1.9
Brought to you by:
doughellmann,
krlosaqp
|
From: Doug H. <dou...@us...> - 2002-08-24 19:48:28
|
Update of /cvsroot/happydoc/HappyDoc/happydoclib
In directory usw-pr-cvs1:/tmp/cvs-serv18310/happydoclib
Modified Files:
path.py
Log Message:
Add trace code and tests.
Index: path.py
===================================================================
RCS file: /cvsroot/happydoc/HappyDoc/happydoclib/path.py,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** path.py 17 Feb 2002 13:49:49 -0000 1.8
--- path.py 24 Aug 2002 19:48:25 -0000 1.9
***************
*** 67,71 ****
# Import Local modules
#
! from happydoclib.StreamFlushTest import StreamFlushTest
#
--- 67,74 ----
# Import Local modules
#
! from StreamFlushTest import StreamFlushTest
! from trace import trace as TRACE
!
! #TRACE.setVerbosity(3)
#
***************
*** 97,102 ****
def applyPrefixToPath(path, prefix):
"Add the prefix value to every part of a given path."
parts = string.split( path, os.sep )
! #print 'PARTS:', parts
prefix_len = len(prefix)
real_parts = []
--- 100,107 ----
def applyPrefixToPath(path, prefix):
"Add the prefix value to every part of a given path."
+ TRACE.into('PATH', 'applyPrefixToPath', path=path, prefix=prefix)
+ drive, path = os.path.splitdrive(path)
parts = string.split( path, os.sep )
! TRACE.writeVar(parts=parts)
prefix_len = len(prefix)
real_parts = []
***************
*** 105,129 ****
pass
elif p not in ( '.', '..' ) and (p[:prefix_len] != prefix):
! #print 'modifying "%s"' % p
p = '%s%s' % (prefix, p)
real_parts.append(p)
! #print 'REAL PARTS:', real_parts
name = apply(os.path.join, real_parts)
! if path and path[0] == os.sep:
name = os.sep + name
return name
def removePrefix(path, prefix):
"Remove prefix from the beginning of path, if present."
one_up = os.path.dirname(path)
common_prefix = commonPrefix(one_up, prefix)
! #print 'PATH: removePrefix( %s, %s )' % (path, prefix)
! #print 'PATH: common_prefix', common_prefix
if common_prefix == prefix:
path = path[len(common_prefix):]
while path and (path[0] == os.sep):
path = path[1:]
! #print 'PATH: result', path
return path
--- 110,152 ----
pass
elif p not in ( '.', '..' ) and (p[:prefix_len] != prefix):
! TRACE.write('modifying "%s"' % p)
p = '%s%s' % (prefix, p)
real_parts.append(p)
! TRACE.writeVar(real_parts=real_parts)
name = apply(os.path.join, real_parts)
! if drive:
! name = os.sep.join((drive, name))
! elif path and path[0] == os.sep:
name = os.sep + name
+ TRACE.outof(name)
return name
def removePrefix(path, prefix):
"Remove prefix from the beginning of path, if present."
+ TRACE.into('PATH', 'removePrefix', path=path, prefix=prefix)
+
+ path=os.path.normcase(path)
+ prefix=os.path.normcase(prefix)
+ TRACE.writeVar(path_after_norm_case=path)
+ TRACE.writeVar(prefix_after_norm_case=prefix)
+
one_up = os.path.dirname(path)
+ TRACE.writeVar(one_up=one_up)
+
common_prefix = commonPrefix(one_up, prefix)
! TRACE.writeVar(common_prefix=common_prefix)
!
if common_prefix == prefix:
path = path[len(common_prefix):]
+ else:
+ TRACE.write('common prefix (%s)' % common_prefix)
+ TRACE.write('does not match specified prefix (%s)' % prefix)
+
+ TRACE.writeVar(pathMinusPrefix=path)
while path and (path[0] == os.sep):
path = path[1:]
!
! TRACE.outof(path)
return path
***************
*** 144,162 ****
subdirectory name matches.
"""
! #print 'commonPrefix(%s, %s)' % (path1, path2)
if not path1 or not path2:
return ''
path1_parts = string.split(path1, os.sep)
path2_parts = string.split(path2, os.sep)
common = []
for p1, p2 in map(None, path1_parts, path2_parts):
! if p1 == p2:
common.append(p1)
else:
break
common = string.join(common, os.sep)
! if path1 and path1[0] == '/' and common and common[0] != '/':
! common = '/' + common
! #print '->"%s"' % common
return common
--- 167,204 ----
subdirectory name matches.
"""
! TRACE.into('PATH', 'commonPrefix', path1=path1, path2=path2)
if not path1 or not path2:
+ TRACE.outof('')
+ return ''
+
+ drive1, path1 = os.path.splitdrive(path1)
+ TRACE.writeVar(drive1=drive1)
+ drive2, path2 = os.path.splitdrive(path2)
+ TRACE.writeVar(drive2=drive2)
+ if drive1 != drive2:
+ TRACE.outof('')
return ''
+
path1_parts = string.split(path1, os.sep)
+ TRACE.writeVar(path1_parts=path1_parts)
path2_parts = string.split(path2, os.sep)
+ TRACE.writeVar(path2_parts=path2_parts)
+
common = []
for p1, p2 in map(None, path1_parts, path2_parts):
! if p1 and p1 == p2:
! TRACE.write('Found common part "%s"' % p1)
common.append(p1)
+ elif not p1:
+ pass
else:
break
common = string.join(common, os.sep)
! if drive1 and common:
! TRACE.write('have a drive spec')
! common = os.path.normcase('%s%s%s' % (drive1, os.sep, common))
! elif path1 and path1[0] == os.sep and common and common[0] != os.sep:
! common = os.sep + common
! TRACE.outof(common)
return common
***************
*** 186,194 ****
# Find the part of path1 which is *not* part of path1prefix
#
! #print 'joinWithCommonMiddle(%s, %s, %s)' % (path1prefix, path1, path2)
common_prefix = commonPrefix(path1prefix, path1)
! #print ' common prefix:', common_prefix
real_base = removePrefix(path1, common_prefix)
! #print ' real base:', real_base
#
# Remove the prefix common to the docset_real_base and
--- 228,237 ----
# Find the part of path1 which is *not* part of path1prefix
#
! TRACE.into('PATH', 'joinWithCommonMiddle', path1prefix=path1prefix,
! path1=path1, path2=path2)
common_prefix = commonPrefix(path1prefix, path1)
! TRACE.writeVar(common_prefix=common_prefix)
real_base = removePrefix(path1, common_prefix)
! TRACE.writeVar(real_base=real_base)
#
# Remove the prefix common to the docset_real_base and
***************
*** 196,205 ****
#
common_prefix = commonPrefix(real_base, path2)
! #print ' common prefix with real base and path2:', common_prefix
path2 = removePrefix(path2, common_prefix)
! #print ' fixed path2:', path2
name = join( path1, path2 )
! #print '->"%s"' % name
return name
--- 239,248 ----
#
common_prefix = commonPrefix(real_base, path2)
! TRACE.write('common prefix with real base and path2:', common_prefix)
path2 = removePrefix(path2, common_prefix)
! TRACE.write('fixed path2:', path2)
name = join( path1, path2 )
! TRACE.outof(name)
return name
***************
*** 222,230 ****
fromName will point directly to toName.
"""
! dbg=0
! if dbg: print '\nPATH: FROM: ', fromName
! if dbg: print 'PATH: TO : ', toName
! if dbg: print 'PATH: BASE: ', baseDirectory
!
#
# Normalize directory names
--- 265,273 ----
fromName will point directly to toName.
"""
! TRACE.into('PATH', 'computeRelativeHTMLLink',
! fromName=fromName,
! toName=toName,
! baseDirectory=baseDirectory,
! )
#
# Normalize directory names
***************
*** 232,237 ****
fromName = os.path.normpath(fromName)
toName = os.path.normpath(toName)
! if dbg: print 'PATH: FROM NORMALIZED : ', fromName
! if dbg: print 'PATH: TO NORMALIZED : ', toName
#
--- 275,280 ----
fromName = os.path.normpath(fromName)
toName = os.path.normpath(toName)
! TRACE.writeVar(fromNameNormalized=fromName)
! TRACE.writeVar(toNameNormalized=toName)
#
***************
*** 240,248 ****
fromName = removePrefix(fromName, baseDirectory)
toName = removePrefix(toName, baseDirectory)
! if dbg: print 'PATH: FROM - PREFIX : ', fromName
! if dbg: print 'PATH: TO - PREFIX : ', toName
if fromName == toName:
! if dbg: print '\tPATH: same name'
relative_link = os.path.basename(toName)
else:
--- 283,291 ----
fromName = removePrefix(fromName, baseDirectory)
toName = removePrefix(toName, baseDirectory)
! TRACE.writeVar(fromNameMinusPrefix=fromName)
! TRACE.writeVar(toNameMinusPrefix=toName)
if fromName == toName:
! TRACE.writeVar(toName=toName)
relative_link = os.path.basename(toName)
else:
***************
*** 251,260 ****
while from_name_no_prefix and (from_name_no_prefix[0] == os.sep):
from_name_no_prefix = from_name_no_prefix[1:]
! if dbg: print '\tPATH: from, no prefix:', from_name_no_prefix
! if dbg and from_name_no_prefix == 'z.html':
! raise 'debug'
subdir_path = os.path.dirname(from_name_no_prefix)
parts = string.split(subdir_path, os.sep)
! if dbg: print '\tPATH: parts:', parts
if parts and parts[0]:
levels = len(string.split(subdir_path, os.sep))
--- 294,301 ----
while from_name_no_prefix and (from_name_no_prefix[0] == os.sep):
from_name_no_prefix = from_name_no_prefix[1:]
! TRACE.writeVar(from_name_no_prefix=from_name_no_prefix)
subdir_path = os.path.dirname(from_name_no_prefix)
parts = string.split(subdir_path, os.sep)
! TRACE.writeVar(parts=parts)
if parts and parts[0]:
levels = len(string.split(subdir_path, os.sep))
***************
*** 266,270 ****
to_name_no_prefix = to_name_no_prefix[1:]
relative_link = "%s%s" % (up_levels, to_name_no_prefix)
! if dbg: print 'PATH: LINK: ', relative_link, '\n'
return relative_link
--- 307,311 ----
to_name_no_prefix = to_name_no_prefix[1:]
relative_link = "%s%s" % (up_levels, to_name_no_prefix)
! TRACE.outof(relative_link)
return relative_link
***************
*** 305,309 ****
def join( *args ):
"os.path.join"
! return apply(os.path.join, args)
def cwd():
--- 346,353 ----
def join( *args ):
"os.path.join"
! TRACE.into('PATH', 'join', args=args)
! result=apply(os.path.join, args)
! TRACE.outof(result)
! return result
def cwd():
***************
*** 349,352 ****
--- 393,405 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testApplyPreifxToPathWin32(self):
+ expected = 'c:\\BLAH_tmp\\BLAH_foo'
+ actual = applyPrefixToPath('c:\\BLAH_tmp\\BLAH_foo', 'BLAH_')
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testApplyPrefixToPathEmpty(self):
***************
*** 373,376 ****
--- 426,438 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testRemovePrefixWin32(self):
+ expected = 'foo'
+ actual = removePrefix('c:\\tmp\\foo', 'c:\\tmp')
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testRemovePrefixNotThere(self):
***************
*** 381,384 ****
--- 443,455 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testRemovePrefixNotThereWin32(self):
+ expected = 'c:\\tmp\\foo'
+ actual = removePrefix('c:\\tmp\\foo', 'c:\\blah')
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testRemovePrefixEmptyPath(self):
***************
*** 407,410 ****
--- 478,491 ----
return
+ if os.name == 'nt':
+ def testCommonPrefixWin32(self):
+ expected = 'c:\\tmp'
+ actual = commonPrefix('c:\\tmp\\foo',
+ 'c:\\tmp\\blah')
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
+
def testCommonPrefixNone(self):
expected = ''
***************
*** 417,420 ****
--- 498,513 ----
return
+ if os.name == 'nt':
+ def testCommonPrefixNoneWin32(self):
+ expected = ''
+ actual = commonPrefix('c:\\var\\tmp\\foo',
+ 'c:\\tmp\\blah',
+ )
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
+
+
def testCommonPrefixEmptyPaths(self):
expected = ''
***************
*** 440,443 ****
--- 533,548 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testJoinWithCommonMiddleWin32(self):
+ expected = 'c:\\root\\one\\two\\three\\filename.txt'
+ actual = joinWithCommonMiddle('c:\\root\\one',
+ 'c:\\root\\one\\two',
+ 'two\\three\\filename.txt'
+ )
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testJoinWithCommonMiddleNotCommon(self):
***************
*** 451,454 ****
--- 556,571 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testJoinWithCommonMiddleNotCommonWin32(self):
+ expected = 'c:\\%s' % os.path.join('root', 'one', 'four', 'two', 'three', 'filename.txt')
+ actual = joinWithCommonMiddle('c:\\%s' % os.path.join('root', 'one', 'five'),
+ 'c:\\%s' % os.path.join('root', 'one', 'four'),
+ os.path.join('two', 'three', 'filename.txt')
+ )
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testJoinWithCommonMiddleEmptyPrefix(self):
***************
*** 462,465 ****
--- 579,594 ----
% (expected, actual)
return
+
+ if os.name == 'nt':
+ def testJoinWithCommonMiddleEmptyPrefixWin32(self):
+ expected = 'c:\\%s' % os.path.join('root', 'one', 'four', 'two', 'three', 'filename.txt')
+ actual = joinWithCommonMiddle('',
+ 'c:\\%s' % os.path.join('root', 'one', 'four'),
+ os.path.join('two', 'three', 'filename.txt')
+ )
+ assert actual == expected, \
+ 'Path modification failed.\n\tExpected "%s",\n\tgot "%s"' \
+ % (expected, actual)
+ return
def testJoinWithCommonMiddleEmptyPath1(self):
***************
*** 543,546 ****
if __name__ == '__main__':
! unittest.main()
!
--- 672,674 ----
if __name__ == '__main__':
! unittest.main()
|