def startTurboGears(): """Handles TurboGears tasks when the CherryPy server starts. This adds the "tg_js" configuration to make MochiKit accessible. It also turns on stdlib logging when in development mode. """ conf = config.get # cherrypy.tools.visit = VisitTool() cherrypy.tools.expose = controllers.expose cherrypy.tools.flash = controllers.flash cherrypy.tools.visit.start_extension() cherrypy.tools.identity.start_extension() if have_toscawidgets: toscawidgets.start_extension() if have_oauth: cherrypy.tools.oauth = OAuthTool() # Bind metadata for SQLAlchemy if conf("sqlalchemy.dburi"): database.bind_metadata() # Call registered startup functions for item in call_on_startup: item()
def status(command, args): bind_metadata() get_model() ret = compare_metadata(metadata, MetaData(metadata.bind)) for l in ret: print l if not ret: print "Database matches model"
def setup_module(): global users_table, test_table, multi_table, fresh_multi_table config.update({ 'sqlalchemy.dburi': 'sqlite:///:memory:', 'fresh.dburi': 'sqlite:///freshtest.db', }) if os.path.exists('freshtest.db'): os.unlink('freshtest.db') fresh_metadata = get_metadata('fresh') if metadata.is_bound(): metadata.bind = None bind_metadata() metadata.bind.echo = True fresh_metadata.bind.echo = True users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) persons_table = Table("persons", metadata, Column("id", Integer, primary_key=True), Column("name", String(40))) addresses_table = Table("addresses", metadata, Column("id", Integer, primary_key=True), Column("address", String(40)), Column("city", String(40)), Column("person_id", Integer, ForeignKey(persons_table.c.id))) multi_table = Table('multi', metadata, Column('id', Integer, primary_key=True)) metadata.create_all() test_table = Table("test", fresh_metadata, Column("id", Integer, primary_key=True), Column("val", String(40))) fresh_multi_table = Table('multi', fresh_metadata, Column('id', Integer, primary_key=True)) fresh_metadata.create_all() mapper(User, users_table) mapper(Person, persons_table) mapper(Address, addresses_table, properties=dict( person=relation(Person, backref='addresses'))) mapper(Test, test_table) start_server()
def __init__(self, timeout): global visit_class visit_class_path = config.get("tools.visit.saprovider.model", "gearshift.visit.savisit.TG_Visit") visit_class = load_class(visit_class_path) if visit_class is None: msg = 'No visit class found for %s' % visit_class_path msg += ', did you run setup.py develop?' log.error(msg) bind_metadata() if visit_class is TG_Visit: mapper(visit_class, visits_table) # base-class' __init__ triggers self.create_model, so mappers need to # be initialized before. super(SqlAlchemyVisitManager, self).__init__(timeout)
class Shell(CommandWithDB): """Convenient version of the Python interactive shell. This shell attempts to locate your configuration file and model module so that it can import everything from your model and make it available in the Python shell namespace. """ desc = "Start a Python prompt with your database available" need_project = True def run(self): """Run the shell""" self.find_config() locals = dict(__name__="tg-admin") try: mod = get_model() if mod: locals.update(mod.__dict__) except (pkg_resources.DistributionNotFound, ImportError), e: mod = None print "Warning: Failed to import your data model: %s" % e print "You will not have access to your data model objects." print if config.get("sqlalchemy.dburi"): using_sqlalchemy = True database.bind_metadata() locals.update(session=database.session, metadata=database.metadata) else: using_sqlalchemy = False class CustomShellMixin(object): def commit_changes(self): if mod: # XXX Can we check somehow, if there are actually any # database changes to be commited? r = raw_input("Do you wish to commit" " your database changes? [yes]") if not r.startswith("n"): if using_sqlalchemy: self.push("session.flush()") else: self.push("hub.commit()") try: # try to use IPython if possible from IPython import iplib, Shell class CustomIPShell(iplib.InteractiveShell, CustomShellMixin): def raw_input(self, *args, **kw): try: # needs decoding (see below)? return iplib.InteractiveShell.raw_input( self, *args, **kw) except EOFError: self.commit_changes() raise EOFError shell = Shell.IPShell(user_ns=locals, shell_class=CustomIPShell) shell.mainloop() except ImportError: import code class CustomShell(code.InteractiveConsole, CustomShellMixin): def raw_input(self, *args, **kw): try: import readline except ImportError: pass try: r = code.InteractiveConsole.raw_input( self, *args, **kw) for encoding in (getattr(sys.stdin, 'encoding', None), sys.getdefaultencoding(), 'utf-8', 'latin-1'): if encoding: try: return r.decode(encoding) except UnicodeError: pass return r except EOFError: self.commit_changes() raise EOFError shell = CustomShell(locals=locals) shell.interact()
def create_model(self): """Create the Visit table if it doesn't already exist.""" bind_metadata() class_mapper(visit_class).local_table.create(checkfirst=True)
def create(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) bind_metadata() get_model() metadata.create_all()