Skip to content

Commit e80e755

Browse files
committed
Add the option to include line numbers in translated prints
1 parent 05b1ea0 commit e80e755

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

examples/translations/ReadMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ seleniumbase translate [SB_FILE].py [LANGUAGE] [ACTION]
5151
``-o`` / ``--overwrite`` (Overwrite the file being translated)
5252
``-c`` / ``--copy`` (Copy the translation to a new ``.py`` file)
5353
54+
* Options:
55+
``-n`` (include line Numbers when using the Print action)
56+
5457
* Examples:
5558
Translate test_1.py into Chinese and only print the output:
5659
>>> seleniumbase translate test_1.py --zh -p

seleniumbase/console_scripts/ReadMe.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ See: http://www.katalon.com/automation-recorder
7171
``-o`` / ``--overwrite`` (Overwrite the file being translated)
7272
``-c`` / ``--copy`` (Copy the translation to a new ``.py`` file)
7373

74+
* Options:
75+
``-n`` (include line Numbers when using the Print action)
76+
7477
* Output:
7578
Translates a SeleniumBase Python file into the language
7679
specified. Method calls and "import" lines get swapped.

seleniumbase/console_scripts/run.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def show_translate_usage():
180180
print(" -p / --print (Print translation output to the screen)")
181181
print(" -o / --overwrite (Overwrite the file being translated)")
182182
print(" -c / --copy (Copy the translation to a new .py file)")
183+
print(" Options:")
184+
print(" -n (include line Numbers when using the Print action)")
183185
print(" Output:")
184186
print(" Translates a SeleniumBase Python file into the language")
185187
print(' specified. Method calls and "import" lines get swapped.')

seleniumbase/translate/translator.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
-p / --print (Print translation output to the screen)
1515
-o / --overwrite (Overwrite the file being translated)
1616
-c / --copy (Copy the translation to a new .py file)
17+
Options:
18+
-n (include line Numbers when using the Print action)
1719
Output:
1820
Translates a SeleniumBase Python file into the language
1921
specified. Method calls and "import" lines get swapped.
@@ -52,6 +54,8 @@ def invalid_run_command(msg=None):
5254
exp += " -p / --print (Print translation output to the screen)\n"
5355
exp += " -o / --overwrite (Overwrite the file being translated)\n"
5456
exp += " -c / --copy (Copy the translation to a new .py file)\n"
57+
exp += " Options:\n"
58+
exp += " -n (include line Numbers when using the Print action)\n"
5559
exp += " Output:\n"
5660
exp += " Translates a SeleniumBase Python file into the language\n"
5761
exp += ' specified. Method calls and "import" lines get swapped.\n'
@@ -68,8 +72,8 @@ def invalid_run_command(msg=None):
6872
raise Exception('INVALID RUN COMMAND!\n%s\n\n%s' % (msg, exp))
6973

7074

71-
def ranges():
72-
# Get the ranges of special characters of Chinese, Japanese, and Korean
75+
def sc_ranges():
76+
# Get the ranges of special characters of Chinese, Japanese, and Korean.
7377
special_char_ranges = ([
7478
{"from": ord(u"\u3300"), "to": ord(u"\u33ff")},
7579
{"from": ord(u"\ufe30"), "to": ord(u"\ufe4f")},
@@ -89,13 +93,15 @@ def ranges():
8993

9094

9195
def is_cjk(char):
92-
# Returns True if the special character is Chinese, Japanese, or Korean
93-
sc = any([range["from"] <= ord(char) <= range["to"] for range in ranges()])
96+
# Returns True if the special character is Chinese, Japanese, or Korean.
97+
sc = any(
98+
[range["from"] <= ord(char) <= range["to"] for range in sc_ranges()])
9499
return sc
95100

96101

97102
def get_width(line):
98-
# Chinese/Japanese/Korean characters take up double width visually
103+
# Return the true width of the line. Not the same as line length.
104+
# Chinese/Japanese/Korean characters take up double width visually.
99105
line_length = len(line)
100106
for char in line:
101107
if is_cjk(char):
@@ -253,6 +259,7 @@ def main():
253259
print_only = False
254260
help_me = False
255261
invalid_cmd = None
262+
line_numbers = False
256263

257264
expected_arg = ("A SeleniumBase Python file")
258265
command_args = sys.argv[2:]
@@ -284,6 +291,8 @@ def main():
284291
copy = True
285292
elif option == "-p" or option == "--print":
286293
print_only = True
294+
elif option == "-n":
295+
line_numbers = True
287296
elif option == "--en" or option == "--english":
288297
new_lang = "English"
289298
elif option == "--zh" or option == "--chinese":
@@ -444,6 +453,7 @@ def main():
444453
raise Exception("\n\n`%s` is not a valid SeleniumBase test file!\n"
445454
"\nExpecting: [%s]\n"
446455
% (seleniumbase_file, expected_arg))
456+
all_code = all_code.replace("\t", " ")
447457
code_lines = all_code.split('\n')
448458

449459
seleniumbase_lines, changed, d_l = process_test_file(code_lines, new_lang)
@@ -482,14 +492,16 @@ def main():
482492
python_code = "\n".join(seleniumbase_lines)
483493
code_width = 1
484494

485-
w = 4 # line number whitespace
486-
num_lines = len(seleniumbase_lines)
487-
if num_lines >= 10:
488-
w = 5
489-
if num_lines >= 100:
490-
w = 6
491-
if num_lines >= 1000:
492-
w = 7
495+
w = 0 # line number whitespace
496+
if line_numbers:
497+
w = 4
498+
num_lines = len(code_lines)
499+
if num_lines >= 10:
500+
w = 5
501+
if num_lines >= 100:
502+
w = 6
503+
if num_lines >= 1000:
504+
w = 7
493505

494506
new_sb_lines = []
495507
for line in seleniumbase_lines:
@@ -626,7 +638,8 @@ def main():
626638

627639
magic_syntax = Syntax(
628640
python_code, "python", theme="monokai",
629-
line_numbers=True, code_width=used_width, word_wrap=False)
641+
line_numbers=line_numbers, code_width=used_width,
642+
word_wrap=False)
630643
magic_console = Console()
631644
print("")
632645
print(save_line)

0 commit comments

Comments
 (0)