Exemplo n.º 1
0
def setup():
    from openlibrary.plugins.openlibrary import (
        sentry,
        home,
        borrow_home,
        stats,
        support,
        events,
        design,
        status,
        authors,
    )

    sentry.setup()
    home.setup()
    design.setup()
    borrow_home.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    authors.setup()

    from openlibrary.plugins.openlibrary import api
    delegate.app.add_processor(web.unloadhook(stats.stats_hook))

    if infogami.config.get('dev_instance') is True:
        from openlibrary.plugins.openlibrary import dev_instance
        dev_instance.setup()

    setup_context_defaults()
    setup_template_globals()
Exemplo n.º 2
0
    def testUnload(self):
        x = web.storage(a=0)

        urls = ("/foo", "foo", "/bar", "bar")

        class foo:
            def GET(self):
                return "foo"

        class bar:
            def GET(self):
                raise web.notfound()

        app = web.application(urls, locals())

        def unload():
            x.a += 1

        app.add_processor(web.unloadhook(unload))

        app.request("/foo")
        self.assertEquals(x.a, 1)

        app.request("/bar")
        self.assertEquals(x.a, 2)
Exemplo n.º 3
0
    def __init__(self, mapping, fvars):

        # Parent constructor
        web.application.__init__(self, mapping, fvars) #@UndefinedVariable

        # The views are bound once for all to the configuration
        config.views = web.template.render("app/views/", globals={
            "all_seasons": lambda: Season.all(),
            "all_polls": lambda: Poll.all(),
            "webparts": webparts,
            "formatting": formatting,
            "dates": dates,
            "zip": zip,
            "getattr": getattr,
            "hasattr": hasattr,
            "class_name": lambda x: x.__class__.__name__,
            "namedtuple": collections.namedtuple,
            "config": config,
            "result_statuses": Result.STATUSES,
            "Events": Events
        })

        # The ORM is bound once since it dynamically loads the engine from the configuration
        config.orm = meta.init_orm(lambda : config.engine)

        # Binds the hooking mechanism & the SQL Alchemy processor
        self.add_processor(web.loadhook(http.init_hooks))
        self.add_processor(web.unloadhook(http.execute_hooks))        
        self.add_processor(http.sqlalchemy_processor)

        # Binds the webparts initialization mechanism
        self.add_processor(web.loadhook(webparts.init_webparts))
Exemplo n.º 4
0
def setup():
    from openlibrary.plugins.openlibrary import (home, inlibrary, borrow_home,
                                                 libraries, stats, support,
                                                 events, design, status,
                                                 merge_editions, authors)

    home.setup()
    design.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    merge_editions.setup()
    authors.setup()

    from openlibrary.plugins.openlibrary import api
    delegate.app.add_processor(web.unloadhook(stats.stats_hook))

    if infogami.config.get("dev_instance") is True:
        from openlibrary.plugins.openlibrary import dev_instance
        dev_instance.setup()

    setup_context_defaults()
    setup_template_globals()
Exemplo n.º 5
0
def init_app():
    app = web.application(urls, globals())
    #app.notfound = api_notfound
    #app.internalerror = api_internalerror
    app.add_processor(web.loadhook(api_loadhook))
    app.add_processor(web.unloadhook(api_unloadhook))    
    return app
Exemplo n.º 6
0
def setup():
    import home, inlibrary, borrow_home, libraries, stats, support, events, status, merge_editions, authors
    
    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    merge_editions.setup()
    authors.setup()
    
    import api
    api.setup()
    
    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))
    
    if infogami.config.get("dev_instance") is True:
        import dev_instance
        dev_instance.setup()

    setup_template_globals()
    setup_logging()
    logger = logging.getLogger("openlibrary")
    logger.info("Application init")
Exemplo n.º 7
0
def setup():
    import home, inlibrary, borrow_home, libraries, stats, support, \
        events, status, merge_editions, authors

    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    merge_editions.setup()
    authors.setup()

    import api
    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))

    if infogami.config.get("dev_instance") is True:
        import dev_instance
        dev_instance.setup()

    setup_context_defaults()
    setup_template_globals()
Exemplo n.º 8
0
def setup():
    from openlibrary.plugins.openlibrary import (home, inlibrary, borrow_home, libraries,
                                                 stats, support, events, design, status,
                                                 merge_editions, authors)

    home.setup()
    design.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    merge_editions.setup()
    authors.setup()

    from openlibrary.plugins.openlibrary import api
    delegate.app.add_processor(web.unloadhook(stats.stats_hook))

    if infogami.config.get("dev_instance") is True:
        from openlibrary.plugins.openlibrary import dev_instance
        dev_instance.setup()

    setup_context_defaults()
    setup_template_globals()
Exemplo n.º 9
0
    def testUnload(self):
        x = web.storage(a=0)

        # fmt: off
        urls = ("/foo", "foo", "/bar", "bar")

        # fmt: on

        class foo:
            def GET(self):
                return "foo"

        class bar:
            def GET(self):
                raise web.notfound()

        app = web.application(urls, locals())

        def unload():
            x.a += 1

        app.add_processor(web.unloadhook(unload))

        app.request("/foo")
        self.assertEqual(x.a, 1)

        app.request("/bar")
        self.assertEqual(x.a, 2)
Exemplo n.º 10
0
    def test_hook(self):
        app.add_processor(web.loadhook(before))
        app.add_processor(web.unloadhook(after))

        self.assertEqual(app.request('/hello').data, 'yx')
        global str
        self.assertEqual(str, 'yxz')
Exemplo n.º 11
0
def init_app():
    app = web.application(urls, globals())
    #app.notfound = api_notfound
    #app.internalerror = api_internalerror
    app.add_processor(web.loadhook(api_loadhook))
    app.add_processor(web.unloadhook(api_unloadhook))
    return app
Exemplo n.º 12
0
  def __new__(cls, *args, **kwargs):
    app = web.application(*args, **kwargs)

    app.add_processor(web.loadhook(cls._connect))
    app.add_processor(web.unloadhook(cls._disconnect))

    return app
Exemplo n.º 13
0
def setup():
    import home, inlibrary, borrow_home, libraries, stats, support, events, status, merge_editions, authors
    
    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()
    status.setup()
    merge_editions.setup()
    authors.setup()
    
    import api
    api.setup()
    
    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))
    
    if infogami.config.get("dev_instance") is True:
        import dev_instance
        dev_instance.setup()

    setup_context_defaults()
    setup_template_globals()
    setup_logging()
    logger = logging.getLogger("openlibrary")
    logger.info("Application init")
Exemplo n.º 14
0
 def test_hook(self):
     app.add_processor(web.loadhook(before))
     app.add_processor(web.unloadhook(after))
    
     self.assertEqual(app.request('/hello').data, 'yx')
     global str
     self.assertEqual(str, 'yxz')
Exemplo n.º 15
0
    def __new__(cls, *args, **kwargs):
        app = web.application(*args, **kwargs)

        app.add_processor(web.loadhook(cls._connect))
        app.add_processor(web.unloadhook(cls._disconnect))

        return app
Exemplo n.º 16
0
 def set_webpy_hooks(self, app, shutdown, rollback):
     try:
         import web
     except ImportError:
         return
     if not hasattr(web, 'application'):
         return
     if not isinstance(app, web.application):
         return
     app.processors.append(0, web.unloadhook(shutdown))
Exemplo n.º 17
0
def setup():
    import home, inlibrary, borrow_home, libraries
    
    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    
    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))
    
    setup_template_globals()
Exemplo n.º 18
0
 def set_webpy_hooks(self, app, shutdown, rollback):
     """Setup the webpy-specific ``web.unloadhook`` to call
     ``db.session.remove()`` after each response.
     """
     try:
         import web
     except ImportError:
         return
     if not hasattr(web, 'application'):
         return
     if not isinstance(app, web.application):
         return
     app.processors.append(0, web.unloadhook(shutdown))
Exemplo n.º 19
0
 def set_webpy_hooks(self, app, shutdown, rollback):
     """Setup the webpy-specific ``web.unloadhook`` to call
     ``db.session.remove()`` after each response.
     """
     try:
         import web
     except ImportError:
         return
     if not hasattr(web, 'application'):
         return
     if not isinstance(app, web.application):
         return
     app.processors.append(0, web.unloadhook(shutdown))
Exemplo n.º 20
0
def createprocessor():
	createsession()
	def pre_hook():
		mylog.loginfo()
	app.add_processor(web.loadhook(pre_hook))
	def post_hook():
		if web.ctx.fullpath[0:6] == "/rest/": 
				if web.ctx.fullpath[6:14] != "resource" \
						and web.ctx.fullpath[6:11] != "photo":
							web.header("content-type", "application/json")
				else:
					return
		else:
			web.header("content-type", "text/html; charset=utf-8")
	app.add_processor(web.unloadhook(post_hook))
Exemplo n.º 21
0
def setup():
    """
    This function is called from the main application startup
    routine to set things up.
    """

    # Initialise the stats filters
    register_filter("all", stats_filters.all)
    register_filter("url", stats_filters.url)
    register_filter("loggedin", stats_filters.loggedin)
    register_filter("not_loggedin", stats_filters.not_loggedin)

    delegate.app.add_processor(web.loadhook(page_load_hook))
    delegate.app.add_processor(web.unloadhook(page_unload_hook))
    delegate.add_exception_hook(lambda: increment_error_count('ol.exceptions'))
Exemplo n.º 22
0
def setup():
    import home, inlibrary, borrow_home, libraries, stats
    
    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    
    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))
    
    setup_template_globals()
    setup_logging()
    logger = logging.getLogger("openlibrary")
    logger.info("Application init")
Exemplo n.º 23
0
def setup():
    import home, inlibrary, borrow_home, libraries, stats, support, events

    home.setup()
    inlibrary.setup()
    borrow_home.setup()
    libraries.setup()
    stats.setup()
    support.setup()
    events.setup()

    from stats import stats_hook
    delegate.app.add_processor(web.unloadhook(stats_hook))

    setup_template_globals()
    setup_logging()
    logger = logging.getLogger("openlibrary")
    logger.info("Application init")
Exemplo n.º 24
0
class index:
    def my_processor(handler):
        print('before handling')
        print('after handling')
        handler = 'hi'
        result = handler
        print(result)
        return result

    app.add_processor(my_processor)

    def my_loadhook():
        web.header('Content-type', "text/html; charset=utf-8")
        input = web.input()
        print(input)

    def my_unloadhook():
        print("my unload hook")

    app.add_processor(web.loadhook(my_loadhook))
    app.add_processor(web.unloadhook(my_unloadhook))
Exemplo n.º 25
0
        cur = conn.cursor()

    return cur


def close_db():
    global conn, cur
    if cur:
        cur = None
    if conn:
        conn.rollback()
        conn.close()
        conn = None


# Retrieves and returns the 404 image from filesystem (TODO: do as redirect instead)
def not_found_image():
    rel_path = 'storage/not-found.jpg'
    abs_file_path = os.path.join(script_dir, rel_path)
    fp = open(abs_file_path, 'r+')
    return fp


# Run the API
if __name__ == '__main__':
    app = web.application(urls, globals())
    app.add_processor(web.loadhook(cursor_db))
    app.add_processor(web.unloadhook(close_db))
    app.run()
Exemplo n.º 26
0
    if web.ctx.status == '200 OK' and accept and not pattern.search(accept):
        web.header('Content-Type', 'text/xml')
    
urls = (
    '/?', 'Service',
    '/category', 'Category',
    '/collection/(.*)', 'Collection',
    '/document/(.*)', 'Document',
)

connection = Connection()
db_name = 'test' if sys.argv[-1] == '--test' else 'test_database'
db = connection[db_name]
app_atom = web.application(urls, locals())
app_atom.add_processor(web.unloadhook(my_unloadhook))

class Service:
    def GET(self):
        """
        Returns a service entity conforming to RFC ...
        >>> req = app_atom.request('/', method='GET')
        >>> req.status
        '200 OK'
        >>> req.headers['Content-Type']
        'application/atomsvc+xml;charset=\"utf-8\"'
        """
        collections = []        
        for name in db.collection_names():
            if not name == 'system.indexes':
                collections.append(E.collection(
Exemplo n.º 27
0
class checkAlive:
  def GET(self):
    return ''  # if exec_query('SELECT NULL') else web.internalerror()


class checkReady:
  def GET(self):
    return ''  # if exec_query('SELECT NULL') else web.internalerror()


if __name__ == '__main__':

  # map uris to classes
  urls = (
    '/covid', 'covid',
    CHECK_ALIVE_PATH, 'checkAlive',
    CHECK_READY_PATH, 'checkReady'
  )
  app = web.application(urls, globals())

  # collect and expose request/response metrics
  if os.environ.get('EXPOSE_METRICS', 'true') != 'false':
    REQUEST_COUNT = prometheus_client.Counter('requests', 'Request Count', ['method', 'path', 'status'])
    REQUEST_LATENCY = prometheus_client.Histogram('request_latency', 'Request Latency', ['method', 'path'])
    app.add_processor(web.loadhook(before_request))
    app.add_processor(web.unloadhook(after_request))
    prometheus_client.start_http_server(8000)

  # start http server
  web.httpserver.runsimple(app.wsgifunc(), ('0.0.0.0', 8080))
Exemplo n.º 28
0
class redirect:
    def GET(self, path):
        print "302 redirect path:", path
        web.seeother('/' + path)


def my_loadhook():
    print "load hook...."
    print "from load hook", web.input()


def my_unloadhook():
    print "unload hook...."


if __name__ == '__main__':
    #load web urls
    app = web.application(urls, globals())
    #add hooks
    app.add_processor(web.loadhook(my_loadhook))
    app.add_processor(web.unloadhook(my_unloadhook))
    #run as log
    #app.run(Log)
    #run as console
    app.run()
    app.stop()
    app.setUp()
    app.tearDown()
    app.close()
Exemplo n.º 29
0
    "/([^/]*)/reindex", "reindex",    
    "/([^/]*)/new_key", "new_key",
    "/([^/]*)/things", "things",
    "/([^/]*)/versions", "versions",
    "/([^/]*)/write", "write",
    "/([^/]*)/account/(.*)", "account",
    "/([^/]*)/permission", "permission",
    "/([^/]*)/log/(\d\d\d\d-\d\d-\d\d:\d+)", 'readlog',
    "/_invalidate", "invalidate"
)

app = web.application(urls, globals(), autoreload=False)

app.add_processor(web.loadhook(setup_remoteip))
app.add_processor(web.loadhook(cache.loadhook))
app.add_processor(web.unloadhook(cache.unloadhook))

def process_exception(e):
    if isinstance(e, common.InfobaseException):
        status = e.status
    else:
        status = "500 Internal Server Error"

    msg = str(e)
    raise web.HTTPError(status, {}, msg)
    
class JSON:
    """JSON marker. instances of this class not escaped by jsonify.
    """
    def __init__(self, json):
        self.json = json
Exemplo n.º 30
0
unauth_urls=('/login','/register','/getpwd')
def auth_hook():
    '''except urls: /login, /register, /getpwd'''
    path = web.ctx.path
    if any(url in path for url in unauth_urls):
        return
    web.ctx.currentuserid = authenticate()

def json_hook():
    '''should we foce all the response is json, or only the GET method ?'''
    web.header('Content-Type', 'application/json')
    #how to get the result and output via json?

app_api.add_processor(web.loadhook(auth_hook))
app_api.add_processor(web.loadhook(httpmethod_hook)) #has order ?
app_api.add_processor(web.unloadhook(json_hook))


################### common function ###########################

class ExtendedEncoder(json.JSONEncoder):
    '''http://stackoverflow.com/questions/6182967/how-to-format-a-mysql-query-into-json-using-webpy'''
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return o.strftime("%Y-%m-%d %H:%M:%S")
        return json.JSONEncoder(self, o)

def authenticate():
    '''http://webpy.org/cookbook/userauthbasic'''
    uid = False
    auth = web.ctx.env.get('HTTP_AUTHORIZATION')
Exemplo n.º 31
0
	if False != session.set_flash:
		session.flash = session.set_flash
		session.set_flash = False
	if False != session.set_error_flash:
		session.error_flash = session.set_error_flash
		session.set_error_flash = False

def feeder_loadhook ():
	session.feeder = feeder.is_alive()

def flash_unloadhook ():
	session.flash = False
	session.error_flash = False

app.add_processor( web.loadhook( permission_loadhook ) )
app.add_processor( web.loadhook( flash_loadhook ) )
app.add_processor( web.loadhook( feeder_loadhook ) )
app.add_processor( web.unloadhook( flash_unloadhook ) )

####### Start the Feeder #######

feeder = Process( target=smf.feeder.run, args=( constants.DEFAULT_CONFIG, ) )
feeder.daemon = True
feeder.start()

####### Start the Server #######
try:
	app.run()
except Exception, e:
	feeder.terminate()
	feeder.join()
Exemplo n.º 32
0
        #i = web.input()
        print 'handle request'
        return "He"


def my_processor(handler):
    print 'before handling'
    result = handler()
    print 'after handling'
    return result


def before_handle():
    print 'before'


def after_handle():
    print 'after'


app = web.application(urls, globals())

app.add_processor(web.loadhook(before_handle))
app.add_processor(web.unloadhook(after_handle))

#app.add_processor(my_processor)

if __name__ == "__main__":

    app.run()
Exemplo n.º 33
0
## main function ###############################################################
"""
try:
	webctx.session
except NameError:
	web.debug("Resetting session ...")
	webctx.session = None
"""

app = None
if __name__ == "__main__":
    web.config.debug = False

    app = service(urls, globals())
    # session setup, make sure to call it only once if in debug mode
    init_session(app)

    app.add_processor(web.loadhook(hooks.load))
    app.add_processor(web.unloadhook(hooks.unload))

    #webctx.session["pid"] += 1
    #print "starting ..."
    #app.add_processor(web.loadhook(loadhook))
    #app.add_processor(web.unloadhook(unloadhook))
    #app.run(config.port, "0.0.0.0")

    #webctx.session = get_session(app)

    app.run(config.port, "0.0.0.0", Log)
Exemplo n.º 34
0
    web.ctx.status = '404 Not Found'
    return web.notfound('404 Not Found')

def internalerror():
    web.ctx.status = '500 Internal Server Error'
    return web.internalerror('500 Internal Server Error')

def handle_validate(func):
    def Function(*args):
        #todo 验证请求

        return func(*args)
    return Function

app.add_processor(web.loadhook(app_processor_start))
app.add_processor(web.unloadhook(app_processor_end))


class Graphic:

    _stream = None
    _org = None
    _format = None
    _org_w = 0
    _org_h = 0
    _quality = 60


    def __init__(self,original_file):
        if not os.path.isfile(original_file):
            raise "no original_file found"
Exemplo n.º 35
0
# 设置时区
baseutils.switch_time_zone()

urls = baseutils.import_modules(APPS)
app = web.application(urls, globals(), autoreload=False)
session = web.session.Session(app, web.session.DiskStore("sessions"))
web.config._session = session


def notfound():
    return web.notfound(render.notfound())


app.notfound = notfound
app.add_processor(web.loadhook(auth_admin))
app.add_processor(web.unloadhook(baseutils.assert_closed))

if __name__ == "__main__":
    if len(sys.argv) == 1:
        app.run()
    elif sys.argv[1] == "deploy":
        #    if "deploy" in sys.argv:
        from flup.server.fcgi import WSGIServer

        func = app.wsgifunc()
        server_address = "/tmp/nowater_novel.sock"
        WSGIServer(
            func,
            bindAddress=server_address,
            maxSpare=16,
            minSpare=16,
Exemplo n.º 36
0
    def my_unloadhook():
        print "my unload hook"

        app.add_processor(web.loadhook(my_loadhook))
        app.add_processor(web.unloadhook(my_unloadhook))
Exemplo n.º 37
0
        header('Content-Type', 'application/x-json-stream')
        try:
            params = parse_response(json_data)
            if 'pfns' in params:
                pfns = params['pfns']
            if 'rse' in params:
                rse = params['rse']
        except ValueError:
            raise generate_http_error(400, 'ValueError', 'Cannot decode json parameter list')

        try:
            declare_bad_file_replicas(rse=rse, pfns=pfns, issuer=ctx.env.get('issuer'))
        except ReplicaNotFound, e:
            raise generate_http_error(404, 'ReplicaNotFound', e.args[0][0])
        except RucioException, e:
            raise generate_http_error(500, e.__class__.__name__, e.args[0][0])
        except Exception, e:
            print format_exc()
            raise InternalError(e)
        raise Created()


"""----------------------
   Web service startup
----------------------"""

app = application(urls, globals())
app.add_processor(loadhook(rucio_loadhook))
app.add_processor(unloadhook(rucio_unloadhook))
application = app.wsgifunc()
Exemplo n.º 38
0
      yield packer.pack(names)
      for row in result:
        resultTuple = (
            row.uid,
            calendar.timegm(row.created_at.timetuple()),
            calendar.timegm(row.agg_ts.timetuple()),
            row.text,
            row.username,
            row.userid
          )
        yield packer.pack(resultTuple)
    else:
      results = {"names": ["uid", "created_at", "agg_ts", "text", "username",
                           "userid"],
                 "data": [(row.uid,
                           row.created_at.strftime("%Y-%m-%d %H:%M:%S"),
                           row.agg_ts.strftime("%Y-%m-%d %H:%M:%S"),
                           row.text,
                           row.username,
                           row.userid) for row in result]}
      self.addStandardHeaders()
      yield utils.jsonEncode(results)



app = ManagedConnectionWebapp(urls, globals())
app.add_processor(web.loadhook(_connect))
app.add_processor(web.unloadhook(_disconnect))

Exemplo n.º 39
0
"""
try:
	webctx.session
except NameError:
	web.debug("Resetting session ...")
	webctx.session = None
"""

app = None
if __name__ == "__main__":
	web.config.debug = False

	app = service(urls, globals())
	# session setup, make sure to call it only once if in debug mode
	init_session(app)
	
	app.add_processor(web.loadhook(hooks.load))
	app.add_processor(web.unloadhook(hooks.unload))

	#webctx.session["pid"] += 1
	#print "starting ..."
	#app.add_processor(web.loadhook(loadhook))
	#app.add_processor(web.unloadhook(unloadhook))
	#app.run(config.port, "0.0.0.0")
	
	#webctx.session = get_session(app)

	app.run(config.port, "0.0.0.0", Log)


Exemplo n.º 40
0
        r'/([^/]*)/save(/.*)', 'save', r'/([^/]*)/save_many', 'save_many',
        r"/([^/]*)/reindex", "reindex", r"/([^/]*)/new_key", "new_key",
        r"/([^/]*)/things", "things", r"/([^/]*)/versions", "versions",
        r"/([^/]*)/write", "write", r"/([^/]*)/account/(.*)", "account",
        r"/([^/]*)/permission", "permission",
        r"/([^/]*)/log/(\d\d\d\d-\d\d-\d\d:\d+)", "readlog",
        r"/([^/]*)/_store/(_.*)", "store_special", r"/([^/]*)/_store/(.*)",
        "store", r"/([^/]*)/_seq/(.*)", "seq", r"/([^/]*)/_recentchanges",
        "recentchanges", r"/([^/]*)/_recentchanges/(\d+)", "change",
        "/_invalidate", "invalidate")

app = web.application(urls, globals(), autoreload=False)

app.add_processor(web.loadhook(setup_remoteip))
app.add_processor(web.loadhook(cache.loadhook))
app.add_processor(web.unloadhook(cache.unloadhook))


def process_exception(e):
    if isinstance(e, common.InfobaseException):
        status = e.status
    else:
        status = "500 Internal Server Error"

    msg = str(e)
    raise web.HTTPError(status, {}, msg)


class JSON:
    """JSON marker. instances of this class not escaped by jsonify.
    """
Exemplo n.º 41
0
# 设置时区
baseutils.switch_time_zone()

urls = baseutils.import_modules(APPS)
app = web.application(urls, globals(), autoreload=False)
session = web.session.Session(app, web.session.DiskStore('sessions'))
web.config._session = session


def notfound():
    return web.notfound(render.notfound())


app.notfound = notfound
app.add_processor(web.loadhook(auth_admin))
app.add_processor(web.unloadhook(baseutils.assert_closed))

if __name__ == '__main__':
    if len(sys.argv) == 1:
        app.run()
    elif sys.argv[1] == 'deploy':
        #    if "deploy" in sys.argv:
        from flup.server.fcgi import WSGIServer
        func = app.wsgifunc()
        server_address = '/tmp/nowater_novel.sock'
        WSGIServer(
            func,
            bindAddress=server_address,
            maxSpare=16,
            minSpare=16,
            #            maxRequests=128,
Exemplo n.º 42
0
		"""
	#global session
	#session = s

if __name__ == "__main__":
	
	curdir = os.path.dirname(__file__)
	curdir=""
	web.config.debug = config.web_debug
	#web.config.debug = False
	
	app = rot(webctx.urls, globals())
	init_session(app)
	
	app.add_processor(web.loadhook(loadhook))
	app.add_processor(web.unloadhook(unloadhook))
	
	# web.myapp = app
	# web.init_session = init_session
	
	app.run(config.port, "0.0.0.0", Log)
else:
	
	web.config.debug = config.web_debug
	app = web.application(webctx.urls, globals())
	init_session(app)
	
	app.add_processor(web.loadhook(loadhook))
	app.add_processor(web.unloadhook(unloadhook))
	
	# web.myapp = app
Exemplo n.º 43
0
    '''except urls: /login, /register, /getpwd'''
    path = web.ctx.path
    if any(url in path for url in unauth_urls):
        return
    web.ctx.currentuserid = authenticate()


def json_hook():
    '''should we foce all the response is json, or only the GET method ?'''
    web.header('Content-Type', 'application/json')
    #how to get the result and output via json?


app_api.add_processor(web.loadhook(auth_hook))
app_api.add_processor(web.loadhook(httpmethod_hook))  #has order ?
app_api.add_processor(web.unloadhook(json_hook))

################### common function ###########################


class ExtendedEncoder(json.JSONEncoder):
    '''http://stackoverflow.com/questions/6182967/how-to-format-a-mysql-query-into-json-using-webpy'''
    def default(self, o):
        if isinstance(o, datetime.datetime):
            return o.strftime("%Y-%m-%d %H:%M:%S")
        return json.JSONEncoder(self, o)


def authenticate():
    '''http://webpy.org/cookbook/userauthbasic'''
    uid = False
Exemplo n.º 44
0
                replicas = params['replicas']
        except ValueError:
            raise generate_http_error(400, 'ValueError',
                                      'Cannot decode json parameter list')

        try:
            for replica in replicas:
                set_tombstone(replica['rse'],
                              replica['scope'],
                              replica['name'],
                              issuer=ctx.env.get('issuer'))
        except ReplicaNotFound as error:
            raise generate_http_error(404, 'ReplicaNotFound', error.args[0])
        except RucioException as error:
            raise generate_http_error(500, error.__class__.__name__,
                                      error.args[0])
        except Exception as error:
            print(format_exc())
            raise InternalError(error)
        raise Created()


"""----------------------
   Web service startup
----------------------"""

APP = application(URLS, globals())
APP.add_processor(loadhook(rucio_loadhook))
APP.add_processor(unloadhook(rucio_unloadhook))
application = APP.wsgifunc()
Exemplo n.º 45
0
            result = conn.execute(sel.order_by(direction(orderByField)))

        if "application/octet-stream" in web.ctx.env.get("HTTP_ACCEPT", ""):
            packer = msgpack.Packer()
            self.addStandardHeaders(content_type="application/octet-stream")
            web.header("X-Accel-Buffering", "no")

            yield packer.pack(names)
            for row in result:
                resultTuple = (row.uid,
                               calendar.timegm(row.created_at.timetuple()),
                               calendar.timegm(row.agg_ts.timetuple()),
                               row.text, row.username, row.userid)
                yield packer.pack(resultTuple)
        else:
            results = {
                "names":
                ["uid", "created_at", "agg_ts", "text", "username", "userid"],
                "data":
                [(row.uid, row.created_at.strftime("%Y-%m-%d %H:%M:%S"),
                  row.agg_ts.strftime("%Y-%m-%d %H:%M:%S"), row.text,
                  row.username, row.userid) for row in result]
            }
            self.addStandardHeaders()
            yield utils.jsonEncode(results)


app = ManagedConnectionWebapp(urls, globals())
app.add_processor(web.loadhook(_connect))
app.add_processor(web.unloadhook(_disconnect))
Exemplo n.º 46
0
        return json.dumps({"success": True})

urls = ("/?", "Index",
        "/dl/(.*)", "Download",
        "/help", "Help",
        "/mkdir/(.*)", "Mkdir",
        "/nodes", "Nodes",
        "/odp", "Odp",
        "/rm/(.*)", "Rm",
        "/rmdir/(.*)", "Rmdir",
        "/s/(.*)", "Search",
        "/upload", "Upload"
        )

def db_load_hook():
    web.ctx.db = DB(os.path.join(config["juno-base"], ".db"))

def db_unload_hook():
    web.ctx.db.close()

web.config.debug = False
app = web.application(urls, globals())
app.add_processor(web.loadhook(db_load_hook))
app.add_processor(web.unloadhook(db_unload_hook))
application = app.wsgifunc()
config = common.load_config()

if __name__ == "__main__":
    app.run()
Exemplo n.º 47
0
def get_app(config_path, sessions_path=""):
    """
        Returns the web.py main application of ICTV.
        Currently, only one application can be run a time due to how data such as assets, database, config files
        or plugins is stored.
    """
    # Loads ICTV core config file
    config_file = get_config(config_path)
    if database.database_path is None:
        database.database_path = config_file['database_uri']

    app_urls = urls
    if config_file['debug']['dummy_login']:
        app_urls += ('/login/(.+)', 'ictv.pages.utils.DummyLogin')

    if config_file['debug']['debug_env']:
        app_urls += ('/debug_env', 'DebugEnv')

    if 'local' in config_file['authentication']:
        app_urls += (
            '/login',
            'ictv.pages.local_login.LoginPage',
            '/reset',
            'ictv.pages.local_login.GetResetLink',
            '/reset/(.+)',
            'ictv.pages.local_login.ResetPage',
            '/logout',
            'ictv.pages.utils.LogoutPage',
        )

    if 'saml2' in config_file['authentication']:
        app_urls += (
            '/shibboleth',
            'ictv.pages.shibboleth.Shibboleth',
            '/shibboleth_metadata',
            'ictv.pages.shibboleth.MetadataPage',
        )

    # Create a base web.py application
    app = web.application(app_urls, globals())
    app.config = config_file

    app.version = ictv.common.__version__

    with open(os.path.join(get_root_path(), 'info' + os.extsep + 'yaml')) as f:
        # Loads ICTV user info texts
        info_texts = yaml.load(f)

    # Load the SMTP config into web.py
    smtp_conf = app.config.get('smtp', None)
    if smtp_conf:
        web.config.smtp_sendername = smtp_conf['sender_name']
        web.config.smtp_server = smtp_conf['host']
        web.config.smtp_port = smtp_conf['port']
        web.config.smtp_username = smtp_conf.get('username', '')
        web.config.smtp_password = smtp_conf.get('password', '')
        web.config.smtp_starttls = smtp_conf.get('starttls', False)

    # Create a persistent HTTP session storage for the app
    app.session = web.session.Session(
        app,
        OptimisticThreadSafeDisktore(os.path.join(sessions_path, 'sessions')))
    # Populate the web.py templates globals
    template_globals = {
        'session': app.session,
        'get_feedbacks': get_feedbacks,
        'get_next_feedbacks': get_next_feedbacks,
        'pop_previous_form': pop_previous_form,
        'UserPermissions': UserPermissions,
        'json': json,
        'str': str,
        'sorted': sorted,
        'hasattr': hasattr,
        'sidebar_collapse': False,
        'show_header': True,
        'show_footer': True,
        're': re,
        'info': info_texts,
        'make_tooltip': make_tooltip,
        'make_alert': make_alert,
        'escape': html.escape,
        'show_reset_password': '******' in app.config['authentication'],
        'homedomain': lambda: web.ctx.homedomain,
        'generate_secret': generate_secret,
        'version': lambda: app.version,
        'pretty_print_size': pretty_print_size,
        'timesince': timesince,
        'User': User,
        'get_user': lambda: User.get(app.session['user']['id'])
    }
    # Init the web.py renderer used for the admin interface
    template_kwargs = {
        'loc': os.path.join(get_root_path(), 'templates/'),
        'cache': not app.config['debug']['debug_on_error'],
        'globals': template_globals
    }
    app.renderer = web.template.render(base='base', **template_kwargs)

    # Init a second web.py renderer without any base template
    app.standalone_renderer = web.template.render(**template_kwargs)

    # Init loggers
    load_loggers_stats()
    # Determine logging level and user feedback when an internal error occurs based on ICTV core config
    level = logging.INFO
    if app.config['debug']['debug_on_error']:
        level = logging.DEBUG
        app.internalerror = web.debugerror
    loggers_to_init = [
        'app', 'pages', 'screens', 'plugin_manager', 'storage_manager',
        'local_login', 'database', 'transcoding_queue'
    ]
    for logger_name in loggers_to_init:
        init_logger(logger_name,
                    level,
                    rotation_interval=app.config['logs']['rotation_interval'],
                    backup_count=app.config['logs']['backup_count'])

    # Init the renderer used for slide, capsule, channel and screen rendering
    app.ictv_renderer = ICTVRenderer(app)
    # Init the plugin manager, used as a gateway between ICTV core and its plugins.
    app.plugin_manager = PluginManager(app)

    # Init the download manager, a download queue which asynchronously downloads assets from the network
    app.download_manager = DownloadManager()
    # Init the cleanup manager which will regularly cleanup unused cached assets
    app.cleanup_scheduler = CleanupScheduler()
    app.cleanup_scheduler.start()
    # Init the video transcoding queue which will convert videos to WebM format using FFmpeg
    app.transcoding_queue = TranscodingQueue()

    # Add an hook to make session available through web.ctx for plugin webapps
    def session_hook():
        web.ctx.session = app.session
        web.template.Template.globals['session'] = app.session

    app.add_processor(web.loadhook(session_hook))

    # Add a preprocessor to populate web.ctx with meaningful values when app is run behind a proxy
    app.add_processor(proxy_web_ctx_processor)
    # Add a preprocessor to encapsulate every SQL requests in a transaction on a per HTTP request basis
    app.add_processor(
        get_request_errors_preprocessor(logging.getLogger('database'),
                                        logging.getLogger('pages')))
    # Add an general authentication processor to handle user authentication
    app.add_processor(get_authentication_processor(app))
    # Add a hook to clean feedbacks from the previous request and prepare next feedbacks to be shown to the user
    app.add_processor(web.unloadhook(rotate_feedbacks))

    # Instantiate plugins through the plugin manager
    app.plugin_manager.instantiate_plugins(app)

    # Load themes and templates into database
    sqlhub.doInTransaction(load_templates_and_themes)

    return app