Changeset 5493

Show
Ignore:
Timestamp:
09/27/08 23:10:50 (3 months ago)
Author:
carndt
Message:

Let 'tg-admin sql' (for SO and SA) and 'tg-admin shell' hanlde missing ORm gracefully

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/turbogears/command/base.py

    r4406 r5493  
    11"""Commands for the TurboGears command line tool.""" 
    22 
     3import glob 
    34import optparse 
     5import os 
    46import sys 
    5 import os 
    6 import os.path 
    7 import glob 
    87 
    98import pkg_resources 
     9import configobj 
     10 
    1011from peak.rules import NoApplicableMethods 
    11  
    12 import configobj 
    13  
    1412import turbogears 
    1513from turbogears.util import get_model, load_project_config, \ 
     
    1715from turbogears.identity import SecureObject, from_any_host 
    1816from turbogears import config, database 
     17 
    1918from sacommand import sacommand 
    2019 
     
    8786 
    8887    def run(self): 
    89         "Executes the sqlobject-admin code." 
     88        """Runs the sqlobject-admin tool or functions from the sacommand module. 
     89        """ 
     90 
    9091        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.
    9293            return 
    9394        else: 
     
    99100                except NoApplicableMethods: 
    100101                    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') 
    101109                return 
    102110 
     
    124132                    sys.argv.insert(2, "--egg") 
    125133 
    126             from sqlobject.manager import command 
    127134            command.the_runner.run(sys.argv) 
    128135 
     
    168175        self.find_config() 
    169176 
    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 
    175187 
    176188        if config.get("sqlalchemy.dburi"): 
     
    182194            using_sqlalchemy = False 
    183195 
     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 
    184209        try: 
    185210            # try to use IPython if possible 
    186             import IPython 
    187  
    188             class CustomIPShell(IPython.iplib.InteractiveShell): 
     211            from IPython import iplib, Shell 
     212 
     213            class CustomIPShell(iplib.InteractiveShell, CustomShellMixin): 
    189214                def raw_input(self, *args, **kw): 
    190215                    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) 
    193219                    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() 
    201221                        raise EOFError 
    202222 
    203             shell = IPython.Shell.IPShell(user_ns=locals, 
    204                                           shell_class=CustomIPShell) 
     223            shell = Shell.IPShell(user_ns=locals, shell_class=CustomIPShell) 
    205224            shell.mainloop() 
    206225        except ImportError: 
    207226            import code 
    208227 
    209             class CustomShell(code.InteractiveConsole): 
     228            class CustomShell(code.InteractiveConsole, CustomShellMixin): 
    210229                def raw_input(self, *args, **kw): 
    211230                    try: 
     
    225244                        return r 
    226245                    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() 
    234247                        raise EOFError 
    235248 
  • branches/1.1/turbogears/command/sacommand.py

    r4881 r5493  
    22from turbogears import config 
    33from turbogears.util import get_model 
     4from turbogears.decorator import simple_decorator 
    45try: 
    56    from sqlalchemy import MetaData, exceptions, Table 
    67    from sqlalchemy import String, Unicode, Text, UnicodeText 
    78    from turbogears.database import bind_metadata, metadata, get_engine 
     9    has_sa = True 
    810except ImportError: 
    9     pass 
     11    from turbogears.util import missing_dependency_error 
     12    has_sa = False 
     13 
     14 
     15@simple_decorator 
     16def 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) 
    1022 
    1123@abstract() 
     
    2840 
    2941@when(sacommand, "command == 'create'") 
     42@check_sa 
    3043def create(command, args): 
    3144    print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) 
     
    3548 
    3649@when(sacommand, "command == 'list'") 
     50@check_sa 
    3751def list_(command, args): 
    3852    get_model() 
     
    4155 
    4256@when(sacommand, "command == 'execute'") 
     57@check_sa 
    4358def execute(command, args): 
    4459    eng = get_engine() 
     
    5267 
    5368@when(sacommand, "command == 'status'") 
     69@check_sa 
    5470def status(command, args): 
    5571    bind_metadata()