MCPcopy Create free account
hub / github.com/celery/celery / FilesystemBackend

Class FilesystemBackend

celery/backends/filesystem.py:25–115  ·  view source on GitHub ↗

File-system result backend. Arguments: url (str): URL to the directory we should use open (Callable): open function to use when opening files unlink (Callable): unlink function to use when deleting files sep (str): directory separator (to join the directory with

Source from the content-addressed store, hash-verified

23
24
25class FilesystemBackend(KeyValueStoreBackend):
26 """File-system result backend.
27
28 Arguments:
29 url (str): URL to the directory we should use
30 open (Callable): open function to use when opening files
31 unlink (Callable): unlink function to use when deleting files
32 sep (str): directory separator (to join the directory with the key)
33 encoding (str): encoding used on the file-system
34 """
35
36 # Result files are opened in binary mode in both directions.
37 supports_result_compression = True
38
39 def __init__(self, url=None, open=open, unlink=os.unlink, sep=os.sep,
40 encoding=default_encoding, *args, **kwargs):
41 super().__init__(*args, **kwargs)
42 self.url = url
43 path = self._find_path(url)
44
45 # Remove forwarding "/" for Windows os
46 if os.name == "nt" and path.startswith("/"):
47 path = path[1:]
48
49 # We need the path and separator as bytes objects
50 self.path = path.encode(encoding)
51 self.sep = sep.encode(encoding)
52
53 self.open = open
54 self.unlink = unlink
55
56 # Let's verify that we've everything setup right
57 self._do_directory_test(b'.fs-backend-' + uuid().encode(encoding))
58
59 def __reduce__(self, args=(), kwargs=None):
60 kwargs = {} if not kwargs else kwargs
61 return super().__reduce__(args, {**kwargs, 'url': self.url})
62
63 def _find_path(self, url):
64 if not url:
65 raise ImproperlyConfigured(E_NO_PATH_SET)
66 if url.startswith('file://localhost/'):
67 return url[16:]
68 if url.startswith('file://'):
69 return url[7:]
70 raise ImproperlyConfigured(E_PATH_NON_CONFORMING_SCHEME)
71
72 def _do_directory_test(self, key):
73 try:
74 self.set(key, b'test value')
75 assert self.get(key) == b'test value'
76 self.delete(key)
77 except OSError:
78 raise ImproperlyConfigured(E_PATH_INVALID)
79
80 def _filename(self, key):
81 return self.sep.join((self.path, key))
82

Calls

no outgoing calls