~sschwarzer/ftputil#58: 
Ability to access source text line for file stats.

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@…)

Status
RESOLVED WONT_FIX
Submitter
ftputiluser (unverified)
Assigned to
No-one
Submitted
12 years ago
Updated
12 years ago
Labels
enhancement library

schwa (unverified) 12 years ago · edit

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.

schwa (unverified) 12 years ago · edit

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.

Register here or Log in to comment, or comment via email.