Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #135 (closed defect: fixed)

Opened 14 years ago

Last modified 12 years ago

[PATCH] web shell should give model objects

Reported by: kevin Owned by: anonymous
Priority: normal Milestone: 0.9
Component: Toolbox Version:
Severity: normal Keywords:
Cc:

Description

The tg-admin shell provides you with your model objects. The web-based shell should do the same.

Change History

comment:1 Changed 14 years ago by Mark Godfrey <mark@…>

  • Summary changed from web shell should give model objects to [PATCH] web shell should give model objects

a lot of this is shamelessly stolen from turbogears/command/init.py, as it is virtually identical to the tg-admin shell.

--- turbogears_old/toolbox/shell.py     2005-11-23 14:46:18.100891792 +0000
+++ turbogears/toolbox/shell.py 2005-11-23 15:25:45.854501712 +0000
@@ -1,6 +1,6 @@
 import turbogears
 from turbogears import controllers
-import sys
+import sys, os
 import StringIO
 from code import InteractiveConsole
 from textwrap import TextWrapper
@@ -67,10 +67,39 @@

         return ( more, output.rstrip() )

+    # Shamelessly borrowed from turbogears.command :)
+    def _get_model(self):
+        "Looks to see if model.py still exists."
+
+        global user_package
+        import setuptools
+        if user_package:
+            candidate = user_package
+        else:
+            candidates = setuptools.find_packages()
+            if candidates:
+                candidate = candidates[0]
+            else:
+                return
+
+        if os.path.exists("%s/model.py" % candidate):
+            package = __import__(candidate, {}, {}, ["model"])
+            user_package = candidate
+
+            if hasattr(package, "model"):
+                return package.model
+
     def new_console(self):
         locs = dict(__name__='__web_console__',__doc__=None,reload_console=self.new_console)
         self.console = InteractiveConsole(locals=locs)

+        mod = self._get_model()
+
+        if mod:
+            locs.update(mod.__dict__)
+        else:
+            locs["__name__"] ="tg-admin"
+
     @turbogears.expose(html="turbogears.toolbox.console")
     def index(self):
         return dict()

comment:2 Changed 14 years ago by Mark Godfrey <mark@…>

Sorry for the re-post, missed a line out of the original patch:

--- turbogears_old/toolbox/shell.py     2005-11-23 14:46:18.100891792 +0000
+++ turbogears/toolbox/shell.py 2005-11-23 15:37:40.688830376 +0000
@@ -1,10 +1,12 @@
 import turbogears
 from turbogears import controllers
-import sys
+import sys, os
 import StringIO
 from code import InteractiveConsole
 from textwrap import TextWrapper

+user_package = None
+
 class WebConsole(controllers.Root):
     """Web based Python interpreter"""
     __label__='WebConsole'
@@ -67,10 +69,39 @@

         return ( more, output.rstrip() )

+    # Shamelessly borrowed from turbogears.command :)
+    def _get_model(self):
+        "Looks to see if model.py still exists."
+
+        global user_package
+        import setuptools
+        if user_package:
+            candidate = user_package
+        else:
+            candidates = setuptools.find_packages()
+            if candidates:
+                candidate = candidates[0]
+            else:
+                return
+
+        if os.path.exists("%s/model.py" % candidate):
+            package = __import__(candidate, {}, {}, ["model"])
+            user_package = candidate
+
+            if hasattr(package, "model"):
+                return package.model
+
     def new_console(self):
         locs = dict(__name__='__web_console__',__doc__=None,reload_console=self.new_console)
         self.console = InteractiveConsole(locals=locs)

+        mod = self._get_model()
+
+        if mod:
+            locs.update(mod.__dict__)
+        else:
+            locs["__name__"] ="tg-admin"
+
     @turbogears.expose(html="turbogears.toolbox.console")
     def index(self):
         return dict()

comment:3 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

I did basically what the patch says. I changed a bit to reflect that get_model is now available directly for use. Committed in [272].

Note: See TracTickets for help on using tickets.