MCPcopy Create free account
hub / github.com/ActiveState/code / _populate

Method _populate

recipes/Python/259147_FSList/recipe-259147.py:326–403  ·  view source on GitHub ↗

The main workhorse function for adding files to the list. Returns the pattern, if it's just a filesystem item and it exists in the filesystem. Returns a list if pattern contains wildcards, or None, if the wildcards do not match anything.

(self, pattern, recursive = False, file_only = False)

Source from the content-addressed store, hash-verified

324 super(FSList, self).extend(data)
325
326 def _populate(self, pattern, recursive = False, file_only = False):
327 """
328 The main workhorse function for adding files to the list.
329 Returns the pattern, if it's just a filesystem item and
330 it exists in the filesystem. Returns a list if pattern contains
331 wildcards, or None, if the wildcards do not match anything.
332 """
333 temp = []
334 wildcard = False
335
336 # look for wildcard characters in the pattern
337 if ('*' in pattern) or ('?' in pattern)\
338 or ('[' in pattern):
339 wildcard = True
340 # items added to the list must exist on the filesystem.
341 if not wildcard and not os.path.exists(pattern):
342 raise ValueError, ("Item not found on file system: %s"% pattern)
343
344 # if it's a directory, assume they want every file there.
345 if os.path.isdir(pattern):
346 pattern = os.path.join(pattern, '*')
347 wildcard = True
348 # expand any wildcards
349 if wildcard:
350 temp.extend(glob.glob(pattern))
351
352 # walk through any directories, if requested,
353 # using the same pattern, if any.
354 if recursive:
355 path, filter = os.path.split(pattern)
356
357 # Go through the path, finding all the directories
358 if wildcard:
359 # try os.walk first, which is in python2.3
360 try:
361 for root, dirs, files in os.walk(path):
362 for dir in dirs:
363 temp.extend(glob.glob(\
364 os.path.join(root, dir, filter)))
365 except AttributeError:
366 # probably running on something less than python2.3
367 dirlist = self._walktree_compat(path)
368 for dir in dirlist:
369 temp.extend(glob.glob(os.path.join(dir, filter)))
370
371 # Finally, kill any links that got through from glob
372 # and remove anything that's not a file, if requested.
373 temp2 = []
374 name = ''
375 while True:
376 try:
377 # use pop() on the theory that it saves memory
378 # (one list is reduced while the other grows).
379 # That's probably not true, but I wouldn't know.
380 name = temp.pop()
381 try:
382 mode = os.stat(name)[ST_MODE]
383 except OSError, err:

Callers 3

__init__Method · 0.95
appendMethod · 0.95
extendMethod · 0.95

Calls 9

_walktree_compatMethod · 0.95
isdirMethod · 0.80
existsMethod · 0.45
joinMethod · 0.45
extendMethod · 0.45
splitMethod · 0.45
walkMethod · 0.45
popMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected