Example #1
0
def handler_set(args, opts=dict(), quiet=False):
    # type: (List[str], Dict[str, Any], bool) -> None
    """
	Set config registry variables in args.
	Args is an array of strings 'key=value' or 'key?value'.

	:param args: Command line arguments.
	:param opts: Command line options.
	:param quiet: Hide output.
	"""
    ucr = _ucr_from_opts(opts)
    with ucr:
        changes = {}  # type: Dict[str, Optional[str]]
        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("W: Missing value for config registry variable '%s'" %
                      (arg, ),
                      file=sys.stderr)
                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:]
            key_exists = ucr.has_key(key,
                                     write_registry_only=True)  # noqa W601
            if (not key_exists or sep == sep_set) and validate_key(key):
                if not quiet:
                    if key_exists:
                        print('Setting %s' % key)
                    else:
                        print('Create %s' % key)
                changes[key] = value
            else:
                if not quiet:
                    if key_exists:
                        print('Not updating %s' % key)
                    else:
                        print('Not setting %s' % key)
        changed = ucr.update(changes)

    _run_changed(ucr, changed,
                 None if quiet else 'W: %s is overridden by scope "%s"')
Example #2
0
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'.
	"""
    ucr = _ucr_from_opts(opts)
    with ucr:
        changes = {}
        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:]
            key_exists = ucr.has_key(key, write_registry_only=True)
            if (not key_exists or sep == sep_set) and validate_key(key):
                if not quiet:
                    if key_exists:
                        print 'Setting %s' % key
                    else:
                        print 'Create %s' % key
                changes[key] = value
            else:
                if not quiet:
                    if key_exists:
                        print 'Not updating %s' % key
                    else:
                        print 'Not setting %s' % key
        changed = ucr.update(changes)

    _run_changed(ucr, changed,
                 None if quiet else 'W: %s is overridden by scope "%s"')
Example #3
0
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))
Example #4
0
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))
def test_validate_key(key, out):
    assert ucrm.validate_key(key, out)
def test_validate_key_invalid(key, out):
    assert not ucrm.validate_key(key, out)