Changeset 5493
- Timestamp:
- 09/27/08 23:10:50 (3 months ago)
- Files:
-
- branches/1.1/turbogears/command/base.py (modified) (8 diffs)
- branches/1.1/turbogears/command/sacommand.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.1/turbogears/command/base.py
r4406 r5493 1 1 """Commands for the TurboGears command line tool.""" 2 2 3 import glob 3 4 import optparse 5 import os 4 6 import sys 5 import os6 import os.path7 import glob8 7 9 8 import pkg_resources 9 import configobj 10 10 11 from peak.rules import NoApplicableMethods 11 12 import configobj13 14 12 import turbogears 15 13 from turbogears.util import get_model, load_project_config, \ … … 17 15 from turbogears.identity import SecureObject, from_any_host 18 16 from turbogears import config, database 17 19 18 from sacommand import sacommand 20 19 … … 87 86 88 87 def run(self): 89 "Executes the sqlobject-admin code." 88 """Runs the sqlobject-admin tool or functions from the sacommand module. 89 """ 90 90 91 if not "--egg" in sys.argv and not turbogears.util.get_project_name(): 91 print " this don't look like a turbogears project"92 print "This doesn't look like a TurboGears project." 92 93 return 93 94 else: … … 99 100 except NoApplicableMethods: 100 101 sacommand("help", []) 102 return 103 104 try: 105 from sqlobject.manager import command 106 except ImportError: 107 from turbogears.util import missing_dependency_error 108 print missing_dependency_error('SQLObject') 101 109 return 102 110 … … 124 132 sys.argv.insert(2, "--egg") 125 133 126 from sqlobject.manager import command127 134 command.the_runner.run(sys.argv) 128 135 … … 168 175 self.find_config() 169 176 170 mod = get_model() 171 if mod: 172 locals = mod.__dict__ 173 else: 174 locals = dict(__name__="tg-admin") 177 locals = dict(__name__="tg-admin") 178 try: 179 mod = get_model() 180 if mod: 181 locals.update(mod.__dict__) 182 except (pkg_resources.DistributionNotFound, ImportError), e: 183 mod = None 184 print "Warning: Failed to import your data model: %s" % e 185 print "You will not have access to your data model objects." 186 print 175 187 176 188 if config.get("sqlalchemy.dburi"): … … 182 194 using_sqlalchemy = False 183 195 196 class CustomShellMixin(): 197 def commit_changes(self): 198 if mod: 199 # XXX Can we check somehow, if there are actually any 200 # database changes to be commited? 201 r = raw_input("Do you wish to commit your " 202 "database changes? [yes]") 203 if not r.startswith("n"): 204 if using_sqlalchemy: 205 self.push("session.flush()") 206 else: 207 self.push("hub.commit()") 208 184 209 try: 185 210 # try to use IPython if possible 186 import IPython187 188 class CustomIPShell( IPython.iplib.InteractiveShell):211 from IPython import iplib, Shell 212 213 class CustomIPShell(iplib.InteractiveShell, CustomShellMixin): 189 214 def raw_input(self, *args, **kw): 190 215 try: 191 return IPython.iplib.InteractiveShell.raw_input(self, 192 *args, **kw) # needs decoding (see below)? 216 # needs decoding (see below)? 217 return iplib.InteractiveShell.raw_input(self, *args, 218 **kw) 193 219 except EOFError: 194 r = raw_input("Do you wish to commit your " 195 "database changes? [yes]") 196 if not r.startswith("n"): 197 if using_sqlalchemy: 198 self.push("session.flush()") 199 else: 200 self.push("hub.commit()") 220 self.commit_changes() 201 221 raise EOFError 202 222 203 shell = IPython.Shell.IPShell(user_ns=locals, 204 shell_class=CustomIPShell) 223 shell = Shell.IPShell(user_ns=locals, shell_class=CustomIPShell) 205 224 shell.mainloop() 206 225 except ImportError: 207 226 import code 208 227 209 class CustomShell(code.InteractiveConsole ):228 class CustomShell(code.InteractiveConsole, CustomShellMixin): 210 229 def raw_input(self, *args, **kw): 211 230 try: … … 225 244 return r 226 245 except EOFError: 227 r = raw_input("Do you wish to commit your " 228 "database changes? [yes]") 229 if not r.startswith("n"): 230 if using_sqlalchemy: 231 self.push("session.flush()") 232 else: 233 self.push("hub.commit()") 246 self.commit_changes() 234 247 raise EOFError 235 248 branches/1.1/turbogears/command/sacommand.py
r4881 r5493 2 2 from turbogears import config 3 3 from turbogears.util import get_model 4 from turbogears.decorator import simple_decorator 4 5 try: 5 6 from sqlalchemy import MetaData, exceptions, Table 6 7 from sqlalchemy import String, Unicode, Text, UnicodeText 7 8 from turbogears.database import bind_metadata, metadata, get_engine 9 has_sa = True 8 10 except ImportError: 9 pass 11 from turbogears.util import missing_dependency_error 12 has_sa = False 13 14 15 @simple_decorator 16 def check_sa(func, command, args): 17 """Tiny decorator checking if SQLAlchemy is installed before execution.""" 18 if not has_sa: 19 print missing_dependency_error('SQLAlchemy') 20 else: 21 return func(command, args) 10 22 11 23 @abstract() … … 28 40 29 41 @when(sacommand, "command == 'create'") 42 @check_sa 30 43 def create(command, args): 31 44 print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) … … 35 48 36 49 @when(sacommand, "command == 'list'") 50 @check_sa 37 51 def list_(command, args): 38 52 get_model() … … 41 55 42 56 @when(sacommand, "command == 'execute'") 57 @check_sa 43 58 def execute(command, args): 44 59 eng = get_engine() … … 52 67 53 68 @when(sacommand, "command == 'status'") 69 @check_sa 54 70 def status(command, args): 55 71 bind_metadata()