Skip to content

Commit

Permalink
Python 3 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dessix committed Aug 7, 2017
1 parent cb3f7f7 commit b974964
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
8 changes: 4 additions & 4 deletions screeps_console/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ def __init__(self, comp):
def loadList(self, listname):

if not listname in self.lists:
autocomplete = pkg_resources.resource_string(__name__, 'data/autocomplete/' + listname + '.dat')
autocomplete = pkg_resources.resource_string(__name__, 'data/autocomplete/' + listname + '.dat').decode("utf-8")
autocomplete_list = autocomplete.splitlines()
autocomplete_list_unique = self.sortList(autocomplete_list)
self.lists[listname] = autocomplete_list_unique
return self.lists[listname]


def sortList(self, list):
autocomplete_list_filtered = [x for x in list if not x.startswith('#') and x != '']
autocomplete_list_unique = {}.fromkeys(autocomplete_list_filtered).keys()
def sortList(self, lst):
autocomplete_list_filtered = [x for x in lst if not x.startswith('#') and x != '']
autocomplete_list_unique = list({}.fromkeys(autocomplete_list_filtered).keys())
autocomplete_list_unique.sort()
return autocomplete_list_unique

Expand Down
15 changes: 7 additions & 8 deletions screeps_console/console.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
#!/usr/bin/env python

from base64 import b64decode
import getopt
import json
import logging
from outputparser import parseLine
from outputparser import tagLine
import screepsapi
import settings
from time import sleep
import websocket
from StringIO import StringIO
import sys
import zlib

Expand Down Expand Up @@ -43,6 +40,7 @@ def on_message(self, ws, message):
except:
print("Unexpected error:", sys.exc_info())
return

data = json.loads(message)

if 'shard' in data[1]:
Expand All @@ -54,13 +52,14 @@ def on_message(self, ws, message):
if 'messages' in data[1]:
stream = []

if 'log' in data[1]['messages']:
stream = stream + data[1]['messages']['log']
messages = data[1]["messages"]
if 'log' in messages:
stream.extend(messages['log'])

if 'results' in data[1]['messages']:
results = data[1]['messages']['results']
if 'results' in messages:
results = messages['results']
results = map(lambda x:'<type="result">'+x+'</type>',results)
stream = stream + results
stream.extend(results)

message_count = len(stream)

Expand Down
22 changes: 10 additions & 12 deletions screeps_console/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def getProcess(self):
console_path = os.path.join(os.path.dirname(sys.argv[0]), 'console.py ')
write_fd = self.loop.watch_pipe(self.onUpdate)
self.proc = subprocess.Popen(
[console_path + ' ' + self.connectionname + ' json'],
[sys.executable + ' ' + console_path + ' ' + self.connectionname + ' json'],
stdout=write_fd,
preexec_fn=os.setsid,
close_fds=True,
Expand All @@ -278,25 +278,23 @@ def disconnect(self):
self.proc = False

def onUpdate(self, data):

# If we lose the connection to the remote system close the console.
if data.startswith('### closed ###'):
if data.startswith(b'### closed ###'):
self.proc = False
self.getProcess()
lostprocess_message = 'reconnecting to server . . .'
self.walker.append(urwid.Text(('logged_response', lostprocess_message)))
self.widget.set_focus(len(self.walker)-1)
return
if data[-1:] != '\n':
self.buffer += data
if data[-1:] != b'\n':
self.buffer += data.decode("utf-8")
return
if len(self.buffer) > 0:
data = self.buffer + data
self.buffer = ''
data_lines = data.rstrip().split('\n')
data_lines = data.decode("utf-8").rstrip().split('\n')
for line_json in data_lines:
try:

if len(line_json) <= 0:
continue
try:
Expand Down Expand Up @@ -388,7 +386,7 @@ def __del__(self):
if server == 'main' or server == 'ptr':
legacyConfig = settings.getLegacySettings()
if legacyConfig:
if raw_input("Upgrade settings file to the new format? (y/n) ") != "y":
if input("Upgrade settings file to the new format? (y/n) ") != "y":
sys.exit(-1)
settings.addConnection('main', legacyConfig['screeps_username'], legacyConfig['screeps_password'])
config = settings.getSettings()
Expand All @@ -403,10 +401,10 @@ def __del__(self):
host = 'screeps.com'
secure = True
else:
host = raw_input("Host: ")
secure = raw_input("Secure (y/n) ") == "y"
username = raw_input("Username: ")
password = raw_input("Password: ")
host = input("Host: ")
secure = input("Secure (y/n) ") == "y"
username = input("Username: ")
password = input("Password: ")
settings.addConnection(server, username, password, host, secure)


Expand Down

0 comments on commit b974964

Please sign in to comment.