本文整理匯總了Python中redash.utils.configuration.ConfigurationContainer.to_json方法的典型用法代碼示例。如果您正苦於以下問題:Python ConfigurationContainer.to_json方法的具體用法?Python ConfigurationContainer.to_json怎麽用?Python ConfigurationContainer.to_json使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redash.utils.configuration.ConfigurationContainer
的用法示例。
在下文中一共展示了ConfigurationContainer.to_json方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: new
# 需要導入模塊: from redash.utils.configuration import ConfigurationContainer [as 別名]
# 或者: from redash.utils.configuration.ConfigurationContainer import to_json [as 別名]
def new(name=None, type=None, options=None):
"""Create new data source."""
if name is None:
name = click.prompt("Name")
if type is None:
print "Select type:"
for i, query_runner_name in enumerate(query_runners.keys()):
print "{}. {}".format(i + 1, query_runner_name)
idx = 0
while idx < 1 or idx > len(query_runners.keys()):
idx = click.prompt("[{}-{}]".format(1, len(query_runners.keys())), type=int)
type = query_runners.keys()[idx - 1]
else:
validate_data_source_type(type)
query_runner = query_runners[type]
schema = query_runner.configuration_schema()
if options is None:
types = {
'string': unicode,
'number': int,
'boolean': bool
}
options_obj = {}
for k, prop in schema['properties'].iteritems():
required = k in schema.get('required', [])
default_value = "<<DEFAULT_VALUE>>"
if required:
default_value = None
prompt = prop.get('title', k.capitalize())
if required:
prompt = "{} (required)".format(prompt)
else:
prompt = "{} (optional)".format(prompt)
value = click.prompt(prompt, default=default_value, type=types[prop['type']], show_default=False)
if value != default_value:
options_obj[k] = value
options = ConfigurationContainer(options_obj, schema)
else:
options = ConfigurationContainer(json.loads(options), schema)
if not options.is_valid():
print "Error: invalid configuration."
exit()
print "Creating {} data source ({}) with options:\n{}".format(type, name, options.to_json())
data_source = models.DataSource.create(name=name,
type=type,
options=options,
org=models.Organization.get_by_slug('default'))
print "Id: {}".format(data_source.id)