Skip to content

Commit 39de600

Browse files
committed
Merge pull request #22 from Synforge/master
Python 3 support
2 parents 347093a + 78d3fd7 commit 39de600

6 files changed

Lines changed: 36 additions & 14 deletions

File tree

slackclient/_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __eq__(self, compare_str):
1313

1414
def __str__(self):
1515
data = ""
16-
for key in self.__dict__.keys():
16+
for key in list(self.__dict__.keys()):
1717
data += "{} : {}\n".format(key, str(self.__dict__[key])[:40])
1818
return data
1919

slackclient/_client.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/python
2-
#mostly a proxy object to abstract how some of this works
2+
# mostly a proxy object to abstract how some of this works
33

44
import json
55

@@ -21,7 +21,8 @@ def api_call(self, method, **kwargs):
2121
return self.server.api_call(method, **kwargs)
2222

2323
def rtm_read(self):
24-
#in the future, this should handle some events internally i.e. channel creation
24+
# in the future, this should handle some events internally i.e. channel
25+
# creation
2526
if self.server:
2627
json_data = self.server.websocket_safe_read()
2728
data = []
@@ -47,5 +48,6 @@ def process_changes(self, data):
4748
self.server.attach_channel(channel["user"], channel["id"], [])
4849
pass
4950

51+
5052
class SlackNotConnected(Exception):
5153
pass

slackclient/_im.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def __eq__(self, compare_str):
1212

1313
def __str__(self):
1414
data = ""
15-
for key in self.__dict__.keys():
15+
for key in list(self.__dict__.keys()):
1616
if key != "server":
1717
data += "{} : {}\n".format(key, str(self.__dict__[key])[:40])
1818
return data

slackclient/_server.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from websocket import create_connection
77
import json
88

9+
910
class Server(object):
1011
def __init__(self, token, connect=True):
1112
self.token = token
@@ -21,16 +22,19 @@ def __init__(self, token, connect=True):
2122

2223
if connect:
2324
self.rtm_connect()
25+
2426
def __eq__(self, compare_str):
2527
if compare_str == self.domain or compare_str == self.token:
2628
return True
2729
else:
2830
return False
31+
2932
def __str__(self):
3033
data = ""
31-
for key in self.__dict__.keys():
34+
for key in list(self.__dict__.keys()):
3235
data += "{} : {}\n".format(key, str(self.__dict__[key])[:40])
3336
return data
37+
3438
def __repr__(self):
3539
return self.__str__()
3640

@@ -39,7 +43,7 @@ def rtm_connect(self, reconnect=False):
3943
if reply.code != 200:
4044
raise SlackConnectionError
4145
else:
42-
login_data = json.loads(reply.read())
46+
login_data = json.loads(reply.read().decode('utf-8'))
4347
if login_data["ok"]:
4448
self.ws_url = login_data['url']
4549
if not reconnect:
@@ -70,7 +74,9 @@ def parse_channel_data(self, channel_data):
7074
channel["name"] = channel["id"]
7175
if "members" not in channel:
7276
channel["members"] = []
73-
self.attach_channel(channel["name"], channel["id"], channel["members"])
77+
self.attach_channel(channel["name"],
78+
channel["id"],
79+
channel["members"])
7480

7581
def parse_user_data(self, user_data):
7682
for user in user_data:
@@ -92,7 +98,10 @@ def ping(self):
9298
return self.send_to_websocket({"type": "ping"})
9399

94100
def websocket_safe_read(self):
95-
"""Returns data if available, otherwise ''. Newlines indicate multiple messages """
101+
""" Returns data if available, otherwise ''. Newlines indicate multiple
102+
messages
103+
"""
104+
96105
data = ""
97106
while True:
98107
try:
@@ -107,14 +116,17 @@ def attach_channel(self, name, id, members=[]):
107116
self.channels.append(Channel(self, name, id, members))
108117

109118
def join_channel(self, name):
110-
print self.api_requester.do(self.token, "channels.join?name={}".format(name)).read()
119+
print(self.api_requester.do(self.token,
120+
"channels.join?name={}".format(name)).read())
111121

112122
def api_call(self, method, **kwargs):
113123
reply = self.api_requester.do(self.token, method, kwargs)
114124
return reply.read()
115125

126+
116127
class SlackConnectionError(Exception):
117128
pass
118129

130+
119131
class SlackLoginError(Exception):
120132
pass

slackclient/_slackrequest.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
import time
2-
import urllib
3-
import urllib2
2+
3+
try:
4+
# Try for Python3
5+
from urllib.parse import urlencode
6+
from urllib.request import urlopen
7+
except:
8+
# Looks like Python2
9+
from urllib import urlencode
10+
from urllib2 import urlopen
11+
412

513
class SlackRequest(object):
614
def __init__(self):
715
pass
816

917
def do(self, token, request="?", post_data={}, domain="slack.com"):
1018
post_data["token"] = token
11-
post_data = urllib.urlencode(post_data)
19+
post_data = urlencode(post_data)
1220
url = 'https://{}/api/{}'.format(domain, request)
13-
return urllib2.urlopen(url, post_data)
21+
return urlopen(url, post_data.encode('utf-8'))
1422

slackclient/_user.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def __eq__(self, compare_str):
1414

1515
def __str__(self):
1616
data = ""
17-
for key in self.__dict__.keys():
17+
for key in list(self.__dict__.keys()):
1818
if key != "server":
1919
data += "{} : {}\n".format(key, str(self.__dict__[key])[:40])
2020
return data

0 commit comments

Comments
 (0)