|
3 | 3 |
|
4 | 4 | XXX references to utf-8 need further investigation. |
5 | 5 | """ |
| 6 | +import binascii |
| 7 | +import functools |
| 8 | +import importlib.util |
6 | 9 | import io |
7 | 10 | import os |
8 | | -import importlib.util |
9 | | -import sys |
10 | | -import time |
11 | | -import stat |
| 11 | +import posixpath |
12 | 12 | import shutil |
| 13 | +import stat |
13 | 14 | import struct |
14 | | -import binascii |
| 15 | +import sys |
15 | 16 | import threading |
| 17 | +import time |
16 | 18 |
|
17 | 19 | try: |
18 | 20 | import zlib # We may need its compression method |
@@ -2102,6 +2104,138 @@ def _compile(file, optimize=-1): |
2102 | 2104 | return (fname, archivename) |
2103 | 2105 |
|
2104 | 2106 |
|
| 2107 | +class Path: |
| 2108 | + """ |
| 2109 | + A pathlib-compatible interface for zip files. |
| 2110 | +
|
| 2111 | + Consider a zip file with this structure:: |
| 2112 | +
|
| 2113 | + . |
| 2114 | + βββ a.txt |
| 2115 | + βββ b |
| 2116 | + βββ c.txt |
| 2117 | + βββ d |
| 2118 | + βββ e.txt |
| 2119 | +
|
| 2120 | + >>> data = io.BytesIO() |
| 2121 | + >>> zf = ZipFile(data, 'w') |
| 2122 | + >>> zf.writestr('a.txt', 'content of a') |
| 2123 | + >>> zf.writestr('b/c.txt', 'content of c') |
| 2124 | + >>> zf.writestr('b/d/e.txt', 'content of e') |
| 2125 | + >>> zf.filename = 'abcde.zip' |
| 2126 | +
|
| 2127 | + Path accepts the zipfile object itself or a filename |
| 2128 | +
|
| 2129 | + >>> root = Path(zf) |
| 2130 | +
|
| 2131 | + From there, several path operations are available. |
| 2132 | +
|
| 2133 | + Directory iteration (including the zip file itself): |
| 2134 | +
|
| 2135 | + >>> a, b = root.iterdir() |
| 2136 | + >>> a |
| 2137 | + Path('abcde.zip', 'a.txt') |
| 2138 | + >>> b |
| 2139 | + Path('abcde.zip', 'b/') |
| 2140 | +
|
| 2141 | + name property: |
| 2142 | +
|
| 2143 | + >>> b.name |
| 2144 | + 'b' |
| 2145 | +
|
| 2146 | + join with divide operator: |
| 2147 | +
|
| 2148 | + >>> c = b / 'c.txt' |
| 2149 | + >>> c |
| 2150 | + Path('abcde.zip', 'b/c.txt') |
| 2151 | + >>> c.name |
| 2152 | + 'c.txt' |
| 2153 | +
|
| 2154 | + Read text: |
| 2155 | +
|
| 2156 | + >>> c.read_text() |
| 2157 | + 'content of c' |
| 2158 | +
|
| 2159 | + existence: |
| 2160 | +
|
| 2161 | + >>> c.exists() |
| 2162 | + True |
| 2163 | + >>> (b / 'missing.txt').exists() |
| 2164 | + False |
| 2165 | +
|
| 2166 | + Coersion to string: |
| 2167 | +
|
| 2168 | + >>> str(c) |
| 2169 | + 'abcde.zip/b/c.txt' |
| 2170 | + """ |
| 2171 | + |
| 2172 | + __repr = "{self.__class__.__name__}({self.root.filename!r}, {self.at!r})" |
| 2173 | + |
| 2174 | + def __init__(self, root, at=""): |
| 2175 | + self.root = root if isinstance(root, ZipFile) else ZipFile(root) |
| 2176 | + self.at = at |
| 2177 | + |
| 2178 | + @property |
| 2179 | + def open(self): |
| 2180 | + return functools.partial(self.root.open, self.at) |
| 2181 | + |
| 2182 | + @property |
| 2183 | + def name(self): |
| 2184 | + return posixpath.basename(self.at.rstrip("/")) |
| 2185 | + |
| 2186 | + def read_text(self, *args, **kwargs): |
| 2187 | + with self.open() as strm: |
| 2188 | + return io.TextIOWrapper(strm, *args, **kwargs).read() |
| 2189 | + |
| 2190 | + def read_bytes(self): |
| 2191 | + with self.open() as strm: |
| 2192 | + return strm.read() |
| 2193 | + |
| 2194 | + def _is_child(self, path): |
| 2195 | + return posixpath.dirname(path.at.rstrip("/")) == self.at.rstrip("/") |
| 2196 | + |
| 2197 | + def _next(self, at): |
| 2198 | + return Path(self.root, at) |
| 2199 | + |
| 2200 | + def is_dir(self): |
| 2201 | + return not self.at or self.at.endswith("/") |
| 2202 | + |
| 2203 | + def is_file(self): |
| 2204 | + return not self.is_dir() |
| 2205 | + |
| 2206 | + def exists(self): |
| 2207 | + return self.at in self._names() |
| 2208 | + |
| 2209 | + def iterdir(self): |
| 2210 | + if not self.is_dir(): |
| 2211 | + raise ValueError("Can't listdir a file") |
| 2212 | + subs = map(self._next, self._names()) |
| 2213 | + return filter(self._is_child, subs) |
| 2214 | + |
| 2215 | + def __str__(self): |
| 2216 | + return posixpath.join(self.root.filename, self.at) |
| 2217 | + |
| 2218 | + def __repr__(self): |
| 2219 | + return self.__repr.format(self=self) |
| 2220 | + |
| 2221 | + def __truediv__(self, add): |
| 2222 | + next = posixpath.join(self.at, add) |
| 2223 | + next_dir = posixpath.join(self.at, add, "") |
| 2224 | + names = self._names() |
| 2225 | + return self._next(next_dir if next not in names and next_dir in names else next) |
| 2226 | + |
| 2227 | + @staticmethod |
| 2228 | + def _add_implied_dirs(names): |
| 2229 | + return names + [ |
| 2230 | + name + "/" |
| 2231 | + for name in map(posixpath.dirname, names) |
| 2232 | + if name and name + "/" not in names |
| 2233 | + ] |
| 2234 | + |
| 2235 | + def _names(self): |
| 2236 | + return self._add_implied_dirs(self.root.namelist()) |
| 2237 | + |
| 2238 | + |
2105 | 2239 | def main(args=None): |
2106 | 2240 | import argparse |
2107 | 2241 |
|
|
0 commit comments