示例#1
0
文件: savisit.py 项目: thraxil/gtreed
 def __init__(self, timeout):
     global visit_class
     super(SqlAlchemyVisitManager,self).__init__(timeout)
     visit_class_path = config.get("visit.saprovider.model",
                            "turbogears.visit.savisit.TG_Visit")
     visit_class = load_class(visit_class_path)
     bind_meta_data()
     if visit_class is TG_Visit:
         assign_mapper(session.context, visit_class, visits_table)
示例#2
0
文件: startup.py 项目: thraxil/gtreed
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.
    """
    config.update({"/tg_static":
            {
            "static_filter.on": True,
            "static_filter.dir":
                os.path.abspath(pkg_resources.resource_filename(__name__, "static")),
            'log_debug_info_filter.on' : False,
            }
        })
    config.update({"/tg_js" :
            {
            "static_filter.on" : True,
            "static_filter.dir" :
                os.path.abspath(pkg_resources.resource_filename(__name__, "static/js")),
            'log_debug_info_filter.on' : False,
            }
        })
    cherrypy.config.environments['development']['log_debug_info_filter.on'] = False
    
    if config.get("decoding_filter.on", path="/") is None:
        config.update({"/": {
            "decoding_filter.on" : True,
            "decoding_filter.encoding" : config.get(
                                        "kid.encoding", "utf8")
        }})
        
    view.load_engines()
    view.loadBaseTemplates()
    global webpath
    webpath = config.get("server.webpath", "")

    if hasattr(cherrypy, "root") and cherrypy.root:
        if not hasattr(cherrypy.root, "_cp_filters"):
            cherrypy.root._cp_filters= []
        morefilters = [EndTransactionsFilter(),
                       NestedVariablesFilter()]
        if webpath:
            morefilters.insert(0, VirtualPathFilter())
        cherrypy.root._cp_filters.extend(morefilters)

    if webpath.startswith("/"):
        webpath = webpath[1:]
    if webpath and not webpath.endswith("/"):
        webpath = webpath + "/"
    isdev = config.get('server.environment') == 'development'
    if not config.get("tg.new_style_logging"):
        if config.get('server.log_to_screen'):
            setuplog = logging.getLogger()
            setuplog.setLevel(logging.DEBUG)
            fmt = logging.Formatter("%(asctime)s %(name)s "
                                    "%(levelname)s %(message)s")
            handler = logging.StreamHandler(sys.stdout)
            handler.setLevel(logging.DEBUG)
            handler.setFormatter(fmt)
            setuplog.addHandler(handler)
        
        logfile = config.get("server.log_file")
        if logfile:
            setuplog = logging.getLogger("turbogears.access")
            setuplog.propagate = 0
            fmt = logging.Formatter("%(message)s")
            handler = logging.FileHandler(logfile)
            handler.setLevel(logging.INFO)
            handler.setFormatter(fmt)
            setuplog.addHandler(handler)

    bonjoursetting = config.get("tg.bonjour", None)
    if bonjoursetting or isdev:
        start_bonjour(bonjoursetting)
    
    if config.get("sqlalchemy.dburi"):
        database.bind_meta_data()

    # Start all TurboGears extensions
    extensions= pkg_resources.iter_entry_points( "turbogears.extensions" )
    for entrypoint in extensions:
        ext= entrypoint.load()
        if hasattr(ext, "start_extension"):
            ext.start_extension()
    
    for item in call_on_startup:
        item()
        
    if config.get("tg.scheduler", False):
        scheduler._start_scheduler()
        log.info("Scheduler started")
示例#3
0
文件: base.py 项目: thraxil/gtreed
def sacreate(command, args):
    print "Creating tables at %s" % (config.get("sqlalchemy.dburi"))
    from turbogears.database import bind_meta_data, metadata
    bind_meta_data()
    get_model()
    metadata.create_all()
示例#4
0
文件: base.py 项目: thraxil/gtreed
    def run(self):
        "Run the shell"
        self.find_config()
        
        mod = get_model()
        if mod:
            locals = mod.__dict__
        else:
            locals = dict(__name__="tg-admin")
            
        if config.get("sqlalchemy.dburi"):
            using_sqlalchemy = True
            database.bind_meta_data()
            locals.update(session=database.session,
                          metadata=database.metadata)
        else:
            using_sqlalchemy = False

        try:
            # try to use IPython if possible
            import IPython

            class CustomIPShell(IPython.iplib.InteractiveShell):
                def raw_input(self, *args, **kw):
                    try:
                        return \
                         IPython.iplib.InteractiveShell.raw_input(self,
                                                    *args, **kw)
                    except EOFError:
                        b = raw_input("Do you wish to commit your "
                                    "database changes? [yes]")
                        if not b.startswith("n"):
                            if using_sqlalchemy:
                                self.push("session.flush()")
                            else:
                                self.push("hub.commit()")
                        raise EOFError

            shell = IPython.Shell.IPShell(user_ns=locals,
                                          shell_class=CustomIPShell)
            shell.mainloop()
        except ImportError:
            import code

            class CustomShell(code.InteractiveConsole):
                def raw_input(self, *args, **kw): 
                    try:
                        import readline
                    except ImportError:
                        pass

                    try:
                        return code.InteractiveConsole.raw_input(self,
                                                        *args, **kw)
                    except EOFError:
                        b = raw_input("Do you wish to commit your "
                                      "database changes? [yes]")
                        if not b.startswith("n"):
                            if using_sqlalchemy:
                                self.push("session.flush()")
                            else:
                                self.push("hub.commit()")
                        raise EOFError

            shell = CustomShell(locals=locals)
            shell.interact()