Opened 9 years ago

Closed 9 years ago

#58 closed enhancement (wontfix)

Ability to access source text line for file stats.

Reported by: ftputiluser Owned by: schwa
Priority: trivial Milestone:
Component: Library Version: 2.6
Keywords: Cc:

Description (last modified by schwa)

Hi, I'm using your awesome lib.

Please consider adding the following functionality: to be able access source text for file attributes entry while doing lstat on file.

Use case: While debugging issue when source FTP server returning dates in GMT timezone I realize that users of my program will have to be able to detect this issue also. So I added functionality to StatResult class to store and retrieve source text line for a file.

class StatResult(tuple):
    ...
    def __init__(self, sequence):
        # Don't call `__init__` via `super`. Construction from a
        #  sequence is implicitly handled by `tuple.__new__`, not
        #  `tuple.__init__`. As a by-product, this avoids a
        #  `DeprecationWarning` in Python 2.6+ .
        # pylint: disable=W0231, W0613
        #
        # These may be overwritten in a `Parser.parse_line` method.
        self._st_name = ""
        self._st_target = None
        self._st_mtime_precision = None
        self._line = ""
        
    def set_line(self,line):
        self._line = line
        
    def get_line(self):
        return self._line

...

adding source line

        stat_result = StatResult(
                      (st_mode, st_ino, st_dev, st_nlink, st_uid,
                       st_gid, st_size, st_atime, st_mtime, st_ctime) )
        stat_result.set_line(line)

Vlad (smartkiwi@…)

Change History (2)

comment:1 Changed 9 years ago by schwa

Description: modified (diff)
Status: newassigned

Hello Vlad,

Thanks for the compliment and your suggestion. :-)

I'll probably add this to the parsers in ftp_stat.py, but nonetheless you can already save the line without modifying the ftputil source code, by using the interface for custom parsers.

Step 1. Define a custom parser class:

from ftputil import ftp_stat


class DebuggingParser(ftp_stat.UnixParser):

    def parse_line(line, time_shift=0.0):
        """Parse a line like `UnixParser` does, but in addition save
        the original line in an attribute `_line` for debugging.
        """
        stat_result = super(ftp_stat.UnixParser, self).\
                      parse_line(line, time_shift)
        stat_result._line = line
        return stat_result

Step 2. Set an instance of this class as the parser to use:

import ftputil

import debugging_parser


ftp_host = ftputil.FTPHost(host, userid, password)
ftp_host.set_parser(debugging_parser.DebuggingParser())
# Use `ftp_host`.
...

You can of course modify the MSParser class in the same way if you need this.

comment:2 Changed 9 years ago by schwa

Resolution: wontfix
Status: assignedclosed

On second thought I decided against adding the source line to every stat object just for debugging. I think it's not appropriate to generally use the extra memory. If necessary you can always use the approach described in the previous comment.

Note: See TracTickets for help on using tickets.