Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
dospath Namespace Reference

Functions

def normcase
 
def isabs
 
def join
 
def splitdrive
 
def split
 
def splitext
 
def basename
 
def dirname
 
def commonprefix
 
def getsize
 
def getmtime
 
def getatime
 
def islink
 
def exists
 
def isdir
 
def isfile
 
def ismount
 
def walk
 
def expanduser
 
def expandvars
 
def normpath
 
def abspath
 

Variables

list __all__
 
 realpath = abspath
 

Detailed Description

Common operations on DOS pathnames.

Function Documentation

def dospath.abspath (   path)
Return an absolute path.

Definition at line 334 of file dospath.py.

References isabs(), join(), and normpath().

335 def abspath(path):
336  """Return an absolute path."""
337  if not isabs(path):
338  path = join(os.getcwd(), path)
339  return normpath(path)
340 
# realpath is a no-op on systems without islink support
def dospath.basename (   p)
Return the tail (basename) part of a path.

Definition at line 96 of file dospath.py.

References split().

96 
97 def basename(p):
98  """Return the tail (basename) part of a path."""
99 
100  return split(p)[1]
101 
def dospath.commonprefix (   m)
Return the longest prefix of all list elements.

Definition at line 108 of file dospath.py.

109 def commonprefix(m):
110  """Return the longest prefix of all list elements."""
111 
112  if not m: return ''
113  prefix = m[0]
114  for item in m:
115  for i in range(len(prefix)):
116  if prefix[:i+1] != item[:i+1]:
117  prefix = prefix[:i]
118  if i == 0: return ''
119  break
120  return prefix
121 
122 
123 # Get size, mtime, atime of files.
def dospath.dirname (   p)
Return the head (dirname) part of a path.

Definition at line 102 of file dospath.py.

References split().

103 def dirname(p):
104  """Return the head (dirname) part of a path."""
105 
106  return split(p)[0]
107 
def dospath.exists (   path)
Does a path exist?
This is false for dangling symbolic links.

Definition at line 147 of file dospath.py.

148 def exists(path):
149  """Does a path exist?
150  This is false for dangling symbolic links."""
151 
152  try:
153  st = os.stat(path)
154  except os.error:
155  return 0
156  return 1
157 
def dospath.expanduser (   path)
Expand paths beginning with '~' or '~user'.
'~' means $HOME; '~user' means that user's home directory.
If the path doesn't begin with '~', or if the user or $HOME is unknown,
the path is returned unchanged (leaving error reporting to whatever
function is called with the expanded path as argument).
See also module 'glob' for expansion of *, ? and [...] in pathnames.
(A function should also be defined to do full *sh-style environment
variable expansion.)

Definition at line 213 of file dospath.py.

214 def expanduser(path):
215  """Expand paths beginning with '~' or '~user'.
216  '~' means $HOME; '~user' means that user's home directory.
217  If the path doesn't begin with '~', or if the user or $HOME is unknown,
218  the path is returned unchanged (leaving error reporting to whatever
219  function is called with the expanded path as argument).
220  See also module 'glob' for expansion of *, ? and [...] in pathnames.
221  (A function should also be defined to do full *sh-style environment
222  variable expansion.)"""
223 
224  if path[:1] != '~':
225  return path
226  i, n = 1, len(path)
227  while i < n and path[i] not in '/\\':
228  i = i+1
229  if i == 1:
230  if not os.environ.has_key('HOME'):
231  return path
232  userhome = os.environ['HOME']
233  else:
234  return path
235  return userhome + path[i:]
236 
def dospath.expandvars (   path)
Expand paths containing shell variable substitutions.
The following rules apply:
    - no expansion within single quotes
    - no escape character, except for '$$' which is translated into '$'
    - ${varname} is accepted.
    - varnames can be made out of letters, digits and the character '_'

Definition at line 237 of file dospath.py.

238 def expandvars(path):
239  """Expand paths containing shell variable substitutions.
240  The following rules apply:
241  - no expansion within single quotes
242  - no escape character, except for '$$' which is translated into '$'
243  - ${varname} is accepted.
244  - varnames can be made out of letters, digits and the character '_'"""
245  # XXX With COMMAND.COM you can use any characters in a variable name,
246  # XXX except '^|<>='.
247 
248  if '$' not in path:
249  return path
250  import string
251  varchars = string.ascii_letters + string.digits + "_-"
252  res = ''
253  index = 0
254  pathlen = len(path)
255  while index < pathlen:
256  c = path[index]
257  if c == '\'': # no expansion within single quotes
258  path = path[index + 1:]
259  pathlen = len(path)
260  try:
261  index = path.index('\'')
262  res = res + '\'' + path[:index + 1]
263  except ValueError:
264  res = res + path
265  index = pathlen -1
266  elif c == '$': # variable or '$$'
267  if path[index + 1:index + 2] == '$':
268  res = res + c
269  index = index + 1
270  elif path[index + 1:index + 2] == '{':
271  path = path[index+2:]
272  pathlen = len(path)
273  try:
274  index = path.index('}')
275  var = path[:index]
276  if os.environ.has_key(var):
277  res = res + os.environ[var]
278  except ValueError:
279  res = res + path
280  index = pathlen - 1
281  else:
282  var = ''
283  index = index + 1
284  c = path[index:index + 1]
285  while c != '' and c in varchars:
286  var = var + c
287  index = index + 1
288  c = path[index:index + 1]
289  if os.environ.has_key(var):
290  res = res + os.environ[var]
291  if c != '':
292  res = res + c
293  else:
294  res = res + c
295  index = index + 1
296  return res
297 
def dospath.getatime (   filename)
Return the last access time of a file, reported by os.stat().

Definition at line 134 of file dospath.py.

135 def getatime(filename):
136  """Return the last access time of a file, reported by os.stat()."""
137  st = os.stat(filename)
138  return st[stat.ST_ATIME]
139 
def dospath.getmtime (   filename)
Return the last modification time of a file, reported by os.stat().

Definition at line 129 of file dospath.py.

130 def getmtime(filename):
131  """Return the last modification time of a file, reported by os.stat()."""
132  st = os.stat(filename)
133  return st[stat.ST_MTIME]
def dospath.getsize (   filename)
Return the size of a file, reported by os.stat().

Definition at line 124 of file dospath.py.

125 def getsize(filename):
126  """Return the size of a file, reported by os.stat()."""
127  st = os.stat(filename)
128  return st[stat.ST_SIZE]
def dospath.isabs (   s)
Return whether a path is absolute.
Trivial in Posix, harder on the Mac or MS-DOS.
For DOS it is absolute if it starts with a slash or backslash (current
volume), or if a pathname after the volume letter and colon starts with
a slash or backslash.

Definition at line 24 of file dospath.py.

References splitdrive().

24 
25 def isabs(s):
26  """Return whether a path is absolute.
27  Trivial in Posix, harder on the Mac or MS-DOS.
28  For DOS it is absolute if it starts with a slash or backslash (current
29  volume), or if a pathname after the volume letter and colon starts with
30  a slash or backslash."""
31 
32  s = splitdrive(s)[1]
33  return s != '' and s[:1] in '/\\'
34 
def dospath.isdir (   path)
Is a path a dos directory?

Definition at line 158 of file dospath.py.

References stat.S_ISDIR().

159 def isdir(path):
160  """Is a path a dos directory?"""
161 
162  try:
163  st = os.stat(path)
164  except os.error:
165  return 0
166  return stat.S_ISDIR(st[stat.ST_MODE])
167 
def dospath.isfile (   path)
Is a path a regular file?

Definition at line 168 of file dospath.py.

References stat.S_ISREG().

169 def isfile(path):
170  """Is a path a regular file?"""
171 
172  try:
173  st = os.stat(path)
174  except os.error:
175  return 0
176  return stat.S_ISREG(st[stat.ST_MODE])
177 
def dospath.islink (   path)
Is a path a symbolic link?
This will always return false on systems where posix.lstat doesn't exist.

Definition at line 140 of file dospath.py.

141 def islink(path):
142  """Is a path a symbolic link?
143  This will always return false on systems where posix.lstat doesn't exist."""
144 
145  return 0
146 
def dospath.ismount (   path)
Is a path a mount point?

Definition at line 178 of file dospath.py.

References isabs(), and splitdrive().

179 def ismount(path):
180  """Is a path a mount point?"""
181  # XXX This degenerates in: 'is this the root?' on DOS
182 
183  return isabs(splitdrive(path)[1])
184 
def dospath.join (   a,
  p 
)
Join two (or more) paths.

Definition at line 35 of file dospath.py.

References isabs().

35 
36 def join(a, *p):
37  """Join two (or more) paths."""
38 
39  path = a
40  for b in p:
41  if isabs(b):
42  path = b
43  elif path == '' or path[-1:] in '/\\:':
44  path = path + b
45  else:
46  path = path + "\\" + b
47  return path
48 
def dospath.normcase (   s)
Normalize the case of a pathname.
On MS-DOS it maps the pathname to lowercase, turns slashes into
backslashes.
Other normalizations (such as optimizing '../' away) are not allowed
(this is done by normpath).
Previously, this version mapped invalid consecutive characters to a
single '_', but this has been removed.  This functionality should
possibly be added as a new function.

Definition at line 11 of file dospath.py.

References string.lower().

11 
12 def normcase(s):
13  """Normalize the case of a pathname.
14  On MS-DOS it maps the pathname to lowercase, turns slashes into
15  backslashes.
16  Other normalizations (such as optimizing '../' away) are not allowed
17  (this is done by normpath).
18  Previously, this version mapped invalid consecutive characters to a
19  single '_', but this has been removed. This functionality should
20  possibly be added as a new function."""
21 
22  return s.replace("/", "\\").lower()
23 
def dospath.normpath (   path)
Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
Also, components of the path are silently truncated to 8+3 notation.

Definition at line 298 of file dospath.py.

References join(), split(), and splitdrive().

299 def normpath(path):
300  """Normalize a path, e.g. A//B, A/./B and A/foo/../B all become A/B.
301  Also, components of the path are silently truncated to 8+3 notation."""
302 
303  path = path.replace("/", "\\")
304  prefix, path = splitdrive(path)
305  while path[:1] == "\\":
306  prefix = prefix + "\\"
307  path = path[1:]
308  comps = path.split("\\")
309  i = 0
310  while i < len(comps):
311  if comps[i] == '.':
312  del comps[i]
313  elif comps[i] == '..' and i > 0 and \
314  comps[i-1] not in ('', '..'):
315  del comps[i-1:i+1]
316  i = i - 1
317  elif comps[i] == '' and i > 0 and comps[i-1] != '':
318  del comps[i]
319  elif '.' in comps[i]:
320  comp = comps[i].split('.')
321  comps[i] = comp[0][:8] + '.' + comp[1][:3]
322  i = i + 1
323  elif len(comps[i]) > 8:
324  comps[i] = comps[i][:8]
325  i = i + 1
326  else:
327  i = i + 1
328  # If the path is now empty, substitute '.'
329  if not prefix and not comps:
330  comps.append('.')
331  return prefix + "\\".join(comps)
332 
333 
def dospath.split (   p)
Split a path into head (everything up to the last '/') and tail
(the rest).  After the trailing '/' is stripped, the invariant
join(head, tail) == p holds.
The resulting head won't end in '/' unless it is the root.

Definition at line 59 of file dospath.py.

References splitdrive().

59 
60 def split(p):
61  """Split a path into head (everything up to the last '/') and tail
62  (the rest). After the trailing '/' is stripped, the invariant
63  join(head, tail) == p holds.
64  The resulting head won't end in '/' unless it is the root."""
65 
66  d, p = splitdrive(p)
67  # set i to index beyond p's last slash
68  i = len(p)
69  while i and p[i-1] not in '/\\':
70  i = i - 1
71  head, tail = p[:i], p[i:] # now tail has no slashes
72  # remove trailing slashes from head, unless it's all slashes
73  head2 = head
74  while head2 and head2[-1] in '/\\':
75  head2 = head2[:-1]
76  head = head2 or head
77  return d + head, tail
78 
def dospath.splitdrive (   p)
Split a path into a drive specification (a drive letter followed
by a colon) and path specification.
It is always true that drivespec + pathspec == p.

Definition at line 49 of file dospath.py.

49 
50 def splitdrive(p):
51  """Split a path into a drive specification (a drive letter followed
52  by a colon) and path specification.
53  It is always true that drivespec + pathspec == p."""
54 
55  if p[1:2] == ':':
56  return p[0:2], p[2:]
57  return '', p
58 
def dospath.splitext (   p)
Split a path into root and extension.
The extension is everything starting at the first dot in the last
pathname component; the root is everything before that.
It is always true that root + ext == p.

Definition at line 79 of file dospath.py.

79 
80 def splitext(p):
81  """Split a path into root and extension.
82  The extension is everything starting at the first dot in the last
83  pathname component; the root is everything before that.
84  It is always true that root + ext == p."""
85 
86  root, ext = '', ''
87  for c in p:
88  if c in '/\\':
89  root, ext = root + ext + c, ''
90  elif c == '.' or ext:
91  ext = ext + c
92  else:
93  root = root + c
94  return root, ext
95 
def dospath.walk (   top,
  func,
  arg 
)
Directory tree walk with callback function.

For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
dirname is the name of the directory, and fnames a list of the names of
the files and subdirectories in dirname (excluding '.' and '..').  func
may modify the fnames list in-place (e.g. via del or slice assignment),
and walk will only recurse into the subdirectories whose names remain in
fnames; this can be used to implement a filter, or to impose a specific
order of visiting.  No semantics are defined for, or required of, arg,
beyond that arg is always passed to func.  It can be used, e.g., to pass
a filename pattern, or a mutable object designed to accumulate
statistics.  Passing None for arg is common.

Definition at line 185 of file dospath.py.

References isdir(), and join().

186 def walk(top, func, arg):
187  """Directory tree walk with callback function.
188 
189  For each directory in the directory tree rooted at top (including top
190  itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
191  dirname is the name of the directory, and fnames a list of the names of
192  the files and subdirectories in dirname (excluding '.' and '..'). func
193  may modify the fnames list in-place (e.g. via del or slice assignment),
194  and walk will only recurse into the subdirectories whose names remain in
195  fnames; this can be used to implement a filter, or to impose a specific
196  order of visiting. No semantics are defined for, or required of, arg,
197  beyond that arg is always passed to func. It can be used, e.g., to pass
198  a filename pattern, or a mutable object designed to accumulate
199  statistics. Passing None for arg is common."""
200 
201  try:
202  names = os.listdir(top)
203  except os.error:
204  return
205  func(arg, top, names)
206  exceptions = ('.', '..')
207  for name in names:
208  if name not in exceptions:
209  name = join(top, name)
210  if isdir(name):
211  walk(name, func, arg)
212 

Variable Documentation

list __all__
Initial value:
1 = ["normcase","isabs","join","splitdrive","split","splitext",
2  "basename","dirname","commonprefix","getsize","getmtime",
3  "getatime","islink","exists","isdir","isfile","ismount",
4  "walk","expanduser","expandvars","normpath","abspath"]

Definition at line 6 of file dospath.py.

realpath = abspath

Definition at line 341 of file dospath.py.