Skip to content

Commit f18390b

Browse files
authored
Merge branch 'master' into api_docs_cleanup
2 parents 3d488c7 + f636aa1 commit f18390b

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

examples/python_scripting.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@ def __init__(self):
2929
super().__init__(use_ipython=True)
3030
self._set_prompt()
3131
self.intro = 'Happy 𝛑 Day. Note the full Unicode support: 😇 💩'
32-
self.self_in_py = True
3332

3433
def _set_prompt(self):
3534
"""Set prompt so it displays the current working directory."""
3635
self.cwd = os.getcwd()
37-
self.prompt = ansi.style('{!r} $ '.format(self.cwd), fg='cyan')
36+
self.prompt = ansi.style(f'{self.cwd} $ ', fg='cyan')
3837

3938
def postcmd(self, stop: bool, line: str) -> bool:
4039
"""Hook method executed just after a command dispatch is finished.
@@ -57,33 +56,31 @@ def do_cd(self, arglist):
5756
if not arglist or len(arglist) != 1:
5857
self.perror("cd requires exactly 1 argument:")
5958
self.do_help('cd')
60-
self.last_result = cmd2.CommandResult('', 'Bad arguments')
59+
self.last_result = 'Bad arguments'
6160
return
6261

6362
# Convert relative paths to absolute paths
6463
path = os.path.abspath(os.path.expanduser(arglist[0]))
6564

6665
# Make sure the directory exists, is a directory, and we have read access
67-
out = ''
6866
err = None
6967
data = None
7068
if not os.path.isdir(path):
71-
err = '{!r} is not a directory'.format(path)
69+
err = f'{path} is not a directory'
7270
elif not os.access(path, os.R_OK):
73-
err = 'You do not have read access to {!r}'.format(path)
71+
err = f'You do not have read access to {path}'
7472
else:
7573
try:
7674
os.chdir(path)
7775
except Exception as ex:
78-
err = '{}'.format(ex)
76+
err = f'{ex}'
7977
else:
80-
out = 'Successfully changed directory to {!r}\n'.format(path)
81-
self.stdout.write(out)
78+
self.poutput(f'Successfully changed directory to {path}')
8279
data = path
8380

8481
if err:
8582
self.perror(err)
86-
self.last_result = cmd2.CommandResult(out, err, data)
83+
self.last_result = data
8784

8885
# Enable tab completion for cd command
8986
def complete_cd(self, text, line, begidx, endidx):
@@ -100,20 +97,17 @@ def do_dir(self, args, unknown):
10097
if unknown:
10198
self.perror("dir does not take any positional arguments:")
10299
self.do_help('dir')
103-
self.last_result = cmd2.CommandResult('', 'Bad arguments')
100+
self.last_result = 'Bad arguments'
104101
return
105102

106103
# Get the contents as a list
107104
contents = os.listdir(self.cwd)
108105

109-
fmt = '{} '
110-
if args.long:
111-
fmt = '{}\n'
112106
for f in contents:
113-
self.stdout.write(fmt.format(f))
114-
self.stdout.write('\n')
107+
self.poutput(f'{f}')
108+
self.poutput('')
115109

116-
self.last_result = cmd2.CommandResult(data=contents)
110+
self.last_result = contents
117111

118112

119113
if __name__ == '__main__':

examples/scripts/conditional.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,27 @@
2424
original_dir = os.getcwd()
2525

2626
# Try to change to the specified directory
27-
app('cd {}'.format(directory))
27+
result = app('cd {}'.format(directory))
2828

2929
# Conditionally do something based on the results of the last command
30-
if self.last_result:
30+
if result:
31+
print(f"STDOUT: {result.stdout}\n")
32+
print(f"STDERR: {result.stderr}\n")
33+
3134
print('\nContents of directory {!r}:'.format(directory))
32-
app('dir -l')
33-
print('{}\n'.format(self.last_result.data))
35+
result = app('dir -l')
36+
37+
print(f"STDOUT: {result.stdout}\n")
38+
print(f"STDERR: {result.stderr}\n")
39+
40+
print('{}\n'.format(result.data))
3441

3542
# Change back to where we were
3643
print('Changing back to original directory: {!r}'.format(original_dir))
3744
app('cd {}'.format(original_dir))
3845
else:
3946
# cd command failed, print a warning
4047
print('Failed to change directory to {!r}'.format(directory))
48+
49+
print(f"STDOUT: {result.stdout}\n")
50+
print(f"STDERR: {result.stderr}\n")

0 commit comments

Comments
 (0)