File indexing completed on 2024-12-01 08:16:20
0001 """ 0002 Module containing classes for getting user input using an editor 0003 """ 0004 0005 # SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im> 0006 # 0007 # SPDX-License-Identifier: GPL-2.0-or-later 0008 0009 import subprocess 0010 import tempfile 0011 0012 import sys 0013 0014 from typing import List 0015 0016 from lab.utils import Utils, LogType 0017 0018 0019 class EditorInput: # pylint: disable=too-few-public-methods 0020 """ 0021 Structure representing the editorinput. 0022 """ 0023 0024 __fulltext: str = "\n" 0025 title: str 0026 body: str 0027 0028 def __fulltext_remove_comments(self) -> None: 0029 newtext = "" 0030 0031 for line in self.__fulltext.splitlines(): 0032 if line.startswith("#"): 0033 continue 0034 0035 newtext += line + "\n" 0036 0037 self.__fulltext = newtext 0038 0039 def __input(self, extra_text: str, placeholder_title: str, placeholder_body: str) -> None: 0040 with tempfile.NamedTemporaryFile("r+") as file: 0041 file.write("# Please enter a title below (one line)\n") 0042 file.write("{}\n".format(placeholder_title)) 0043 file.write("# Please enter a description below (optional) (multiple lines)\n") 0044 file.write("{}\n".format(placeholder_body)) 0045 file.write("\n") 0046 file.write("# Lines starting with '#' will be ignored.\n") 0047 file.write("# An empty title aborts the workflow.\n") 0048 file.write("# {}".format(extra_text)) 0049 file.flush() 0050 0051 subprocess.call(Utils.editor() + [file.name]) 0052 0053 file.seek(0) 0054 self.__fulltext = file.read() 0055 0056 def __fulltext_valid(self) -> bool: 0057 lines = self.__fulltext.splitlines() 0058 0059 if not lines or not lines[0]: 0060 Utils.log(LogType.ERROR, "The first line (title) can't be empty") 0061 return False 0062 0063 return True 0064 0065 def __init__( 0066 self, extra_text: str = "", placeholder_title: str = "", placeholder_body: str = "" 0067 ) -> None: 0068 self.__input(extra_text, placeholder_title, placeholder_body) 0069 self.__fulltext_remove_comments() 0070 if not self.__fulltext_valid(): 0071 Utils.log(LogType.ERROR, "Text not valid, aborting") 0072 sys.exit(1) 0073 0074 lines: List[str] = self.__fulltext.splitlines() 0075 0076 self.title = lines[0] 0077 self.body = "\n".join(lines[1:])