Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Some changes to telnet console:

Pablo Hoffman38b57931528b
diff --git a/docs/topics/telnetconsole.rst b/docs/topics/telnetconsole.rst
index 8b9efa5a7..5833457b8 100644
--- a/docs/topics/telnetconsole.rst
+++ b/docs/topics/telnetconsole.rst
@@ -31,13 +31,13 @@ the console you need to type::
You need the telnet program which comes installed by default in Windows, and
most Linux distros.
-Available aliases in the telnet console
-=======================================
+Available variables in the telnet console
+=========================================
The telnet console is like a regular Python shell running inside the Scrapy
-process, so you can do anything from it including imports, etc.
+process, so you can do anything from it including importing new modules, etc.
-However, the telnet console comes with some default shortcuts defined for
+However, the telnet console comes with some default variables defined for
convenience:
+----------------+-------------------------------------------------------------------+
@@ -126,3 +126,17 @@ To stop::
>>> engine.stop()
Connection closed by foreign host.
+Telnet Console signals
+======================
+
+.. signal:: update_telnet_vars
+.. function:: update_telnet_vars(telnet_vars)
+
+ Sent just before the telnet console is opened. You can hook up to this
+ signal to add, remove or update the variables that will be available in the
+ telnet local namespace. In order to do that, you need to update the
+ ``telnet_vars`` dict in your handler.
+
+ :param telnet_vars: the dict of telnet variables
+ :type telnet_vars: dict
+
diff --git a/scrapy/conf/default_settings.py b/scrapy/conf/default_settings.py
index 635e549ea..f86615b32 100644
--- a/scrapy/conf/default_settings.py
+++ b/scrapy/conf/default_settings.py
@@ -110,7 +110,7 @@ EXTENSIONS = {}
EXTENSIONS_BASE = {
'scrapy.contrib.corestats.CoreStats': 0,
'scrapy.management.web.WebConsole': 0,
- 'scrapy.management.telnet.TelnetConsole': 0,
+ 'scrapy.telnet.TelnetConsole': 0,
'scrapy.contrib.webconsole.scheduler.SchedulerQueue': 0,
'scrapy.contrib.webconsole.livestats.LiveStats': 0,
'scrapy.contrib.webconsole.spiderctl.Spiderctl': 0,
diff --git a/scrapy/management/telnet.py b/scrapy/telnet.py
similarity index 50%
rename from scrapy/management/telnet.py
rename to scrapy/telnet.py
index ba1315bb8..49deed463 100644
--- a/scrapy/management/telnet.py
+++ b/scrapy/telnet.py
@@ -15,6 +15,7 @@ from scrapy.core.exceptions import NotConfigured
from scrapy.core.manager import scrapymanager
from scrapy.spider import spiders
from scrapy.stats import stats
+from scrapy.utils.signal import send_catch_log
from scrapy.utils.trackref import print_live_refs
from scrapy.utils.engine import print_engine_status
from scrapy.conf import settings
@@ -25,30 +26,38 @@ try:
except ImportError:
hpy = None
-# if you add entries here also update topics/telnetconsole.rst
-telnet_namespace = {
- 'engine': scrapymanager.engine,
- 'manager': scrapymanager,
- 'extensions': extensions,
- 'stats': stats,
- 'spiders': spiders,
- 'settings': settings,
- 'est': print_engine_status,
- 'p': pprint.pprint,
- 'prefs': print_live_refs,
- 'hpy': hpy,
-}
-
-def makeProtocol():
- return telnet.TelnetTransport(telnet.TelnetBootstrapProtocol,
- insults.ServerProtocol, manhole.Manhole, telnet_namespace)
+# signal to update telnet variables
+# args: telnet_vars
+update_telnet_vars = object()
+
class TelnetConsole(protocol.ServerFactory):
def __init__(self):
if not settings.getbool('TELNETCONSOLE_ENABLED'):
raise NotConfigured
- self.protocol = makeProtocol
self.noisy = False
port = settings.getint('TELNETCONSOLE_PORT')
reactor.callWhenRunning(reactor.listenTCP, port, self)
+
+ def protocol(self):
+ telnet_vars = self._get_telnet_vars()
+ return telnet.TelnetTransport(telnet.TelnetBootstrapProtocol,
+ insults.ServerProtocol, manhole.Manhole, telnet_vars)
+
+ def _get_telnet_vars(self):
+ # Note: if you add entries here also update topics/telnetconsole.rst
+ telnet_vars = {
+ 'engine': scrapymanager.engine,
+ 'manager': scrapymanager,
+ 'extensions': extensions,
+ 'stats': stats,
+ 'spiders': spiders,
+ 'settings': settings,
+ 'est': print_engine_status,
+ 'p': pprint.pprint,
+ 'prefs': print_live_refs,
+ 'hpy': hpy,
+ }
+ send_catch_log(update_telnet_vars, telnet_vars=telnet_vars)
+ return telnet_vars