Skip to content

Commit 1fbf587

Browse files
authored
Refactor: Extract init_tmp_with_template() inner function (#47)
The new ``_template_text_for_temp_file()`` function creates the text written to the temporary file.
1 parent 18430e8 commit 1fbf587

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/blurb/blurb.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,21 @@ def find_editor():
817817
error('Could not find an editor! Set the EDITOR environment variable.')
818818

819819

820+
def _template_text_for_temp_file():
821+
text = template
822+
823+
# Ensure that there is a trailing space after '.. gh-issue:' to make
824+
# filling in the template easier.
825+
issue_line = ".. gh-issue:"
826+
without_space = "\n" + issue_line + "\n"
827+
with_space = "\n" + issue_line + " \n"
828+
if without_space not in text:
829+
sys.exit("Can't find gh-issue line to ensure there's a space on the end!")
830+
text = text.replace(without_space, with_space)
831+
832+
return text
833+
834+
820835
@subcommand
821836
def add():
822837
"""
@@ -829,24 +844,9 @@ def add():
829844
os.close(handle)
830845
atexit.register(lambda : os.unlink(tmp_path))
831846

832-
def init_tmp_with_template():
833-
with open(tmp_path, "wt", encoding="utf-8") as file:
834-
# hack:
835-
# my editor likes to strip trailing whitespace from lines.
836-
# normally this is a good idea. but in the case of the template
837-
# it's unhelpful.
838-
# so, manually ensure there's a space at the end of the gh-issue line.
839-
text = template
840-
841-
issue_line = ".. gh-issue:"
842-
without_space = "\n" + issue_line + "\n"
843-
with_space = "\n" + issue_line + " \n"
844-
if without_space not in text:
845-
sys.exit("Can't find gh-issue line to ensure there's a space on the end!")
846-
text = text.replace(without_space, with_space)
847-
file.write(text)
848-
849-
init_tmp_with_template()
847+
text = _template_text_for_temp_file()
848+
with open(tmp_path, "w", encoding="utf-8") as file:
849+
file.write(text)
850850

851851
# We need to be clever about EDITOR.
852852
# On the one hand, it might be a legitimate path to an

0 commit comments

Comments
 (0)