本文整理汇总了Python中click.ParamType方法的典型用法代码示例。如果您正苦于以下问题:Python click.ParamType方法的具体用法?Python click.ParamType怎么用?Python click.ParamType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类click
的用法示例。
在下文中一共展示了click.ParamType方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handle_oneof
# 需要导入模块: import click [as 别名]
# 或者: from click import ParamType [as 别名]
def handle_oneof(oneof_schema: list) -> tuple:
"""
Custom handle of `oneOf` JSON schema validator. Tried to match primitive type and see if it should be allowed
to be passed multiple timns into a command
:param oneof_schema: `oneOf` JSON schema
:return: Tuple of :class:`click.ParamType`, ``multiple`` flag and ``description`` of option
"""
oneof_dict = {schema["type"]: schema for schema in oneof_schema}
click_type = None
multiple = False
description = None
for key, value in oneof_dict.items():
if key == "array":
continue
elif key in SCHEMA_BASE_MAP:
if oneof_dict.get("array") and oneof_dict["array"]["items"]["type"] == key:
multiple = True
# Found a match to a primitive type
click_type = SCHEMA_BASE_MAP[key]
description = value.get("title")
break
return click_type, multiple, description
示例2: json_schema_to_click_type
# 需要导入模块: import click [as 别名]
# 或者: from click import ParamType [as 别名]
def json_schema_to_click_type(schema: dict) -> tuple:
"""
A generic handler of a single property JSON schema to :class:`click.ParamType` converter
:param schema: JSON schema property to operate on
:return: Tuple of :class:`click.ParamType`, `description`` of option and optionally a :class:`click.Choice`
if the allowed values are a closed list (JSON schema ``enum``)
"""
choices = None
if isinstance(schema["type"], list):
if "string" in schema["type"]:
schema["type"] = "string"
click_type = SCHEMA_BASE_MAP[schema["type"]]
description = schema.get("title")
if schema.get("enum"):
# todo handle multi type enums better (or at all)
enum = [value for value in schema["enum"] if isinstance(value, str)]
choices = click.Choice(enum)
return click_type, description, choices
示例3: json_schema_to_click_type
# 需要导入模块: import click [as 别名]
# 或者: from click import ParamType [as 别名]
def json_schema_to_click_type(schema: dict) -> tuple:
"""
A generic handler of a single property JSON schema to :class:`click.ParamType` converter
:param schema: JSON schema property to operate on
:return: Tuple of :class:`click.ParamType`, `description`` of option and optionally a :class:`click.Choice`
if the allowed values are a closed list (JSON schema ``enum``)
"""
choices = None
if isinstance(schema["type"], list):
if "string" in schema["type"]:
schema["type"] = "string"
click_type = SCHEMA_BASE_MAP[schema["type"]]
description = schema.get("title")
if schema.get("enum"):
choices = click.Choice(schema["enum"])
return click_type, description, choices
示例4: convert
# 需要导入模块: import click [as 别名]
# 或者: from click import ParamType [as 别名]
def convert(self, value, param, ctx):
# comply with ParamType requirements
if value is None:
return None
if value.lower() == "all":
if self.accept_multiple:
return LayerType.ALL
else:
self.fail("'all' was not expected", param, ctx)
elif value.lower() == "new":
if self.accept_new:
return LayerType.NEW
else:
self.fail("'new' was not expected", param, ctx)
try:
if self.accept_multiple:
id_arr = list(map(int, value.split(",")))
for i in id_arr:
if i < 1:
raise TypeError
return id_arr
else:
return int(value)
except TypeError:
self.fail(f"unexpected {value!r} of type {type(value).__name__}", param, ctx)
except ValueError:
self.fail(f"{value!r} is not a valid value", param, ctx)
示例5: convert
# 需要导入模块: import click [as 别名]
# 或者: from click import ParamType [as 别名]
def convert(self, value, param, ctx):
"""
ParamType.convert() is the actual processing method that takes a
provided parameter and parses it.
"""
# passthrough conditions: None or already processed
if value is None or isinstance(value, tuple):
return value
# split the value on the first colon, leave the rest intact
splitval = value.split(":", 1)
# first element is the endpoint_id
endpoint_id = click.UUID(splitval[0])
# get the second element, defaulting to `None` if there was no colon in
# the original value
try:
path = splitval[1]
except IndexError:
path = None
# coerce path="" to path=None
# means that we treat "enpdoint_id" and "endpoint_id:" equivalently
path = path or None
if path is None and self.path_required:
self.fail("The path component is required", param=param)
return (endpoint_id, path)