Exemplo n.º 1
0
def handler_search(args, opts=dict()):
	# type: (List[str], Dict[str, Any]) -> Iterator[str]
	"""
	Search for registry variable.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
	search_keys = opts.get('key', False)
	search_values = opts.get('value', False)
	search_all = opts.get('all', False)
	count_search = int(search_keys) + int(search_values) + int(search_all)
	if count_search > 1:
		print('E: at most one out of [--key|--value|--all] may be set', file=sys.stderr)
		sys.exit(1)
	elif count_search == 0:
		search_keys = True
	search_values |= search_all
	search_keys |= search_all

	if not args:
		regex = [re.compile('')]
	else:
		try:
			regex = [re.compile(_) for _ in args]
		except re.error as ex:
			print('E: invalid regular expression: %s' % (ex,), file=sys.stderr)
			sys.exit(1)

	# 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)

	category = opts.get('category', None)
	if category and not info.get_category(category):
		print('E: unknown category: "%s"' % (category,), file=sys.stderr)
		sys.exit(1)

	ucr = ConfigRegistry()
	ucr.load()

	details = _SHOW_EMPTY | _SHOW_DESCRIPTION
	if opts.get('non-empty', False):
		details &= ~_SHOW_EMPTY
	if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False):
		details &= ~_SHOW_DESCRIPTION
	if ucr.is_true('ucr/output/scope', False):
		details |= _SHOW_SCOPE
	if opts.get('verbose', False):
		details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION

	all_vars = {}  # type: Dict[str, Tuple[Optional[str], Optional[cri.Variable], Optional[str]]] # key: (value, vinfo, scope)
	for key, var in info.get_variables(category).items():
		all_vars[key] = (None, var, None)
	for key, (scope, value) in ucr.items(getscope=True):
		try:
			all_vars[key] = (value, all_vars[key][1], scope)
		except LookupError:
			all_vars[key] = (value, None, scope)

	for key, (value2, vinfo, scope2) in all_vars.items():
		for reg in regex:
			if any((
				search_keys and reg.search(key),
				search_values and value2 and reg.search(value2),
				search_all and vinfo and reg.search(vinfo.get('description', ''))
			)):
				yield variable_info_string(key, value2, vinfo, details=details)
				break

	if _SHOW_EMPTY & details and not OPT_FILTERS['shell'][2]:
		patterns = {}  # type: Dict
		for arg in args or ('',):
			patterns.update(info.describe_search_term(arg))
		for pattern, vinfo in patterns.items():
			yield variable_info_string(pattern, None, vinfo, details=details)
Exemplo n.º 2
0
def handler_search(args, opts=dict()):
    # type: (List[str], Dict[str, Any]) -> Iterator[str]
    """
	Search for registry variable.

	:param args: Command line arguments.
	:param opts: Command line options.
	"""
    search_keys = opts.get('key', False)
    search_values = opts.get('value', False)
    search_all = opts.get('all', False)
    count_search = int(search_keys) + int(search_values) + int(search_all)
    if count_search > 1:
        print('E: at most one out of [--key|--value|--all] may be set',
              file=sys.stderr)
        sys.exit(1)
    elif count_search == 0:
        search_keys = True
    search_values |= search_all
    search_keys |= search_all

    if args:
        try:
            search = re.compile('|'.join(
                ('(?:%s)' % (_, ) for _ in args))).search
        except re.error as ex:
            print('E: invalid regular expression: %s' % (ex, ),
                  file=sys.stderr)
            sys.exit(1)
    else:
        search = lambda x: True  # type: ignore # noqa: E731

    info = _get_config_registry_info()

    category = opts.get('category', None)
    if category and not info.get_category(category):
        print('E: unknown category: "%s"' % (category, ), file=sys.stderr)
        sys.exit(1)

    ucr = ConfigRegistry()
    ucr.load()

    details = _SHOW_EMPTY | _SHOW_DESCRIPTION
    if opts.get('non-empty', False):
        details &= ~_SHOW_EMPTY
    if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False):
        details &= ~_SHOW_DESCRIPTION
    if ucr.is_true('ucr/output/scope', False):
        details |= _SHOW_SCOPE
    if opts.get('verbose', False):
        details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION

    all_vars = {
    }  # type: Dict[str, Tuple[Optional[str], Optional[cri.Variable], Optional[str]]] # key: (value, vinfo, scope)
    for key, var in info.get_variables(category).items():
        all_vars[key] = (None, var, None)
    for key, (scope, value) in ucr.items(getscope=True):
        try:
            all_vars[key] = (value, all_vars[key][1], scope)
        except LookupError:
            all_vars[key] = (value, None, scope)

    for key, (value2, vinfo, scope2) in all_vars.items():
        if any((search_keys and search(key), search_values and value2
                and search(value2), search_all and vinfo
                and search(vinfo.get('description', '')))):
            yield variable_info_string(key, value2, vinfo, details=details)

    if _SHOW_EMPTY & details and not OPT_FILTERS['shell'][2]:
        patterns = {}  # type: Dict
        for arg in args or ('', ):
            patterns.update(info.describe_search_term(arg))
        for pattern, vinfo in patterns.items():
            yield variable_info_string(pattern, None, vinfo, details=details)
Exemplo n.º 3
0
	import univention.config_registry_info as cri  # pylint: disable-msg=W0403
	cri.set_language('en')
	info = cri.ConfigRegistryInfo(install_mode=False)

	category = opts.get('category', None)
	if category and not info.get_category(category):
		print >> sys.stderr, 'E: unknown category: "%s"' % (category,)
		sys.exit(1)

	ucr = ConfigRegistry()
	ucr.load()

	details = _SHOW_EMPTY | _SHOW_DESCRIPTION
	if opts.get('non-empty', False):
		details &= ~_SHOW_EMPTY
	if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False):
		details &= ~_SHOW_DESCRIPTION
	if ucr.is_true('ucr/output/scope', False):
		details |= _SHOW_SCOPE
	if opts.get('verbose', False):
		details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION

	all_vars = {}  # key: (value, vinfo, scope)
	for key, var in info.get_variables(category).items():
		all_vars[key] = (None, var, None)
	for key, (scope, value) in ucr.items(getscope=True):
		try:
			all_vars[key] = (value, all_vars[key][1], scope)
		except LookupError:
			all_vars[key] = (value, None, scope)
 def test_is_true_invalid_direct(self):
     """Test invalid is_true(value)."""
     ucr = ConfigRegistry()
     for value in ('yes ', ' yes', ''):
         self.assertFalse(ucr.is_true(value=value), 'is_true(v=%r)' % value)
Exemplo n.º 5
0
    import univention.config_registry_info as cri  # pylint: disable-msg=W0403
    cri.set_language('en')
    info = cri.ConfigRegistryInfo(install_mode=False)

    category = opts.get('category', None)
    if category and not info.get_category(category):
        print >> sys.stderr, 'E: unknown category: "%s"' % (category, )
        sys.exit(1)

    ucr = ConfigRegistry()
    ucr.load()

    details = _SHOW_EMPTY | _SHOW_DESCRIPTION
    if opts.get('non-empty', False):
        details &= ~_SHOW_EMPTY
    if opts.get('brief', False) or ucr.is_true('ucr/output/brief', False):
        details &= ~_SHOW_DESCRIPTION
    if ucr.is_true('ucr/output/scope', False):
        details |= _SHOW_SCOPE
    if opts.get('verbose', False):
        details |= _SHOW_CATEGORIES | _SHOW_DESCRIPTION

    all_vars = {}  # key: (value, vinfo, scope)
    for key, var in info.get_variables(category).items():
        all_vars[key] = (None, var, None)
    for key, (scope, value) in ucr.items(getscope=True):
        try:
            all_vars[key] = (value, all_vars[key][1], scope)
        except LookupError:
            all_vars[key] = (value, None, scope)
 def test_is_true_valid_direct(self):
     """Test valid is_true(value)."""
     ucr = ConfigRegistry()
     for value in ('YES', 'Yes', 'yes', 'true', '1', 'enable', 'enabled',
                   'on'):
         self.assertTrue(ucr.is_true(value=value), 'is_true(v=%r)' % value)
 def test_is_true_invalid(self):
     """Test invalid is_true()."""
     ucr = ConfigRegistry()
     for ucr['foo'] in ('yes ', ' yes', ''):
         self.assertFalse(ucr.is_true('foo'), 'is_true(%(foo)r)' % ucr)
 def test_is_true_valid(self):
     """Test valid is_true()."""
     ucr = ConfigRegistry()
     for ucr['foo'] in ('YES', 'yes', 'Yes', 'true', '1', 'enable',
                        'enabled', 'on'):
         self.assertTrue(ucr.is_true('foo'), 'is_true(%(foo)r)' % ucr)
 def test_is_true_default(self):
     """Test is_true(default)."""
     ucr = ConfigRegistry()
     self.assertTrue(ucr.is_true('foo', True))
     self.assertFalse(ucr.is_true('foo', False))
 def test_is_true_unset(self):
     """Test unset is_true()."""
     ucr = ConfigRegistry()
     self.assertFalse(ucr.is_true('foo'))