Skip to content

Commit 7080a75

Browse files
committed
Add "sbase mkpres" console script for creating presentations
1 parent 49695e9 commit 7080a75

File tree

3 files changed

+335
-0
lines changed

3 files changed

+335
-0
lines changed

seleniumbase/console_scripts/ReadMe.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ methods, which are "open", "type", "click",
8181
basic boilerplate option, only the "open" method
8282
is included.
8383

84+
### mkpres
85+
86+
* Usage:
87+
``sbase mkpres [FILE.py] [LANGUAGE OPTIONS]``
88+
89+
* Example:
90+
``sbase mkpres new_presentation.py``
91+
92+
* Language Options:
93+
``--en`` / ``--English`` | ``--zh`` / ``--Chinese``
94+
``--nl`` / ``--Dutch`` | ``--fr`` / ``--French``
95+
``--it`` / ``--Italian`` | ``--ja`` / ``--Japanese``
96+
``--ko`` / ``--Korean`` | ``--pt`` / ``--Portuguese``
97+
``--ru`` / ``--Russian`` | ``--es`` / ``--Spanish``
98+
99+
* Output:
100+
Creates a new presentation with 3 example slides.
101+
If the file already exists, an error is raised.
102+
By default, the slides are written in English.
103+
Slides use "serif" theme & "fade" transition.
104+
This code can be used as a base boilerplate.
105+
84106
### options
85107

86108
* Usage:

seleniumbase/console_scripts/run.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
sbase install chromedriver
1010
sbase mkdir ui_tests
1111
sbase mkfile new_test.py
12+
sbase mkpres new_presentation.py
1213
sbase options
1314
sbase convert webdriver_unittest_file.py
1415
sbase print my_first_test.py -n
@@ -67,6 +68,7 @@ def show_basic_usage():
6768
sc += (" install [DRIVER] [OPTIONS]\n")
6869
sc += (" mkdir [DIRECTORY] [OPTIONS]\n")
6970
sc += (" mkfile [FILE.py] [OPTIONS]\n")
71+
sc += (" mkpres [FILE.py] [LANGUAGE OPTIONS]\n")
7072
sc += (" options (List common pytest options)\n")
7173
sc += (" print [FILE] [OPTIONS]\n")
7274
sc += (" translate [SB_FILE.py] [LANG] [ACTION]\n")
@@ -183,6 +185,33 @@ def show_mkfile_usage():
183185
print("")
184186

185187

188+
def show_mkpres_usage():
189+
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
190+
c3 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
191+
cr = colorama.Style.RESET_ALL
192+
sc = (" " + c2 + "** " + c3 + "mkpres" + c2 + " **" + cr)
193+
print(sc)
194+
print("")
195+
print(" Usage:")
196+
print(" seleniumbase mkpres [FILE.py] [LANGUAGE OPTIONS]")
197+
print(" OR: sbase mkpres [FILE.py] [LANGUAGE OPTIONS]")
198+
print(" Example:")
199+
print(" sbase mkpres new_presentation.py")
200+
print(" Language Options:")
201+
print(" --en / --English | --zh / --Chinese")
202+
print(" --nl / --Dutch | --fr / --French")
203+
print(" --it / --Italian | --ja / --Japanese")
204+
print(" --ko / --Korean | --pt / --Portuguese")
205+
print(" --ru / --Russian | --es / --Spanish")
206+
print(" Output:")
207+
print(" Creates a new presentation with 3 example slides.")
208+
print(" If the file already exists, an error is raised.")
209+
print(" By default, the slides are written in English.")
210+
print(' Slides use "serif" theme & "fade" transition.')
211+
print(" This code can be used as a base boilerplate.")
212+
print("")
213+
214+
186215
def show_convert_usage():
187216
c2 = colorama.Fore.BLUE + colorama.Back.LIGHTGREEN_EX
188217
c3 = colorama.Fore.BLUE + colorama.Back.LIGHTYELLOW_EX
@@ -540,6 +569,7 @@ def show_detailed_help():
540569
show_install_usage()
541570
show_mkdir_usage()
542571
show_mkfile_usage()
572+
show_mkpres_usage()
543573
show_convert_usage()
544574
show_print_usage()
545575
show_translate_usage()
@@ -591,6 +621,13 @@ def main():
591621
else:
592622
show_basic_usage()
593623
show_mkfile_usage()
624+
elif command == "mkpres":
625+
if len(command_args) >= 1:
626+
from seleniumbase.console_scripts import sb_mkpres
627+
sb_mkpres.main()
628+
else:
629+
show_basic_usage()
630+
show_mkpres_usage()
594631
elif command == "convert":
595632
if len(command_args) == 1:
596633
from seleniumbase.utilities.selenium_ide import convert_ide
@@ -718,6 +755,10 @@ def main():
718755
print("")
719756
show_mkfile_usage()
720757
return
758+
elif command_args[0] == "mkpres":
759+
print("")
760+
show_mkpres_usage()
761+
return
721762
elif command_args[0] == "convert":
722763
print("")
723764
show_convert_usage()
Lines changed: 272 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,272 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Creates a new SeleniumBase presentation with boilerplate code.
4+
5+
Usage:
6+
seleniumbase mkpres [FILE.py] [LANGUAGE OPTIONS]
7+
or sbase mkpres [FILE.py] [LANGUAGE OPTIONS]
8+
9+
Example:
10+
sbase mkpres new_presentation.py
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 presentation with 3 example slides.
21+
If the file already exists, an error is raised.
22+
By default, the slides are written in English.
23+
Slides use "serif" theme & "fade" transition.
24+
This code can be used as a base 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 = (" ** mkpres **\n\n")
35+
exp += " Usage:\n"
36+
exp += " seleniumbase mkpres [FILE.py] [LANGUAGE OPTIONS]\n"
37+
exp += " OR sbase mkpres [FILE.py] [LANGUAGE OPTIONS]\n"
38+
exp += " Example:\n"
39+
exp += " sbase mkpres new_presentation.py\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 presentation with 3 example slides.\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 += ' Slides use "serif" theme & "fade" transition.\n'
51+
exp += ' This code can be used as a base 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 mkpres" '
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+
146+
hello = "Hello"
147+
update_text = "Update Text"
148+
goodbye = "Goodbye"
149+
class_name = "MyTestClass"
150+
151+
if language == "Chinese":
152+
hello = "你好"
153+
update_text = "更新文本"
154+
goodbye = "再见"
155+
class_name = "我的测试类"
156+
elif language == "Dutch":
157+
hello = "Hallo"
158+
update_text = "Tekst Bijwerken"
159+
goodbye = "Dag"
160+
class_name = "MijnTestklasse"
161+
elif language == "French":
162+
hello = "Bonjour"
163+
update_text = "Modifier Texte"
164+
goodbye = "Au revoir"
165+
class_name = "MaClasseDeTest"
166+
elif language == "Italian":
167+
hello = "Ciao"
168+
update_text = "Aggiornare Testo"
169+
goodbye = "Addio"
170+
class_name = "MiaClasseDiTest"
171+
elif language == "Japanese":
172+
hello = "こんにちは"
173+
update_text = "テキストを更新"
174+
goodbye = "さようなら"
175+
class_name = "私のテストクラス"
176+
elif language == "Korean":
177+
hello = "여보세요"
178+
update_text = "텍스트를 업데이트"
179+
goodbye = "안녕"
180+
class_name = "테스트_클래스"
181+
elif language == "Portuguese":
182+
hello = "Olá"
183+
update_text = "Atualizar Texto"
184+
goodbye = "Tchau"
185+
class_name = "MinhaClasseDeTeste"
186+
elif language == "Russian":
187+
hello = "Привет"
188+
update_text = "обновить текст"
189+
goodbye = "До свидания"
190+
class_name = "МойТестовыйКласс"
191+
elif language == "Spanish":
192+
hello = "Hola"
193+
update_text = "Actualizar Texto"
194+
goodbye = "Adiós"
195+
class_name = "MiClaseDePrueba"
196+
197+
import_line = "from seleniumbase import BaseCase"
198+
parent_class = "BaseCase"
199+
class_line = "class MyTestClass(BaseCase):"
200+
if language != "English":
201+
from seleniumbase.translate.master_dict import MD_F
202+
import_line = MD_F.get_import_line(language)
203+
parent_class = MD_F.get_lang_parent_class(language)
204+
class_line = "class %s(%s):" % (class_name, parent_class)
205+
settings = 'theme="serif", transition="fade"'
206+
img_src = 'src="https://seleniumbase.io/cdn/img/sb6.png"'
207+
hello_page = (
208+
"\n '<h1>%s</h1><br />'"
209+
"\n '<img %s>'"
210+
'' % (hello, img_src))
211+
update_text_page = "<h2><b>*</b> %s <b>*</b></h2>" % update_text
212+
goodbye_page = "<h2>%s</h2><p>Use SeleniumBase!</p>" % goodbye
213+
214+
data = []
215+
data.append("%s" % import_line)
216+
data.append("")
217+
data.append("")
218+
data.append("%s" % class_line)
219+
data.append("")
220+
data.append(" def test_presentation(self):")
221+
data.append(' self.create_presentation(%s)' % settings)
222+
data.append(' self.add_slide(%s)' % hello_page)
223+
data.append(' self.add_slide("%s")' % update_text_page)
224+
data.append(' self.add_slide("%s")' % goodbye_page)
225+
data.append(' self.begin_presentation(filename="%s")' % html_name)
226+
data.append("")
227+
228+
new_data = []
229+
if language == "English":
230+
new_data = data
231+
else:
232+
from seleniumbase.translate.master_dict import MD
233+
from seleniumbase.translate.master_dict import MD_L_Codes
234+
md = MD.md
235+
lang_codes = MD_L_Codes.lang
236+
nl_code = lang_codes[language]
237+
dl_code = lang_codes["English"]
238+
for line in data:
239+
found_swap = False
240+
replace_count = line.count("self.") # Total possible replacements
241+
for key in md.keys():
242+
original = "self." + md[key][dl_code] + "("
243+
if original in line:
244+
replacement = "self." + md[key][nl_code] + "("
245+
new_line = line.replace(original, replacement)
246+
found_swap = True
247+
replace_count -= 1
248+
if replace_count == 0:
249+
break # Done making replacements
250+
else:
251+
# There might be another method to replace in the line.
252+
# Example: self.assert_true("Name" in self.get_title())
253+
line = new_line
254+
continue
255+
if found_swap:
256+
if new_line.endswith(" # noqa"): # Remove flake8 skip
257+
new_line = new_line[0:-len(" # noqa")]
258+
new_data.append(new_line)
259+
continue
260+
new_data.append(line)
261+
data = new_data
262+
file = codecs.open(file_path, "w+", "utf-8")
263+
file.writelines("\r\n".join(data))
264+
file.close()
265+
success = (
266+
'\n' + c1 + '* Presentation: "' + file_name + '" was created! *'
267+
'' + cr + '\n')
268+
print(success)
269+
270+
271+
if __name__ == "__main__":
272+
invalid_run_command()

0 commit comments

Comments
 (0)