1 | """ |
---|
2 | Ported to Python 3. |
---|
3 | """ |
---|
4 | |
---|
5 | from io import BytesIO |
---|
6 | |
---|
7 | from zope.interface import implementer |
---|
8 | from twisted.internet import defer |
---|
9 | from twisted.protocols import basic |
---|
10 | from allmydata.interfaces import IImmutableFileNode, ICheckable |
---|
11 | from allmydata.uri import LiteralFileURI |
---|
12 | |
---|
13 | |
---|
14 | class _ImmutableFileNodeBase(object): |
---|
15 | |
---|
16 | def get_write_uri(self): |
---|
17 | return None |
---|
18 | |
---|
19 | def get_readonly_uri(self): |
---|
20 | return self.get_uri() |
---|
21 | |
---|
22 | def is_mutable(self): |
---|
23 | return False |
---|
24 | |
---|
25 | def is_readonly(self): |
---|
26 | return True |
---|
27 | |
---|
28 | def is_unknown(self): |
---|
29 | return False |
---|
30 | |
---|
31 | def is_allowed_in_immutable_directory(self): |
---|
32 | return True |
---|
33 | |
---|
34 | def raise_error(self): |
---|
35 | pass |
---|
36 | |
---|
37 | def __hash__(self): |
---|
38 | return self.u.__hash__() |
---|
39 | |
---|
40 | def __eq__(self, other): |
---|
41 | if isinstance(other, _ImmutableFileNodeBase): |
---|
42 | return self.u == other.u |
---|
43 | else: |
---|
44 | return False |
---|
45 | |
---|
46 | def __ne__(self, other): |
---|
47 | return not self == other |
---|
48 | |
---|
49 | |
---|
50 | @implementer(IImmutableFileNode, ICheckable) |
---|
51 | class LiteralFileNode(_ImmutableFileNodeBase): |
---|
52 | |
---|
53 | def __init__(self, filecap): |
---|
54 | assert isinstance(filecap, LiteralFileURI) |
---|
55 | self.u = filecap |
---|
56 | |
---|
57 | def get_size(self): |
---|
58 | return len(self.u.data) |
---|
59 | def get_current_size(self): |
---|
60 | return defer.succeed(self.get_size()) |
---|
61 | |
---|
62 | def get_cap(self): |
---|
63 | return self.u |
---|
64 | def get_readcap(self): |
---|
65 | return self.u |
---|
66 | def get_verify_cap(self): |
---|
67 | return None |
---|
68 | def get_repair_cap(self): |
---|
69 | return None |
---|
70 | |
---|
71 | def get_uri(self): |
---|
72 | return self.u.to_string() |
---|
73 | |
---|
74 | def get_storage_index(self): |
---|
75 | return None |
---|
76 | |
---|
77 | def check(self, monitor, verify=False, add_lease=False): |
---|
78 | return defer.succeed(None) |
---|
79 | |
---|
80 | def check_and_repair(self, monitor, verify=False, add_lease=False): |
---|
81 | return defer.succeed(None) |
---|
82 | |
---|
83 | def read(self, consumer, offset=0, size=None): |
---|
84 | if size is None: |
---|
85 | data = self.u.data[offset:] |
---|
86 | else: |
---|
87 | data = self.u.data[offset:offset+size] |
---|
88 | |
---|
89 | # We use twisted.protocols.basic.FileSender, which only does |
---|
90 | # non-streaming, i.e. PullProducer, where the receiver/consumer must |
---|
91 | # ask explicitly for each chunk of data. There are only two places in |
---|
92 | # the Twisted codebase that can't handle streaming=False, both of |
---|
93 | # which are in the upload path for an FTP/SFTP server |
---|
94 | # (protocols.ftp.FileConsumer and |
---|
95 | # vfs.adapters.ftp._FileToConsumerAdapter), neither of which is |
---|
96 | # likely to be used as the target for a Tahoe download. |
---|
97 | |
---|
98 | d = basic.FileSender().beginFileTransfer(BytesIO(data), consumer) |
---|
99 | d.addCallback(lambda lastSent: consumer) |
---|
100 | return d |
---|
101 | |
---|
102 | # IReadable, IFileNode, IFilesystemNode |
---|
103 | def get_best_readable_version(self): |
---|
104 | return defer.succeed(self) |
---|
105 | |
---|
106 | |
---|
107 | def download_best_version(self): |
---|
108 | return defer.succeed(self.u.data) |
---|
109 | |
---|
110 | |
---|
111 | download_to_data = download_best_version |
---|
112 | get_size_of_best_version = get_current_size |
---|