Skip to content

Commit

Permalink
Merge pull request #17 from screepers/proper_servers
Browse files Browse the repository at this point in the history
Add new config system with support for multiple servers
  • Loading branch information
tedivm authored Aug 5, 2017
2 parents 2d1a1ac + ca5f0d4 commit e65292a
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 93 deletions.
24 changes: 0 additions & 24 deletions .settings.dist.yaml

This file was deleted.

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


## Settings

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

Typically there is little reason to edit it manually. When you attempt to connect
to a server for the first time it will ask for your credentials (and if it's a
private server for the host), which will then be saved for future use.


## Launching

The interactive shell can be used for both reading console output and sending
console commands to the server.

```bash
$ screepsconsole
```

By default it connects to the main server, but it can also connect to PTR.

```bash
$ screepsconsole ptr
```
cp .settings.dist.yaml ~/.screeps_settings.yaml

The system also works with private servers. Any label can be used, and unlike
the main server the system will ask for a host (include the port) and whether
the shard uses https.

```bash
$ screepsconsole screepsplus
```

The settings file is in yaml and takes various authentication tokens.
It is possible to clear a saved connection.

```yaml
# Copy this to .settings.yaml and fill it out.
```bash
$ screepsconsole clear connectionname
```

# Screeps account info
# Your username is your full email address.
screeps_username:
screeps_password:
screeps_ptr: false
You can also call the python script directly.

# Proxy configuration
http_proxy:
http_proxy_port:
```bash
$ ./screeps_console/interactive.py
```


## Launching
## Streaming Console

To stream the console output directly to your terminal's stdout run the
`console.py` application.
To stream the console output directly to your terminal's stdout without the
ability to send command use the `console.py` application.

```bash
$ ./screeps_console/console.py
```

This project also offers an interactive shell that can be used for both reading
console output and sending console commands to the server.

If you've installed using the provided make file you can launch
Like the Interactive version different servers can be specified.

```bash
$ screepsconsole
$ ./screeps_console/console.py ptr
```

You can also call the python script directly.
The output can also be sent in JSON.

```bash
$ ./screeps_console/interactive.py
$ ./screeps_console/console.py main json
```


Expand Down
19 changes: 8 additions & 11 deletions screeps_console/command.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

from autocomplete import Autocomplete
import calendar
import screepsapi
from settings import getSettings
import settings
import re
from themes import themes
import time
Expand All @@ -19,7 +18,8 @@ class Processor(object):
'help': 'list'
}

def __init__(self):
def __init__(self, connectionname):
self.connectionname = connectionname
self.lastkeytime = 0
self.listbox = False
self.listwalker = False
Expand All @@ -28,22 +28,18 @@ def __init__(self):
self.getApiClient()
self.autocomplete = Autocomplete(self)


def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit, consolemonitor):
self.listbox = listbox # console
self.listwalker = listwalker
self.edit = edit
self.loop = loop
self.consolemonitor = consolemonitor


def getApiClient(self):
if not self.apiclient:
settings = getSettings()
self.apiclient = screepsapi.API(
u=settings['screeps_username'],
p=settings['screeps_password'],
ptr=settings['screeps_ptr'],
host=settings['screeps_host'])
return self.apiclient
return settings.getApiClient(self.connectionname)


def onInput(self, key):

Expand Down Expand Up @@ -86,6 +82,7 @@ def onInput(self, key):

return


def onEnter(self, key):
self.listbox.setAutoscroll(True)
userInput = self.edit
Expand Down
22 changes: 13 additions & 9 deletions screeps_console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from outputparser import parseLine
from outputparser import tagLine
import screepsapi
from settings import getSettings
import settings
from time import sleep
import websocket
from StringIO import StringIO
Expand Down Expand Up @@ -43,7 +43,6 @@ 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 @@ -66,8 +65,9 @@ def on_message(self, ws, message):
message_count = len(stream)

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

if __name__ == "__main__":

opts, args = getopt.getopt(sys.argv[1:], "hi:o:",["ifile=","ofile="])
settings = getSettings()
screepsconsole = ScreepsConsole(user=settings['screeps_username'], password=settings['screeps_password'], ptr=settings['screeps_ptr'], host=settings['screeps_host'])
if len(sys.argv) < 2:
server = 'main'
else:
server = sys.argv[1]

config = settings.getConnection(server)
screepsconsole = ScreepsConsole(user=config['username'], password=config['password'], secure=config['secure'], host=config['host'])

if len(sys.argv) > 1:
if sys.argv[1] == 'interactive':
if len(sys.argv) > 2:
if sys.argv[2] == 'interactive':
screepsconsole.format = 'tag'
if sys.argv[1] == 'json':
if sys.argv[2] == 'json':
screepsconsole.format = 'json'

screepsconsole.start()
72 changes: 58 additions & 14 deletions screeps_console/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from os.path import expanduser
import outputparser
import re
from settings import getSettings
import settings
import signal
import subprocess
import sys
Expand All @@ -23,15 +23,17 @@ class ScreepsInteractiveConsole:
userInput = False
consoleMonitor = False

def __init__(self):
def __init__(self, connection_name):
try:
self.connection_name = connection_name
frame = self.getFrame()
comp = self.getCommandProcessor()
self.loop = urwid.MainLoop(urwid.AttrMap(frame, 'bg'),
unhandled_input=comp.onInput,
palette=themes['dark'])

self.consoleMonitor = ScreepsConsoleMonitor(self.consoleWidget,
self.consoleMonitor = ScreepsConsoleMonitor(connection_name,
self.consoleWidget,
self.listWalker,
self.loop)

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



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

Expand All @@ -73,16 +74,16 @@ def getConsole(self):
def getConsoleListWalker(self):
if not self.listWalker:
self.listWalker = consoleWalker([self.getWelcomeMessage()])
settings = getSettings()
if 'max_buffer' in settings:
self.listWalker.max_buffer = settings['max_buffer']
config = settings.getSettings()
if 'max_buffer' in config:
self.listWalker.max_buffer = config['max_buffer']
else:
self.listWalker.max_buffer = 200000

return self.listWalker

def getCommandProcessor(self):
return command.Processor()
return command.Processor(self.connection_name)

def getWelcomeMessage(self):
return urwid.Text(('default', 'Welcome to the Screeps Interactive Console'))
Expand Down Expand Up @@ -167,9 +168,9 @@ def manageBufferHistory(self):
file_contents = myfile.read()
file_contents_line = file_contents.splitlines()
num_lines = len(file_contents_line)
settings = getSettings()
if 'max_history' in settings:
max_scroll = settings['max_history']
config = settings.getSettings()
if 'max_history' in config:
max_scroll = config['max_history']
else:
max_scroll = 200000

Expand Down Expand Up @@ -241,7 +242,8 @@ class ScreepsConsoleMonitor:
focus = False
filters = []

def __init__(self, widget, walker, loop):
def __init__(self, connectionname, widget, walker, loop):
self.connectionname = connectionname
self.widget = widget
self.walker = walker
self.loop = loop
Expand All @@ -255,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 + ' json'],
[console_path + ' ' + self.connectionname + ' json'],
stdout=write_fd,
preexec_fn=os.setsid,
close_fds=True,
Expand Down Expand Up @@ -366,4 +368,46 @@ def __del__(self):


if __name__ == "__main__":
ScreepsInteractiveConsole()

if len(sys.argv) < 2:
server = 'main'
else:
server = sys.argv[1]

if server == 'clear':
if len(sys.argv) < 3:
server = 'main'
else:
server = sys.argv[2]
settings.removeConnection(server)
sys.exit(0)

connectionSettings = settings.getConnection(server)

if not connectionSettings:
if server == 'main' or server == 'ptr':
legacyConfig = settings.getLegacySettings()
if legacyConfig:
if raw_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()
config['smooth_scroll'] = legacyConfig['smooth_scroll']
config['max_scroll'] = legacyConfig['max_scroll']
config['max_history'] = legacyConfig['max_history']
settings.saveSettings(config)
connectionSettings = settings.getConnection(server)

if not connectionSettings:
if server is 'main':
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: ")
settings.addConnection(server, username, password, host, secure)


ScreepsInteractiveConsole(server)
Loading

0 comments on commit e65292a

Please sign in to comment.