~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_readdir_pyx.pyx

Add bzrlib.pyutils, which has get_named_object, a wrapper around __import__.

This is used to replace various ad hoc implementations of the same logic,
notably the version used in registry's _LazyObjectGetter which had a bug when
getting a module without also getting a member.  And of course, this new
function has unit tests, unlike the replaced code.

This also adds a KnownHooksRegistry subclass to provide a more natural home for
some other logic.

I'm not thrilled about the name of the new module or the new functions, but it's
hard to think of good names for such generic functionality.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
    int closedir(DIR * dir)
107
107
    dirent *readdir(DIR *dir)
108
108
 
 
109
cdef object _directory
109
110
_directory = 'directory'
 
111
cdef object _chardev
110
112
_chardev = 'chardev'
 
113
cdef object _block
111
114
_block = 'block'
 
115
cdef object _file
112
116
_file = 'file'
 
117
cdef object _fifo
113
118
_fifo = 'fifo'
 
119
cdef object _symlink
114
120
_symlink = 'symlink'
 
121
cdef object _socket
115
122
_socket = 'socket'
 
123
cdef object _unknown
116
124
_unknown = 'unknown'
117
 
_missing = 'missing'
118
125
 
119
126
# add a typedef struct dirent dirent to workaround pyrex
120
127
cdef extern from 'readdir.h':
161
168
 
162
169
from bzrlib import osutils
163
170
 
 
171
cdef object _safe_utf8
 
172
_safe_utf8 = osutils.safe_utf8
164
173
 
165
174
cdef class UTF8DirReader:
166
175
    """A dir reader for utf8 file systems."""
167
176
 
168
 
    cdef readonly object _safe_utf8
169
 
    cdef _directory, _chardev, _block, _file, _fifo, _symlink
170
 
    cdef _socket, _unknown
171
 
 
172
 
    def __init__(self):
173
 
        self._safe_utf8 = osutils.safe_utf8
174
 
        self._directory = _directory
175
 
        self._chardev = _chardev
176
 
        self._block = _block
177
 
        self._file = _file
178
 
        self._fifo = _fifo
179
 
        self._symlink = _symlink
180
 
        self._socket = _socket
181
 
        self._unknown = _unknown
182
 
 
183
177
    def kind_from_mode(self, int mode):
184
178
        """Get the kind of a path from a mode status."""
185
179
        return self._kind_from_mode(mode)
187
181
    cdef _kind_from_mode(self, int mode):
188
182
        # Files and directories are the most common - check them first.
189
183
        if S_ISREG(mode):
190
 
            return self._file
 
184
            return _file
191
185
        if S_ISDIR(mode):
192
 
            return self._directory
 
186
            return _directory
193
187
        if S_ISCHR(mode):
194
 
            return self._chardev
 
188
            return _chardev
195
189
        if S_ISBLK(mode):
196
 
            return self._block
 
190
            return _block
197
191
        if S_ISLNK(mode):
198
 
            return self._symlink
 
192
            return _symlink
199
193
        if S_ISFIFO(mode):
200
 
            return self._fifo
 
194
            return _fifo
201
195
        if S_ISSOCK(mode):
202
 
            return self._socket
203
 
        return self._unknown
 
196
            return _socket
 
197
        return _unknown
204
198
 
205
199
    def top_prefix_to_starting_dir(self, top, prefix=""):
206
200
        """See DirReader.top_prefix_to_starting_dir."""
207
 
        return (self._safe_utf8(prefix), None, None, None,
208
 
            self._safe_utf8(top))
 
201
        return (_safe_utf8(prefix), None, None, None, _safe_utf8(top))
209
202
 
210
203
    def read_dir(self, prefix, top):
211
204
        """Read a single directory from a utf8 file system.
307
300
        if orig_dir_fd == -1:
308
301
            raise_os_error(errno, "open: ", ".")
309
302
        if -1 == chdir(path):
 
303
            # Ignore the return value, because we are already raising an
 
304
            # exception
 
305
            close(orig_dir_fd)
310
306
            raise_os_error(errno, "chdir: ", path)
311
307
    else:
312
308
        orig_dir_fd = -1