@@ -355,6 +355,14 @@ def get_parser() -> argparse.ArgumentParser:
355
355
dest = "omit_empty_versions" ,
356
356
help = "Omit empty versions from the output. Default: unset (false)." ,
357
357
)
358
+ parser .add_argument (
359
+ "-e" ,
360
+ "--encoding" ,
361
+ action = "store" ,
362
+ dest = "encoding" ,
363
+ help = "Specify the encoding to use when reading and writing files. Default: utf-8." ,
364
+ default = "utf-8" ,
365
+ )
358
366
parser .add_argument (
359
367
"-Z" ,
360
368
"--no-zerover" ,
@@ -541,6 +549,7 @@ def build_and_render(
541
549
filter_commits : str | None = None ,
542
550
jinja_context : dict [str , Any ] | None = None ,
543
551
versioning : Literal ["pep440" , "semver" ] = "semver" ,
552
+ encoding : str = "utf-8" ,
544
553
) -> tuple [Changelog , str ]:
545
554
"""Build a changelog and render it.
546
555
@@ -627,7 +636,7 @@ def build_and_render(
627
636
# render new entries in-place
628
637
if in_place :
629
638
# read current changelog lines
630
- with open (output ) as changelog_file : # type: ignore[arg-type]
639
+ with open (output , encoding = encoding ) as changelog_file : # type: ignore[arg-type]
631
640
lines = changelog_file .read ().splitlines ()
632
641
633
642
# prepare version regex and marker line
@@ -671,7 +680,7 @@ def build_and_render(
671
680
lines [marker : marker + marker2 + 2 ] = [rendered ]
672
681
673
682
# write back updated changelog lines
674
- with open (output , "w" ) as changelog_file : # type: ignore[arg-type]
683
+ with open (output , "w" , encoding = encoding ) as changelog_file : # type: ignore[arg-type]
675
684
changelog_file .write ("\n " .join (lines ).rstrip ("\n " ) + "\n " )
676
685
677
686
# overwrite output file
@@ -682,7 +691,7 @@ def build_and_render(
682
691
if output is sys .stdout :
683
692
sys .stdout .write (rendered )
684
693
else :
685
- with open (output , "w" ) as stream : # type: ignore[arg-type]
694
+ with open (output , "w" , encoding = encoding ) as stream : # type: ignore[arg-type]
686
695
stream .write (rendered )
687
696
688
697
return changelog , rendered
@@ -692,6 +701,7 @@ def get_release_notes(
692
701
input_file : str | Path = "CHANGELOG.md" ,
693
702
version_regex : str = DEFAULT_VERSION_REGEX ,
694
703
marker_line : str = DEFAULT_MARKER_LINE ,
704
+ encoding : str = "utf-8" ,
695
705
) -> str :
696
706
"""Get release notes from existing changelog.
697
707
@@ -708,7 +718,7 @@ def get_release_notes(
708
718
release_notes = []
709
719
found_marker = False
710
720
found_version = False
711
- with open (input_file ) as changelog :
721
+ with open (input_file , encoding = encoding ) as changelog :
712
722
for line in changelog :
713
723
line = line .strip () # noqa: PLW2901
714
724
if not found_marker :
@@ -731,6 +741,7 @@ def output_release_notes(
731
741
version_regex : str = DEFAULT_VERSION_REGEX ,
732
742
marker_line : str = DEFAULT_MARKER_LINE ,
733
743
output_file : str | TextIO | None = None ,
744
+ encoding : str = "utf-8" ,
734
745
) -> None :
735
746
"""Print release notes from existing changelog.
736
747
@@ -743,11 +754,11 @@ def output_release_notes(
743
754
output_file: Where to print/write the release notes.
744
755
"""
745
756
output_file = output_file or sys .stdout
746
- release_notes = get_release_notes (input_file , version_regex , marker_line )
757
+ release_notes = get_release_notes (input_file , version_regex , marker_line , encoding = encoding )
747
758
try :
748
759
output_file .write (release_notes ) # type: ignore[union-attr]
749
760
except AttributeError :
750
- with open (output_file , "w" ) as file : # type: ignore[arg-type]
761
+ with open (output_file , "w" , encoding = encoding ) as file : # type: ignore[arg-type]
751
762
file .write (release_notes )
752
763
753
764
@@ -770,6 +781,7 @@ def main(args: list[str] | None = None) -> int:
770
781
version_regex = settings ["version_regex" ],
771
782
marker_line = settings ["marker_line" ],
772
783
output_file = None , # force writing to stdout
784
+ encoding = settings ["encoding" ],
773
785
)
774
786
return 0
775
787
0 commit comments