Skip to content

Commit e65292a

Browse files
authored
Merge pull request #17 from screepers/proper_servers
Add new config system with support for multiple servers
2 parents 2d1a1ac + ca5f0d4 commit e65292a

File tree

6 files changed

+217
-93
lines changed

6 files changed

+217
-93
lines changed

.settings.dist.yaml

Lines changed: 0 additions & 24 deletions
This file was deleted.

README.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,75 @@ Note: This application requires Python 2, not 3.
2424
1. Determine where your Python is installed with `which python` - assume it is `/path/to/python` and replace that below
2525
1. Set up the `virtualenv` with the command `virtualenv -p /path/to/python env`
2626
1. Use that new `virtualenv` with the command `source env/bin/activate`
27-
2. Run `make`
28-
3. Run `make install`
29-
4. Create settings (as per the instructions below)
30-
5. Run `screepsconsole` from inside the terminal
27+
1. Run `make`
28+
1. Run `make install`
29+
1. Run `screepsconsole` from inside the terminal
3130

3231

3332
## Settings
3433

35-
The settings file is a yaml file. Begin by copying the settings.dist file to
36-
.settings.yaml in the directory you will be calling the shell from, or you can
37-
store the file in your home directory.
34+
The settings file is created automatically and placed in `~.screepsconsole.yaml`.
3835

36+
Typically there is little reason to edit it manually. When you attempt to connect
37+
to a server for the first time it will ask for your credentials (and if it's a
38+
private server for the host), which will then be saved for future use.
39+
40+
41+
## Launching
42+
43+
The interactive shell can be used for both reading console output and sending
44+
console commands to the server.
45+
46+
```bash
47+
$ screepsconsole
48+
```
49+
50+
By default it connects to the main server, but it can also connect to PTR.
51+
52+
```bash
53+
$ screepsconsole ptr
3954
```
40-
cp .settings.dist.yaml ~/.screeps_settings.yaml
55+
56+
The system also works with private servers. Any label can be used, and unlike
57+
the main server the system will ask for a host (include the port) and whether
58+
the shard uses https.
59+
60+
```bash
61+
$ screepsconsole screepsplus
4162
```
4263

43-
The settings file is in yaml and takes various authentication tokens.
64+
It is possible to clear a saved connection.
4465

45-
```yaml
46-
# Copy this to .settings.yaml and fill it out.
66+
```bash
67+
$ screepsconsole clear connectionname
68+
```
4769

48-
# Screeps account info
49-
# Your username is your full email address.
50-
screeps_username:
51-
screeps_password:
52-
screeps_ptr: false
70+
You can also call the python script directly.
5371

54-
# Proxy configuration
55-
http_proxy:
56-
http_proxy_port:
72+
```bash
73+
$ ./screeps_console/interactive.py
5774
```
5875

5976

60-
## Launching
77+
## Streaming Console
6178

62-
To stream the console output directly to your terminal's stdout run the
63-
`console.py` application.
79+
To stream the console output directly to your terminal's stdout without the
80+
ability to send command use the `console.py` application.
6481

6582
```bash
6683
$ ./screeps_console/console.py
6784
```
6885

69-
This project also offers an interactive shell that can be used for both reading
70-
console output and sending console commands to the server.
71-
72-
If you've installed using the provided make file you can launch
86+
Like the Interactive version different servers can be specified.
7387

7488
```bash
75-
$ screepsconsole
89+
$ ./screeps_console/console.py ptr
7690
```
7791

78-
You can also call the python script directly.
92+
The output can also be sent in JSON.
7993

8094
```bash
81-
$ ./screeps_console/interactive.py
95+
$ ./screeps_console/console.py main json
8296
```
8397

8498

screeps_console/command.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11

22
from autocomplete import Autocomplete
33
import calendar
4-
import screepsapi
5-
from settings import getSettings
4+
import settings
65
import re
76
from themes import themes
87
import time
@@ -19,7 +18,8 @@ class Processor(object):
1918
'help': 'list'
2019
}
2120

22-
def __init__(self):
21+
def __init__(self, connectionname):
22+
self.connectionname = connectionname
2323
self.lastkeytime = 0
2424
self.listbox = False
2525
self.listwalker = False
@@ -28,22 +28,18 @@ def __init__(self):
2828
self.getApiClient()
2929
self.autocomplete = Autocomplete(self)
3030

31+
3132
def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit, consolemonitor):
3233
self.listbox = listbox # console
3334
self.listwalker = listwalker
3435
self.edit = edit
3536
self.loop = loop
3637
self.consolemonitor = consolemonitor
3738

39+
3840
def getApiClient(self):
39-
if not self.apiclient:
40-
settings = getSettings()
41-
self.apiclient = screepsapi.API(
42-
u=settings['screeps_username'],
43-
p=settings['screeps_password'],
44-
ptr=settings['screeps_ptr'],
45-
host=settings['screeps_host'])
46-
return self.apiclient
41+
return settings.getApiClient(self.connectionname)
42+
4743

4844
def onInput(self, key):
4945

@@ -86,6 +82,7 @@ def onInput(self, key):
8682

8783
return
8884

85+
8986
def onEnter(self, key):
9087
self.listbox.setAutoscroll(True)
9188
userInput = self.edit

screeps_console/console.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from outputparser import parseLine
88
from outputparser import tagLine
99
import screepsapi
10-
from settings import getSettings
10+
import settings
1111
from time import sleep
1212
import websocket
1313
from StringIO import StringIO
@@ -43,7 +43,6 @@ def on_message(self, ws, message):
4343
except:
4444
print("Unexpected error:", sys.exc_info())
4545
return
46-
4746
data = json.loads(message)
4847

4948
if 'shard' in data[1]:
@@ -66,8 +65,9 @@ def on_message(self, ws, message):
6665
message_count = len(stream)
6766

6867
if message_count > 0:
68+
config = settings.getSettings()
6969
# Make sure the delay doesn't cause an overlap into other ticks
70-
if 'smooth_scroll' in settings and settings['smooth_scroll'] is True:
70+
if 'smooth_scroll' in config and config['smooth_scroll'] is True:
7171
message_delay = 0.2 / message_count
7272
if message_delay > 0.07:
7373
message_delay = 0.07
@@ -111,14 +111,18 @@ def start(self):
111111

112112
if __name__ == "__main__":
113113

114-
opts, args = getopt.getopt(sys.argv[1:], "hi:o:",["ifile=","ofile="])
115-
settings = getSettings()
116-
screepsconsole = ScreepsConsole(user=settings['screeps_username'], password=settings['screeps_password'], ptr=settings['screeps_ptr'], host=settings['screeps_host'])
114+
if len(sys.argv) < 2:
115+
server = 'main'
116+
else:
117+
server = sys.argv[1]
118+
119+
config = settings.getConnection(server)
120+
screepsconsole = ScreepsConsole(user=config['username'], password=config['password'], secure=config['secure'], host=config['host'])
117121

118-
if len(sys.argv) > 1:
119-
if sys.argv[1] == 'interactive':
122+
if len(sys.argv) > 2:
123+
if sys.argv[2] == 'interactive':
120124
screepsconsole.format = 'tag'
121-
if sys.argv[1] == 'json':
125+
if sys.argv[2] == 'json':
122126
screepsconsole.format = 'json'
123127

124128
screepsconsole.start()

screeps_console/interactive.py

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from os.path import expanduser
99
import outputparser
1010
import re
11-
from settings import getSettings
11+
import settings
1212
import signal
1313
import subprocess
1414
import sys
@@ -23,15 +23,17 @@ class ScreepsInteractiveConsole:
2323
userInput = False
2424
consoleMonitor = False
2525

26-
def __init__(self):
26+
def __init__(self, connection_name):
2727
try:
28+
self.connection_name = connection_name
2829
frame = self.getFrame()
2930
comp = self.getCommandProcessor()
3031
self.loop = urwid.MainLoop(urwid.AttrMap(frame, 'bg'),
3132
unhandled_input=comp.onInput,
3233
palette=themes['dark'])
3334

34-
self.consoleMonitor = ScreepsConsoleMonitor(self.consoleWidget,
35+
self.consoleMonitor = ScreepsConsoleMonitor(connection_name,
36+
self.consoleWidget,
3537
self.listWalker,
3638
self.loop)
3739

@@ -56,7 +58,6 @@ def getFrame(self):
5658
return frame_widget
5759

5860

59-
6061
def getHeader(self):
6162
return urwid.AttrMap(urwid.Text("Screeps Interactive Console", align='center'), 'header')
6263

@@ -73,16 +74,16 @@ def getConsole(self):
7374
def getConsoleListWalker(self):
7475
if not self.listWalker:
7576
self.listWalker = consoleWalker([self.getWelcomeMessage()])
76-
settings = getSettings()
77-
if 'max_buffer' in settings:
78-
self.listWalker.max_buffer = settings['max_buffer']
77+
config = settings.getSettings()
78+
if 'max_buffer' in config:
79+
self.listWalker.max_buffer = config['max_buffer']
7980
else:
8081
self.listWalker.max_buffer = 200000
8182

8283
return self.listWalker
8384

8485
def getCommandProcessor(self):
85-
return command.Processor()
86+
return command.Processor(self.connection_name)
8687

8788
def getWelcomeMessage(self):
8889
return urwid.Text(('default', 'Welcome to the Screeps Interactive Console'))
@@ -167,9 +168,9 @@ def manageBufferHistory(self):
167168
file_contents = myfile.read()
168169
file_contents_line = file_contents.splitlines()
169170
num_lines = len(file_contents_line)
170-
settings = getSettings()
171-
if 'max_history' in settings:
172-
max_scroll = settings['max_history']
171+
config = settings.getSettings()
172+
if 'max_history' in config:
173+
max_scroll = config['max_history']
173174
else:
174175
max_scroll = 200000
175176

@@ -241,7 +242,8 @@ class ScreepsConsoleMonitor:
241242
focus = False
242243
filters = []
243244

244-
def __init__(self, widget, walker, loop):
245+
def __init__(self, connectionname, widget, walker, loop):
246+
self.connectionname = connectionname
245247
self.widget = widget
246248
self.walker = walker
247249
self.loop = loop
@@ -255,7 +257,7 @@ def getProcess(self):
255257
console_path = os.path.join(os.path.dirname(sys.argv[0]), 'console.py ')
256258
write_fd = self.loop.watch_pipe(self.onUpdate)
257259
self.proc = subprocess.Popen(
258-
[console_path + ' json'],
260+
[console_path + ' ' + self.connectionname + ' json'],
259261
stdout=write_fd,
260262
preexec_fn=os.setsid,
261263
close_fds=True,
@@ -366,4 +368,46 @@ def __del__(self):
366368

367369

368370
if __name__ == "__main__":
369-
ScreepsInteractiveConsole()
371+
372+
if len(sys.argv) < 2:
373+
server = 'main'
374+
else:
375+
server = sys.argv[1]
376+
377+
if server == 'clear':
378+
if len(sys.argv) < 3:
379+
server = 'main'
380+
else:
381+
server = sys.argv[2]
382+
settings.removeConnection(server)
383+
sys.exit(0)
384+
385+
connectionSettings = settings.getConnection(server)
386+
387+
if not connectionSettings:
388+
if server == 'main' or server == 'ptr':
389+
legacyConfig = settings.getLegacySettings()
390+
if legacyConfig:
391+
if raw_input("Upgrade settings file to the new format? (y/n) ") != "y":
392+
sys.exit(-1)
393+
settings.addConnection('main', legacyConfig['screeps_username'], legacyConfig['screeps_password'])
394+
config = settings.getSettings()
395+
config['smooth_scroll'] = legacyConfig['smooth_scroll']
396+
config['max_scroll'] = legacyConfig['max_scroll']
397+
config['max_history'] = legacyConfig['max_history']
398+
settings.saveSettings(config)
399+
connectionSettings = settings.getConnection(server)
400+
401+
if not connectionSettings:
402+
if server is 'main':
403+
host = 'screeps.com'
404+
secure = True
405+
else:
406+
host = raw_input("Host: ")
407+
secure = raw_input("Secure (y/n) ") == "y"
408+
username = raw_input("Username: ")
409+
password = raw_input("Password: ")
410+
settings.addConnection(server, username, password, host, secure)
411+
412+
413+
ScreepsInteractiveConsole(server)

0 commit comments

Comments
 (0)