score.shell¶
This module provides a quick python shell where your project is initialized.
Quickstart¶
Just install the module and you’re good to go:
$ score shell
Python 3.4.3 (default, Jan 8 2016, 11:18:01)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(score)
<class 'score.init.initializer.ConfiguredScore'>
If you want other environment variables in your shell, you will need to register callback functions in your configuration file to add them at startup:
[shell]
callbacks = pyth.to.my.custom.init_shell_env
def init_shell_env(env):
import random
lines = [
"I'm, afraid we're fresh out of that, sir.",
"I'm afraid we never have that at the end of the week, sir, we get it fresh on Monday.",
"Ah! It's beeeen on order, sir, for two weeks. Was expecting it this morning.",
"Normally, sir, yes. Today the van broke down.",
"The cat's eaten it.",
"Well, we don't get much call for it around here, sir.",
"I'll have a look, sir ... nnnnnnnnnnnnnnnno.",
]
env['get_cheese'] = lambda cheese: random.choice(lines)
$ score shell
Python 3.4.3 (default, Jan 8 2016, 11:18:01)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> get_cheese('Red Leicester')
"I'm afraid we never have that at the end of the week, sir, we get it fresh on Monday."
You can also use ipython or bpython if you want:
[shell]
backend = bpython
Configuration¶
-
score.shell.
init
(confdict, ctx=None)[source]¶ Initializes this module acoording to the SCORE module initialization guidelines with the following configuration keys:
- backend python
- The shell backend to use. You can use either of the shells prvided by
this module (“python”, “ipython” and “bpython”) or a string, that will
be resolved using
score.init.parse_dotted_path()
. - backend.autoinstall True
- Whether the given backend should be installed automatically, if it was not found.
- callbacks
- A
list
ofdotted python paths
to functions that will be called before the shell is spawned. Every callback will receive a dict representing the variables that will be available in the shell. The callbacks are free to add further variables to this list.
Details¶
Backend¶
A backend, in this context, is a score.shell.Shell
instance. This
module comes with some shell backend pre-installed, but you can add your own by
subclassing Shell
and providing the path to your
class as backend:
[shell]
backend = path.to.my.custom_shell
API¶
Configuration¶
-
score.shell.
init
(confdict, ctx=None)[source] Initializes this module acoording to the SCORE module initialization guidelines with the following configuration keys:
- backend python
- The shell backend to use. You can use either of the shells prvided by
this module (“python”, “ipython” and “bpython”) or a string, that will
be resolved using
score.init.parse_dotted_path()
. - backend.autoinstall True
- Whether the given backend should be installed automatically, if it was not found.
- callbacks
- A
list
ofdotted python paths
to functions that will be called before the shell is spawned. Every callback will receive a dict representing the variables that will be available in the shell. The callbacks are free to add further variables to this list.
-
class
score.shell.
ConfiguredShellModule
[source]¶ This module’s
configuration class
.It also implements a __call__() method for evaluating arbitrary one-liners inside the shell environment and returning the result:
>>> from score.init import init_from_file >>> score = init_from_file('path/to/your/config.file') >>> score.shell('1 + 1') 2 >>> score.shell('get_cheese("Stilton")') "I'll have a look, sir ... nnnnnnnnnnnnnnnno."
It is also possible to use this feature without any command, in which case you will get an instance of the configured shell backend:
>>> score.shell() Python 3.4.3 (default, Jan 8 2016, 11:18:01) [GCC 5.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> get_cheese('Red Leicester') "I'm, afraid we're fresh out of that, sir." >>> exit() >>> # we left the spawned shell and landed on the original shell >>> # (the one we called score.shell() on)
-
backend
¶ The
score.shell.Shell
instance the module was configured with.
-
callbacks
¶ The list of resolved callback objects to use for constructing the shell environment.
-
-
class
score.shell.
Shell
[source]¶ Abstract base class for shell backends.
-
spawn
(env)[source]¶ Creates an interactive instance of this shell, where all variables from given env are available. Performs possible auto-installation and passes the control to
_spawn()
for the ractual work.
-
_is_available
()[source]¶ Whether this shell is installed. Must return True, if this shell’s
_spawn()
could be called immediately or False, if it’s_install()
needs to be invoked first.
-