-
Notifications
You must be signed in to change notification settings - Fork 149
/
client.py
287 lines (233 loc) · 10.6 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
285
286
import XRootD
from pyxrootd import client
from XRootD.responses import XRootDStatus, StatInfo, StatInfoVFS, \
LocationInfo, DirectoryList, ProtocolInfo
class Client(object):
# Doing both the fast backend and the pythonic frontend
# Moving the border of the bindings, more python-heavy
# Syntactic sugar
# There are now 2 client objects... hmmm
# This is more stable and self-documenting
# Remove keywords in libpyxrootd? Let Python handle them up here?
# I can put enums and handlers inside the actual class
# Only unmodifiable immutable objects can live here, there's no way we're
# reimplementing any methods...
"""The client class
:param url: The URL of the server to connect with
:type url: string
"""
def __init__(self, url):
self.__fs = client.FileSystem(url)
@property
def url(self):
"""The server URL object, instance of :mod:`XRootD.client.URL`"""
return self.__fs.url
def locate(self, path, flags, timeout=0, callback=None):
"""Locate a file.
:param path: path to the file to be located
:type path: string
:param flags: An `ORed` combination of :mod:`XRootD.enums.OpenFlags`
:returns: :class:`XRootD.responses.XRootDStatus` and
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, LocationInfo)
return XRootDStatus(self.__fs.locate(path, flags, timeout, callback))
status, response = self.__fs.locate(path, flags, timeout)
if response: response = LocationInfo(response)
return XRootDStatus(status), response
def deeplocate(self, path, flags, timeout=0, callback=None):
"""Locate a file, recursively locate all disk servers.
:param path: path to the file to be located
:type path: string
:param flags: An `ORed` combination of :mod:`XRootD.enums.OpenFlags`
:returns: tuple containing status and location info (see above)
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, LocationInfo)
return XRootDStatus(self.__fs.deeplocate(path, flags, timeout, callback))
status, response = self.__fs.deeplocate(path, flags, timeout)
if response: response = LocationInfo(response)
return XRootDStatus(status), response
def mv(self, source, dest, timeout=0, callback=None):
"""Move a directory or a file.
:param source: the file or directory to be moved
:type source: string
:param dest: the new name
:type dest: string
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.mv(source, dest, timeout, callback))
status, response = self.__fs.mv(source, dest, timeout)
return XRootDStatus(status), None
def query(self, querycode, arg, timeout=0, callback=None):
"""Obtain server information.
:param querycode: the query code as specified in
:mod:`XRootD.enums.QueryCode`
:param arg: query argument
:type arg: string
:returns: the query response or None if there was an error
:rtype: string
.. note::
For more information about XRootD query codes and arguments, see
`the relevant section in the protocol reference
<http://xrootd.slac.stanford.edu/doc/prod/XRdv299.htm#_Toc337053385>`_.
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.query(querycode, arg, timeout, callback))
status, response = self.__fs.query(querycode, arg, timeout)
return XRootDStatus(status), response
def truncate(self, path, size, timeout=0, callback=None):
"""Truncate a file.
:param path: path to the file to be truncated
:type path: string
:param size: file size
:type size: integer
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.truncate(path, size, timeout, callback))
status, response = self.__fs.truncate(path, size, timeout)
return XRootDStatus(status), None
def rm(self, path, timeout=0, callback=None):
"""Remove a file.
:param path: path to the file to be removed
:type path: string
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.rm(path, timeout, callback))
status, response = self.__fs.rm(path, timeout)
return XRootDStatus(status), None
def mkdir(self, path, flags=0, mode=0, timeout=0, callback=None):
"""Create a directory.
:param path: path to the directory to create
:type path: string
:param flags: An `ORed` combination of :mod:`XRootD.enums.MkDirFlags`
where the default is `MkDirFlags.NONE`
:param mode: the initial file access mode, an `ORed` combination of
:mod:`XRootD.enums.AccessMode` where the default is
`AccessMode.NONE`
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.mkdir(path, flags, mode, timeout, callback))
status, response = self.__fs.mkdir(path, flags, mode, timeout)
return XRootDStatus(status), None
def rmdir(self, path, timeout=0, callback=None):
"""Remove a directory.
:param path: path to the directory to remove
:type path: string
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.rmdir(path, timeout, callback))
status, response = self.__fs.rmdir(path, timeout)
return XRootDStatus(status), None
def chmod(self, path, mode=0, timeout=0, callback=None):
"""Change access mode on a directory or a file.
:param path: path to the file/directory to change access mode
:type path: string
:param mode: An `OR`ed` combination of :mod:`XRootD.enums.AccessMode`
where the default is `AccessMode.NONE`
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.chmod(path, mode, timeout, callback))
status, response = self.__fs.chmod(path, mode, timeout)
return XRootDStatus(status), None
def ping(self, timeout=0, callback=None):
"""Check if the server is alive.
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.ping(timeout, callback))
status, response = self.__fs.ping(timeout)
return XRootDStatus(status), None
def stat(self, path, timeout=0, callback=None):
"""Obtain status information for a path.
:param path: path to the file/directory to stat
:type path: string
:returns: tuple containing status dictionary and stat info
dictionary (see below)
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, StatInfo)
return XRootDStatus(self.__fs.stat(path, timeout, callback))
status, response = self.__fs.stat(path, timeout)
if response: response = StatInfo(response)
return XRootDStatus(status), response
def statvfs(self, path, timeout=0, callback=None):
"""Obtain status information for a Virtual File System.
:param path: path to the file/directory to stat
:type path: string
:returns: tuple containing status dictionary and statvfs info
dictionary (see below)
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, StatInfoVFS)
return XRootDStatus(self.__fs.statvfs(path, timeout, callback))
status, response = self.__fs.statvfs(path, timeout)
if response: response = StatInfoVFS(response)
return XRootDStatus(status), response
def protocol(self, timeout=0, callback=None):
"""Obtain server protocol information.
:returns: tuple containing status dictionary and protocol info
dictionary (see below)
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, ProtocolInfo)
return XRootDStatus(self.__fs.protocol(timeout, callback))
status, response = self.__fs.protocol(timeout)
if response: response = ProtocolInfo(response)
return XRootDStatus(status), response
def dirlist(self, path, flags=0, timeout=0, callback=None):
"""List entries of a directory.
:param path: path to the directory to list
:type path: string
:param flags: An `ORed` combination of :mod:`XRootD.enums.DirListFlags`
where the default is `DirListFlags.NONE`
:returns: tuple containing status dictionary and directory
list info dictionary (see below)
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, DirectoryList)
return XRootDStatus(self.__fs.dirlist(path, flags, timeout, callback))
status, response = self.__fs.dirlist(path, flags, timeout)
if response: response = DirectoryList(response)
return XRootDStatus(status), response
def sendinfo(self, info, timeout=0, callback=None):
"""Send info to the server (up to 1024 characters).
:param info: the info string to be sent
:type info: string
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.sendinfo(info, timeout, callback))
status, response = self.__fs.sendinfo(info, timeout)
return XRootDStatus(status), response
def prepare(self, files, flags, priority=0, timeout=0, callback=None):
"""Prepare one or more files for access.
:param files: list of files to be prepared
:type files: list
:param flags: An `ORed` combination of
:mod:`XRootD.enums.PrepareFlags`
:param priority: priority of the request 0 (lowest) - 3 (highest)
:type priority: integer
:returns: tuple containing status dictionary and None
"""
if callback:
callback = XRootD.client.CallbackWrapper(callback, None)
return XRootDStatus(self.__fs.prepare(files, flags, priority, timeout,
callback))
status, response = self.__fs.prepare(files, flags, priority, timeout)
return XRootDStatus(status), response