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
825b8a2557
commit
a3c380d9d7
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(
|
||||||
|
|
|
||||||
|
|
@ -818,3 +818,13 @@ class CustomRow(OrderedDict):
|
||||||
def __iter__(self):
|
def __iter__(self):
|
||||||
for column in self.columns:
|
for column in self.columns:
|
||||||
yield self[column]
|
yield self[column]
|
||||||
|
|
||||||
|
|
||||||
|
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