def test_keyword_case(self): config_dict = self.config.as_dict() config_dict['keyword_case'] = 'upper' self.config = configuration.Configuration(**config_dict) self.do_format_test("""\ foo(bar baz) """, """\ foo(BAR BAZ) """) config_dict = self.config.as_dict() config_dict['keyword_case'] = 'lower' self.config = configuration.Configuration(**config_dict) self.do_format_test("""\ foo(bar baz) """, """\ foo(bar baz) """) config_dict = self.config.as_dict() config_dict['command_case'] = 'unchanged' self.config = configuration.Configuration(**config_dict) self.do_format_test("""\ foo(BaR bAz) """, """\ foo(bar baz) """)
def test_command_case(self): with self.subTest(): assert_format(self, """\ FOO(bar baz) """, """\ foo(bar baz) """) config_dict = self.config.as_dict() config_dict['command_case'] = 'upper' self.config = configuration.Configuration(**config_dict) with self.subTest(): assert_format(self, """\ foo(bar baz) """, """\ FOO(bar baz) """) config_dict = self.config.as_dict() config_dict['command_case'] = 'unchanged' self.config = configuration.Configuration(**config_dict) with self.subTest(): assert_format(self, """\ FoO(bar baz) """, """\ FoO(bar baz) """)
def test_command_case(self): self.do_format_test("""\ FOO(bar baz) """, """\ foo(bar baz) """) config_dict = self.config.as_dict() config_dict['command_case'] = 'upper' self.config = configuration.Configuration(**config_dict) self.do_format_test("""\ foo(bar baz) """, """\ FOO(bar baz) """) config_dict = self.config.as_dict() config_dict['command_case'] = 'unchanged' self.config = configuration.Configuration(**config_dict) self.do_format_test("""\ FoO(bar baz) """, """\ FoO(bar baz) """)
def FormatCMake(contents): from cmake_format import configuration, lexer, parse, formatter cfg = configuration.Configuration() tokens = lexer.tokenize(contents) parse_tree = parse.parse(tokens) box_tree = formatter.layout_tree(parse_tree, cfg) return formatter.write_tree(box_tree, cfg, contents)
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 standard_funs.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 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 setup_argparser(arg_parser): """ Add argparse options to the parser. """ arg_parser.add_argument('-v', '--version', action='version', version=cmake_format.VERSION) arg_parser.add_argument( "-f", "--format", choices=["page", "stub"], default="stub", help="whether to output a standalone `page` complete with <html></html> " "tags, or just the annotated content") arg_parser.add_argument('-o', '--outfile-path', default=None, help='Where to write the formatted file. ' 'Default is stdout.') arg_parser.add_argument('-c', '--config-file', help='path to configuration file') arg_parser.add_argument('infilepaths', nargs='*') configuration.Configuration().add_to_argparser(arg_parser)
def setup_argparse(argparser): argparser.add_argument('-v', '--version', action='version', version=cmake_format.VERSION) argparser.add_argument('-l', '--log-level', default="info", choices=["error", "warning", "info", "debug"]) mutex = argparser.add_mutually_exclusive_group() mutex.add_argument('--dump-config', choices=['yaml', 'json', 'python'], default=None, const='python', nargs='?', help='If specified, print the default configuration to ' 'stdout and exit') mutex.add_argument('-o', '--outfile-path', default=None, help='Write errors to this file. ' 'Default is stdout.') argparser.add_argument('-c', '--config-files', nargs='+', help='path to configuration file(s)') argparser.add_argument('infilepaths', nargs='*') configuration.Configuration().add_to_argparser(argparser)
def dump_config(outfmt, outfile): """ Dump the default configuration to stdout """ cfg = configuration.Configuration() if outfmt == 'yaml': import yaml yaml.dump(cfg.as_dict(), sys.stdout, indent=2, default_flow_style=False) return elif 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 exec_sidecar(test, body, meta, input_str): """ Assert a formatting and, optionally, a lex, parse, or layout tree. """ if input_str is None: input_str = body expect_lex = meta.pop("expect_lex", None) if expect_lex is not None: with test.subTest(phase="lex"): assert_lex(test, input_str, expect_lex) expect_parse = meta.pop("expect_parse", None) if expect_parse is not None: with test.subTest(phase="parse"): assert_parse(test, input_str, expect_parse) expect_layout = meta.pop("expect_layout", None) if expect_layout is not None: with test.subTest(phase="layout"): assert_layout(test, input_str, expect_layout) test.config = configuration.Configuration(**meta) # TODO(josh): just move this into the configuration for the one test where # it's needed. test.config.parse.fn_spec.add('foo', flags=['BAR', 'BAZ'], kwargs={ "HEADERS": '*', "SOURCES": '*', "DEPENDS": '*' }) with test.subTest(phase="format"): assert_format(test, input_str, body)
def add_config_options(arg_parser): """ Add configuration options as flags to the argument parser """ format_group = arg_parser.add_argument_group( title="Formatter Configuration", description="Override configfile options affecting general formatting") comment_group = arg_parser.add_argument_group( title="Comment Formatting", description="Override config options affecting comment formatting") misc_group = arg_parser.add_argument_group( title="Misc Options", description="Override miscellaneous config options") default_config = configuration.Configuration().as_dict() if sys.version_info[0] >= 3: value_types = (str, int, float) else: value_types = (str, unicode, int, float) for key in configuration.Configuration.get_field_names(): if key in configuration.COMMENT_GROUP: optgroup = comment_group elif key in configuration.MISC_GROUP: optgroup = misc_group else: optgroup = format_group value = default_config[key] helptext = configuration.VARDOCS.get(key, None) # NOTE(josh): argparse store_true isn't what we want here because we want # to distinguish between "not specified" = "default" and "specified" if key == 'additional_commands': continue elif isinstance(value, bool): optgroup.add_argument('--' + key.replace('_', '-'), nargs='?', default=None, const=(not value), type=configuration.parse_bool, help=helptext) elif isinstance(value, value_types): optgroup.add_argument('--' + key.replace('_', '-'), type=type(value), help=helptext, choices=configuration.VARCHOICES.get(key, None)) elif value is None: # If the value is None then we can't really tell what it's supposed to # be. I guess let's assume string in this case. optgroup.add_argument('--' + key.replace('_', '-'), help=helptext, choices=configuration.VARCHOICES.get(key, None)) # NOTE(josh): argparse behavior is that if the flag is not specified on # the command line the value will be None, whereas if it's specified with # no arguments then the value will be an empty list. This exactly what we # want since we can ignore `None` values. elif isinstance(value, (list, tuple)): typearg = None if value: typearg = type(value[0]) optgroup.add_argument('--' + key.replace('_', '-'), nargs='*', type=typearg, help=helptext)
def setup_argparser(argparser): """ Add argparse options to the parser. """ argparser.add_argument('-v', '--version', action='version', version=cmake_format.VERSION) argparser.add_argument('-l', '--log-level', default="info", choices=["error", "warning", "info", "debug"]) mutex = argparser.add_mutually_exclusive_group() mutex.add_argument('--dump-config', choices=['yaml', 'json', 'python'], default=None, const='python', nargs='?', help='If specified, print the default configuration to ' 'stdout and exit') mutex.add_argument('--dump', choices=['lex', 'parse', 'layout', 'markup'], default=None) argparser.add_argument( "--no-help", action="store_false", dest="with_help", help="When used with --dump-config, will omit helptext comments in the" " output") argparser.add_argument( "--no-default", action="store_false", dest="with_defaults", help="When used with --dump-config, will omit any unmodified " "configuration value.") mutex = argparser.add_mutually_exclusive_group() mutex.add_argument('-i', '--in-place', action='store_true') mutex.add_argument( '--check', action='store_true', help="Exit with status code 0 if formatting would not change file " "contents, or status code 1 if it would") mutex.add_argument('-o', '--outfile-path', default=None, help='Where to write the formatted file. ' 'Default is stdout.') argparser.add_argument('-c', '--config-files', nargs='+', help='path to configuration file(s)') argparser.add_argument('infilepaths', nargs='*') configuration.Configuration().add_to_argparser(argparser)
def __init__(self, parse_db=None, lint_ctx=None, config=None): if parse_db is None: from cmake_format import parse_funs parse_db = parse_funs.get_parse_db() self.parse_db = parse_db if lint_ctx is None: lint_ctx = MockEverything() self.lint_ctx = lint_ctx if config is None: config = configuration.Configuration() self.config = config
def test_auto_line_endings(self): config_dict = self.config.as_dict() config_dict['line_ending'] = 'auto' self.config = configuration.Configuration(**config_dict) self.do_format_test( " #[[*********************************************\r\n" " * Information line 1\r\n" " * Information line 2\r\n" " ************************************************]]\r\n", " #[[*********************************************\r\n" " * Information line 1\r\n" " * Information line 2\r\n" " ************************************************]]\r\n")
def setUp(self): self.config = configuration.Configuration() parse_db = parse_funs.get_parse_db() self.parse_ctx = parse.ParseContext(parse_db) self.config.parse.fn_spec.add('foo', flags=['BAR', 'BAZ'], kwargs={ "HEADERS": '*', "SOURCES": '*', "DEPENDS": '*' }) self.parse_ctx.parse_db.update( parse_funs.get_funtree(self.config.parse.fn_spec))
def setup_argparse(argparser): argparser.add_argument('-v', '--version', action='version', version=cmake_format.VERSION) argparser.add_argument('-l', '--log-level', default="info", choices=["error", "warning", "info", "debug"]) mutex = argparser.add_mutually_exclusive_group() mutex.add_argument('--dump-config', choices=['yaml', 'json', 'python'], default=None, const='python', nargs='?', help='If specified, print the default configuration to ' 'stdout and exit') mutex.add_argument('-o', '--outfile-path', default=None, help='Write errors to this file. ' 'Default is stdout.') argparser.add_argument( "--no-help", action="store_false", dest="with_help", help="When used with --dump-config, will omit helptext comments in the" " output") argparser.add_argument( "--no-default", action="store_false", dest="with_defaults", help="When used with --dump-config, will omit any unmodified " "configuration value.") argparser.add_argument( "--suppress-decorations", action="store_true", help="Suppress the file title decoration and summary statistics") argparser.add_argument('-c', '--config-files', nargs='+', help='path to configuration file(s)') argparser.add_argument('infilepaths', nargs='*') configuration.Configuration().add_to_argparser(argparser)
def test_windows_line_endings_output(self): config_dict = self.config.as_dict() config_dict['line_ending'] = 'windows' self.config = configuration.Configuration(**config_dict) self.do_format_test( """\ #[[********************************************* * Information line 1 * Information line 2 ************************************************]]""", " #[[*********************************************\r\n" " * Information line 1\r\n" " * Information line 2\r\n" " ************************************************]]\r\n")
def test_windows_line_endings_output(self): config_dict = self.config.as_dict() config_dict['line_ending'] = 'windows' self.config = configuration.Configuration(**config_dict) self.source_str = """\ #[[********************************************* * Information line 1 * Information line 2 ************************************************]]""" self.expect_format = ( "#[[*********************************************\r\n" "* Information line 1\r\n" "* Information line 2\r\n" "************************************************]]\r\n")
def test_auto_line_endings(self): config_dict = self.config.as_dict() config_dict['line_ending'] = 'auto' self.config = configuration.Configuration(**config_dict) self.source_str = ( "#[[*********************************************\r\n" "* Information line 1\r\n" "* Information line 2\r\n" "************************************************]]\r\n") self.expect_format = ( "#[[*********************************************\r\n" "* Information line 1\r\n" "* Information line 2\r\n" "************************************************]]\r\n")
def __init__(self, parse_db=None, lint_ctx=None, config=None): if parse_db is None: from cmake_format import parse_funs parse_db = parse_funs.get_parse_db() self.parse_db = parse_db if lint_ctx is None: lint_ctx = MockEverything() self.lint_ctx = lint_ctx if config is None: from cmake_format import configuration config = configuration.Configuration() self.config = config # List of currently open parse nodes. Only used by nodes below # the statement level. self.argstack = []
def __init__(self, *args, **kwargs): super(TestBase, self).__init__(*args, **kwargs) self.config = configuration.Configuration() self.parse_db = parse_funs.get_parse_db() self.source_str = None self.expect_lex = None self.expect_parse = None self.expect_layout = None self.expect_format = None # NOTE(josh): hacky introspective way of automatically calling # assertExpectations() at the end of every test_XXX() function for name in dir(self): if not name.startswith("test_"): continue value = getattr(self, name) if callable(value): setattr(self, name, WrapTestWithRunFun(self, value))
def main(): rootdir = os.sep.join(os.path.realpath(__file__).split(os.sep)[:-3]) argparser = argparse.ArgumentParser(description=__doc__) argparser.add_argument("--sourcefile", default=os.path.join( rootdir, "cmake_format/doc/configopts.rst")) argparser.add_argument("outfile", nargs="?", default="-") args = argparser.parse_args() with io.open(args.sourcefile, "r", encoding="utf-8") as infile: data = parse_sourcefile(infile) if args.outfile == "-": args.outfile = os.dup(sys.stdout.fileno()) with io.open(args.outfile, "w", encoding="utf-8") as outfile: outfile.write(".. _configopts:\n\n") write_outfile(outfile, data["global"], configuration.Configuration(), "global")
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 dump_config(outfmt, outfile): """ Dump the default configuration to stdout """ cfg = configuration.Configuration() if outfmt == 'yaml': import yaml yaml.dump(cfg.as_dict(), sys.stdout, indent=2, default_flow_style=False) return elif 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(): value = configuration.serialize(getattr(cfg, key)) outfile.write('{} = {}\n'.format(key, ppr.pformat(value)))
def main(): """Parse arguments, open files, start work.""" arg_parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE_STRING) arg_parser.add_argument('-v', '--version', action='version', version='0.3.2') mutex = arg_parser.add_mutually_exclusive_group() mutex.add_argument('--dump-config', choices=['yaml', 'json', 'python'], help='If specified, print the default configuration to ' 'stdout and exit') mutex.add_argument('-i', '--in-place', action='store_true') mutex.add_argument('-o', '--outfile-path', default=None, help='Where to write the formatted file. ' 'Default is stdout.') arg_parser.add_argument('-c', '--config-file', help='path to configuration file') arg_parser.add_argument('infilepaths', nargs='*') optgroup = arg_parser.add_argument_group( title='Formatter Configuration', description='Override configfile options') for key, value in configuration.Configuration().as_dict().items(): flag = '--{}'.format(key.replace('_', '-')) if isinstance(value, (dict, list)): continue if key == 'additional_commands': continue optgroup.add_argument(flag, type=type(value), default=argparse.SUPPRESS) args = arg_parser.parse_args() if args.dump_config: dump_config(args.dump_config, sys.stdout) sys.exit(0) assert args.in_place is False or args.outfile_path is None, \ "if inplace is specified than outfile is invalid" assert (len(args.infilepaths) == 1 or (args.in_place is True or args.outfile_path == '-')), \ ("if more than one input file is specified, then formatting must be done" " in-place or written to stdout") if args.outfile_path is None: args.outfile_path = '-' for infile_path in args.infilepaths: config_dict = get_config(infile_path, args.config_file) config_dict.update(vars(args)) cfg = configuration.Configuration(**config_dict) if args.in_place: outfile = tempfile.NamedTemporaryFile(delete=False, mode='w') else: if args.outfile_path == '-': outfile = sys.stdout else: outfile = open(args.outfile_path, 'w') parse_ok = True try: with open(infile_path, 'r') as infile: try: process_file(cfg, infile, outfile) except: sys.stderr.write( 'Error while processing {}\n'.format(infile_path)) raise except: parse_ok = False sys.stderr.write('While processing {}\n'.format(infile_path)) raise finally: if args.in_place: outfile.close() if parse_ok: shutil.move(outfile.name, infile_path) else: if args.outfile_path != '-': outfile.close() return 0
def main(): """Parse arguments, open files, start work.""" logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s") argparser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE_STRING) setup_argparse(argparser) args = argparser.parse_args() logging.getLogger().setLevel(getattr(logging, args.log_level.upper())) if '-' in args.infilepaths: assert len(args.infilepaths) == 1, \ "You cannot mix stdin as an input with other input files" if args.outfile_path is None: args.outfile_path = '-' outfile_arg = args.outfile_path if outfile_arg == '-': outfile_arg = os.dup(sys.stdout.fileno()) returncode = 0 outdict = collections.OrderedDict() for infile_path in args.infilepaths: cfg = configuration.Configuration() if infile_path == '-': infile_path = os.dup(sys.stdin.fileno()) try: infile = io.open( infile_path, mode='r', encoding=cfg.encode.input_encoding, newline='') except (IOError, OSError): logger.error("Failed to open %s for read", infile_path) returncode = 1 try: with infile: intext = infile.read() except UnicodeDecodeError: logger.error( "Unable to read %s as %s", infile_path, cfg.encode.input_encoding) try: parse_tree = process_file(cfg, intext) cmd_spec = process_tree(parse_tree) outdict.update(cmd_spec) except: logger.warning('While processing %s', infile_path) raise outfile = io.open(outfile_arg, mode='w', encoding="utf-8", newline='') if args.output_format == "json": json.dump(outdict, outfile, indent=2) elif args.output_format == "yaml": import yaml __main__.yaml_register_odict(yaml.SafeDumper) __main__.yaml_register_odict(yaml.Dumper) yaml.dump(outdict, outfile, indent=2, default_flow_style=False, sort_keys=False) elif args.output_format == "python": ppr = pprint.PrettyPrinter(indent=2, width=80) outfile.write(ppr.pformat(dict(outdict))) else: logger.error("Unrecognized output format {}".format(args.output_format)) outfile.write("\n") outfile.close() return returncode
def main(): """Parse arguments, open files, start work.""" # set up main logger, which logs everything. We'll leave this one logging # to the console logging.basicConfig(level=logging.INFO) arg_parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE_STRING) setup_argparser(arg_parser) args = arg_parser.parse_args() assert (len(args.infilepaths) == 1 or args.outfile_path is None), \ ("if more than one input file is specified, then annotates must be " "written to stdout") if args.outfile_path is None: args.outfile_path = '-' if '-' in args.infilepaths: assert len(args.infilepaths) == 1, \ "You cannot mix stdin as an input with other input files" assert args.outfile_path == '-', \ "If stdin is the input file, then stdout must be the output file" for infile_path in args.infilepaths: # NOTE(josh): have to load config once for every file, because we may pick # up a new config file location for each path if infile_path == '-': config_dict = __main__.get_config(os.getcwd(), args.config_file) else: config_dict = __main__.get_config(infile_path, args.config_file) 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) if args.outfile_path == '-': # NOTE(josh): The behavior or sys.stdout is different in python2 and # python3. sys.stdout is opened in 'w' mode which means that write() # takes strings in python2 and python3 and, in particular, in python3 # it does not take byte arrays. io.StreamWriter will write to # it with byte arrays (assuming it was opened with 'wb'). So we use # io.open instead of open in this case outfile = io.open(os.dup(sys.stdout.fileno()), mode='w', encoding=cfg.output_encoding, newline='') else: outfile = io.open(args.outfile_path, 'w', encoding=cfg.output_encoding, newline='') if infile_path == '-': infile = io.open(os.dup(sys.stdin.fileno()), mode='r', encoding=cfg.input_encoding, newline='') else: infile = io.open(infile_path, 'r', encoding=cfg.input_encoding) try: with infile: annotate_file(cfg, infile, outfile, args.format) except: sys.stderr.write('While processing {}\n'.format(infile_path)) raise finally: outfile.close() return 0
def __init__(self, *args, **kwargs): super(TestCanonicalLayout, self).__init__(*args, **kwargs) self.config = configuration.Configuration()
def inner_main(): """Parse arguments, open files, start work.""" arg_parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, usage=USAGE_STRING) setup_argparser(arg_parser) args = arg_parser.parse_args() logging.getLogger().setLevel(getattr(logging, args.log_level.upper())) if args.dump_config: config_dict = get_config(os.getcwd(), args.config_files) dump_config(args, config_dict, sys.stdout) sys.exit(0) if args.dump_config or args.dump: assert not args.in_place, \ ("-i/--in-place not allowed when dumping") assert args.in_place is False or args.outfile_path is None, \ "if inplace is specified than outfile is invalid" assert (len(args.infilepaths) == 1 or (args.in_place is True or args.outfile_path is None)), \ ("if more than one input file is specified, then formatting must be done" " in-place or written to stdout") if args.outfile_path is None: args.outfile_path = '-' if '-' in args.infilepaths: assert len(args.infilepaths) == 1, \ "You cannot mix stdin as an input with other input files" assert args.outfile_path == '-', \ "If stdin is the input file, then stdout must be the output file" argparse_dict = get_argdict(args) returncode = 0 for infile_path in args.infilepaths: # NOTE(josh): have to load config once for every file, because we may pick # up a new config file location for each path if infile_path == '-': config_dict = get_config(os.getcwd(), args.config_files) else: config_dict = get_config(infile_path, args.config_files) cfg = configuration.Configuration(**config_dict) cfg.legacy_consume(argparse_dict) if infile_path == '-': infile = io.open(os.dup(sys.stdin.fileno()), mode='r', encoding=cfg.encode.input_encoding, newline='') else: infile = io.open(infile_path, 'r', encoding=cfg.encode.input_encoding, newline='') with infile: intext = infile.read() try: outtext, reflow_valid = process_file(cfg, intext, args.dump) if cfg.format.require_valid_layout and not reflow_valid: logger.error("Failed to format %s", infile_path) returncode = 1 continue except: logger.warning('While processing %s', infile_path) raise if args.check: if intext != outtext: returncode = 1 continue if args.in_place: if intext == outtext: logger.debug("No delta for %s", infile_path) continue tempfile_path = infile_path + ".cmf-temp" outfile = io.open(tempfile_path, 'w', encoding=cfg.encode.output_encoding, newline='') else: if args.outfile_path == '-': # NOTE(josh): The behavior of sys.stdout is different in python2 and # python3. sys.stdout is opened in 'w' mode which means that write() # takes strings in python2 and python3 and, in particular, in python3 # it does not take byte arrays. io.StreamWriter will write to # it with byte arrays (assuming it was opened with 'wb'). So we use # io.open instead of open in this case outfile = io.open(os.dup(sys.stdout.fileno()), mode='w', encoding=cfg.encode.output_encoding, newline='') else: outfile = io.open(args.outfile_path, 'w', encoding=cfg.encode.output_encoding, newline='') with outfile: outfile.write(outtext) if args.in_place: shutil.copymode(infile_path, tempfile_path) shutil.move(tempfile_path, infile_path) return returncode
def onefile_main(infile_path, args, argparse_dict): """ Find config, open file, process, write result """ # NOTE(josh): have to load config once for every file, because we may pick # up a new config file location for each path if infile_path == '-': config_dict = get_config(os.getcwd(), args.config_files) else: config_dict = get_config(infile_path, args.config_files) cfg = configuration.Configuration(**config_dict) cfg.legacy_consume(argparse_dict) if cfg.format.disable: return if infile_path == '-': infile = io.open(os.dup(sys.stdin.fileno()), mode='r', encoding=cfg.encode.input_encoding, newline='') else: infile = io.open(infile_path, 'r', encoding=cfg.encode.input_encoding, newline='') with infile: intext = infile.read() try: outtext, reflow_valid = process_file(cfg, intext, args.dump) if cfg.format.require_valid_layout and not reflow_valid: raise common.FormatError("Failed to format {}".format(infile_path)) except: logger.warning('While processing %s', infile_path) raise if args.check: if intext != outtext: raise common.FormatError("Check failed: {}".format(infile_path)) if args.in_place: if intext == outtext: logger.debug("No delta for %s", infile_path) return tempfile_path = infile_path + ".cmf-temp" outfile = io.open(tempfile_path, 'w', encoding=cfg.encode.output_encoding, newline='') else: if args.outfile_path == '-': # NOTE(josh): The behavior of sys.stdout is different in python2 and # python3. sys.stdout is opened in 'w' mode which means that write() # takes strings in python2 and python3 and, in particular, in python3 # it does not take byte arrays. io.StreamWriter will write to # it with byte arrays (assuming it was opened with 'wb'). So we use # io.open instead of open in this case outfile = io.open(os.dup(sys.stdout.fileno()), mode='w', encoding=cfg.encode.output_encoding, newline='') else: outfile = io.open(args.outfile_path, 'w', encoding=cfg.encode.output_encoding, newline='') with outfile: outfile.write(outtext) if args.in_place: shutil.copymode(infile_path, tempfile_path) shutil.move(tempfile_path, infile_path)