Skip to content

Commit 5a88fcf

Browse files
authored
Merge pull request #864 from python-cmd2/reduce_settable_attributes
Reduce settable attributes and renamed locals_in_py
2 parents 95f8d88 + 691d775 commit 5a88fcf

24 files changed

+67
-94
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.9.26 (TBD, 2020)
2+
* Breaking changes
3+
* Renamed `locals_in_py` attribute of `cmd2.Cmd` to `self_in_py`
4+
* The following public attributes of `cmd2.Cmd` are no longer settable at runtime by end users:
5+
* `continuation_prompt`
6+
* `self_in_py`
7+
* `prompt`
8+
19
## 0.9.25 (January 26, 2020)
210
* Enhancements
311
* Reduced what gets put in package downloadable from PyPI (removed irrelevant CI config files and such)

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -318,14 +318,11 @@ example/transcript_regex.txt:
318318
# regexes on prompts just make the trailing space obvious
319319
(Cmd) set
320320
allow_style: Terminal
321-
continuation_prompt: >/ /
322321
debug: False
323322
echo: False
324323
editor: /.*?/
325324
feedback_to_output: False
326-
locals_in_py: True
327325
maxrepeats: 3
328-
prompt: (Cmd)/ /
329326
quiet: False
330327
timing: False
331328
```
@@ -350,14 +347,14 @@ Open source projects using cmd2
350347

351348
Here are a few examples of open-source projects which use `cmd2`:
352349

350+
* [Jok3r](http://www.jok3r-framework.com)
351+
* Network & Web Pentest Automation Framework
353352
* [CephFS Shell](http://docs.ceph.com/docs/master/cephfs/cephfs-shell/)
354353
* [Ceph](https://ceph.com/) is a distributed object, block, and file storage platform
355354
* [JSShell](https://github.com/Den1al/JSShell)
356355
* An interactive multi-user web JavaScript shell
357356
* [psiTurk](https://psiturk.org)
358357
* An open platform for science on Amazon Mechanical Turk
359-
* [Jok3r](http://www.jok3r-framework.com)
360-
* Network & Web Pentest Automation Framework
361358
* [Poseidon](https://github.com/CyberReboot/poseidon)
362359
* Leverages software-defined networks (SDNs) to acquire and then feed network traffic to a number of machine learning techniques
363360
* [Unipacker](https://github.com/unipacker/unipacker)

cmd2/cmd2.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,28 +181,23 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
181181
super().__init__(completekey=completekey, stdin=stdin, stdout=stdout)
182182

183183
# Attributes which should NOT be dynamically settable via the set command at runtime
184-
# To prevent a user from altering these with the py/ipy commands, remove locals_in_py from the
185-
# settable dictionary during your applications's __init__ method.
186184
self.default_to_shell = False # Attempt to run unrecognized commands as shell commands
187185
self.quit_on_sigint = False # Quit the loop on interrupt instead of just resetting prompt
188186
self.allow_redirection = allow_redirection # Security setting to prevent redirection of stdout
189187

190188
# Attributes which ARE dynamically settable via the set command at runtime
191-
self.continuation_prompt = '> '
192189
self.debug = False
193190
self.echo = False
194191
self.editor = Cmd.DEFAULT_EDITOR
195192
self.feedback_to_output = False # Do not include nonessentials in >, | output by default (things like timing)
196-
self.locals_in_py = False
193+
self.quiet = False # Do not suppress nonessential output
194+
self.timing = False # Prints elapsed time for each command
197195

198196
# The maximum number of CompletionItems to display during tab completion. If the number of completion
199197
# suggestions exceeds this number, they will be displayed in the typical columnized format and will
200198
# not include the description value of the CompletionItems.
201199
self.max_completion_items = 50
202200

203-
self.quiet = False # Do not suppress nonessential output
204-
self.timing = False # Prints elapsed time for each command
205-
206201
# To make an attribute settable with the "do_set" command, add it to this ...
207202
self.settable = \
208203
{
@@ -211,18 +206,21 @@ def __init__(self, completekey: str = 'tab', stdin=None, stdout=None, *,
211206
'(valid values: {}, {}, {})'.format(ansi.STYLE_TERMINAL,
212207
ansi.STYLE_ALWAYS,
213208
ansi.STYLE_NEVER)),
214-
'continuation_prompt': 'On 2nd+ line of input',
215209
'debug': 'Show full error stack on error',
216210
'echo': 'Echo command issued into output',
217211
'editor': 'Program used by ``edit``',
218212
'feedback_to_output': 'Include nonessentials in `|`, `>` results',
219-
'locals_in_py': 'Allow access to your application in py via self',
220213
'max_completion_items': 'Maximum number of CompletionItems to display during tab completion',
221-
'prompt': 'The prompt issued to solicit input',
222214
'quiet': "Don't print nonessential feedback",
223215
'timing': 'Report execution times'
224216
}
225217

218+
# Use as prompt for multiline commands on the 2nd+ line of input
219+
self.continuation_prompt = '> '
220+
221+
# Allow access to your application in embedded Python shells and scripts py via self
222+
self.self_in_py = False
223+
226224
# Commands to exclude from the help menu and tab completion
227225
self.hidden_commands = ['eof', '_relative_load', '_relative_run_script']
228226

@@ -3118,7 +3116,7 @@ def py_quit():
31183116
self.py_locals['quit'] = py_quit
31193117
self.py_locals['exit'] = py_quit
31203118

3121-
if self.locals_in_py:
3119+
if self.self_in_py:
31223120
self.py_locals['self'] = self
31233121
elif 'self' in self.py_locals:
31243122
del self.py_locals['self']
@@ -3238,7 +3236,7 @@ def load_ipy(cmd2_app: Cmd, py_bridge: PyBridge):
32383236
exec("{} = py_bridge".format(cmd2_app.py_bridge_name))
32393237

32403238
# Add self variable pointing to cmd2_app, if allowed
3241-
if cmd2_app.locals_in_py:
3239+
if cmd2_app.self_in_py:
32423240
exec("self = cmd2_app")
32433241

32443242
# Delete these names from the environment so IPython can't use them

docs/features/builtin_commands.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,11 @@ within a running application:
9393
9494
(Cmd) set --long
9595
allow_style: Terminal # Allow ANSI text style sequences in output (valid values: Terminal, Always, Never)
96-
continuation_prompt: > # On 2nd+ line of input
9796
debug: False # Show full error stack on error
9897
echo: False # Echo command issued into output
9998
editor: vim # Program used by ``edit``
10099
feedback_to_output: False # include nonessentials in `|`, `>` results
101-
locals_in_py: False # Allow access to your application in py via self
102100
max_completion_items: 50 # Maximum number of CompletionItems to display during tab completion
103-
prompt: (Cmd) # The prompt issued to solicit input
104101
quiet: False # Don't print nonessential feedback
105102
timing: False # Report execution times
106103

docs/features/embedded_python_shells.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ arguments, it enters an interactive Python session. The session can call
88
your cmd2 application while maintaining isolation.
99

1010
You may optionally enable full access to to your application by setting
11-
``locals_in_py`` to ``True``. Enabling this flag adds ``self`` to the python
11+
``self_in_py`` to ``True``. Enabling this flag adds ``self`` to the python
1212
session, which is a reference to your Cmd2 application. This can be useful for
13-
debugging your application. To prevent users from enabling this ability
14-
manually you'll need to remove ``locals_in_py`` from the ``settable``
15-
dictionary.
13+
debugging your application.
1614

1715
The ``app`` object (or your custom name) provides access to application
1816
commands through raw commands. For example, any application command call be

docs/features/initialization.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ capabilities which you may wish to utilize while initializing the app::
3939
self.continuation_prompt = '... '
4040

4141
# Allow access to your application in py and ipy via self
42-
self.locals_in_py = True
42+
self.self_in_py = True
4343

4444
# Set the default category name
4545
self.default_category = 'cmd2 Built-in Commands'
@@ -110,7 +110,7 @@ override:
110110
DisabledCommand objects.
111111
- **echo**: if ``True``, each command the user issues will be repeated to the
112112
screen before it is executed. This is particularly useful when running
113-
scripts. This behavior does not occur when a running command at the prompt.
113+
scripts. This behavior does not occur when running a command at the prompt.
114114
(Default: ``False``)
115115
- **editor**: text editor program to use with *edit* command (e.g. ``vim``)
116116
- **exclude_from_history**: commands to exclude from the *history* command
@@ -125,7 +125,7 @@ override:
125125
of results in a Python script or interactive console. Built-in commands don't
126126
make use of this. It is purely there for user-defined commands and
127127
convenience.
128-
- **locals_in_py**: if ``True`` allow access to your application in *py*
128+
- **self_in_py**: if ``True`` allow access to your application in *py*
129129
command via ``self`` (Default: ``False``)
130130
- **macros**: dictionary of macro names and their values
131131
- **max_completion_items**: max number of CompletionItems to display during
@@ -139,7 +139,7 @@ override:
139139
``app``)
140140
- **py_locals**: dictionary that defines specific variables/functions available
141141
in Python shells and scripts (provides more fine-grained control than making
142-
everything available with **locals_in_py**)
142+
everything available with **self_in_py**)
143143
- **quiet**: if ``True`` then completely suppress nonessential output (Default:
144144
``False``)
145145
- **quit_on_sigint**: if ``True`` quit the main loop on interrupt instead of

docs/features/multiline_commands.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@ blank line is *always* considered a command terminator (cannot be overridden).
1010

1111
In multiline commands, output redirection characters like ``>`` and ``|`` are
1212
part of the command arguments unless they appear after the terminator.
13+
14+
Continuation prompt
15+
-------------------
16+
17+
When a user types a :ref:`Multiline Command
18+
<features/multiline_commands:Multiline Commands>` it may span more than one
19+
line of input. The prompt for the first line of input is specified by the
20+
``cmd2.Cmd.prompt`` instance attribute - see
21+
:ref:`features/prompt:Customizing the Prompt`. The prompt for subsequent lines
22+
of input is defined by the ``cmd2.Cmd.continuation_prompt`` attribute.

docs/features/prompt.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@ Prompt
33

44
``cmd2`` can issue a prompt before soliciting user input.
55

6+
Customizing the Prompt
7+
----------------------
8+
9+
This prompt can be configured by setting the `cmd2.Cmd.prompt` instance
10+
attribute. This contains the string which should be printed as a prompt
11+
for user input.
12+
613
Asynchronous Feedback
714
---------------------
815

docs/features/settings.rst

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,6 @@ This setting can be one of three values:
4747
- ``Always`` - ANSI escape sequences are always passed through to the output
4848

4949

50-
continuation_prompt
51-
~~~~~~~~~~~~~~~~~~~
52-
53-
When a user types a :ref:`Multiline Command
54-
<features/multiline_commands:Multiline Commands>` it may span more than one
55-
line of input. The prompt for the first line of input is specified by the
56-
:ref:`features/settings:prompt` setting. The prompt for subsequent lines of
57-
input is defined by this setting.
58-
59-
6050
debug
6151
~~~~~
6252

@@ -71,7 +61,7 @@ echo
7161

7262
If ``True``, each command the user issues will be repeated to the screen
7363
before it is executed. This is particularly useful when running scripts.
74-
This behavior does not occur when a running command at the prompt.
64+
This behavior does not occur when running a command at the prompt.
7565

7666

7767
editor
@@ -95,13 +85,6 @@ feedback output will be mixed in with and indistinguishable from output
9585
generated with :meth:`~cmd2.cmd2.Cmd.poutput`.
9686

9787

98-
locals_in_py
99-
~~~~~~~~~~~~
100-
101-
Allow access to your application in one of the
102-
:ref:`features/embedded_python_shells:Embedded Python Shells` via ``self``.
103-
104-
10588
max_completion_items
10689
~~~~~~~~~~~~~~~~~~~~
10790

@@ -115,13 +98,6 @@ they will be displayed in the typical columnized format and will not include
11598
the description text of the CompletionItem.
11699

117100

118-
prompt
119-
~~~~~~
120-
121-
This setting contains the string which should be printed as a prompt for user
122-
input.
123-
124-
125101
quiet
126102
~~~~~
127103

@@ -180,14 +156,13 @@ You may want to prevent a user from modifying a builtin setting. A setting
180156
must appear in the :attr:`cmd2.cmd2.Cmd.settable` dictionary in order for it
181157
to be available to the :ref:`features/builtin_commands:set` command.
182158

183-
Let's say your program does not have any
184-
:ref:`features/multiline_commands:Multiline Commands`. You might want to hide
185-
the :ref:`features/settings:continuation_prompt` setting from your users since
186-
it is only applicable to multiline commands. To do so, remove it from the
159+
Let's say that you never want end users of your program to be able to enable
160+
full debug tracebacks to print out if an error occurs. You might want to hide
161+
the :ref:`features/settings:debug` setting. To do so, remove it from the
187162
:attr:`cmd2.cmd2.Cmd.settable` dictionary after you initialize your object::
188163

189164
class MyApp(cmd2.Cmd):
190165

191166
def __init__(self):
192167
super().__init__()
193-
self.settable.pop('continuation_prompt')
168+
self.settable.pop('debug')

docs/features/transcripts.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ the path instead of specifying it verbatim, or we can escape the slashes::
146146
invisible, you can add a regular expression to match them, so that you can
147147
see where they are when you look at the transcript::
148148

149-
(Cmd) set prompt
150-
prompt: (Cmd)/ /
149+
(Cmd) set editor
150+
editor: vim/ /
151151

152152
Some terminal emulators strip trailing space when you copy text from them.
153153
This could make the actual data generated by your app different than the

0 commit comments

Comments
 (0)