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
|
||||
import sys
|
||||
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):
|
||||
|
|
@ -36,17 +41,22 @@ class Config(click.ParamType):
|
|||
return
|
||||
name, value = config.split(":")
|
||||
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
|
||||
# Type checking
|
||||
default = DEFAULT_CONFIG[name]
|
||||
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(
|
||||
'"{}" should be on/off/true/false'.format(name), param, ctx
|
||||
'"{}" should be on/off/true/false/1/0'.format(name), param, ctx
|
||||
)
|
||||
return
|
||||
return name, value.lower() in ('on', 'true', '1')
|
||||
elif isinstance(default, int):
|
||||
if not value.isdigit():
|
||||
self.fail(
|
||||
|
|
|
|||
|
|
@ -818,3 +818,13 @@ class CustomRow(OrderedDict):
|
|||
def __iter__(self):
|
||||
for column in self.columns:
|
||||
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