Skip to content
This repository was archived by the owner on Jun 11, 2025. It is now read-only.

Commit 179cc73

Browse files
authored
Update conn.py, adding readTokenFromStr and writeTokenToStr
1 parent 00797b1 commit 179cc73

File tree

1 file changed

+41
-16
lines changed

1 file changed

+41
-16
lines changed

skpy/conn.py

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -292,22 +292,19 @@ def setTokenFile(self, path):
292292
"""
293293
self.tokenFile = path
294294

295-
def readToken(self):
295+
def readTokenFromStr(self, tokens):
296296
"""
297-
Attempt to re-establish a connection using previously acquired tokens.
297+
Attempt to re-establish a connection using previously acquired tokens from a string.
298298
299299
If the Skype token is valid but the registration token is invalid, a new endpoint will be registered.
300300
301+
Args:
302+
tokens (str): string containing tokens
303+
301304
Raises:
302-
.SkypeAuthException: if the token file cannot be used to authenticate
305+
.SkypeAuthException: if the token string cannot be used to authenticate
303306
"""
304-
if not self.tokenFile:
305-
raise SkypeAuthException("No token file specified")
306-
try:
307-
with open(self.tokenFile, "r") as f:
308-
lines = f.read().splitlines()
309-
except OSError:
310-
raise SkypeAuthException("Token file doesn't exist or not readable")
307+
lines = tokens.splitlines()
311308
try:
312309
user, skypeToken, skypeExpiry, regToken, regExpiry, msgsHost = lines
313310
skypeExpiry = datetime.fromtimestamp(int(skypeExpiry))
@@ -325,7 +322,40 @@ def readToken(self):
325322
self.msgsHost = msgsHost
326323
else:
327324
self.getRegToken()
325+
326+
def readToken(self):
327+
"""
328+
Attempt to re-establish a connection using previously acquired tokens.
329+
330+
If the Skype token is valid but the registration token is invalid, a new endpoint will be registered.
331+
332+
Raises:
333+
.SkypeAuthException: if the token file cannot be used to authenticate
334+
"""
335+
if not self.tokenFile:
336+
raise SkypeAuthException("No token file specified")
337+
try:
338+
with open(self.tokenFile, "r") as f:
339+
tokens = f.read()
340+
except OSError:
341+
raise SkypeAuthException("Token file doesn't exist or not readable")
342+
self.readTokenFromStr(tokens)
328343

344+
def writeTokenToStr(self):
345+
"""
346+
Return details of the current connection into a string.
347+
348+
This can be used by :meth:`readTokenFromStr` to re-authenticate at a later time.
349+
"""
350+
return "\n".join([
351+
self.userId,
352+
self.tokens["skype"],
353+
str(int(time.mktime(self.tokenExpiry["skype"].timetuple()))),
354+
self.tokens["reg"],
355+
str(int(time.mktime(self.tokenExpiry["reg"].timetuple()))),
356+
self.msgsHost
357+
]) + "\n"
358+
329359
def writeToken(self):
330360
"""
331361
Store details of the current connection in the named file.
@@ -336,12 +366,7 @@ def writeToken(self):
336366
with os.fdopen(os.open(self.tokenFile, os.O_WRONLY | os.O_CREAT, 0o600), "w") as f:
337367
# When opening files via os, truncation must be done manually.
338368
f.truncate()
339-
f.write(self.userId + "\n")
340-
f.write(self.tokens["skype"] + "\n")
341-
f.write(str(int(time.mktime(self.tokenExpiry["skype"].timetuple()))) + "\n")
342-
f.write(self.tokens["reg"] + "\n")
343-
f.write(str(int(time.mktime(self.tokenExpiry["reg"].timetuple()))) + "\n")
344-
f.write(self.msgsHost + "\n")
369+
f.write(self.writeTokenToStr())
345370

346371
def verifyToken(self, auth):
347372
"""

0 commit comments

Comments
 (0)