Add simple range checking to config options

This commit is contained in:
Unrud 2017-06-02 12:42:19 +02:00
parent 95a8c7b903
commit 881757815f

View File

@ -23,10 +23,30 @@ Give a configparser-like interface to read and write configuration.
""" """
import math
import os import os
from collections import OrderedDict from collections import OrderedDict
from configparser import RawConfigParser as ConfigParser from configparser import RawConfigParser as ConfigParser
def positive_int(value):
value = int(value)
if value < 0:
raise ValueError("value is negative: %d" % value)
return value
def positive_float(value):
value = float(value)
if not math.isfinite(value):
raise ValueError("value is infinite")
if math.isnan(value):
raise ValueError("value is not a number")
if value < 0:
raise ValueError("value is negative: %f" % value)
return value
# Default configuration # Default configuration
INITIAL_CONFIG = OrderedDict([ INITIAL_CONFIG = OrderedDict([
("server", OrderedDict([ ("server", OrderedDict([
@ -49,15 +69,15 @@ INITIAL_CONFIG = OrderedDict([
("max_connections", { ("max_connections", {
"value": "20", "value": "20",
"help": "maximum number of parallel connections", "help": "maximum number of parallel connections",
"type": int}), "type": positive_int}),
("max_content_length", { ("max_content_length", {
"value": "10000000", "value": "10000000",
"help": "maximum size of request body in bytes", "help": "maximum size of request body in bytes",
"type": int}), "type": positive_int}),
("timeout", { ("timeout", {
"value": "10", "value": "10",
"help": "socket timeout", "help": "socket timeout",
"type": int}), "type": positive_int}),
("ssl", { ("ssl", {
"value": "False", "value": "False",
"help": "use SSL connection", "help": "use SSL connection",
@ -115,7 +135,7 @@ INITIAL_CONFIG = OrderedDict([
("delay", { ("delay", {
"value": "1", "value": "1",
"help": "incorrect authentication delay", "help": "incorrect authentication delay",
"type": float})])), "type": positive_float})])),
("rights", OrderedDict([ ("rights", OrderedDict([
("type", { ("type", {
"value": "owner_only", "value": "owner_only",