/
clients.py
executable file
·1324 lines (1130 loc) · 44.9 KB
/
clients.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Copyright 2009 - 2016 Glencoe Software, Inc. All rights reserved.
Use is subject to license terms supplied in LICENSE.txt
"""
from __future__ import print_function
from builtins import zip
from builtins import range
from builtins import object
from future.utils import bytes_to_native_str
from future.utils import native_str
__save__ = __name__
__name__ = 'omero'
try:
api = __import__('omero.api')
model = __import__('omero.model')
util = __import__('omero.util')
sys = __import__('omero.sys')
import omero.all
finally:
__name__ = __save__
del __save__
sys = __import__("sys")
import threading
import logging
import IceImport
import Ice
import re
import ssl
import uuid
from past.builtins import basestring
IceImport.load("Glacier2_Router_ice")
import Glacier2
if sys.version_info >= (3, 0, 0):
# Keep str behavior on Python 2
from builtins import str
class BaseClient(object):
"""
Central client-side blitz entry point, and should be in sync with
OmeroJava's omero.client and OmeroCpp's omero::client.
Typical usage includes::
# Uses --Ice.Config argument or ICE_CONFIG variable
client = omero.client()
# Defines "omero.host"
client = omero.client(host = host)
# Defines "omero.host" and "omero.port"
client = omero.client(host = host, port = port)
"""
def __init__(self, args=None, id=None, host=None, port=None, pmap=None):
"""
Constructor which takes one sys.argv-style list, one initialization
data, one host string, one port integer, and one properties map, in
that order. *However*, to simplify use, we reassign values based on
their type with a warning printed. A cleaner approach is to use named
parameters.
::
c1 = omero.client(None, None, "host", myPort) # Correct
c2 = omero.client(host = "host", port = myPort) # Correct
# Works with warning
c3 = omero.client("host", myPort)
Both "Ice" and "omero" prefixed properties will be parsed.
Defines the state variables::
__previous : InitializationData from any previous communicator, if
any. Used to re-initialization the client
post-closeSession()
__ic : communicator. Nullness => init() needed on
createSession()
__sf : current session. Nullness => createSession() needed.
__resources: if non-null, hs access to this client instance and
will periodically call sf.keepAlive(None) in order to
keep any session alive. This can be enabled either
via the omero.keep_alive configuration property, or
by calling the enableKeepAlive() method.
Once enabled, the period cannot be adjusted during a
single session.
Modifying these variables outside of the accessors can lead to
undefined behavior.
Equivalent to all OmeroJava and OmeroCpp constructors
"""
args, id, host, port, pmap = self._repair(args, id, host, port, pmap)
# Setting all protected values to prevent AttributeError
self.__agent = "OMERO.py" #: See setAgent
self.__ip = None #: See setIP
self.__insecure = False
self.__previous = None
self.__ic = None
self.__oa = None
self.__cb = None
self.__sf = None
self.__uuid = None
self.__resources = None
self.__lock = threading.RLock()
# Logging
self.__logger = logging.getLogger("omero.client")
logging.basicConfig() # Does nothing if already configured
# Reassigning based on argument type
args, id, host, port, pmap = self._repair(args, id, host, port, pmap)
if not args:
args = []
# hosturl overrides all other args
hosturl = self._check_for_hosturl(host, port, pmap)
if hosturl:
# omero.clients does a lot of mysterious magic autodetection.
# If host is set it overrides the endpoint so instead set host to
# None and store the host in a separate property omero.url.host
host = None
port = hosturl['port']
args.append(self._get_endpoint_from_hosturl(hosturl))
# Copying args since we don't really want them edited
if args:
# See ticket:5516 To prevent issues on systems where the base
# class of path.path is unicode, we will encode all unicode
# strings here.
args = [arg.encode("utf-8") if isinstance(arg, str)
else arg for arg in args]
args = [bytes_to_native_str(x) for x in args]
# Under Python 2 this can still leave us with 'unicode'
args = [native_str(x) for x in args]
# Equiv to multiple constructors. #######################
if id is None:
id = Ice.InitializationData()
if id.properties is None:
id.properties = Ice.createProperties(args)
id.properties.parseCommandLineOptions("omero", args)
if host:
id.properties.setProperty("omero.host", str(host))
if hosturl:
id.properties.setProperty("omero.url.host", hosturl['server'])
if not port:
port = id.properties.getPropertyWithDefault(
"omero.port", str(omero.constants.GLACIER2PORT))
id.properties.setProperty("omero.port", str(port))
if pmap:
for k, v in list(pmap.items()):
id.properties.setProperty(str(k), str(v))
self._initData(id)
def _repair(self, args, id, host, port, pmap):
"""
Takes the 5 arguments passed to the __init__ method
and attempts to re-order them based on their types.
This allows for simplified usage without parameter
names.
"""
types = [list, Ice.InitializationData, basestring, int, dict]
original = [args, id, host, port, pmap]
repaired = [None, None, None, None, None]
# Check all to see if valid
valid = True
for i in range(0, len(types)):
if None != original[i] and not isinstance(original[i], types[i]):
valid = False
break
if valid:
return original
# Now try to find corrections.
for i in range(0, len(types)):
found = None
for j in range(0, len(types)):
if isinstance(original[j], types[i]):
if not found:
found = original[j]
else:
raise omero.ClientError(
"Found two arguments of same type: " +
str(types[i]))
if found:
repaired[i] = found
return repaired
def _initData(self, id):
"""
Initializes the current client via an Ice.InitializationData
instance. This is called by all of the constructors, but may
also be called on createSession(name, pass) if a previous
call to closeSession() has nulled the Ice.Communicator.
"""
if not id:
raise omero.ClientError("No initialization data provided.")
# Strictly necessary for this class to work
self._optSetProp(id, "Ice.ImplicitContext", "Shared")
self._optSetProp(id, "Ice.ACM.Client.Timeout",
str(omero.constants.ACMCLIENTTIMEOUT))
self._optSetProp(id, "Ice.ACM.Client.Heartbeat",
str(omero.constants.ACMCLIENTHEARTBEAT))
self._optSetProp(id, "Ice.CacheMessageBuffers", "0")
self._optSetProp(id, "Ice.RetryIntervals", "-1")
self._optSetProp(id, "Ice.Default.EndpointSelection", "Ordered")
self._optSetProp(id, "Ice.Default.PreferSecure", "1")
self._optSetProp(id, "Ice.Plugin.IceSSL", "IceSSL:createIceSSL")
prop = "IceSSL.Ciphers"
try:
if sys.platform == "darwin":
self._optSetProp(id, prop, "(AES_256) (DH_anon.*AES)")
elif ssl.OPENSSL_VERSION_INFO >= (1, 1):
self._optSetProp(id, prop, "HIGH:ADH:@SECLEVEL=0")
else:
self._optSetProp(id, prop, "HIGH:ADH")
except Exception:
# OPENSSL_VERSION_INFO not available for 2.6, fall back to default
self._optSetProp(id, prop, "HIGH:ADH")
self._optSetProp(id, "IceSSL.VerifyDepthMax", "6")
self._optSetProp(id, "IceSSL.VerifyPeer", "0")
self._optSetProp(id, "IceSSL.Protocols", "tls1_0,tls1_1,tls1_2")
# Setting block size
self._optSetProp(
id, "omero.block_size", str(omero.constants.DEFAULTBLOCKSIZE))
# Setting MessageSizeMax
self._optSetProp(
id, "Ice.MessageSizeMax", str(omero.constants.MESSAGESIZEMAX))
# Setting ConnectTimeout
self.parseAndSetInt(id, "Ice.Override.ConnectTimeout",
omero.constants.CONNECTTIMEOUT)
# Set large thread pool max values for all communicators
for x in ("Client", "Server"):
sizemax = id.properties.getProperty(
"Ice.ThreadPool.%s.SizeMax" % x)
if not sizemax or len(sizemax) == 0:
id.properties.setProperty(
"Ice.ThreadPool.%s.SizeMax" % x, "50")
# Port, setting to default if not present
port = self.parseAndSetInt(id, "omero.port",
omero.constants.GLACIER2PORT)
# Default Router, set a default and then replace
router = id.properties.getProperty("Ice.Default.Router")
if not router or len(router) == 0:
router = str(omero.constants.DEFAULTROUTER)
host = id.properties.getPropertyWithDefault(
"omero.host", """<"omero.host" not set>""")
router = router.replace("@omero.port@", str(port))
router = router.replace("@omero.host@", str(host))
id.properties.setProperty("Ice.Default.Router", router)
# Dump properties
dump = id.properties.getProperty("omero.dump")
if len(dump) > 0:
m = self.getPropertyMap(id.properties)
keys = list(m.keys())
keys.sort()
for key in keys:
print("%s=%s" % (key, m[key]))
self.__lock.acquire()
try:
if self.__ic:
raise omero.ClientError("Client already initialized")
try:
self.__ic = Ice.initialize(id)
except Ice.EndpointParseException:
msg = "No host specified. "
msg += "Use omero.client(HOSTNAME), ICE_CONFIG, or similar."
raise omero.ClientError(msg)
if not self.__ic:
raise omero.ClientError("Improper initialization")
# Register Object Factory
try:
import ObjectFactoryRegistrar as ofr
except ImportError:
from . import ObjectFactoryRegistrar as ofr
ofr.registerObjectFactory(self.__ic, self)
for of in list(omero.rtypes.ObjectFactories.values()):
of.register(self.__ic)
# Define our unique identifier (used during close/detach)
self.__uuid = str(uuid.uuid4())
ctx = self.__ic.getImplicitContext()
if not ctx:
raise omero.ClientError(
"Ice.ImplicitContext not set to Shared")
ctx.put(omero.constants.CLIENTUUID, self.__uuid)
# ticket:2951 - sending user group
group = id.properties.getPropertyWithDefault("omero.group", "")
if group:
ctx.put("omero.group", group)
finally:
self.__lock.release()
def _check_for_hosturl(self, host, port, pmap):
"""
Checks whether the host is a URL, returns a dict of parameters if so
"""
# omero/util/sessions.py may initialise this class with a property-map
if not host and pmap and 'omero.host' in pmap:
host = pmap['omero.host']
if not port and pmap and 'omero.port' in pmap:
port = pmap['omero.port']
if not host:
return {}
hostmatch = re.match(
r'(?P<protocol>\w+)://'
r'(?P<server>[^:/]+)'
r'(:(?P<port>\d+))?'
r'(?P<path>/.*)?$',
host)
if hostmatch:
hosturl = hostmatch.groupdict()
if not hosturl['port']:
hosturl['port'] = port
if not hosturl['port']:
default_ports = {
'ws': 80,
'wss': 443,
'tcp': 4063,
'ssl': 4064,
}
try:
hosturl['port'] = default_ports[hosturl['protocol']]
except KeyError:
raise omero.ClientError(
"Port required for protocol: " + hosturl['protocol'])
hosturl['port'] = int(hosturl['port'])
else:
hosturl = {}
return hosturl
def _get_endpoint_from_hosturl(self, hosturl):
"""
Gets Ice.Default.Router from a dictionary of hosturl parameters
"""
ice_router = (
'--Ice.Default.Router=OMERO.Glacier2/router:{protocol} '
'-p {port} '
'-h {server}'.format(**hosturl))
if hosturl['path']:
ice_router += ' -r {path}'.format(**hosturl)
return ice_router
def setAgent(self, agent):
"""
Sets the omero.model.Session#getUserAgent() string for
this client. Every session creation will be passed this argument.
Finding open sessions with the same agent can be done via
omero.api.ISessionPrx#getMyOpenAgentSessions(String).
"""
self.__agent = agent
def setIP(self, ip):
"""
Sets the omero.model.Session#getUserIP() string for
this client. Every session creation will be passed this argument.
Finding open sessions with the same IP can be done via
omero.api.ISessionPrx#getMyOpenIPSessions(ip).
"""
self.__ip = ip
def isSecure(self):
"""
Specifies whether or not this client was created via a call to
createClient with a boolean of False. If insecure, then all
remote calls will use the insecure connection defined by the server.
"""
return not self.__insecure
def createClient(self, secure):
"""
Creates a possibly insecure omero.client instance and calls
joinSession using the current getSessionId value. If secure is False,
then first the "omero.router.insecure" configuration property is
retrieved from the server and used as the value of
"Ice.Default.Router" for the new client. Any exception thrown during
creation is passed on to the caller.
Note: detachOnDestroy has NOT been called on the session in the
returned client.
Clients are responsible for doing this immediately if such desired.
"""
props = self.getPropertyMap()
if not secure:
insecure = self.getSession().getConfigService().getConfigValue(
"omero.router.insecure")
if insecure is not None and insecure != "":
# insecure still has @omero.host@, so we need to substitute it
router = self.getRouter(self.getCommunicator())
if router is not None:
for endpoint in router.ice_getEndpoints():
host = endpoint.getInfo().host
if host != "":
insecure = insecure.replace("@omero.host@", str(host))
props["Ice.Default.Router"] = insecure
else:
self.__logger.warn(
"Could not retrieve \"omero.router.insecure\"")
nClient = omero.client(props)
nClient.__insecure = not secure
nClient.setAgent("%s;secure=%s" % (self.__agent, secure))
nClient.joinSession(self.getSessionId())
return nClient
def __del__(self):
"""
Calls closeSession() and ignores any exceptions.
Equivalent to close() in OmeroJava or omero::client::~client()
"""
try:
self.closeSession()
except Exception as e:
# It is perfectly normal for the session to have been closed
# before garbage collection
# though for some reason I can't match this exception with the
# Glacier2.SessionNotExistException class.
# Using str matching instead.
if 'Glacier2.SessionNotExistException' not in str(e.__class__):
self.__logger.warning(
"..Ignoring error in client.__del__:" + str(e.__class__))
def getCommunicator(self):
"""
Returns the Ice.Communicator for this instance or throws
an exception if None.
"""
self.__lock.acquire()
try:
if not self.__ic:
raise omero.ClientError(
"No Ice.Communicator active; call createSession() "
"or create a new client instance")
return self.__ic
finally:
self.__lock.release()
def getAdapter(self):
"""
Returns the Ice.ObjectAdapter for this instance or throws
an exception if None.
"""
self.__lock.acquire()
try:
if not self.__oa:
raise omero.ClientError(
"No Ice.ObjectAdapter active; call createSession() "
"or create a new client instance")
return self.__oa
finally:
self.__lock.release()
def getSession(self, blocking=True):
"""
Returns the current active session or throws an exception if none has
been created since the last closeSession()
If blocking is False, then self.__lock is not acquired and the value
of self.__sf is simply returned. Clients must properly handle the
situation where this value is None.
"""
if not blocking:
return self.__sf
self.__lock.acquire(blocking)
try:
sf = self.__sf
if not sf:
raise omero.ClientError("No session available")
return sf
finally:
self.__lock.release()
def getSessionId(self):
"""
Returns the UUID for the current session without making a remote call.
Uses getSession() internally and will throw an exception if no session
is active.
"""
return self.getSession().ice_getIdentity().name
def getCategory(self):
"""
Returns the category which should be used for all callbacks
passed to the server.
"""
return self.getRouter(self.__ic).getCategoryForClient()
def getImplicitContext(self):
"""
Returns the Ice.ImplicitContext which defines what properties
will be sent on every method invocation.
"""
return self.getCommunicator().getImplicitContext()
def getContext(self, group=None):
"""
Returns a copy of the implicit context's context, i.e.
dict(getImplicitContext().getContext()) for use as the
last argument to any remote method.
"""
ctx = self.getImplicitContext().getContext()
ctx = dict(ctx)
if group is not None:
ctx["omero.group"] = str(group)
return ctx
def getProperties(self):
"""
Returns the active properties for this instance
"""
self.__lock.acquire()
try:
return self.__ic.getProperties()
finally:
self.__lock.release()
def getProperty(self, key):
"""
Returns the property for the given key or "" if none present
"""
return self.getProperties().getProperty(key)
def getPropertyMap(self, properties=None):
"""
Returns all properties which are prefixed with "omero." or "Ice."
"""
if properties is None:
properties = self.getProperties()
rv = dict()
for prefix in ["omero", "Ice"]:
for k, v in list(properties.getPropertiesForPrefix(prefix).items()):
rv[k] = v
return rv
def getDefaultBlockSize(self):
"""
Returns the user-configured "omero.block_size" property or
omero.constants.DEFAULTBLOCKSIZE if none is set.
"""
try:
return int(self.getProperty("omero.block_size"))
except:
return omero.constants.DEFAULTBLOCKSIZE
def joinSession(self, session):
"""
Uses the given session uuid as name
and password to rejoin a running session
"""
return self.createSession(session, session)
def createSession(self, username=None, password=None):
"""
Performs the actual logic of logging in, which is done via the
getRouter(). Disallows an extant ServiceFactoryPrx, and
tries to re-create a null Ice.Communicator. A null or empty
username will throw an exception, but an empty password is allowed.
"""
import omero
self.__lock.acquire()
try:
# Checking state
if self.__sf:
raise omero.ClientError(
"Session already active. "
"Create a new omero.client or closeSession()")
if not self.__ic:
if not self.__previous:
raise omero.ClientError(
"No previous data to recreate communicator.")
self._initData(self.__previous)
self.__previous = None
# Check the required properties
if not username:
username = self.getProperty("omero.user")
elif isinstance(username, omero.RString):
username = username.val
if not username or len(username) == 0:
raise omero.ClientError("No username specified")
if not password:
password = self.getProperty("omero.pass")
elif isinstance(password, omero.RString):
password = password.val
if not password:
raise omero.ClientError("No password specified")
# Acquire router and get the proxy
prx = None
retries = 0
while retries < 3:
reason = None
if retries > 0:
self.__logger.warning(
"%s - createSession retry: %s" % (reason, retries))
try:
ctx = self.getContext()
ctx[omero.constants.AGENT] = self.__agent
if self.__ip is not None:
ctx[omero.constants.IP] = self.__ip
rtr = self.getRouter(self.__ic)
prx = rtr.createSession(username, password, ctx)
# Create the adapter
self.__oa = self.__ic.createObjectAdapterWithRouter(
"omero.ClientCallback", rtr)
self.__oa.activate()
id = Ice.Identity()
id.name = self.__uuid
id.category = rtr.getCategoryForClient()
self.__cb = BaseClient.CallbackI(self.__ic, self.__oa, id)
self.__oa.add(self.__cb, id)
break
except omero.WrappedCreateSessionException as wrapped:
if not wrapped.concurrency:
raise wrapped # We only retry concurrency issues.
reason = "%s:%s" % (wrapped.type, wrapped.reason)
retries = retries + 1
except Ice.ConnectTimeoutException as cte:
reason = "Ice.ConnectTimeoutException:%s" % str(cte)
retries = retries + 1
if not prx:
raise omero.ClientError("Obtained null object prox")
# Check type
self.__sf = omero.api.ServiceFactoryPrx.uncheckedCast(prx)
if not self.__sf:
raise omero.ClientError(
"Obtained object proxy is not a ServiceFactory")
# Configure keep alive
self.startKeepAlive()
# Set the client callback on the session
# and pass it to icestorm
try:
raw = self.__oa.createProxy(self.__cb.id)
self.__sf.setCallback(
omero.api.ClientCallbackPrx.uncheckedCast(raw))
# self.__sf.subscribe("/public/HeartBeat", raw)
except:
self.__del__()
raise
# Set the session uuid in the implicit context
self.getImplicitContext().put(
omero.constants.SESSIONUUID, self.getSessionId())
return self.__sf
finally:
self.__lock.release()
def enableKeepAlive(self, seconds):
"""
Resets the "omero.keep_alive" property on the current
Ice.Communicator which is used on initialization to determine
the time-period between Resource checks. The __resources
instance will be created as soon as an active session is
detected.
"""
self.__lock.acquire()
try:
# A communicator must be configured!
ic = self.getCommunicator()
# Setting this here guarantees that after closeSession()
# the next createSession() will use the new value despite
# what was in the configuration file
ic.getProperties().setProperty("omero.keep_alive", str(seconds))
# If there's not a session, there should be no
# __resources but just in case since startKeepAlive
# could have been called manually.
if seconds <= 0:
self.stopKeepAlive()
else:
try:
# If there's a session, then go ahead and
# start the keep alive.
self.getSession()
self.startKeepAlive()
except omero.ClientError:
pass
finally:
self.__lock.release()
def startKeepAlive(self):
"""
Start a new __resources instance, stopping any that current exists
IF omero.keep_alive is greater than 1.
"""
self.__lock.acquire()
try:
ic = self.getCommunicator()
props = ic.getProperties()
seconds = -1
try:
seconds = props.getPropertyWithDefault(
"omero.keep_alive", "-1")
seconds = int(seconds)
except ValueError:
pass
# Any existing resource should be shutdown.
if self.__resources is not None:
self.stopKeepAlive()
# If seconds is more than 0, a new one should be started.
if seconds > 0:
self.__resources = omero.util.Resources(seconds)
class Entry(object):
def __init__(self, c):
self.c = c
def cleanup(self):
pass
def check(self):
sf = self.c._BaseClient__sf
ic = self.c._BaseClient__ic
if sf is not None:
try:
sf.keepAlive(None)
except Exception:
if ic is not None:
ic.getLogger().warning(
"Proxy keep alive failed.")
return False
return True
self.__resources.add(Entry(self))
finally:
self.__lock.release()
def stopKeepAlive(self):
self.__lock.acquire()
try:
if self.__resources is not None:
try:
self.__resources.cleanup()
finally:
self.__resources = None
finally:
self.__lock.release()
def getManagedRepository(self, description=False):
repos = self.getSession().sharedResources().repositories()
repoMap = list(zip(repos.proxies, repos.descriptions))
prx = None
for (prx, desc) in repoMap:
if not prx:
continue
prx = omero.grid.ManagedRepositoryPrx.checkedCast(prx)
if prx:
break
if description:
return(prx, desc)
else:
return prx
def getRouter(self, comm):
"""
Acquires the default router, and throws an exception
if it is not of type Glacier2.Router. Also sets the
Ice.ImplicitContext on the router proxy.
"""
prx = comm.getDefaultRouter()
if not prx:
raise omero.ClientError("No default router found.")
router = Glacier2.RouterPrx.uncheckedCast(prx)
if not router:
raise omero.ClientError("Error obtaining Glacier2 router")
# For whatever reason, we have to set the context
# on the router context here as well
router = router.ice_context(comm.getImplicitContext().getContext())
return router
def sha1(self, filename):
"""
Calculates the local sha1 for a file.
"""
try:
from hashlib import sha1 as sha_new
except ImportError:
from sha import new as sha_new
digest = sha_new()
file = open(filename, 'rb')
try:
while True:
block = file.read(1024)
if not block:
break
digest.update(block)
finally:
file.close()
return digest.hexdigest()
def upload(self, filename, name=None, path=None, type=None, ofile=None,
block_size=1024):
"""
Utility method to upload a file to the server.
"""
if not self.__sf:
raise omero.ClientError("No session. Use createSession first.")
import os
import types
if not filename or not isinstance(filename, basestring):
raise omero.ClientError("Non-null filename must be provided")
if not os.path.exists(filename):
raise omero.ClientError("File does not exist: " + filename)
from omero_ext.path import path as __path__
filepath = __path__(filename)
file = open(filename, 'rb')
try:
size = os.path.getsize(file.name)
if block_size > size:
block_size = size
if not ofile:
ofile = omero.model.OriginalFileI()
ofile.hash = omero.rtypes.rstring(self.sha1(file.name))
ofile.hasher = omero.model.ChecksumAlgorithmI()
ofile.hasher.value = omero.rtypes.rstring("SHA1-160")
abspath = filepath.normpath().abspath()
if not ofile.name:
if name:
ofile.name = omero.rtypes.rstring(name)
else:
ofile.name = omero.rtypes.rstring(str(abspath.basename()))
if not ofile.path:
ofile.path = omero.rtypes.rstring(
str(abspath.dirname())+os.path.sep)
if not ofile.mimetype:
if type:
# ofile.mimetype = 'application/octet-stream' by default
ofile.mimetype = omero.rtypes.rstring(type)
# Disabled with group permissions #1434
# if permissions:
# ofile.details.permissions = permissions
up = self.__sf.getUpdateService()
ofile = up.saveAndReturnObject(ofile)
prx = self.__sf.createRawFileStore()
try:
prx.setFileId(ofile.id.val)
prx.truncate(size) # ticket:2337
self.write_stream(file, prx, block_size)
finally:
prx.close()
finally:
file.close()
return ofile
def write_stream(self, file, prx, block_size=1024*1024):
offset = 0
while True:
block = file.read(block_size)
if not block:
break
prx.write(block, offset, len(block))
offset += len(block)
def download(self, ofile, filename=None, block_size=1024*1024,
filehandle=None):
if not self.__sf:
raise omero.ClientError("No session. Use createSession first.")
# Search for objects in all groups. See #12146
ctx = self.getContext(group=-1)
prx = self.__sf.createRawFileStore()
try:
if not ofile or not ofile.id:
raise omero.ClientError("No file to download")
ofile = self.__sf.getQueryService().get(
"OriginalFile", ofile.id.val, ctx)
prx.setFileId(ofile.id.val, ctx)
size = None
if prx.size() is None:
name = omero.rtypes.unwrap(ofile.name)
mimetype = omero.rtypes.unwrap(ofile.mimetype)
raise omero.ClientError(
("invalid size for OriginalFile '%s' "
"(mimetype:%s)") % (name, mimetype))
else:
size = prx.size()
if block_size > size:
block_size = size
if filehandle is None:
if filename is None:
raise omero.ClientError(
"no filename or filehandle specified")
filehandle = open(filename, 'wb')
else:
if filename:
raise omero.ClientError(
"filename and filehandle specified.")
offset = 0
try:
while (offset+block_size) < size:
data = prx.read(offset, block_size)
try:
filehandle.write(data)
except TypeError:
# for Python 3.5
filehandle.write(bytes_to_native_str(data))
offset += block_size
data = prx.read(offset, size - offset)
try:
filehandle.write(data)
except TypeError:
# for Python 3.5
filehandle.write(bytes_to_native_str(data))
finally:
if filename:
filehandle.close()
finally:
prx.close()
def submit(self, req, loops=10, ms=500,
failonerror=True, ctx=None, failontimeout=True):
handle = self.getSession().submit(req, ctx)
return self.waitOnCmd(
handle, loops=loops, ms=ms,
failonerror=failonerror,
failontimeout=failontimeout,