|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Creates a new SeleniumBase chart presentation with boilerplate code. |
| 4 | +
|
| 5 | +Usage: |
| 6 | + seleniumbase mkchart [FILE.py] [LANG] |
| 7 | + or sbase mkchart [FILE.py] [LANG] |
| 8 | +
|
| 9 | +Example: |
| 10 | + sbase mkchart new_chart.py --en |
| 11 | +
|
| 12 | +Language Options: |
| 13 | + --en / --English | --zh / --Chinese |
| 14 | + --nl / --Dutch | --fr / --French |
| 15 | + --it / --Italian | --ja / --Japanese |
| 16 | + --ko / --Korean | --pt / --Portuguese |
| 17 | + --ru / --Russian | --es / --Spanish |
| 18 | +
|
| 19 | +Output: |
| 20 | + Creates a new SeleniumBase chart presentation. |
| 21 | + If the file already exists, an error is raised. |
| 22 | + By default, the slides are written in English, |
| 23 | + and use a "sky" theme with "slide" transition. |
| 24 | + The chart can be used as a basic boilerplate. |
| 25 | +""" |
| 26 | + |
| 27 | +import codecs |
| 28 | +import colorama |
| 29 | +import os |
| 30 | +import sys |
| 31 | + |
| 32 | + |
| 33 | +def invalid_run_command(msg=None): |
| 34 | + exp = (" ** mkchart **\n\n") |
| 35 | + exp += " Usage:\n" |
| 36 | + exp += " seleniumbase mkchart [FILE.py] [LANG]\n" |
| 37 | + exp += " OR sbase mkchart [FILE.py] [LANG]\n" |
| 38 | + exp += " Example:\n" |
| 39 | + exp += " sbase mkchart new_chart.py --en\n" |
| 40 | + exp += " Language Options:\n" |
| 41 | + exp += " --en / --English | --zh / --Chinese\n" |
| 42 | + exp += " --nl / --Dutch | --fr / --French\n" |
| 43 | + exp += " --it / --Italian | --ja / --Japanese\n" |
| 44 | + exp += " --ko / --Korean | --pt / --Portuguese\n" |
| 45 | + exp += " --ru / --Russian | --es / --Spanish\n" |
| 46 | + exp += " Output:\n" |
| 47 | + exp += ' Creates a new SeleniumBase chart presentation.\n' |
| 48 | + exp += ' If the file already exists, an error is raised.\n' |
| 49 | + exp += ' By default, the slides are written in English,\n' |
| 50 | + exp += ' and use a "sky" theme with "slide" transition.\n' |
| 51 | + exp += ' The chart can be used as a basic boilerplate.\n' |
| 52 | + if not msg: |
| 53 | + raise Exception('INVALID RUN COMMAND!\n\n%s' % exp) |
| 54 | + elif msg == "help": |
| 55 | + print("\n%s" % exp) |
| 56 | + sys.exit() |
| 57 | + else: |
| 58 | + raise Exception('INVALID RUN COMMAND!\n\n%s\n%s\n' % (exp, msg)) |
| 59 | + |
| 60 | + |
| 61 | +def main(): |
| 62 | + c1 = "" |
| 63 | + c5 = "" |
| 64 | + c7 = "" |
| 65 | + cr = "" |
| 66 | + if "linux" not in sys.platform: |
| 67 | + colorama.init(autoreset=True) |
| 68 | + c1 = colorama.Fore.BLUE + colorama.Back.LIGHTCYAN_EX |
| 69 | + c5 = colorama.Fore.RED + colorama.Back.LIGHTYELLOW_EX |
| 70 | + c7 = colorama.Fore.BLACK + colorama.Back.MAGENTA |
| 71 | + cr = colorama.Style.RESET_ALL |
| 72 | + |
| 73 | + help_me = False |
| 74 | + error_msg = None |
| 75 | + invalid_cmd = None |
| 76 | + language = "English" |
| 77 | + |
| 78 | + command_args = sys.argv[2:] |
| 79 | + file_name = command_args[0] |
| 80 | + if file_name == "-h" or file_name == "--help": |
| 81 | + invalid_run_command("help") |
| 82 | + elif not file_name.endswith(".py"): |
| 83 | + error_msg = 'File name must end with ".py"!' |
| 84 | + elif "*" in file_name or len(str(file_name)) < 4: |
| 85 | + error_msg = 'Invalid file name!' |
| 86 | + elif file_name.startswith("-"): |
| 87 | + error_msg = 'File name cannot start with "-"!' |
| 88 | + elif "/" in str(file_name) or "\\" in str(file_name): |
| 89 | + error_msg = 'File must be created in the current directory!' |
| 90 | + elif os.path.exists(os.getcwd() + '/' + file_name): |
| 91 | + error_msg = ( |
| 92 | + 'File "%s" already exists in this directory!' % file_name) |
| 93 | + if error_msg: |
| 94 | + error_msg = c5 + "ERROR: " + error_msg + cr |
| 95 | + invalid_run_command(error_msg) |
| 96 | + |
| 97 | + if len(command_args) >= 2: |
| 98 | + options = command_args[1:] |
| 99 | + for option in options: |
| 100 | + option = option.lower() |
| 101 | + if option == "-h" or option == "--help": |
| 102 | + help_me = True |
| 103 | + elif option == "--en" or option == "--english": |
| 104 | + language = "English" |
| 105 | + elif option == "--zh" or option == "--chinese": |
| 106 | + language = "Chinese" |
| 107 | + elif option == "--nl" or option == "--dutch": |
| 108 | + language = "Dutch" |
| 109 | + elif option == "--fr" or option == "--french": |
| 110 | + language = "French" |
| 111 | + elif option == "--it" or option == "--italian": |
| 112 | + language = "Italian" |
| 113 | + elif option == "--ja" or option == "--japanese": |
| 114 | + language = "Japanese" |
| 115 | + elif option == "--ko" or option == "--korean": |
| 116 | + language = "Korean" |
| 117 | + elif option == "--pt" or option == "--portuguese": |
| 118 | + language = "Portuguese" |
| 119 | + elif option == "--ru" or option == "--russian": |
| 120 | + language = "Russian" |
| 121 | + elif option == "--es" or option == "--spanish": |
| 122 | + language = "Spanish" |
| 123 | + else: |
| 124 | + invalid_cmd = "\n===> INVALID OPTION: >> %s <<\n" % option |
| 125 | + invalid_cmd = invalid_cmd.replace('>> ', ">>" + c5 + " ") |
| 126 | + invalid_cmd = invalid_cmd.replace(' <<', " " + cr + "<<") |
| 127 | + invalid_cmd = invalid_cmd.replace('>>', c7 + ">>" + cr) |
| 128 | + invalid_cmd = invalid_cmd.replace('<<', c7 + "<<" + cr) |
| 129 | + help_me = True |
| 130 | + break |
| 131 | + if help_me: |
| 132 | + invalid_run_command(invalid_cmd) |
| 133 | + |
| 134 | + if language != "English" and sys.version_info[0] == 2: |
| 135 | + print("") |
| 136 | + msg = 'Multi-language support for "sbase mkchart" ' |
| 137 | + msg += 'is not available on Python 2!' |
| 138 | + msg = "\n" + c5 + msg + cr |
| 139 | + msg += '\nPlease run in "English" mode or upgrade to Python 3!\n' |
| 140 | + raise Exception(msg) |
| 141 | + |
| 142 | + dir_name = os.getcwd() |
| 143 | + file_path = "%s/%s" % (dir_name, file_name) |
| 144 | + html_name = file_name.replace(".py", ".html") |
| 145 | + class_name = "MyTestClass" |
| 146 | + item = "Item" |
| 147 | + select_option = "Select option" |
| 148 | + chart_options = '"pie", "bar", "column", "line", "area"' |
| 149 | + |
| 150 | + if language == "Chinese": |
| 151 | + class_name = "我的测试类" |
| 152 | + item = "目的" |
| 153 | + select_option = "选择选项" |
| 154 | + chart_options = '"饼图", "条形图", "柱形图", "折线图", "面积图"' |
| 155 | + elif language == "Dutch": |
| 156 | + class_name = "MijnTestklasse" |
| 157 | + item = "Voorwerp" |
| 158 | + select_option = "Optie selecteren" |
| 159 | + chart_options = '"cirkel", "staaf", "kolom", "lijn", "vlak"' |
| 160 | + elif language == "French": |
| 161 | + class_name = "MaClasseDeTest" |
| 162 | + item = "Objet" |
| 163 | + select_option = "Sélectionner option" |
| 164 | + chart_options = '"secteurs" "barres" "colonnes" "linéaire" "aires"' |
| 165 | + elif language == "Italian": |
| 166 | + class_name = "MiaClasseDiTest" |
| 167 | + item = "Oggetto" |
| 168 | + select_option = "Selezionare opzione" |
| 169 | + chart_options = '"torta", "barre", "colonne", "linee", "area"' |
| 170 | + elif language == "Japanese": |
| 171 | + class_name = "私のテストクラス" |
| 172 | + item = "物体" |
| 173 | + select_option = "でオプションを選択" |
| 174 | + chart_options = '"円", "棒", "縦棒", "折れ線", "面"' |
| 175 | + elif language == "Korean": |
| 176 | + class_name = "테스트_클래스" |
| 177 | + item = "물체" |
| 178 | + select_option = "옵션 선택" |
| 179 | + chart_options = '"원형", "막대", "열", "선", "영역"' |
| 180 | + elif language == "Portuguese": |
| 181 | + class_name = "MinhaClasseDeTeste" |
| 182 | + item = "Objeto" |
| 183 | + select_option = "Selecionar opção" |
| 184 | + chart_options = '"pizza", "barras", "colunas", "linhas", "área"' |
| 185 | + elif language == "Russian": |
| 186 | + class_name = "МойТестовыйКласс" |
| 187 | + item = "Вещь" |
| 188 | + select_option = "Выбрать опцию" |
| 189 | + chart_options = '"круговую" "бар" "столбчатую" "линейную" "области"' |
| 190 | + elif language == "Spanish": |
| 191 | + class_name = "MiClaseDePrueba" |
| 192 | + item = "Objeto" |
| 193 | + select_option = "Seleccionar opción" |
| 194 | + chart_options = '"circular", "barras", "columnas", "líneas", "área"' |
| 195 | + |
| 196 | + import_line = "from seleniumbase import BaseCase" |
| 197 | + parent_class = "BaseCase" |
| 198 | + class_line = "class MyTestClass(BaseCase):" |
| 199 | + if language != "English": |
| 200 | + from seleniumbase.translate.master_dict import MD_F |
| 201 | + import_line = MD_F.get_import_line(language) |
| 202 | + parent_class = MD_F.get_lang_parent_class(language) |
| 203 | + class_line = "class %s(%s):" % (class_name, parent_class) |
| 204 | + settings = 'theme="sky", transition="slide"' |
| 205 | + chart_settings = 'title="Chart 1"' |
| 206 | + add_slide = '"<p>Chart Demo</p>" + self.extract_chart()' |
| 207 | + data = [] |
| 208 | + data.append("%s" % import_line) |
| 209 | + data.append("") |
| 210 | + data.append("") |
| 211 | + data.append("%s" % class_line) |
| 212 | + data.append("") |
| 213 | + data.append(" def test_chart_presentation(self):") |
| 214 | + data.append(' self.create_presentation(%s)' % settings) |
| 215 | + data.append("") |
| 216 | + data.append(' # %s => %s' % (select_option, chart_options)) |
| 217 | + data.append(' self.create_pie_chart(%s)' % chart_settings) |
| 218 | + data.append(' self.add_data_point("%s A", 50)' % item) |
| 219 | + data.append(' self.add_data_point("%s B", 40)' % item) |
| 220 | + data.append(' self.add_data_point("%s C", 35)' % item) |
| 221 | + data.append(' self.add_data_point("%s D", 30)' % item) |
| 222 | + data.append(' self.add_data_point("%s E", 25)' % item) |
| 223 | + data.append(' self.add_data_point("%s F", 20)' % item) |
| 224 | + data.append(' self.add_slide(%s)' % add_slide) |
| 225 | + data.append("") |
| 226 | + data.append(' self.begin_presentation(filename="%s")' % html_name) |
| 227 | + data.append("") |
| 228 | + |
| 229 | + new_data = [] |
| 230 | + if language == "English": |
| 231 | + new_data = data |
| 232 | + else: |
| 233 | + from seleniumbase.translate.master_dict import MD |
| 234 | + from seleniumbase.translate.master_dict import MD_L_Codes |
| 235 | + md = MD.md |
| 236 | + lang_codes = MD_L_Codes.lang |
| 237 | + nl_code = lang_codes[language] |
| 238 | + dl_code = lang_codes["English"] |
| 239 | + for line in data: |
| 240 | + found_swap = False |
| 241 | + replace_count = line.count("self.") # Total possible replacements |
| 242 | + for key in md.keys(): |
| 243 | + original = "self." + md[key][dl_code] + "(" |
| 244 | + if original in line: |
| 245 | + replacement = "self." + md[key][nl_code] + "(" |
| 246 | + new_line = line.replace(original, replacement) |
| 247 | + found_swap = True |
| 248 | + replace_count -= 1 |
| 249 | + if replace_count == 0: |
| 250 | + break # Done making replacements |
| 251 | + else: |
| 252 | + # There might be another method to replace in the line. |
| 253 | + # Example: self.assert_true("Name" in self.get_title()) |
| 254 | + line = new_line |
| 255 | + continue |
| 256 | + if found_swap: |
| 257 | + if new_line.endswith(" # noqa"): # Remove flake8 skip |
| 258 | + new_line = new_line[0:-len(" # noqa")] |
| 259 | + new_data.append(new_line) |
| 260 | + continue |
| 261 | + new_data.append(line) |
| 262 | + data = new_data |
| 263 | + file = codecs.open(file_path, "w+", "utf-8") |
| 264 | + file.writelines("\r\n".join(data)) |
| 265 | + file.close() |
| 266 | + success = ( |
| 267 | + '\n' + c1 + '* Chart Presentation: "' + file_name + '" was created! *' |
| 268 | + '' + cr + '\n') |
| 269 | + print(success) |
| 270 | + |
| 271 | + |
| 272 | +if __name__ == "__main__": |
| 273 | + invalid_run_command() |
0 commit comments