def test_save_load(self): """Save and re-load UCR.""" ucr = ConfigRegistry() ucr['foo'] = 'bar' ucr.save() ucr = ConfigRegistry() ucr.load() self.assertEqual(ucr['foo'], 'bar')
def _setup_layers(): ucr = ConfigRegistry(write_registry=ConfigRegistry.FORCED) ucr['foo'] = 'FORCED' ucr['bar'] = 'FORCED' ucr.save() ucr = ConfigRegistry() ucr['bar'] = 'NORMAL' ucr['baz'] = 'NORMAL' ucr.save() ucr = ConfigRegistry() ucr.load() return ucr
def test_layering(self, levels): """Check layer priorities 50layer-priority""" msb = 0 for layer in range(4): if levels & (1 << layer): ucr = ConfigRegistry(write_registry=layer) ucr["key"] = str(layer) ucr.save() msb = layer ucr = ConfigRegistry() ucr.load() assert ucr["key"] == (str(msb) if levels else None)
def ucrf(ucr0): """ Return a pre-initialized UCR instance. """ ucr = ConfigRegistry(write_registry=ConfigRegistry.FORCED) ucr['foo'] = 'FORCED' ucr['bar'] = 'FORCED' ucr.save() ucr = ConfigRegistry() ucr['bar'] = 'NORMAL' ucr['baz'] = 'NORMAL' ucr.save() ucr0.load() return ucr0
def handler_unset(args, opts=dict()): """ Unset config registry variables in args. """ current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() handlers = ConfigHandlers() handlers.load() changed = {} for arg in args: if reg.has_key(arg, write_registry_only=True): oldvalue = reg[arg] print 'Unsetting %s' % arg del reg[arg] changed[arg] = (oldvalue, '') k = reg.get(arg, None, getscope=True) replog('unset', current_scope, reg, arg, oldvalue) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is still set in scope "%s"' % \ (arg, SCOPE[k[0]]) else: msg = "W: The config registry variable '%s' does not exist" print >> sys.stderr, msg % (arg,) reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))
def handler_unset(args, opts=dict()): """ Unset config registry variables in args. """ current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() handlers = ConfigHandlers() handlers.load() changed = {} for arg in args: if reg.has_key(arg, write_registry_only=True): oldvalue = reg[arg] print 'Unsetting %s' % arg del reg[arg] changed[arg] = (oldvalue, '') k = reg.get(arg, None, getscope=True) replog('unset', current_scope, reg, arg, oldvalue) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is still set in scope "%s"' % \ (arg, SCOPE[k[0]]) else: msg = "W: The config registry variable '%s' does not exist" print >> sys.stderr, msg % (arg, ) reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))
def test_threading(self, tmpdir): """Multiple threads accessing same registry.""" DO_LOCKING = True THREADS = 10 ITERATIONS = 1000 BASE, PRIME = 7, 23 KEY = 'x' * PRIME SKEY, SVALUE = 'always', 'there' ConfigRegistry.PREFIX = str(tmpdir) ucr = ConfigRegistry() ucr[SKEY] = SVALUE ucr.save() lock = Lock() if DO_LOCKING else DummyLock() def run(tid): for iteration in range(ITERATIONS): i = tid + iteration random = pow(BASE, i, PRIME) key = KEY[:random + 1] with lock: ucr.load() assert ucr[SKEY] == SVALUE, 'tid=%d iter=%d %r' % ( tid, iteration, ucr.items()) try: del ucr[key] except LookupError: ucr[key] = '%d %d' % (tid, iteration) if i % 10 == 0 and tid % 10 == 0: with lock: ucr.save() threads = [] for tid in range(THREADS): thread = Thread(target=run, name='%d' % tid, args=(tid, )) threads.append(thread) for thread in threads: thread.start() for thread in threads: thread.join()
def _register_variable_default_values(ucr): # type: (ConfigRegistry) -> None """Create base-default.conf layer containig all default values""" info = _get_config_registry_info() _ucr = ConfigRegistry(write_registry=ConfigRegistry.DEFAULTS) _ucr.load() defaults = {} # type: Dict[str, Optional[str]] default_variables = info.get_variables() for key, variable in default_variables.items(): value = variable.get('default') if value: defaults[key] = value for key in _ucr: if key not in default_variables: defaults[key] = None changed = dict((key, (old, new)) for key, (old, new) in _ucr.update(defaults).items() if old != new) _ucr.save() ucr.load() _run_changed(ucr, changed, 'I: %s will be set in scope "%s"')
def handler_set(args, opts=dict(), quiet=False): """ Set config registry variables in args. Args is an array of strings 'key=value' or 'key?value'. """ handlers = ConfigHandlers() handlers.load() current_scope = ConfigRegistry.NORMAL reg = None if opts.get('ldap-policy', False): current_scope = ConfigRegistry.LDAP reg = ConfigRegistry(write_registry=current_scope) elif opts.get('force', False): current_scope = ConfigRegistry.FORCED reg = ConfigRegistry(write_registry=current_scope) elif opts.get('schedule', False): current_scope = ConfigRegistry.SCHEDULE reg = ConfigRegistry(write_registry=current_scope) else: reg = ConfigRegistry() reg.lock() try: reg.load() changed = {} for arg in args: sep_set = arg.find('=') # set sep_def = arg.find('?') # set if not already set if sep_set == -1 and sep_def == -1: print >> sys.stderr, \ "W: Missing value for config registry variable '%s'" % \ (arg,) continue else: if sep_set > 0 and sep_def == -1: sep = sep_set elif sep_def > 0 and sep_set == -1: sep = sep_def else: sep = min(sep_set, sep_def) key = arg[0:sep] value = arg[sep + 1:] old = reg.get(key) if (old is None or sep == sep_set) and validate_key(key): if not quiet: if reg.has_key(key, write_registry_only=True): print 'Setting %s' % key else: print 'Create %s' % key k = reg.get(key, None, getscope=True) if k and k[0] > current_scope: print >> sys.stderr, \ 'W: %s is overridden by scope "%s"' % \ (key, SCOPE[k[0]]) reg[key] = value changed[key] = (old, value) replog('set', current_scope, reg, key, old, value) else: if not quiet: if old is not None: print 'Not updating %s' % key else: print 'Not setting %s' % key reg.save() finally: reg.unlock() handlers(changed.keys(), (reg, changed))