示例#1
0
文件: frontend.py 项目: B-Rich/smart
def handler_get(args, opts=dict()):
	"""Return config registry variable."""
	ucr = ConfigRegistry()
	ucr.load()

	if not args[0] in ucr:
		return
	if OPT_FILTERS['shell'][2]:
		print '%s: %s' % (args[0], ucr.get(args[0], ''))
	else:
		print ucr.get(args[0], '')
示例#2
0
def handler_get(args, opts=dict()):
    """Return config registry variable."""
    ucr = ConfigRegistry()
    ucr.load()

    if not args[0] in ucr:
        return
    if OPT_FILTERS['shell'][2]:
        print '%s: %s' % (args[0], ucr.get(args[0], ''))
    else:
        print ucr.get(args[0], '')
 def test_update(self):
     """Test update()."""
     ucr = ConfigRegistry()
     ucr['foo'] = 'foo'
     ucr['bar'] = 'bar'
     ucr.update({
         'foo': None,
         'bar': 'baz',
         'baz': 'bar',
     })
     self.assertEqual(ucr.get('foo'), None)
     self.assertEqual(ucr.get('bar'), 'baz')
     self.assertEqual(ucr.get('baz'), 'bar')
示例#4
0
def handler_info(args, opts=dict()):
    # type: (List[str], Dict[str, Any]) -> Iterator[str]
    """
	Print variable info.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
    ucr = ConfigRegistry()
    ucr.load()
    # Import located here, because on module level, a circular import would be
    # created
    import univention.config_registry_info as cri  # pylint: disable-msg=W0403
    cri.set_language('en')
    info = cri.ConfigRegistryInfo(install_mode=False)

    for arg in args:
        try:
            yield variable_info_string(arg,
                                       ucr.get(arg, None),
                                       info.get_variable(arg),
                                       details=_SHOW_EMPTY | _SHOW_DESCRIPTION
                                       | _SHOW_CATEGORIES)
        except UnknownKeyException as ex:
            print(ex, file=sys.stderr)
示例#5
0
def handler_get(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> Iterator[str]
	"""
	Return config registry variable.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	ucr = ConfigRegistry()
	ucr.load()

	if not args[0] in ucr:
		return
	if OPT_FILTERS['shell'][2]:
		yield '%s: %s' % (args[0], ucr.get(args[0], ''))
	else:
		yield ucr.get(args[0], '')
示例#6
0
	def __init__(self, ucr=None):
		# type: (ConfigRegistry) -> None
		if ucr is None:
			ucr = ConfigRegistry()
			ucr.load()
		if isinstance(ucr, ConfigRegistry):
			ucr = VengefulConfigRegistry(ucr)

		self.handler = ucr.get('interfaces/handler', 'ifplugd')
		self.primary = ucr.get('interfaces/primary', 'eth0')
		try:
			self.ipv4_gateway = IPv4Address(ucr['gateway'])
		except KeyError:
			self.ipv4_gateway = None
		except ValueError:
			self.ipv4_gateway = False
		try:
			# <https://tools.ietf.org/html/rfc4007#section-11>
			# As a common notation to specify the scope zone, an
			# implementation SHOULD support the following format:
			# <address>%<zone_id>
			parts = ucr['ipv6/gateway'].rsplit('%', 1)
			gateway = parts.pop(0)
			zone_index = parts[0] if parts else None
			self.ipv6_gateway = IPv6Address(gateway)
			self.ipv6_gateway_zone_index = zone_index
		except KeyError:
			self.ipv6_gateway = None
			self.ipv6_gateway_zone_index = None
		except ValueError:
			self.ipv6_gateway = False
			self.ipv6_gateway_zone_index = None

		self._all_interfaces = {}  # type: Dict[str, _Iface]
		for key, value in ucr.items():
			if not value:
				continue
			match = RE_IFACE.match(key)
			if not match:
				continue
			iface, subkey, ipv6_name = match.groups()
			data = self._all_interfaces.setdefault(iface, _Iface(name=iface))
			data[subkey] = value
			if ipv6_name:
				data.ipv6_names.add(ipv6_name)
示例#7
0
文件: frontend.py 项目: B-Rich/smart
def handler_info(args, opts=dict()):
	"""Print variable info."""
	reg = ConfigRegistry()
	reg.load()
	# Import located here, because on module level, a circular import would be
	# created
	import univention.config_registry_info as cri  # pylint: disable-msg=W0403
	cri.set_language('en')
	info = cri.ConfigRegistryInfo(install_mode=False)

	for arg in args:
		try:
			print_variable_info_string(arg, reg.get(arg, None),
					info.get_variable(arg),
					details=_SHOW_EMPTY | _SHOW_DESCRIPTION | _SHOW_CATEGORIES)
		except UnknownKeyException, ex:
			print >> sys.stderr, ex
def handler_get(args, opts=dict()):
    # type: (List[str], Dict[str, Any]) -> Iterator[str]
    """
	Return config registry variable.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
    ucr = ConfigRegistry()
    ucr.load()
    key = args[0]
    value = ucr.get(key)
    if value is None:
        return
    elif OPT_FILTERS['shell'][2]:
        yield '%s: %s' % (key, value)
    else:
        yield value
示例#9
0
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))
示例#10
0
文件: frontend.py 项目: B-Rich/smart
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))
示例#11
0
def handler_info(args, opts=dict()):
    """Print variable info."""
    ucr = ConfigRegistry()
    ucr.load()
    # Import located here, because on module level, a circular import would be
    # created
    import univention.config_registry_info as cri  # pylint: disable-msg=W0403
    cri.set_language('en')
    info = cri.ConfigRegistryInfo(install_mode=False)

    for arg in args:
        try:
            print_variable_info_string(arg,
                                       ucr.get(arg, None),
                                       info.get_variable(arg),
                                       details=_SHOW_EMPTY | _SHOW_DESCRIPTION
                                       | _SHOW_CATEGORIES)
        except UnknownKeyException as ex:
            print >> sys.stderr, ex
示例#12
0
def handler_info(args, opts=dict()):
    # type: (List[str], Dict[str, Any]) -> Iterator[str]
    """
	Print variable info.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
    ucr = ConfigRegistry()
    ucr.load()
    info = _get_config_registry_info()

    for arg in args:
        try:
            yield variable_info_string(arg,
                                       ucr.get(arg, None),
                                       info.get_variable(arg),
                                       details=_SHOW_EMPTY | _SHOW_DESCRIPTION
                                       | _SHOW_CATEGORIES | _SHOW_DEFAULT)
        except UnknownKeyException as ex:
            print(ex, file=sys.stderr)
 def test_scope_get_normal(self):
     """Test NORMAL ucr.get(key, default)."""
     ucr = ConfigRegistry()
     ucr['foo'] = 'bar'
     self.assertEqual(ucr.get('foo', getscope=True),
                      (ConfigRegistry.NORMAL, 'bar'))
示例#14
0
 def test_scope_get_schedule(self, ucr0):
     """Test SCHEDULE ucr.get(key, default)."""
     ucr = ConfigRegistry(write_registry=ConfigRegistry.SCHEDULE)
     ucr['foo'] = 'bar'
     assert ucr.get('foo',
                    getscope=True) == (ConfigRegistry.SCHEDULE, 'bar')
示例#15
0
 def test_scope_get_forced(self, ucr0):
     """Test FORCED ucr.get(key, default)."""
     ucr = ConfigRegistry(write_registry=ConfigRegistry.FORCED)
     ucr['foo'] = 'bar'
     assert ucr.get('foo', getscope=True) == (ConfigRegistry.FORCED, 'bar')
 def test_scope_get_forced(self):
     """Test FORCED ucr.get(key, default)."""
     ucr = ConfigRegistry(write_registry=ConfigRegistry.FORCED)
     ucr['foo'] = 'bar'
     self.assertEqual(ucr.get('foo', getscope=True),
                      (ConfigRegistry.FORCED, 'bar'))
示例#17
0
 def test_scope_get_ldap(self, ucr0):
     """Test LDAP ucr.get(key, default)."""
     ucr = ConfigRegistry(write_registry=ConfigRegistry.LDAP)
     ucr['foo'] = 'bar'
     assert ucr.get('foo', getscope=True) == (ConfigRegistry.LDAP, 'bar')
 def test_default_get(self):
     """Test ucr.get(key, default)."""
     ucr = ConfigRegistry()
     self.assertEqual(ucr.get('foo', self), self)
 def test_unset_get(self):
     """Test unset ucr[key]."""
     ucr = ConfigRegistry()
     self.assertEqual(ucr.get('foo'), None)
 def test_get(self):
     """Test set ucr.get(key)."""
     ucr = ConfigRegistry()
     ucr['foo'] = 'bar'
     self.assertEqual(ucr.get('foo'), 'bar')
 def test_scope_get_ldap(self):
     """Test LDAP ucr.get(key, default)."""
     ucr = ConfigRegistry(write_registry=ConfigRegistry.LDAP)
     ucr['foo'] = 'bar'
     self.assertEqual(ucr.get('foo', getscope=True),
                      (ConfigRegistry.LDAP, 'bar'))
示例#22
0
文件: frontend.py 项目: B-Rich/smart
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_empty_get(self):
     """Test empty ucr.get(key)."""
     ucr = ConfigRegistry()
     ucr['foo'] = ''
     self.assertEqual(ucr.get('foo'), '')
示例#24
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))