|
1 | 1 | import ast |
2 | 2 | import atexit |
| 3 | +import contextlib |
3 | 4 | import copy |
4 | 5 | import errno |
5 | 6 | import json |
@@ -69,6 +70,16 @@ class NonAnonymousLoginWithoutPassword(RuntimeError): |
69 | 70 | pass |
70 | 71 |
|
71 | 72 |
|
| 73 | +@contextlib.contextmanager |
| 74 | +def attr_changed(obj, attrname, value): |
| 75 | + old = getattr(obj, attrname, None) |
| 76 | + try: |
| 77 | + setattr(obj, attrname, value) |
| 78 | + yield |
| 79 | + finally: |
| 80 | + setattr(obj, attrname, old) |
| 81 | + |
| 82 | + |
72 | 83 | class iRODSSession: |
73 | 84 |
|
74 | 85 | def library_features(self): |
@@ -354,11 +365,36 @@ def port(self): |
354 | 365 | return self.pool.account.port |
355 | 366 |
|
356 | 367 | @property |
357 | | - def server_version(self): |
| 368 | + def server_version(self): return self._server_version() |
| 369 | + |
| 370 | + def raw_server_version(self): |
| 371 | + """Returns the same version tuple as iRODSSession's server_version property, but |
| 372 | + does not require successful authentication. |
| 373 | + """ |
| 374 | + import irods.connection |
| 375 | + with self.clone().pool.no_auto_authenticate() as pool: |
| 376 | + return irods.connection.Connection( |
| 377 | + pool, pool.account |
| 378 | + ).server_version |
| 379 | + |
| 380 | + RAW_SERVER_VERSION = staticmethod(lambda s:s.raw_server_version()) |
| 381 | + |
| 382 | + def _server_version(self, version_func = None): |
| 383 | + """The server version can be retrieved by the usage |
| 384 | + session._server_version() |
| 385 | + with conditional substitution by another version by use of the environment variable: |
| 386 | + PYTHON_IRODSCLIENT_REPORTED_SERVER_VERSION. |
| 387 | +
|
| 388 | + Also: if iRODSServer.RAW_SERVER_VERSION is passed in version_func, the true server |
| 389 | + version can be accessed without first going through authentication. |
| 390 | + Example: |
| 391 | + ses = irods.helpers.make_session() |
| 392 | + vsn = ses._server_version( ses.RAW_SERVER_VERSION ) |
| 393 | + """ |
358 | 394 | reported_vsn = os.environ.get("PYTHON_IRODSCLIENT_REPORTED_SERVER_VERSION", "") |
359 | 395 | if reported_vsn: |
360 | 396 | return tuple(ast.literal_eval(reported_vsn)) |
361 | | - return self.__server_version() |
| 397 | + return self.__server_version() if version_func is None else version_func(self) |
362 | 398 |
|
363 | 399 | def __server_version(self): |
364 | 400 | try: |
|
0 commit comments