[openstack-dev] [nova][oslo] oslo.config and import chains

Matthew Booth mbooth at redhat.com
Thu Aug 7 11:15:16 UTC 2014


I'm sure this is well known, but I recently encountered this problem for
the second time.

---
foo:
import oslo.config as cfg

import bar

CONF = cfg.CONF
CONF.register_opts('foo_opt')

---
bar:
import oslo.config as cfg

CONF = cfg.CONF

def bar_func(arg=CONF.foo_opt):
  pass
---

importing foo results in an error in bar because CONF.foo_opt doesn't
exist. This is because bar is imported before CONF.register_opts.
CONF.import_opt() fails in the same way because it just imports foo and
hits the exact same problem when foo imports bar.

A (the?) solution is to register_opts() in foo before importing any
modules which might also use oslo.config. This also allows import_opt()
to work in bar, which you should do to remove any dependency on import
order:

---
foo:
import oslo.config as cfg

CONF = cfg.CONF
CONF.register_opts('foo_opt')

import bar

---
bar:
import oslo.config as cfg

CONF = cfg.CONF
CONF.import_opt('foo_opt', 'foo')

def bar_func(arg=CONF.foo_opt):
  pass
---

Even if it's old news it's worth a refresher because it was a bit of a
headscratcher.

Matt
-- 
Matthew Booth
Red Hat Engineering, Virtualisation Team

Phone: +442070094448 (UK)
GPG ID:  D33C3490
GPG FPR: 3733 612D 2D05 5458 8A8A 1600 3441 EA19 D33C 3490



More information about the OpenStack-dev mailing list