@@ -49,6 +49,35 @@ def _get_default_option(option_name: str) -> Any:
4949 return getattr (default_values , option_name )
5050
5151
52+ def _determine_linesep (
53+ strategy : str = "preserve" , filenames : Tuple [str , ...] = ()
54+ ) -> str :
55+ """
56+ Determine and return linesep string for OutputWriter to use.
57+ Valid strategies: "LF", "CRLF", "native", "preserve"
58+ When preserving, files are checked in order for existing newlines.
59+ """
60+ if strategy == "preserve" :
61+ for fname in filenames :
62+ try :
63+ with open (fname , "rb" ) as existing_file :
64+ existing_text = existing_file .read ()
65+ except FileNotFoundError :
66+ continue
67+ if b"\r \n " in existing_text :
68+ strategy = "CRLF"
69+ break
70+ elif b"\n " in existing_text :
71+ strategy = "LF"
72+ break
73+ return {
74+ "native" : os .linesep ,
75+ "LF" : "\n " ,
76+ "CRLF" : "\r \n " ,
77+ "preserve" : "\n " ,
78+ }[strategy ]
79+
80+
5281@click .command (context_settings = {"help_option_names" : ("-h" , "--help" )})
5382@click .version_option (** version_option_kwargs )
5483@click .pass_context
@@ -510,24 +539,9 @@ def cli(
510539
511540 log .debug ("" )
512541
513- # Determine linesep for OutputWriter to use
514- if newline == "preserve" :
515- for fname in (output_file .name , * src_files ):
516- if os .path .exists (fname ):
517- with open (fname , "rb" ) as existing_file :
518- existing_text = existing_file .read ().decode ()
519- if "\r \n " in existing_text :
520- newline = "CRLF"
521- break
522- elif "\n " in existing_text :
523- newline = "LF"
524- break
525- linesep = {
526- "native" : os .linesep ,
527- "LF" : "\n " ,
528- "CRLF" : "\r \n " ,
529- "preserve" : "\n " ,
530- }[newline ]
542+ linesep = _determine_linesep (
543+ strategy = newline , filenames = (output_file .name , * src_files )
544+ )
531545
532546 ##
533547 # Output
0 commit comments