Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register scheme "p9" in fsspec #4

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ fs = p9fs.P9FileSystem(
host='127.0.0.1',
port=564,
username='nobody',
version='9P2000.L',
)

print(fs.ls('.'))
```

```python
import fsspec

with fsspec.open('p9://[email protected]:564/folder/data.csv?version=9P2000.L') as f:
data = f.read()
```

## TODO

* `auth`
Expand Down
5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "p9fs"
version = "0.0.2"
version = "0.0.3"
description = "9P implementation of Python fsspec"
license = {file = "LICENSE"}
readme = "README.md"
Expand Down Expand Up @@ -28,6 +28,9 @@ tests = [
"pytest",
]

[project.entry-points."fsspec.specs"]
p9 = "p9fs.P9FileSystem"

[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
Expand Down
28 changes: 24 additions & 4 deletions src/p9fs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Optional, List, Dict, Union

import fsspec.spec
from fsspec import utils

from py9p import fid
from py9p import py9p
Expand Down Expand Up @@ -45,18 +46,18 @@ class P9FileSystem(fsspec.AbstractFileSystem):

host: str
port: int
username: Optional[str]
username: str
password: Optional[str]
version: Version
verbose: bool

def __init__(
self,
host: str,
port: int = py9p.PORT,
username: Optional[str] = None,
port: int,
username: str,
password: Optional[str] = None,
version: Union[str, Version] = Version.v9P2000,
version: Union[str, Version] = Version.v9P2000L,
verbose: bool = False,
**kwargs,
):
Expand All @@ -69,6 +70,7 @@ def __init__(
password: 9P password
version: one of '9P2000', '9P2000.u', '9P2000.L', or Version
"""
super().__init__(**kwargs)
self.host = host
self.port = port
self.username = username
Expand All @@ -83,6 +85,24 @@ def __init__(
super().__init__(**kwargs)
self._connect()

@classmethod
def _strip_protocol(cls, path):
path = utils.infer_storage_options(path)["path"]
return path.lstrip('/')

@staticmethod
def _get_kwargs_from_urls(path):
# Example: 9p://nobody@host:port/directory/file.csv
options = utils.infer_storage_options(path)
options.pop('path', None)
options.pop('protocol', None)
url_query = options.pop('url_query')
if url_query:
for item in url_query.split('&'):
key, value = item.split('=')
options[key] = value
return options

def _connect(self):
s = socket.socket(socket.AF_INET)
s.connect((self.host, self.port))
Expand Down
8 changes: 8 additions & 0 deletions tests/user/test_p9fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import pathlib

import fsspec
import pytest

import p9fs
Expand Down Expand Up @@ -152,3 +153,10 @@ def test_copy(fs, exported_path):

fs.copy('test_copy/yyy', '.')
assert fs.isfile('yyy')


def test_registration(fs, exported_path):
url = f'p9://{fs.username}@{fs.host}:{fs.port}/xxx?version={fs.version.value}'
with fsspec.open(url, 'r') as f:
data = f.read()
assert data == 'This is a test content'