mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Extract string-to-bool logic into utils.py
This commit is contained in:
parent
d0a578c0fc
commit
7e0caa1e62
2 changed files with 25 additions and 5 deletions
|
|
@ -7,7 +7,12 @@ import shutil
|
||||||
from subprocess import call, check_output
|
from subprocess import call, check_output
|
||||||
import sys
|
import sys
|
||||||
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS
|
from .app import Datasette, DEFAULT_CONFIG, CONFIG_OPTIONS
|
||||||
from .utils import temporary_docker_directory, temporary_heroku_directory
|
from .utils import (
|
||||||
|
temporary_docker_directory,
|
||||||
|
temporary_heroku_directory,
|
||||||
|
value_as_boolean,
|
||||||
|
ValueAsBooleanError,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class StaticMount(click.ParamType):
|
class StaticMount(click.ParamType):
|
||||||
|
|
@ -36,17 +41,22 @@ class Config(click.ParamType):
|
||||||
return
|
return
|
||||||
name, value = config.split(":")
|
name, value = config.split(":")
|
||||||
if name not in DEFAULT_CONFIG:
|
if name not in DEFAULT_CONFIG:
|
||||||
self.fail("{} is not a valid option".format(name), param, ctx)
|
self.fail(
|
||||||
|
"{} is not a valid option (--help-config to see all)".format(
|
||||||
|
name
|
||||||
|
), param, ctx
|
||||||
|
)
|
||||||
return
|
return
|
||||||
# Type checking
|
# Type checking
|
||||||
default = DEFAULT_CONFIG[name]
|
default = DEFAULT_CONFIG[name]
|
||||||
if isinstance(default, bool):
|
if isinstance(default, bool):
|
||||||
if value.lower() not in ('on', 'off', 'true', 'false', '1', '0'):
|
try:
|
||||||
|
return name, value_as_boolean(value)
|
||||||
|
except ValueAsBooleanError:
|
||||||
self.fail(
|
self.fail(
|
||||||
'"{}" should be on/off/true/false'.format(name), param, ctx
|
'"{}" should be on/off/true/false/1/0'.format(name), param, ctx
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
return name, value.lower() in ('on', 'true', '1')
|
|
||||||
elif isinstance(default, int):
|
elif isinstance(default, int):
|
||||||
if not value.isdigit():
|
if not value.isdigit():
|
||||||
self.fail(
|
self.fail(
|
||||||
|
|
|
||||||
|
|
@ -800,3 +800,13 @@ def path_with_format(request, format, extra_qs=None):
|
||||||
elif request.query_string:
|
elif request.query_string:
|
||||||
path = "{}?{}".format(path, request.query_string)
|
path = "{}?{}".format(path, request.query_string)
|
||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
|
def value_as_boolean(value):
|
||||||
|
if value.lower() not in ('on', 'off', 'true', 'false', '1', '0'):
|
||||||
|
raise ValueAsBooleanError
|
||||||
|
return value.lower() in ('on', 'true', '1')
|
||||||
|
|
||||||
|
|
||||||
|
class ValueAsBooleanError(ValueError):
|
||||||
|
pass
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue