-
Notifications
You must be signed in to change notification settings - Fork 409
localmemory: init #1905
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
Closed
Closed
localmemory: init #1905
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
from fsspec.implementations.memory import MemoryFileSystem | ||
|
||
logger = logging.getLogger("fsspec.localmemoryfs") | ||
|
||
|
||
class LocalMemoryFileSystem(MemoryFileSystem): | ||
"""A filesystem based on a dict of BytesIO objects | ||
This is a local filesystem so different instances of this class | ||
point to different memory filesystems. | ||
""" | ||
|
||
pseudo_dirs = None | ||
protocol = "localmemory" | ||
root_marker = "/" | ||
_intrans = False | ||
cachable = False # same as: skip_instance_cache = True | ||
|
||
def __init__(self, *args, **kwargs): | ||
self.logger = logger # global | ||
|
||
self.store = {} # local | ||
self.pseudo_dirs = [""] # local | ||
super().__init__(*args, **kwargs) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,11 @@ class MemoryFileSystem(AbstractFileSystem): | |||||||
pseudo_dirs = [""] # global, do not overwrite! | ||||||||
protocol = "memory" | ||||||||
root_marker = "/" | ||||||||
_intrans = False | ||||||||
|
||||||||
def __init__(self, *args, **kwargs): | ||||||||
self.logger = logger | ||||||||
super().__init__(*args, **kwargs) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
@classmethod | ||||||||
def _strip_protocol(cls, path): | ||||||||
|
@@ -147,7 +152,7 @@ def rmdir(self, path): | |||||||
raise FileNotFoundError(path) | ||||||||
|
||||||||
def info(self, path, **kwargs): | ||||||||
logger.debug("info: %s", path) | ||||||||
self.logger.debug("info: %s", path) | ||||||||
path = self._strip_protocol(path) | ||||||||
if path in self.pseudo_dirs or any( | ||||||||
p.startswith(path + "/") for p in list(self.store) + self.pseudo_dirs | ||||||||
|
@@ -202,7 +207,7 @@ def _open( | |||||||
elif mode in {"wb", "xb"}: | ||||||||
if mode == "xb" and self.exists(path): | ||||||||
raise FileExistsError | ||||||||
m = MemoryFile(self, path, kwargs.get("data")) | ||||||||
m = MemoryFile(self, path, kwargs.get("data"), self.logger) | ||||||||
if not self._intrans: | ||||||||
m.commit() | ||||||||
return m | ||||||||
|
@@ -215,7 +220,7 @@ def cp_file(self, path1, path2, **kwargs): | |||||||
path2 = self._strip_protocol(path2) | ||||||||
if self.isfile(path1): | ||||||||
self.store[path2] = MemoryFile( | ||||||||
self, path2, self.store[path1].getvalue() | ||||||||
self, path2, self.store[path1].getvalue(), self.logger | ||||||||
) # implicit copy | ||||||||
elif self.isdir(path1): | ||||||||
if path2 not in self.pseudo_dirs: | ||||||||
|
@@ -224,7 +229,7 @@ def cp_file(self, path1, path2, **kwargs): | |||||||
raise FileNotFoundError(path1) | ||||||||
|
||||||||
def cat_file(self, path, start=None, end=None, **kwargs): | ||||||||
logger.debug("cat: %s", path) | ||||||||
self.logger.debug("cat: %s", path) | ||||||||
path = self._strip_protocol(path) | ||||||||
try: | ||||||||
return bytes(self.store[path].getbuffer()[start:end]) | ||||||||
|
@@ -283,8 +288,9 @@ class MemoryFile(BytesIO): | |||||||
No need to provide fs, path if auto-committing (default) | ||||||||
""" | ||||||||
|
||||||||
def __init__(self, fs=None, path=None, data=None): | ||||||||
logger.debug("open file %s", path) | ||||||||
def __init__(self, fs=None, path=None, data=None, logger=logger): | ||||||||
self.logger = logger | ||||||||
self.logger.debug("open file %s", path) | ||||||||
self.fs = fs | ||||||||
self.path = path | ||||||||
self.created = datetime.now(tz=timezone.utc) | ||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import fsspec | ||
from fsspec.implementations.localmemory import LocalMemoryFileSystem | ||
|
||
|
||
def test_protocol(): | ||
# this should not throw: ValueError: Protocol not known: localmemory | ||
fsspec.filesystem("localmemory") | ||
|
||
|
||
def test_init(): | ||
fs1 = LocalMemoryFileSystem() | ||
fs2 = LocalMemoryFileSystem() | ||
|
||
# check that fs1 and fs2 are different instances of LocalMemoryFileSystem | ||
assert id(fs1) != id(fs2) | ||
assert id(fs1.store) != id(fs2.store) | ||
assert id(fs1.pseudo_dirs) != id(fs2.pseudo_dirs) | ||
|
||
fs1.touch("/fs1.txt") | ||
fs2.touch("/fs2.txt") | ||
assert fs1.ls("/", detail=False) == ["/fs1.txt"] | ||
assert fs2.ls("/", detail=False) == ["/fs2.txt"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why set the class variable even if none of the instances see it?