Skip to content

Commit 6e6fb06

Browse files
authored
Merge pull request #7 from sbesson/omero_user_token_getter
Fix the output of omero_user_token get utility
2 parents d8d58d4 + 5070dac commit 6e6fb06

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

omero_user_token/__init__.py

+24-17
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,41 @@ def setter(server, port, user, password, time_to_idle):
6666
client.closeSession()
6767

6868

69-
def login():
69+
def get_token():
7070
"""
71-
Returns an omero.client object from the current user token. The
72-
session the token refers to has had a join attempt made upon it. If
73-
the token file does not exist a FileNotFoundError will be raised.
71+
Returns the current user token
72+
Raises FileNotFoundError if the token file does not exist
7473
"""
75-
token_path = assert_and_get_token_path()
76-
with open(token_path, 'r') as token_file:
77-
token = token_file.read().strip()
78-
omero_session_key = token[:token.find('@')]
79-
host, port = token[token.find('@') + 1:].split(':')
80-
client = omero.client(host, int(port))
81-
try:
82-
session = client.joinSession(omero_session_key)
83-
session.detachOnDestroy()
84-
except Exception:
85-
pass
86-
return client
74+
with open(assert_and_get_token_path(), 'r') as token_path:
75+
return token_path.read().strip()
76+
77+
78+
def login(token):
79+
"""
80+
Returns an omero.client object from a valid token. The
81+
session the token refers to has had a join attempt made upon it.
82+
"""
83+
omero_session_key = token[:token.find('@')]
84+
host, port = token[token.find('@') + 1:].split(':')
85+
client = omero.client(host, int(port))
86+
try:
87+
session = client.joinSession(omero_session_key)
88+
session.detachOnDestroy()
89+
except Exception:
90+
pass
91+
return client
8792

8893

8994
def getter():
9095
try:
91-
client = login()
96+
token = get_token()
97+
client = login(token)
9298
try:
9399
client.getSession()
94100
except Exception:
95101
sys.exit('ERROR: Token is invalid!')
96102
finally:
97103
client.closeSession()
104+
return token
98105
except FileNotFoundError:
99106
sys.exit('ERROR: No token available, `omero_user_token set` required!')

0 commit comments

Comments
 (0)