Commit 56f5e36c authored by kaiyou's avatar kaiyou

Allow to insert populated fields inplace

parent 65846ff9
import celery import celery
def populate(context, args, key, table): def populate(context, args, key, table, replace=True):
""" Populate items in context and arguments from a reference table """ Populate items in context and arguments from a reference table
Useful to populate accounts, services, and other items that repeat Useful to populate accounts, services, and other items that repeat
across a configuration file. across a configuration file.
The default behavior is to replace the entry with the match in the
reference table. If this is disabled, the entry is deleted and the
match is expected to be a dict, it is then merged in the context or
args.
""" """
conf = celery.current_app.scheduler.config conf = celery.current_app.scheduler.config
for item in (context, args): for item in (context, args):
if key in item and type(item[key]) is str: if replace:
item[key] = conf[table].get(item[key], item[key]) if key in item and type(item[key]) is str:
elif key in item and type(item[key]) is list: item[key] = conf[table].get(item[key], item[key])
item[key] = { elif key in item and type(item[key]) is list:
entry: conf[table].get(entry, entry) item[key] = {
for entry in context[key] entry: conf[table].get(entry, entry)
} for entry in context[key]
}
else:
if key in item and type(item[key]) is str:
item.update(conf[table][item[key]])
del item[key]
return (context, args) return (context, args)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment