source: test/test_base.py @ 1791:866a59de515d

Last change on this file since 1791:866a59de515d was 1791:866a59de515d, checked in by Stefan Schwarzer <sschwarzer@…>, 20 months ago
Add `dir_line` function The function can be used to construct lines as they would be returned in an FTP listing. This is especially useful if we need to control the date or time relative to the current date or time.
File size: 3.2 KB
Line 
1# Copyright (C) 2003-2019, Stefan Schwarzer <sschwarzer@sschwarzer.net>
2# and ftputil contributors (see `doc/contributors.txt`)
3# See the file LICENSE for licensing terms.
4
5import ftputil
6
7from test import mock_ftplib
8
9
10# Factory to produce `FTPHost`-like classes from a given `FTPHost`
11# class and (usually) a given `MockSession` class.
12def ftp_host_factory(session_factory=mock_ftplib.MockUnixFormatSession,
13                     ftp_host_class=ftputil.FTPHost):
14    return ftp_host_class("dummy_host", "dummy_user", "dummy_password",
15                          session_factory=session_factory)
16
17
18def dir_line(mode_string="-r--r--r--",
19             nlink=1,
20             user="dummy_user",
21             group="dummy_group",
22             size=512,
23             date_=None,
24             datetime_=None,
25             name="dummy_name",
26             link_target=None):
27    """
28    Return a line as it would be returned by an FTP `DIR` invocation.
29
30    Some values are handled specially:
31
32    - One of `date_` or `datetime_` must be given, the other must be `None`.
33
34      If `date_` is given, it must be a `datetime.date` object. The timestamp
35      in the `DIR` line is formatted like "Apr 22 2019", with the concrete
36      value taken from the `date_` object.
37
38      If `datetime_` is given, it must be a `datetime.datetime` object. The
39      timestamp in the `DIR` line is formatted like "Apr 22 16:50", with the
40      concrete value taken from the `datetime_` object. Timezone information in
41      the `datetime_` object is ignored.
42
43    - If `link_target` is left at the default `None`, the name part is the
44      value from the `name` argument. If `link_target` isn't `None`, the name
45      part of the `DIR` line is formatted as "name -> link_target".
46
47    Note that the spacing between the parts of the line isn't necessarily
48    exactly what you'd get from an FTP server because the parser in ftputil
49    doesn't take the exact amount of spaces into account, so the `DIR` lines
50    don't have to be that accurate.
51
52    Examples:
53
54      # Result:
55      # "drwxr-xr-x  2  dummy_user dummy_group  182  Apr 22 16:50  file_name"
56      line = dir_line(mode_string="-rw-rw-r--",
57                      nlink=2,
58                      size=182,
59                      datetime=datetime.datetime.now(),
60                      name="file_name")
61 
62      # Result:
63      # "drwxr-xr-x  1  dummy_user dummy_group  512  Apr 22 2019  dir_name -> dir_target"
64      line = dir_line(mode_string="drwxr-xr-x",
65                      date=datetime.date.today(),
66                      name="dir_name",
67                      link_target="dir_target")
68    """
69    # Date or datetime. We must have exactly one of `date_` and `datetime_`
70    # set. The other value must be `None`.
71    assert [date_, datetime_].count(None) == 1, "specify exactly one of `date_` and `datetime_`"
72    if date_:
73        datetime_string = date_.strftime("%b %d %Y")
74    else:
75        datetime_string = datetime_.strftime("%b %d %H:%M")
76    # Name, possibly with link target
77    if not link_target:
78        name_string = name
79    else:
80        name_string = "{} -> {}".format(name, link_target)
81    #
82    return "{}  {}  {} {}  {}  {}  {}".format(mode_string, nlink, user, group, size,
83                                              datetime_string, name_string)
Note: See TracBrowser for help on using the repository browser.