A utility to read/write protobuf data to ipfs.
Reading:
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from myprotobuf_pb2 import MyProtobuf
store = Store(
Index.from_filename("myfile.txt"),
ipfs=Ipfs(host="127.0.0.1", port="5001"),
reader=MyProtobuf()
)
store.read()
print(store.reader)
Writing:
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from myprotobuf_pb2 import MyProtobuf
store = Store(
Index.from_filename("myfile.txt"),
ipfs=Ipfs(host="127.0.0.1", port="5001"),
writer=MyProtobuf()
)
store.add()
Write with multiple indexes.
Create a tiered file structure based on IDs.
├── fashion/
├── designer_1.manufacturer_1
├── designer_2.manufacturer_1
├── deal_16.data
├── designer_4.manufacturer_3
├── deal_1.data
├── deal_2.data
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from deal_pb2 import Deal
index = Index(
prefix="fashion",
index={
"designer": str(uuid.uuid4()),
"manufacturer": str(uuid.uuid4())
}, subindex=Index(
index={
"deal": str(uuid.uuid4())
}
)
)
data = Deal(type=Type.BUZZ, content="fizz")
store = Store(index=index, ipfs=Ipfs(), writer=data)
store.add()
Query the multiple indexes:
Ex: get all deals with designer id "123"
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from deal_pb2 import Deal
query_index = Index(
prefix="fashion",
index={
"designer": "123"
}
)
reader = Deal()
store = Store.query(query_index, ipfs, reader)
print(reader)
Source code in ipfskvs/store.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 | @dataclass
class Store():
"""A utility to read/write protobuf data to ipfs.
Reading:
```py
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from myprotobuf_pb2 import MyProtobuf
store = Store(
Index.from_filename("myfile.txt"),
ipfs=Ipfs(host="127.0.0.1", port="5001"),
reader=MyProtobuf()
)
store.read()
print(store.reader)
```
Writing:
```py
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from myprotobuf_pb2 import MyProtobuf
store = Store(
Index.from_filename("myfile.txt"),
ipfs=Ipfs(host="127.0.0.1", port="5001"),
writer=MyProtobuf()
)
store.add()
```
Write with multiple indexes.
Create a tiered file structure based on IDs.
```
├── fashion/
├── designer_1.manufacturer_1
├── designer_2.manufacturer_1
├── deal_16.data
├── designer_4.manufacturer_3
├── deal_1.data
├── deal_2.data
```
```py
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from deal_pb2 import Deal
index = Index(
prefix="fashion",
index={
"designer": str(uuid.uuid4()),
"manufacturer": str(uuid.uuid4())
}, subindex=Index(
index={
"deal": str(uuid.uuid4())
}
)
)
data = Deal(type=Type.BUZZ, content="fizz")
store = Store(index=index, ipfs=Ipfs(), writer=data)
store.add()
```
Query the multiple indexes:
Ex: get all deals with designer id "123"
```py
from ipfskvs.index import Index
from ipfskvs.ipfs import Ipfs
from ipfskvs.store import Store
from deal_pb2 import Deal
query_index = Index(
prefix="fashion",
index={
"designer": "123"
}
)
reader = Deal()
store = Store.query(query_index, ipfs, reader)
print(reader)
```
"""
index: Index
writer: Message
reader: Message
def __init__(
self: Self,
index: Index,
ipfs: Ipfs,
writer: Message = None,
reader: Message = None) -> None:
"""Construct a Store object.
Args:
index (Index): An object representing the filepath
ipfs (Ipfs): The IPFS client
writer (Message, optional): The protobuf object with the
data to write to ipfs on `.write()`. Defaults to None.
reader (Message, optional): The protobuf object to populate
when reading the data from ipfs with `.read()`.
Defaults to None.
"""
self.index = index
self.ipfs = ipfs
self.writer = writer
self.reader = reader
def read(self: Self) -> None:
"""Read the data from ipfs into `self.reader`.
Raises:
FileNotFoundError: An exception is raised if the file is
not found on IPFS.
"""
filename = self.index.get_filename()
LOG.info(f"Reading {filename}")
result = self.ipfs.read(filename)
if not result:
raise FileNotFoundError(
errno.ENOENT,
os.strerror(errno.ENOENT),
filename
)
LOG.debug(result)
self.reader.ParseFromString(result)
def write(self: Self) -> None:
"""Write the protobuf data from `self.writer` to IPFS."""
raise NotImplementedError("For now, just use `add` and `delete`")
self.ipfs.write(
self.index.get_filename(),
self.writer.SerializeToString()
)
def add(self: Self) -> None:
"""Add the protobuf data from `self.writer` to IPFS."""
filename = self.index.get_filename()
data = self.writer.SerializeToString()
LOG.info(f"Adding {data} to {filename}")
self.ipfs.add(filename, data)
def delete(self: Self) -> None:
"""Only needed for local testing."""
filename = self.index.get_filename()
LOG.info(f"Deleting file: {filename}")
self.ipfs.delete(filename)
@staticmethod
def to_dataframe(
data: List[Store],
protobuf_parsers: Dict[str, FunctionType]) -> pd.DataFrame:
"""Convert a list of Store objects to a pandas dataframe.
The data for each Store must be read into memory beforehand;
using `store.read()`
Args:
data (List[Store]): The list of Store objects with Indexes
protobuf_parsers: (Dict[str, function]): key, value pair of
key (str) --> pandas column name
value (function) --> how to extract the value from the store
The function should accept a Store object and return Any
Returns:
pd.DataFrame: The index and subindex data
reformatted into a dataframe
"""
pandas_input = {}
for store in data:
# add metadata
metadata = store.index.get_metadata()
LOG.debug(f"Found metadata: {metadata}")
for key in metadata:
if key not in pandas_input:
LOG.debug(f"Found key: {key}")
pandas_input[key] = []
pandas_input[key].append(metadata[key])
# add top level data from the reader
for key in protobuf_parsers:
if key not in pandas_input:
pandas_input[key] = []
parsed_data = protobuf_parsers[key](store)
LOG.debug(f"Parsed {parsed_data} for {key} from {store}")
pandas_input[key].append(parsed_data)
# load the data into a pandas dataframe
return pd.DataFrame.from_dict(pandas_input)
@staticmethod
def query_indexes(query_index: Index, ipfs: Ipfs) -> List[Index]:
"""Query ipfs based on the `query_index` param.
Args:
query_index (Index): The Index object to use for the query.
ipfs (Ipfs): The IPFS client.
Returns:
List[Index]: The matching filenames found in ipfs, loaded
into a list of Index objects
"""
result = []
# list the files in the directory
path = query_index.get_filename()
response = ipfs.list_files(path)
filenames = [file['Name'] for file in response['Entries']]
for filename in filenames:
# Listing the same file twice indicates the base case
# ex:
# path = `ls dir1/dir2` --> filenames = ["filename"]
# path = `ls dir1/dir2/filename` --> filenames = ["filename"]
if filename in path:
return [query_index]
# filter filenames based on the index
full_filename = f"{path}/{filename}".replace("//", "/")
LOG.debug(f"Current filename: {full_filename}")
from_index = Index.from_filename(
filename=full_filename,
has_prefix=query_index.prefix
)
if query_index.matches(from_index):
LOG.debug(f"Matched {from_index} with {query_index}")
result += Store.query_indexes(from_index, ipfs)
return result
@staticmethod
def query(
query_index: Index,
ipfs: Ipfs,
reader: Message) -> Iterator[Store]:
"""Query ipfs based on the `query_index` param.
Find the filenames matching the query_index.
Read the file contents from ipfs for each matching filename.
Parse the file contents into the reader protobuf object.
Args:
query_index (Index): The Index object to use for the query.
ipfs (Ipfs): The IPFS client.
reader (Message): _description_
Yields:
Iterator[Store]: The list of matching Store objects with
file content loaded into the `reader` attribute
"""
for response_index in Store.query_indexes(query_index, ipfs):
store = Store(index=response_index, reader=reader, ipfs=ipfs)
store.read()
yield store
|
__init__(index, ipfs, writer=None, reader=None)
Construct a Store object.
Parameters:
Name |
Type |
Description |
Default |
index |
Index
|
An object representing the filepath |
required
|
ipfs |
Ipfs
|
The IPFS client |
required
|
writer |
Message
|
The protobuf object with the
data to write to ipfs on .write() . Defaults to None. |
None
|
reader |
Message
|
The protobuf object to populate
when reading the data from ipfs with .read() .
Defaults to None. |
None
|
Source code in ipfskvs/store.py
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 | def __init__(
self: Self,
index: Index,
ipfs: Ipfs,
writer: Message = None,
reader: Message = None) -> None:
"""Construct a Store object.
Args:
index (Index): An object representing the filepath
ipfs (Ipfs): The IPFS client
writer (Message, optional): The protobuf object with the
data to write to ipfs on `.write()`. Defaults to None.
reader (Message, optional): The protobuf object to populate
when reading the data from ipfs with `.read()`.
Defaults to None.
"""
self.index = index
self.ipfs = ipfs
self.writer = writer
self.reader = reader
|
read()
Read the data from ipfs into self.reader
.
Raises:
Type |
Description |
FileNotFoundError
|
An exception is raised if the file is
not found on IPFS. |
Source code in ipfskvs/store.py
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 | def read(self: Self) -> None:
"""Read the data from ipfs into `self.reader`.
Raises:
FileNotFoundError: An exception is raised if the file is
not found on IPFS.
"""
filename = self.index.get_filename()
LOG.info(f"Reading {filename}")
result = self.ipfs.read(filename)
if not result:
raise FileNotFoundError(
errno.ENOENT,
os.strerror(errno.ENOENT),
filename
)
LOG.debug(result)
self.reader.ParseFromString(result)
|
add()
Add the protobuf data from self.writer
to IPFS.
Source code in ipfskvs/store.py
| def add(self: Self) -> None:
"""Add the protobuf data from `self.writer` to IPFS."""
filename = self.index.get_filename()
data = self.writer.SerializeToString()
LOG.info(f"Adding {data} to {filename}")
self.ipfs.add(filename, data)
|
delete()
Only needed for local testing.
Source code in ipfskvs/store.py
| def delete(self: Self) -> None:
"""Only needed for local testing."""
filename = self.index.get_filename()
LOG.info(f"Deleting file: {filename}")
self.ipfs.delete(filename)
|
to_dataframe(data, protobuf_parsers)
staticmethod
Convert a list of Store objects to a pandas dataframe.
The data for each Store must be read into memory beforehand;
using store.read()
Parameters:
Name |
Type |
Description |
Default |
data |
List[Store]
|
The list of Store objects with Indexes |
required
|
protobuf_parsers |
Dict[str, FunctionType]
|
(Dict[str, function]): key, value pair of
key (str) --> pandas column name
value (function) --> how to extract the value from the store
The function should accept a Store object and return Any |
required
|
Returns:
Type |
Description |
pd.DataFrame
|
pd.DataFrame: The index and subindex data
reformatted into a dataframe |
Source code in ipfskvs/store.py
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220 | @staticmethod
def to_dataframe(
data: List[Store],
protobuf_parsers: Dict[str, FunctionType]) -> pd.DataFrame:
"""Convert a list of Store objects to a pandas dataframe.
The data for each Store must be read into memory beforehand;
using `store.read()`
Args:
data (List[Store]): The list of Store objects with Indexes
protobuf_parsers: (Dict[str, function]): key, value pair of
key (str) --> pandas column name
value (function) --> how to extract the value from the store
The function should accept a Store object and return Any
Returns:
pd.DataFrame: The index and subindex data
reformatted into a dataframe
"""
pandas_input = {}
for store in data:
# add metadata
metadata = store.index.get_metadata()
LOG.debug(f"Found metadata: {metadata}")
for key in metadata:
if key not in pandas_input:
LOG.debug(f"Found key: {key}")
pandas_input[key] = []
pandas_input[key].append(metadata[key])
# add top level data from the reader
for key in protobuf_parsers:
if key not in pandas_input:
pandas_input[key] = []
parsed_data = protobuf_parsers[key](store)
LOG.debug(f"Parsed {parsed_data} for {key} from {store}")
pandas_input[key].append(parsed_data)
# load the data into a pandas dataframe
return pd.DataFrame.from_dict(pandas_input)
|
query_indexes(query_index, ipfs)
staticmethod
Query ipfs based on the query_index
param.
Parameters:
Name |
Type |
Description |
Default |
query_index |
Index
|
The Index object to use for the query. |
required
|
ipfs |
Ipfs
|
The IPFS client. |
required
|
Returns:
Type |
Description |
List[Index]
|
List[Index]: The matching filenames found in ipfs, loaded
into a list of Index objects |
Source code in ipfskvs/store.py
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 | @staticmethod
def query_indexes(query_index: Index, ipfs: Ipfs) -> List[Index]:
"""Query ipfs based on the `query_index` param.
Args:
query_index (Index): The Index object to use for the query.
ipfs (Ipfs): The IPFS client.
Returns:
List[Index]: The matching filenames found in ipfs, loaded
into a list of Index objects
"""
result = []
# list the files in the directory
path = query_index.get_filename()
response = ipfs.list_files(path)
filenames = [file['Name'] for file in response['Entries']]
for filename in filenames:
# Listing the same file twice indicates the base case
# ex:
# path = `ls dir1/dir2` --> filenames = ["filename"]
# path = `ls dir1/dir2/filename` --> filenames = ["filename"]
if filename in path:
return [query_index]
# filter filenames based on the index
full_filename = f"{path}/{filename}".replace("//", "/")
LOG.debug(f"Current filename: {full_filename}")
from_index = Index.from_filename(
filename=full_filename,
has_prefix=query_index.prefix
)
if query_index.matches(from_index):
LOG.debug(f"Matched {from_index} with {query_index}")
result += Store.query_indexes(from_index, ipfs)
return result
|
query(query_index, ipfs, reader)
staticmethod
Query ipfs based on the query_index
param.
Find the filenames matching the query_index.
Read the file contents from ipfs for each matching filename.
Parse the file contents into the reader protobuf object.
Parameters:
Name |
Type |
Description |
Default |
query_index |
Index
|
The Index object to use for the query. |
required
|
ipfs |
Ipfs
|
The IPFS client. |
required
|
reader |
Message
|
description |
required
|
Yields:
Type |
Description |
Iterator[Store]
|
Iterator[Store]: The list of matching Store objects with
file content loaded into the reader attribute |
Source code in ipfskvs/store.py
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 | @staticmethod
def query(
query_index: Index,
ipfs: Ipfs,
reader: Message) -> Iterator[Store]:
"""Query ipfs based on the `query_index` param.
Find the filenames matching the query_index.
Read the file contents from ipfs for each matching filename.
Parse the file contents into the reader protobuf object.
Args:
query_index (Index): The Index object to use for the query.
ipfs (Ipfs): The IPFS client.
reader (Message): _description_
Yields:
Iterator[Store]: The list of matching Store objects with
file content loaded into the `reader` attribute
"""
for response_index in Store.query_indexes(query_index, ipfs):
store = Store(index=response_index, reader=reader, ipfs=ipfs)
store.read()
yield store
|