~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to lib/fileservice_lib/listing.py

  • Committer: wagrant
  • Date: 2008-07-16 02:04:37 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:887
fileservice_lib: Refactor directory listings to be less triplicated and
                 easier to read.

Show diffs side-by-side

added added

removed removed

Lines of Context:
193
193
    """
194
194
    # Are we in 'revision mode' - has someone sent the 'r' query
195
195
    # Work out the revisions from query
196
 
    revision = None
197
 
 
198
196
    r_str = req.get_fieldstorage().getfirst("r")
199
197
    revision = common.svn.revision_from_string(r_str)
200
198
 
 
199
    # Now we need to get a directory listing (ls). We also need a function (fn)
 
200
    # that will give us a (filename, attributes) pair for each type.
201
201
    # Start by trying to do an SVN status, so we can report file version
202
202
    # status
203
 
    listing = {}
204
203
    try:
205
204
        if revision:
206
 
            ls_list = svnclient.list(path, revision=revision, recurse=False)
207
 
            for ls in ls_list:
208
 
                filename, attrs = PysvnList_tofileinfo(path, ls)
209
 
                listing[filename.decode('utf-8')] = attrs
 
205
            ls = svnclient.list(path, revision=revision, recurse=False)
 
206
            fn = PysvnList_tofileinfo
210
207
        else:
211
 
            status_list = svnclient.status(path, recurse=False, get_all=True,
 
208
            ls = svnclient.status(path, recurse=False, get_all=True,
212
209
                        update=False)
213
 
            for status in status_list:
214
 
                filename, attrs = PysvnStatus_to_fileinfo(path, status)
215
 
                listing[filename.decode('utf-8')] = attrs
 
210
            fn = PysvnStatus_to_fileinfo
216
211
    except pysvn.ClientError:
217
212
        # Presumably the directory is not under version control.
218
213
        # Fallback to just an OS file listing.
 
214
        ls = []
 
215
        fn = file_to_fileinfo
219
216
        try:
220
 
            for filename in os.listdir(path):
221
 
                listing[filename.decode('utf-8')] = file_to_fileinfo(path, filename)
 
217
            ls = os.listdir(path)
222
218
        except OSError:
223
219
            # Non-directories will error - that's OK, we just want the "."
224
220
            pass
225
221
        # The subversion one includes "." while the OS one does not.
226
222
        # Add "." to the output, so the caller can see we are
227
223
        # unversioned.
228
 
        mtime = os.path.getmtime(path)
229
 
        listing["."] = file_to_fileinfo(path, "")
 
224
        ls.append('.')
 
225
 
 
226
    listing = {}
 
227
    for bit in ls:
 
228
        filename, attrs = fn(path, bit)
 
229
        listing[filename.decode('utf-8')] = attrs
230
230
 
231
231
    if ignore_dot_files:
232
232
        for fn in listing.keys():
272
272
 
273
273
def file_to_fileinfo(path, filename):
274
274
    """Given a filename (relative to a given path), gets all the info "ls"
275
 
    needs to display about the filename. Returns a dict containing a number
276
 
    of fields related to the file (excluding the filename itself)."""
277
 
    fullpath = path if len(filename) == 0 else os.path.join(path, filename)
278
 
    return _fullpath_stat_fileinfo(fullpath)
 
275
    needs to display about the filename. Returns pair mapping filename to
 
276
    a dict containing a number of other fields."""
 
277
    fullpath = path if filename in ('', '.') else os.path.join(path, filename)
 
278
    return filename, _fullpath_stat_fileinfo(fullpath)
279
279
 
280
280
def PysvnStatus_to_fileinfo(path, status):
281
281
    """Given a PysvnStatus object, gets all the info "ls"