def dump_config(args, config_dict, outfile): """ Dump the default configuration to stdout """ outfmt = args.dump_config for key, value in vars(args).items(): if (key in configuration.Configuration.get_field_names() and value is not None): config_dict[key] = value cfg = configuration.Configuration(**config_dict) # Don't dump default per-command configs for key in commands.get_default_config(): cfg.per_command.pop(key, None) if outfmt == 'yaml': import yaml yaml_register_odict(yaml.SafeDumper) yaml_register_odict(yaml.Dumper) yaml.dump(cfg.as_odict(), outfile, indent=2, default_flow_style=False, sort_keys=False) return if outfmt == 'json': json.dump(cfg.as_odict(), outfile, indent=2) outfile.write('\n') return configuration.python_dump(cfg, outfile)
def dump_config(args, config_dict, outfile): """ Dump the default configuration to stdout """ outfmt = args.dump_config config_dict.update(get_argdict(args)) cfg = configuration.Configuration(**config_dict) # Don't dump default per-command configs for key in commands.get_default_config(): cfg.misc.per_command.pop(key, None) if outfmt == 'yaml': import yaml yaml_register_odict(yaml.SafeDumper) yaml_register_odict(yaml.Dumper) yaml.dump(cfg.as_odict(args.with_help, args.with_defaults), outfile, indent=2, default_flow_style=False, sort_keys=False) return if outfmt == 'json': json.dump(cfg.as_odict(args.with_help, args.with_defaults), outfile, indent=2) outfile.write('\n') return cfg.dump(outfile, with_help=args.with_help, with_defaults=args.with_defaults)
def _update_derived(self): self.per_command_ = commands.get_default_config() for command, cdict in self.per_command.items(): if not isinstance(cdict, dict): logging.warning("Invalid override of type %s for %s", type(cdict), command) continue command = command.lower() if command not in self.per_command_: self.per_command_[command] = {} self.per_command_[command].update(cdict)
def __init__(self, **kwargs): # pylint: disable=W0613 per_command = kwargs.pop("per_command", {}) super(MiscConfig, self).__init__(**kwargs) self.per_command = commands.get_default_config() for command, cdict in per_command.items(): if not isinstance(cdict, dict): logging.warning("Invalid override of type %s for %s", type(cdict), command) continue command = command.lower() if command not in self.per_command: self.per_command[command] = {} self.per_command[command].update(cdict)
def dump_config(args, config_dict, outfile): """ Dump the default configuration to stdout """ outfmt = args.dump_config for key, value in vars(args).items(): if (key in configuration.Configuration.get_field_names() and value is not None): config_dict[key] = value cfg = configuration.Configuration(**config_dict) # Don't dump default per-command configs for key in commands.get_default_config(): cfg.per_command.pop(key, None) if outfmt == 'yaml': import yaml yaml.dump(cfg.as_dict(), sys.stdout, indent=2, default_flow_style=False) return if outfmt == 'json': json.dump(cfg.as_dict(), sys.stdout, indent=2) sys.stdout.write('\n') return ppr = pprint.PrettyPrinter(indent=2) for key in cfg.get_field_names(): helptext = configuration.VARDOCS.get(key, None) if helptext: for line in textwrap.wrap(helptext, 78): outfile.write('# ' + line + '\n') value = getattr(cfg, key) if isinstance(value, dict): outfile.write('{} = {}\n\n'.format(key, json.dumps(value, indent=2))) else: outfile.write('{} = {}\n\n'.format(key, ppr.pformat(value)))
def __init__(self, line_width=80, tab_size=2, max_subgroups_hwrap=2, max_pargs_hwrap=6, separate_ctrl_name_with_space=False, separate_fn_name_with_space=False, dangle_parens=False, dangle_align=None, min_prefix_chars=4, max_prefix_chars=10, max_lines_hwrap=2, bullet_char=None, enum_char=None, line_ending=None, command_case=None, keyword_case=None, additional_commands=None, always_wrap=None, enable_sort=True, autosort=False, enable_markup=True, first_comment_is_literal=False, literal_comment_pattern=None, fence_pattern=None, ruler_pattern=None, emit_byteorder_mark=False, hashruler_min_length=10, canonicalize_hashrulers=True, input_encoding=None, output_encoding=None, per_command=None, layout_passes=None, **_): # pylint: disable=W0613 # pylint: disable=too-many-locals self.line_width = line_width self.tab_size = tab_size self.max_subgroups_hwrap = max_subgroups_hwrap self.max_pargs_hwrap = max_pargs_hwrap self.separate_ctrl_name_with_space = separate_ctrl_name_with_space self.separate_fn_name_with_space = separate_fn_name_with_space self.dangle_parens = dangle_parens self.dangle_align = get_default(dangle_align, "prefix") self.min_prefix_chars = min_prefix_chars self.max_prefix_chars = max_prefix_chars self.max_lines_hwrap = max_lines_hwrap self.bullet_char = str(bullet_char)[0] if bullet_char is None: self.bullet_char = '*' self.enum_char = str(enum_char)[0] if enum_char is None: self.enum_char = '.' self.line_ending = get_default(line_ending, "unix") self.command_case = get_default(command_case, "canonical") assert self.command_case in ("lower", "upper", "canonical", "unchanged") self.keyword_case = get_default(keyword_case, "unchanged") assert self.keyword_case in ("lower", "upper", "unchanged") self.additional_commands = get_default(additional_commands, { 'foo': { 'flags': ['BAR', 'BAZ'], 'kwargs': { 'HEADERS': '*', 'SOURCES': '*', 'DEPENDS': '*' } } }) self.always_wrap = get_default(always_wrap, []) self.enable_sort = enable_sort self.autosort = autosort self.enable_markup = enable_markup self.first_comment_is_literal = first_comment_is_literal self.literal_comment_pattern = literal_comment_pattern self.fence_pattern = get_default(fence_pattern, markup.FENCE_PATTERN) self.ruler_pattern = get_default(ruler_pattern, markup.RULER_PATTERN) self.emit_byteorder_mark = emit_byteorder_mark self.hashruler_min_length = hashruler_min_length self.canonicalize_hashrulers = canonicalize_hashrulers self.layout_passes = get_default(layout_passes, {}) self.input_encoding = get_default(input_encoding, "utf-8") self.output_encoding = get_default(output_encoding, "utf-8") self.fn_spec = commands.get_fn_spec() if additional_commands is not None: for command_name, spec in additional_commands.items(): self.fn_spec.add(command_name, **spec) self.per_command = commands.get_default_config() for command, cdict in get_default(per_command, {}).items(): if not isinstance(cdict, dict): logging.warning("Invalid override of type %s for %s", type(cdict), command) continue command = command.lower() if command not in self.per_command: self.per_command[command] = {} self.per_command[command].update(cdict) assert self.line_ending in ("windows", "unix", "auto"), \ r"Line ending must be either 'windows', 'unix', or 'auto'" self.endl = {'windows': '\r\n', 'unix': '\n', 'auto': '\n'}[self.line_ending] # TODO(josh): this does not belong here! We need some global state for # formatting and the only thing we have accessible through the whole format # stack is this config object... so I'm abusing it by adding this field here self.first_token = None