Changeset 332 for pyndexter/trunk/pyndexter/file.py
- Timestamp:
- 30/04/06 14:09:58 (4 years ago)
- Files:
-
- 1 modified
-
pyndexter/trunk/pyndexter/file.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
pyndexter/trunk/pyndexter/file.py
r331 r332 1 1 import sys 2 2 import codecs 3 import os.path 4 from fnmatch import fnmatch 5 from dircache import listdir 3 import os 4 from stat import * 6 5 from urlparse import urlsplit, urlunsplit 7 6 … … 11 10 def __init__(self, root, include=['*'], exclude=[], predicate=None): 12 11 """ Expose a subset of the file system for searching. """ 12 Source.__init__(self, include, exclude, predicate) 13 13 self.root = os.path.normpath(root) 14 self.include = include15 self.exclude = exclude16 self.predicate = predicate or self._glob_predicate17 14 self.encoding = sys.getfilesystemencoding() 18 15 … … 21 18 path = path.strip(os.path.sep) 22 19 root_path = os.path.join(self.root, path) 23 for file in listdir(root_path):20 for file in os.listdir(root_path): 24 21 full_path = os.path.join(root_path, file) 25 if os.path.isdir(full_path): 22 try: 23 stat = os.lstat(full_path) 24 except OSError: 25 continue 26 if not self.predicate(full_path) or not os.access(full_path, os.R_OK): 27 continue 28 if S_ISDIR(stat.st_mode): 26 29 for file in walk_path(os.path.join(path, file)): 27 30 yield file 28 elif self.predicate(full_path) and os.path.exists(full_path): 29 # TODO Stat for normal files + readability 30 yield self._file2uri(full_path) 31 elif S_ISREG(stat.st_mode): 32 yield (self._file2uri(full_path).decode(self.encoding), stat) 31 33 32 for file in walk_path('/'): 33 yield file.decode(self.encoding) 34 for file, stat in walk_path('/'): 35 self._state[file] = stat.st_mtime 36 yield file 34 37 35 38 def matches(self, uri): 36 scheme, netloc, path, query, fragment = urlsplit(uri )39 scheme, netloc, path, query, fragment = urlsplit(uri, 'file') 37 40 path = os.path.normpath(path) 38 return scheme in ('file', '')and \41 return scheme == 'file' and \ 39 42 path.startswith(self.root) and \ 40 43 self.predicate(path) … … 67 70 68 71 def _uri2file(self, uri): 69 scheme, location, path, query, fragment = urlsplit(uri )70 if scheme not in ('file', ''):72 scheme, location, path, query, fragment = urlsplit(uri, 'file') 73 if scheme not in 'file': 71 74 raise InvalidURI("URI scheme in '%s' not supported by FileSource" 72 75 % scheme) … … 76 79 % uri) 77 80 return path.decode(self.encoding) 78 79 def _glob_predicate(self, file):80 for pattern in self.exclude:81 if fnmatch(file, pattern):82 return False83 for pattern in self.include:84 if fnmatch(file, pattern):85 return True86 return False87
