|
19 | 19 | RE_LINUX = re.compile( |
20 | 20 | r""" |
21 | 21 | ^ |
22 | | - ([ldrwx-]{10}) |
| 22 | + ([-dlpscbD]) |
| 23 | + ([r-][w-][xsS-][r-][w-][xsS-][r-][w-][xtT-][\.\+]?) |
23 | 24 | \s+? |
24 | 25 | (\d+) |
25 | 26 | \s+? |
|
55 | 56 |
|
56 | 57 |
|
57 | 58 | def get_decoders(): |
58 | | - """ |
59 | | - Returns all available FTP LIST line decoders with their matching regexes. |
60 | | - """ |
| 59 | + """Return all available FTP LIST line decoders with their matching regexes.""" |
61 | 60 | decoders = [ |
62 | 61 | (RE_LINUX, decode_linux), |
63 | 62 | (RE_WINDOWSNT, decode_windowsnt), |
@@ -110,14 +109,14 @@ def _decode_linux_time(mtime): |
110 | 109 |
|
111 | 110 |
|
112 | 111 | def decode_linux(line, match): |
113 | | - perms, links, uid, gid, size, mtime, name = match.groups() |
114 | | - is_link = perms.startswith("l") |
115 | | - is_dir = perms.startswith("d") or is_link |
| 112 | + ty, perms, links, uid, gid, size, mtime, name = match.groups() |
| 113 | + is_link = ty == "l" |
| 114 | + is_dir = ty == "d" or is_link |
116 | 115 | if is_link: |
117 | 116 | name, _, _link_name = name.partition("->") |
118 | 117 | name = name.strip() |
119 | 118 | _link_name = _link_name.strip() |
120 | | - permissions = Permissions.parse(perms[1:]) |
| 119 | + permissions = Permissions.parse(perms) |
121 | 120 |
|
122 | 121 | mtime_epoch = _decode_linux_time(mtime) |
123 | 122 |
|
@@ -148,13 +147,34 @@ def _decode_windowsnt_time(mtime): |
148 | 147 |
|
149 | 148 |
|
150 | 149 | def decode_windowsnt(line, match): |
151 | | - """ |
152 | | - Decodes a Windows NT FTP LIST line like one of these: |
| 150 | + """Decode a Windows NT FTP LIST line. |
| 151 | +
|
| 152 | + Examples: |
| 153 | + Decode a directory line:: |
| 154 | +
|
| 155 | + >>> line = "11-02-18 02:12PM <DIR> images" |
| 156 | + >>> match = RE_WINDOWSNT.match(line) |
| 157 | + >>> pprint(decode_windowsnt(line, match)) |
| 158 | + {'basic': {'is_dir': True, 'name': 'images'}, |
| 159 | + 'details': {'modified': 1518358320.0, 'type': 1}, |
| 160 | + 'ftp': {'ls': '11-02-18 02:12PM <DIR> images'}} |
| 161 | +
|
| 162 | + Decode a file line:: |
| 163 | +
|
| 164 | + >>> line = "11-02-18 03:33PM 9276 logo.gif" |
| 165 | + >>> match = RE_WINDOWSNT.match(line) |
| 166 | + >>> pprint(decode_windowsnt(line, match)) |
| 167 | + {'basic': {'is_dir': False, 'name': 'logo.gif'}, |
| 168 | + 'details': {'modified': 1518363180.0, 'size': 9276, 'type': 2}, |
| 169 | + 'ftp': {'ls': '11-02-18 03:33PM 9276 logo.gif'}} |
| 170 | +
|
| 171 | + Alternatively, the time might also be present in 24-hour format:: |
153 | 172 |
|
154 | | - `11-02-18 02:12PM <DIR> images` |
155 | | - `11-02-18 03:33PM 9276 logo.gif` |
| 173 | + >>> line = "11-02-18 15:33 9276 logo.gif" |
| 174 | + >>> match = RE_WINDOWSNT.match(line) |
| 175 | + >>> decode_windowsnt(line, match)["details"]["modified"] |
| 176 | + 1518363180.0 |
156 | 177 |
|
157 | | - Alternatively, the time (02:12PM) might also be present in 24-hour format (14:12). |
158 | 178 | """ |
159 | 179 | is_dir = match.group("size") == "<DIR>" |
160 | 180 |
|
|
0 commit comments