Changeset 1005:feefb419cb76


Ignore:
Timestamp:
Dec 25, 2010, 1:14:25 AM (11 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use a common method to get stat results from a directory listing.
The handling of empty lines within a directory listing has been
moved to `Parser.ignores_line`.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ftp_stat.py

    r1004 r1005  
    7575        return a false value.
    7676        """
     77        # Ignore empty lines stemming from only a line break.
     78        if not line.strip():
     79            # Yes, ignore the line if it's empty.
     80            return True
    7781        # Either a `_SRE_Match` instance or `None`
    7882        match = self._total_regex.search(line)
     
    406410        return self._host._dir(path)
    407411
     412    def _stat_results_from_dir(self, path):
     413        """
     414        Yield stat results extracted from the directory listing `path`.
     415        Omit the special entries for the directory itself and its parent
     416        directory.
     417        """
     418        lines = self._host_dir(path)
     419        for line in lines:
     420            if self._parser.ignores_line(line):
     421                continue
     422            # For `listdir`, we are interested in just the names,
     423            #  but we use the `time_shift` parameter to have the
     424            #  correct timestamp values in the cache.
     425            stat_result = self._parser.parse_line(line,
     426                                                  self._host.time_shift())
     427            if stat_result._st_name in [self._host.curdir, self._host.pardir]:
     428                continue
     429            loop_path = self._path.join(path, stat_result._st_name)
     430            self._lstat_cache[loop_path] = stat_result
     431            yield stat_result
     432
    408433    def _real_listdir(self, path):
    409434        """
     
    422447                  path)
    423448        # Set up for `for` loop.
    424         lines = self._host_dir(path)
    425         # Exit the method now if there aren't any files.
    426         if lines == ['']:
    427             return []
    428449        names = []
    429         for line in lines:
    430             if self._parser.ignores_line(line):
    431                 continue
    432             # For `listdir`, we are interested in just the names,
    433             #  but we use the `time_shift` parameter to have the
    434             #  correct timestamp values in the cache.
    435             stat_result = self._parser.parse_line(line,
    436                                                   self._host.time_shift())
    437             loop_path = self._path.join(path, stat_result._st_name)
    438             self._lstat_cache[loop_path] = stat_result
     450        for stat_result in self._stat_results_from_dir(path):
    439451            st_name = stat_result._st_name
    440             if st_name not in (self._host.curdir, self._host.pardir):
    441                 names.append(st_name)
     452            names.append(st_name)
    442453        return names
    443454
     
    480491        #  possible.
    481492        lstat_result_for_path = None
    482         lines = self._host_dir(dirname)
    483         for line in lines:
    484             if self._parser.ignores_line(line):
    485                 continue
    486             stat_result = self._parser.parse_line(line,
    487                           self._host.time_shift())
    488             loop_path = self._path.join(dirname, stat_result._st_name)
    489             self._lstat_cache[loop_path] = stat_result
     493        for stat_result in self._stat_results_from_dir(dirname):
    490494            # Needed to work without cache or with disabled cache.
    491495            if stat_result._st_name == basename:
Note: See TracChangeset for help on using the changeset viewer.