Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Flexible datastore options. PoC.
path = "datastores"
ENV = jinja2.Environment(loader=jinja2.ChoiceLoader([
jinja2.FileSystemLoader(os.path.join(os.getcwd(), path)),
# jinja2.PackageLoader("online-oslo-parser", "datastores")
]))
CONF = cfg.CONF
def create_config_group(name):
reg = cfg.OptGroup(name, title='%s options' % name,
help="Oslo option group designed "
"for %s datastore" % name)
CONF.register_group(reg)
return reg
def build_list_opts_for(conf_group, opts):
list_group = []
for opt_key, opt_values in opts.iteritems():
list_group.append(
cfg.ListOpt(opt_key, default=opt_values["default"],
help=opt_values["help"]))
if list_group:
CONF.register_opts(list_group, conf_group)
def build_dict_opts_for(conf_group, opts):
dict_group = []
for opt_key, opt_values in opts.iteritems():
dict_group.append(
cfg.DictOpt(opt_key, default=opt_values["default"],
help=opt_values["help"]))
if dict_group:
CONF.register_opts(dict_group, conf_group)
def build_string_opts_for(conf_group, opts):
string_group = []
for opt_key, opt_values in opts.iteritems():
string_group.append(
cfg.StrOpt(opt_key, default=opt_values["default"],
help=opt_values["help"]))
if string_group:
CONF.register_opts(string_group, conf_group)
def build_int_opts_for(conf_group, opts):
int_group = []
for opt_key, opt_values in opts.iteritems():
int_group.append(
cfg.IntOpt(opt_key, default=opt_values["default"],
help=opt_values["help"]))
if int_group:
CONF.register_opts(int_group, conf_group)
def build_boolean_opts_for(conf_group, opts):
bool_group = []
for opt_key, opt_values in opts.iteritems():
bool_group.append(
cfg.BoolOpt(opt_key, default=opt_values["default"],
help=opt_values["help"]))
if bool_group:
CONF.register_opts(bool_group, conf_group)
if "__main__" == __name__:
datastore_tmplts = []
list_opts = []
group = None
for template in ENV.list_templates():
options = json.loads(ENV.get_template(template).render())
group = create_config_group(template[:-5])
build_list_opts_for(group, options.get("list_options", {}))
build_string_opts_for(group, options.get("string_options", {}))
build_dict_opts_for(group, options.get("dict_options", {}))
build_boolean_opts_for(group, options.get("boolean_options", {}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment