def __init__(self, ucr=None): # type: (ConfigRegistry) -> None if ucr is None: ucr = ConfigRegistry() ucr.load() if isinstance(ucr, ConfigRegistry): ucr = VengefulConfigRegistry(ucr) self.primary = ucr.get('interfaces/primary', 'eth0') try: self.ipv4_gateway = IPv4Address( u"%(gateway)s" % ucr) # type: Union[IPv4Address, None, bool] 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( u"%s" % (gateway, )) # type: Union[IPv6Address, None, bool] 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)
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)
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) for key, (value, vinfo, scope) in all_vars.items(): for reg in regex: if ((search_keys and reg.search(key)) or (search_values and value and reg.search(value)) or (search_all and vinfo and reg.search(vinfo.get('description', '')))): print_variable_info_string(key, value, vinfo, details=details) break if _SHOW_EMPTY & details and not OPT_FILTERS['shell'][2]:
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)