Skip to content

Commit f12843e

Browse files
authored
Merge pull request #32 from lkluft/ipns
Add AsyncIPNSFileSystem
2 parents 2abf718 + c58f756 commit f12843e

File tree

3 files changed

+21
-15
lines changed

3 files changed

+21
-15
lines changed

ipfsspec/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
from .async_ipfs import AsyncIPFSFileSystem
1+
from .async_ipfs import AsyncIPFSFileSystem, AsyncIPNSFileSystem
22

33
from ._version import get_versions
44
__version__ = get_versions()['version']
55
del get_versions
66

7-
__all__ = ["__version__", "AsyncIPFSFileSystem"]
7+
__all__ = ["__version__", "AsyncIPFSFileSystem", "AsyncIPNSFileSystem"]

ipfsspec/async_ipfs.py

+18-13
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ def _raise_not_found_for_status(self, response, url):
9090
class AsyncIPFSGateway(AsyncIPFSGatewayBase):
9191
resolution = "path"
9292

93-
def __init__(self, url):
93+
def __init__(self, url, protocol="ipfs"):
9494
self.url = url
95+
self.protocol = protocol
9596

9697
async def api_get(self, endpoint, session, **kwargs):
9798
res = await session.get(self.url + "/api/v0/" + endpoint, params=kwargs, trace_request_ctx={'gateway': self.url})
@@ -106,7 +107,7 @@ async def api_post(self, endpoint, session, **kwargs):
106107
async def _cid_req(self, method, path, headers=None, **kwargs):
107108
headers = headers or {}
108109
if self.resolution == "path":
109-
res = await method(self.url + "/ipfs/" + path, trace_request_ctx={'gateway': self.url}, headers=headers, **kwargs)
110+
res = await method("/".join((self.url, self.protocol, path)), trace_request_ctx={'gateway': self.url}, headers=headers, **kwargs)
110111
elif self.resolution == "subdomain":
111112
raise NotImplementedError("subdomain resolution is not yet implemented")
112113
else:
@@ -145,17 +146,17 @@ async def get_client(**kwargs):
145146
return aiohttp.ClientSession(**kwargs)
146147

147148

148-
def gateway_from_file(gateway_path):
149+
def gateway_from_file(gateway_path, protocol="ipfs"):
149150
if gateway_path.exists():
150151
with open(gateway_path) as gw_file:
151152
ipfs_gateway = gw_file.readline().strip()
152153
logger.debug("using IPFS gateway from %s: %s", gateway_path, ipfs_gateway)
153-
return AsyncIPFSGateway(ipfs_gateway)
154+
return AsyncIPFSGateway(ipfs_gateway, protocol=protocol)
154155
return None
155156

156157

157158
@lru_cache
158-
def get_gateway():
159+
def get_gateway(protocol="ipfs"):
159160
"""
160161
Get IPFS gateway according to IPIP-280
161162
@@ -166,29 +167,29 @@ def get_gateway():
166167
ipfs_gateway = os.environ.get("IPFS_GATEWAY", "")
167168
if ipfs_gateway:
168169
logger.debug("using IPFS gateway from IPFS_GATEWAY environment variable: %s", ipfs_gateway)
169-
return AsyncIPFSGateway(ipfs_gateway)
170+
return AsyncGateway(ipfs_gateway, protocol)
170171

171172
# internal configuration: accept IPFSSPEC_GATEWAYS for backwards compatibility
172173
if ipfsspec_gateways := os.environ.get("IPFSSPEC_GATEWAYS", ""):
173174
ipfs_gateway = ipfsspec_gateways.split()[0]
174175
logger.debug("using IPFS gateway from IPFSSPEC_GATEWAYS environment variable: %s", ipfs_gateway)
175176
warnings.warn("The IPFSSPEC_GATEWAYS environment variable is deprecated, please configure your IPFS Gateway according to IPIP-280, e.g. by using the IPFS_GATEWAY environment variable or using the ~/.ipfs/gateway file.", DeprecationWarning)
176-
return AsyncIPFSGateway(ipfs_gateway)
177+
return AsyncGateway(ipfs_gateway, protocol)
177178

178179
# check various well-known files for possible gateway configurations
179180
if ipfs_path := os.environ.get("IPFS_PATH", ""):
180-
if ipfs_gateway := gateway_from_file(Path(ipfs_path) / "gateway"):
181+
if ipfs_gateway := gateway_from_file(Path(ipfs_path) / "gateway", protocol):
181182
return ipfs_gateway
182183

183184
if home := os.environ.get("HOME", ""):
184-
if ipfs_gateway := gateway_from_file(Path(home) / ".ipfs" / "gateway"):
185+
if ipfs_gateway := gateway_from_file(Path(home) / ".ipfs" / "gateway", protocol):
185186
return ipfs_gateway
186187

187188
if config_home := os.environ.get("XDG_CONFIG_HOME", ""):
188-
if ipfs_gateway := gateway_from_file(Path(config_home) / "ipfs" / "gateway"):
189+
if ipfs_gateway := gateway_from_file(Path(config_home) / "ipfs" / "gateway", protocol):
189190
return ipfs_gateway
190191

191-
if ipfs_gateway := gateway_from_file(Path("/etc") / "ipfs" / "gateway"):
192+
if ipfs_gateway := gateway_from_file(Path("/etc") / "ipfs" / "gateway", protocol):
192193
return ipfs_gateway
193194

194195
system = platform.system()
@@ -213,7 +214,7 @@ def get_gateway():
213214
candidates = []
214215

215216
for candidate in candidates:
216-
if ipfs_gateway := gateway_from_file(candidate):
217+
if ipfs_gateway := gateway_from_file(candidate, protocol):
217218
return ipfs_gateway
218219

219220
# if we reach this point, no gateway is configured
@@ -249,7 +250,7 @@ def __init__(self, asynchronous=False, loop=None, client_kwargs=None, **storage_
249250

250251
@property
251252
def gateway(self):
252-
return get_gateway()
253+
return get_gateway(self.protocol)
253254

254255
@staticmethod
255256
def close_session(loop, session):
@@ -300,3 +301,7 @@ def open(self, path, mode="rb", block_size=None, cache_options=None, **kwargs):
300301
def ukey(self, path):
301302
"""returns the CID, which is by definition an unchanging identitifer"""
302303
return self.info(path)["CID"]
304+
305+
306+
class AsyncIPNSFileSystem(AsyncIPFSFileSystem):
307+
protocol = "ipns"

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
entry_points={
3131
'fsspec.specs': [
3232
'ipfs=ipfsspec.AsyncIPFSFileSystem',
33+
'ipns=ipfsspec.AsyncIPNSFileSystem',
3334
],
3435
},
3536
)

0 commit comments

Comments
 (0)