Changeset 1576:f2998542a221


Ignore:
Timestamp:
Oct 4, 2014, 6:08:41 PM (7 years ago)
Author:
Stefan Schwarzer <sschwarzer@…>
Branch:
default
Message:
Use named constants for datetime precisions.

In unit tests, explicitly test for precision values.
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ftputil/stat.py

    r1573 r1576  
    2222# These can be used to write custom parsers.
    2323__all__ = ["StatResult", "Parser", "UnixParser", "MSParser"]
     24
     25
     26# Datetime precision values in seconds.
     27MINUTE_PRECISION  = 60
     28DAY_PRECISION     = 24 * 60 * 60
     29UNKNOWN_PRECISION = None
    2430
    2531
     
    4955        self._st_name = ""
    5056        self._st_target = None
    51         self._st_mtime_precision = None
     57        self._st_mtime_precision = UNKNOWN_PRECISION
    5258
    5359    def __getattr__(self, attr_name):
     
    240246            st_mtime = self._mktime( (year, month, day,
    241247                                      hour, minute, 0, 0, 0, -1) )
    242             # Precise up to a day.
    243             st_mtime_precision = 24 * 60 * 60
     248            st_mtime_precision = DAY_PRECISION
    244249        else:
    245250            # `year_or_time` is a time hh:mm.
     
    251256            st_mtime = self._mktime( (year, month, day,
    252257                                      hour, minute, 0, 0, 0, -1) )
    253             # Times are precise up to a minute.
    254             st_mtime_precision = 60
     258            st_mtime_precision = MINUTE_PRECISION
    255259            # Rhs of comparison: Transform client time to server time
    256260            # (as on the lhs), so both can be compared with respect
     
    480484        stat_result._st_target = None
    481485        # mtime precision in seconds
    482         stat_result._st_mtime_precision = 60
     486        stat_result._st_mtime_precision = MINUTE_PRECISION
    483487        return stat_result
    484488
  • test/test_stat.py

    r1574 r1576  
    1414import ftputil.error
    1515import ftputil.stat
     16from ftputil.stat import MINUTE_PRECISION, DAY_PRECISION, UNKNOWN_PRECISION
    1617
    1718from test import test_base
     
    5657            # Convert to list to compare with the list `expected_stat_results`.
    5758            parse_result = parser.parse_line(line)
    58             stat_result = list(parse_result) + [parse_result._st_name,
    59                                                 parse_result._st_target]
     59            stat_result = list(parse_result) + \
     60                          [parse_result._st_mtime_precision,
     61                           parse_result._st_name,
     62                           parse_result._st_target]
    6063            # Convert time tuple to seconds.
    6164            expected_stat_result[8] = \
     
    104107        expected_stat_results = [
    105108          [17901, None, None, 2, "45854", "200", 512, None,
    106            (2000, 5, 4, 0, 0, 0), None, "chemeng link", "chemeng target"],
     109           (2000, 5, 4, 0, 0, 0), None, DAY_PRECISION,
     110           "chemeng link", "chemeng target"],
    107111          [33188, None, None, 1, "45854", "200", 4604, None,
    108            (self._expected_year(), 12, 19, 23, 11, 0), None,
     112           (self._expected_year(), 12, 19, 23, 11, 0), None, MINUTE_PRECISION,
    109113           "index.html", None],
    110114          [17901, None, None, 2, "45854", "200", 512, None,
    111            (2000, 5, 29, 0, 0, 0), None, "os2", None],
     115           (2000, 5, 29, 0, 0, 0), None, DAY_PRECISION,
     116           "os2", None],
    112117          [32768, None, None, 2, "45854", "200", 512, None,
    113            (2000, 5, 29, 0, 0, 0), None, "some_file", None],
     118           (2000, 5, 29, 0, 0, 0), None, DAY_PRECISION,
     119           "some_file", None],
    114120          [41471, None, None, 2, "45854", "200", 512, None,
    115            (2000, 5, 29, 0, 0, 0), None, "osup", "../os2"]
     121           (2000, 5, 29, 0, 0, 0), None, DAY_PRECISION,
     122           "osup", "../os2"]
    116123          ]
    117124        self._test_valid_lines(ftputil.stat.UnixParser, lines,
     
    164171        expected_stat_results = [
    165172          [17901, None, None, 2, None, "200", 512, None,
    166            (2000, 5, 4, 0, 0, 0), None, "chemeng link", "chemeng target"],
     173           (2000, 5, 4, 0, 0, 0), None, DAY_PRECISION,
     174           "chemeng link", "chemeng target"],
    167175          [33188, None, None, 1, None, "200", 4604, None,
    168            (self._expected_year(), 12, 19, 23, 11, 0), None,
     176           (self._expected_year(), 12, 19, 23, 11, 0), None, MINUTE_PRECISION,
    169177           "index.html", None],
    170178          [17901, None, None, 2, None, "200", 512, None,
    171            (2000, 5, 29, 0, 0, 0), None, "os2", None],
     179           (2000, 5, 29, 0, 0, 0), None, DAY_PRECISION,
     180           "os2", None],
    172181          [41471, None, None, 2, None, "200", 512, None,
    173            (2000, 5, 29, 0, 0, 0), None, "osup", "../os2"]
     182           (2000, 5, 29, 0, 0, 0), None, DAY_PRECISION,
     183           "osup", "../os2"]
    174184          ]
    175185        self._test_valid_lines(ftputil.stat.UnixParser, lines,
    176186                               expected_stat_results)
    177187
    178     def test_times_before_the_epoch_for_unix(self):
     188    def test_pre_epoch_times_for_unix(self):
    179189        # See http://ftputil.sschwarzer.net/trac/ticket/83 .
    180190        # `mirrors.ibiblio.org` returns dates before the "epoch" that
     
    188198        expected_stat_result = \
    189199          [33188, None, None, 1, "45854", "200", 4604, None,
    190            EPOCH, None, "index.html", None]
     200           EPOCH, None, DAY_PRECISION, "index.html", None]
    191201        # Make shallow copies to avoid converting the time tuple more
    192202        # than once in _test_valid_lines`.
     
    210220        expected_stat_results = [
    211221          [16640, None, None, None, None, None, None, None,
    212            (2001, 7, 27, 11, 16, 0), None, "Test", None],
     222           (2001, 7, 27, 11, 16, 0), None, MINUTE_PRECISION,
     223           "Test", None],
    213224          [16640, None, None, None, None, None, None, None,
    214            (1995, 10, 23, 15, 25, 0), None, "WindowsXP", None],
     225           (1995, 10, 23, 15, 25, 0), None, MINUTE_PRECISION,
     226           "WindowsXP", None],
    215227          [33024, None, None, None, None, None, 12266720, None,
    216            (2000, 7, 17, 14, 8, 0), None, "test.exe", None],
     228           (2000, 7, 17, 14, 8, 0), None, MINUTE_PRECISION,
     229           "test.exe", None],
    217230          [33024, None, None, None, None, None, 12266720, None,
    218            (2009, 7, 17, 0, 8, 0), None, "test.exe", None],
     231           (2009, 7, 17, 0, 8, 0), None, MINUTE_PRECISION,
     232           "test.exe", None],
    219233          [33024, None, None, None, None, None, 12266720, None,
    220            (2009, 7, 17, 12, 8, 0), None, "test.exe", None]
     234           (2009, 7, 17, 12, 8, 0), None, MINUTE_PRECISION,
     235           "test.exe", None]
    221236          ]
    222237        self._test_valid_lines(ftputil.stat.MSParser, lines,
     
    232247        expected_stat_results = [
    233248          [16640, None, None, None, None, None, None, None,
    234            (2012, 10, 19, 15, 13, 0), None, "SYNCDEST", None],
     249           (2012, 10, 19, 15, 13, 0), None, MINUTE_PRECISION,
     250           "SYNCDEST", None],
    235251          [16640, None, None, None, None, None, None, None,
    236            (2012, 10, 19, 15, 13, 0), None, "SYNCSOURCE", None],
     252           (2012, 10, 19, 15, 13, 0), None, MINUTE_PRECISION,
     253           "SYNCSOURCE", None],
    237254          [16640, None, None, None, None, None, None, None,
    238            EPOCH, None, "SYNC", None],
     255           EPOCH, None, MINUTE_PRECISION,
     256           "SYNC", None],
    239257          ]
    240258        self._test_valid_lines(ftputil.stat.MSParser, lines,
Note: See TracChangeset for help on using the changeset viewer.