Skip to content

Commit 068e83f

Browse files
committed
Refactored script and made OS independent by removing rm calls
1 parent 7b6db96 commit 068e83f

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

examples/scripts/save_help_text.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
import os
1010
import sys
1111
import tempfile
12-
from typing import List
12+
from typing import List, TextIO
1313

1414
ASTERISKS = "********************************************************"
1515

1616

1717
def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
18-
"""Returns a list of sub-commands for an ArgumentParser"""
19-
18+
"""Get a list of sub-commands for an ArgumentParser"""
2019
sub_cmds = []
2120

2221
# Check if this is parser has sub-commands
@@ -37,43 +36,58 @@ def get_sub_commands(parser: argparse.ArgumentParser) -> List[str]:
3736
return sub_cmds
3837

3938

40-
# Make sure we have access to self
41-
if 'self' not in globals():
42-
print("Run 'set locals_in_py true' and then rerun this script")
39+
def add_command_to_script(command: str, temp_file: TextIO, outfile_path: str) -> None:
40+
"""
41+
Write to the script the commands necessary to write both a
42+
header and help text for a command to the output file
43+
"""
44+
header = '{}\\nCOMMAND: {}\\n{}\\n'.format(ASTERISKS, command, ASTERISKS)
45+
temp_file.write('py print("{}") >> {}\n'.format(header, outfile_path))
46+
temp_file.write('help {} >> {}\n'.format(command, outfile_path))
4347

44-
# Make sure the user passed in an output file
45-
elif len(sys.argv) != 2:
46-
print("Usage: {} <output_file>".format(os.path.basename(sys.argv[0])))
47-
else:
48-
outfile = os.path.expanduser(sys.argv[1])
4948

50-
# Get a list of all commands and help topics and then filter out duplicates
51-
to_print = set(self.get_all_commands()) | set(self.get_help_topics())
49+
def main():
50+
"""Main function of this script"""
51+
52+
# Make sure we have access to self
53+
if 'self' not in globals():
54+
print("Run 'set locals_in_py true' and then rerun this script")
55+
return
56+
57+
# Make sure the user passed in an output file
58+
if len(sys.argv) != 2:
59+
print("Usage: {} <output_file>".format(os.path.basename(sys.argv[0])))
60+
return
61+
62+
# Get output file path
63+
outfile = os.path.expanduser(sys.argv[1])
5264

5365
# Create a script that will call help on each command and topic and write its output to a file
54-
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as temp:
66+
with tempfile.NamedTemporaryFile(mode='w+t', delete=False) as temp_file:
5567

5668
# First delete any existing output file
57-
temp.write('!rm -f {}\n'.format(outfile))
69+
temp_file.write('py if os.path.exists("{0}"): os.remove("{0}")\n'.format(outfile))
70+
71+
# Get a list of all commands and help topics and then filter out duplicates
72+
to_save = set(self.get_all_commands()) | set(self.get_help_topics())
5873

59-
for item in to_print:
60-
header = '{}\\nCOMMAND: {}\\n{}\\n'.format(ASTERISKS, item, ASTERISKS)
61-
temp.write('py print("{}") >> {}\n'.format(header, outfile))
62-
temp.write('help {} >> {}\n'.format(item, outfile))
74+
for item in to_save:
75+
add_command_to_script(item, temp_file, outfile)
6376

6477
# Add any sub-commands
6578
for subcmd in get_sub_commands(getattr(self.cmd_func(item), 'argparser', None)):
6679
full_cmd = '{} {}'.format(item, subcmd)
67-
header = '{}\\nCOMMAND: {}\\n{}\\n'.format(ASTERISKS, full_cmd, ASTERISKS)
68-
69-
temp.write('py print("{}") >> {}\n'.format(header, outfile))
70-
temp.write('help {} >> {}\n'.format(full_cmd, outfile))
80+
add_command_to_script(full_cmd, temp_file, outfile)
7181

7282
# Inform the user where output was written
73-
temp.write('py print("Output written to {}")\n'.format(outfile))
83+
temp_file.write('py print("Output written to {}")\n'.format(outfile))
7484

7585
# Have the script delete itself as its last step
76-
temp.write('!rm -f {}\n'.format(temp.name))
86+
temp_file.write('py os.remove("{}")\n'.format(temp_file.name))
7787

7888
# Tell cmd2 to run the script as its next command
79-
self.cmdqueue.insert(0, "load '{}'".format(temp.name))
89+
self.cmdqueue.insert(0, 'load "{}"'.format(temp_file.name))
90+
91+
92+
# Run main function
93+
main()

0 commit comments

Comments
 (0)