Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1176)

Side by Side Diff: Lib/genericpath.py

Issue 4755: Common path prefix
Patch Set: Created 10 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 """ 1 """
2 Path operations common to more than one OS 2 Path operations common to more than one OS
3 Do not use directly. The OS specific modules import the appropriate 3 Do not use directly. The OS specific modules import the appropriate
4 functions from this module themselves. 4 functions from this module themselves.
5 """ 5 """
6 import os 6 import os
7 import stat 7 import stat
8 8
9 __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime', 9 __all__ = ['commonprefix', 'exists', 'getatime', 'getctime', 'getmtime',
10 'getsize', 'isdir', 'isfile'] 10 'getsize', 'isdir', 'isfile']
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 return os.stat(filename).st_atime 59 return os.stat(filename).st_atime
60 60
61 61
62 def getctime(filename): 62 def getctime(filename):
63 """Return the metadata change time of a file, reported by os.stat().""" 63 """Return the metadata change time of a file, reported by os.stat()."""
64 return os.stat(filename).st_ctime 64 return os.stat(filename).st_ctime
65 65
66 66
67 # Return the longest prefix of all list elements. 67 # Return the longest prefix of all list elements.
68 def commonprefix(m): 68 def commonprefix(m):
69 "Given a list of pathnames, returns the longest common leading component" 69 "Given a list of pathnames, returns the longest common leading string"
70 if not m: return '' 70 if not m: return ''
71 s1 = min(m) 71 s1 = min(m)
72 s2 = max(m) 72 s2 = max(m)
73 for i, c in enumerate(s1): 73 for i, c in enumerate(s1):
74 if c != s2[i]: 74 if c != s2[i]:
75 return s1[:i] 75 return s1[:i]
76 return s1 76 return s1
77
78 # Return the longest prefix of all list elements.
79 def commonpathprefix(m, separator=None):
80 """Given a list of pathnames, returns the longest common leading component.
81
82 Unlike commonprefix(), commonpathprefix() considers path elements as
83 delimited by the defined separator (default: os.sep).
84 """
85 if not m:
86 return ''
87 if separator is None:
88 import os
89 separator = os.sep
90 common = []
91 s1 = min(m).split(separator)
92 s2 = max(m).split(separator)
93 while s1 and s2 and s1[0] == s2[0]:
94 common.append(s1[0])
95 del s1[0], s2[0]
96 return separator.join(common)
77 97
78 # Split a path in root and extension. 98 # Split a path in root and extension.
79 # The extension is everything starting at the last dot in the last 99 # The extension is everything starting at the last dot in the last
80 # pathname component; the root is everything before that. 100 # pathname component; the root is everything before that.
81 # It is always true that root + ext == p. 101 # It is always true that root + ext == p.
82 102
83 # Generic implementation of splitext, to be parametrized with 103 # Generic implementation of splitext, to be parametrized with
84 # the separators 104 # the separators
85 def _splitext(p, sep, altsep, extsep): 105 def _splitext(p, sep, altsep, extsep):
86 """Split the extension from a pathname. 106 """Split the extension from a pathname.
(...skipping 10 matching lines...) Expand all
97 dotIndex = p.rfind(extsep) 117 dotIndex = p.rfind(extsep)
98 if dotIndex > sepIndex: 118 if dotIndex > sepIndex:
99 # skip all leading dots 119 # skip all leading dots
100 filenameIndex = sepIndex + 1 120 filenameIndex = sepIndex + 1
101 while filenameIndex < dotIndex: 121 while filenameIndex < dotIndex:
102 if p[filenameIndex:filenameIndex+1] != extsep: 122 if p[filenameIndex:filenameIndex+1] != extsep:
103 return p[:dotIndex], p[dotIndex:] 123 return p[:dotIndex], p[dotIndex:]
104 filenameIndex += 1 124 filenameIndex += 1
105 125
106 return p, p[:0] 126 return p, p[:0]
OLDNEW
« Doc/library/os.path.rst ('K') | « Doc/library/os.path.rst ('k') | Lib/macpath.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+