Skip to content

Commit

Permalink
[wip][broken] begin porting config where used
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Walladge committed Dec 29, 2017
1 parent 9555cbc commit a5323bf
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 53 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ end_of_line = lf
[*.{bat,ps1}]
end_of_line = crlf

[*.{py,pyx,pxd}]
max_line_length = 80


# Tab indentation for makefiles
[Makefile]
Expand Down
22 changes: 0 additions & 22 deletions piqueserver/cfg.py

This file was deleted.

17 changes: 17 additions & 0 deletions piqueserver/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,34 @@
# You should have received a copy of the GNU General Public License
# along with piqueserver. If not, see <http://www.gnu.org/licenses/>.

import sys
import os
import collections
import json

import six
import toml

import piqueserver


# supported config format constants to avoid typos
DEFAULT_FORMAT = 'TOML'
TOML_FORMAT = 'TOML'
JSON_FORMAT = 'JSON'

# global constants we need to know at the start
SERVER_VERSION = '%s - %s' % (sys.platform, piqueserver.__version__)
PKG_NAME = 'piqueserver'
CONFIG_PATH = os.environ.get('XDG_CONFIG_HOME', '~/.config') + '/piqueserver'
CONFIG_DIR = os.path.expanduser(CONFIG_PATH)
MAXMIND_DOWNLOAD = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz'

# (major, minor) versions of python we are supporting
# used on startup to emit a warning if not running on a supported version
SUPPORTED_PYTHONS = ((2,7), (3,4), (3,5), (3,6))



class ConfigStore():
'''
Expand Down
3 changes: 3 additions & 0 deletions piqueserver/config/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ trusted = [ "trustedpass"]
# default duration in minutes a banned player will be banned for
default_duration = 1440

#file = "bans.txt"

# TODO: document
publish = false
publish_port = 32885
Expand Down Expand Up @@ -159,6 +161,7 @@ advance_on_win = true

# time in seconds before player respawns
# TODO: document how spawn time is calculated (it's not always the time given)
# (default: 8)
respawn_time = 16

# if respawns should line up with each other (multiple players will respawn at
Expand Down
38 changes: 15 additions & 23 deletions piqueserver/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@
import gzip
import json

from piqueserver import cfg
from piqueserver.config import config, TOML_FORMAT, JSON_FORMAT
from piqueserver.config import (config, TOML_FORMAT, JSON_FORMAT,
PKG_NAME, MAXMIND_DOWNLOAD, SUPPORTED_PYTHONS, CONFIG_DIR,
CONFIG_PATH
)

MAXMIND_DOWNLOAD = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz'

# (major, minor) versions of python we are supporting
# used on startup to emit a warning if not running on a supported version
SUPPORTED_PYTHONS = ((2,7), (3,4), (3,5), (3,6))

def get_git_rev():
if not os.path.exists(".git"):
Expand All @@ -38,14 +35,14 @@ def get_git_rev():
def copy_config():
config_source = os.path.dirname(os.path.abspath(__file__)) + '/config'
print('Attempting to copy example config to %s (origin: %s).' %
(cfg.config_dir, config_source))
(CONFIG_DIR, config_source))
try:
shutil.copytree(config_source, cfg.config_dir)
shutil.copytree(config_source, CONFIG_DIR)
except Exception as e: # pylint: disable=broad-except
print(e)
sys.exit(1)

print('Complete! Please edit the files in %s to your liking.' % cfg.config_dir)
print('Complete! Please edit the files in %s to your liking.' % CONFIG_DIR)


def update_geoip(target_dir):
Expand Down Expand Up @@ -95,8 +92,8 @@ def main():
'Please see https://github.com/piqueserver/piqueserver/wiki/Supported-Environments for more information.')

description = '%s is an open-source Python server implementation ' \
'for the voxel-based game "Ace of Spades".' % cfg.pkg_name
arg_parser = argparse.ArgumentParser(prog=cfg.pkg_name,
'for the voxel-based game "Ace of Spades".' % PKG_NAME
arg_parser = argparse.ArgumentParser(prog=PKG_NAME,
description=description)

arg_parser.add_argument('-c', '--config-file', default=None,
Expand All @@ -107,10 +104,10 @@ def main():
help='add extra json parameters '
'(overrides the ones present in the config file)')

arg_parser.add_argument('-d', '--config-dir', default=cfg.config_dir,
arg_parser.add_argument('-d', '--config-dir', default=CONFIG_DIR,
help='specify the directory which contains '
'maps, scripts, etc (in correctly named '
'subdirs) - default is %s' % cfg.config_path)
'subdirs) - default is %s' % CONFIG_PATH)

arg_parser.add_argument('--copy-config', action='store_true',
help='copies the default/example config dir to '
Expand All @@ -121,15 +118,6 @@ def main():

args = arg_parser.parse_args()

# populate the global config with values from args
cfg.config_dir = args.config_dir

if args.config_file is None:
cfg.config_file = os.path.join(cfg.config_dir, 'config.json')
else:
cfg.config_file = args.config_file

cfg.json_parameters = args.json_parameters

# find and load the config
format_ = None
Expand Down Expand Up @@ -157,6 +145,10 @@ def main():
if args.json_parameters:
config.update_from_dict(json.loads(args.json_parameters))

# override the config_dir from cli args
config_dir = config.option('config_dir')
config_dir.set(args.config_dir)

run = True

# copy config and update geoip can happen at the same time
Expand Down
20 changes: 12 additions & 8 deletions piqueserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
from twisted.python.logfile import DailyLogFile
from twisted.web import client as web_client

from piqueserver import cfg
from piqueserver.config import config as config_store
from piqueserver.config import config, SERVER_VERSION, CONFIG_DIR

import pyspades.debug
from pyspades.server import (ServerProtocol, Team)
Expand All @@ -62,6 +61,10 @@
# won't be used; just need to be executed
import piqueserver.core_commands

bans_config = config.section('bans')
game_config = config.section('game')
config_dir = config.option('config_dir', default=CONFIG_DIR)

def check_passwords(passwords):
'''
Validator function to be run when the passwords configuration item is updated/set.
Expand Down Expand Up @@ -191,7 +194,7 @@ class FeatureProtocol(ServerProtocol):
game_mode = None # default to None so we can check
time_announce_schedule = None

server_version = cfg.server_version
server_version = SERVER_VERSION

default_fog = (128, 232, 255)

Expand All @@ -208,8 +211,9 @@ def __init__(self, interface, config):
self.bans = NetworkDict()

# attempt to load a saved bans list
bans_file = bans_config.option('file', default='bans.txt').get()
try:
with open(os.path.join(cfg.config_dir, 'bans.txt'), 'r') as f:
with open(os.path.join(config_dir, 'bans.txt'), 'r') as f:
self.bans.read_list(json.load(f))
except FileNotFoundError as e:
# if it doesn't exist, then no bans, no error
Expand All @@ -221,12 +225,11 @@ def __init__(self, interface, config):

self.hard_bans = set() # possible DDoS'ers are added here
self.player_memory = deque(maxlen=100)
self.config = config
if len(self.name) > MAX_SERVER_NAME_SIZE:
print('(server name too long; it will be truncated to "%s")' % (
self.name[:MAX_SERVER_NAME_SIZE]))
self.respawn_time = config.get('respawn_time', 8)
self.respawn_waves = config.get('respawn_waves', False)
self.respawn_time = game_config.option('respawn_time', default=8).get()
self.respawn_waves = game_config.option('respawn_waves', default=False).get()
game_mode = config.get('game_mode', 'ctf')
if game_mode == 'ctf':
self.game_mode = CTF_MODE
Expand Down Expand Up @@ -268,8 +271,9 @@ def __init__(self, interface, config):
self.set_god_build = config.get('set_god_build', False)
self.debug_log = config.get('debug_log', False)
if self.debug_log:
# TODO: make this configurable
pyspades.debug.open_debug_log(
os.path.join(cfg.config_dir, 'debug.log'))
os.path.join(config_dir, 'debug.log'))
ssh = config.get('ssh', {})
if ssh.get('enabled', False):
from piqueserver.ssh import RemoteConsole
Expand Down

0 comments on commit a5323bf

Please sign in to comment.