示例#1
0
def main(opt):

    do_server = opt.server
    do_html = opt.html
    do_connect = opt.connect
    do_browser = not opt.server and not opt.html and not opt.connect

    if do_browser:
        if opt.browser:
            util.msg(
                '--browser flag is obsolete; browser mode is now the default')
        else:
            util.msg(
                'browser mode is now the default; use --html out.html to generate static html'
            )

    if do_browser:
        do_server = True

    # --server or browser mode
    if do_server:
        httpd = None
        for opt.port in range(opt.port, opt.port + 100):
            try:
                Handler.exit_on_close = not opt.server
                httpd = BaseHTTPServer.HTTPServer(('', opt.port), Handler)
                break
            except Exception as e:
                util.msg('can\'t open port %d: %s' % (opt.port, e))
        if not httpd:
            raise e
        httpd.ses = Ses(opt, path='/0', server=True)  # default session
        url = 'http://localhost:%d' % opt.port
        util.msg('listening for a browser request for %s' % url)
        if do_browser:
            browser(opt, url)
        httpd.serve_forever()

    # --connect
    elif do_connect:
        args = ' '.join(pipes.quote(s) for s in sys.argv[1:])
        args = urllib.urlencode({'args': args})
        url = opt.connect + '/open?' + args
        browser(opt, url)

    # standalone static html
    elif do_html:
        util.msg('generating html file', do_html)
        ses = Ses(opt, server=False)
        ses.out = open(do_html, 'w')
        html.page(ses)
示例#2
0
def main(opt):

    do_server = opt.server
    do_html = opt.html
    do_connect = opt.connect
    do_browser = not opt.server and not opt.html and not opt.connect

    if do_browser:
        if opt.browser:
            util.msg('--browser flag is obsolete; browser mode is now the default')
        else:
            util.msg('browser mode is now the default; use --html out.html to generate static html')

    if do_browser:
        do_server = True

    # --server or browser mode
    if do_server:
        httpd = None
        for opt.port in range(opt.port, opt.port+100):
            try:
                Handler.exit_on_close = not opt.server
                httpd = BaseHTTPServer.HTTPServer(('', opt.port), Handler)
                break
            except Exception as e:
                util.msg('can\'t open port %d: %s' % (opt.port, e))
        if not httpd:
            raise e
        httpd.ses = Ses(opt, path='/0', server=True) # default session
        url = 'http://localhost:%d' % opt.port
        util.msg('listening for a browser request for %s' % url)
        if do_browser:
            browser(opt, url)
        httpd.serve_forever()

    # --connect
    elif do_connect:
        args = ' '.join(pipes.quote(s) for s in sys.argv[1:])
        args = urllib.urlencode({'args': args})
        url = opt.connect + '/open?' + args
        browser(opt, url)

    # standalone static html
    elif do_html:
        util.msg('generating html file', do_html)
        ses = Ses(opt, server=False)
        ses.out = open(do_html, 'w')
        html.page(ses)
示例#3
0
    def do_GET(self):

        # parse command-line args passed in url query string as an 'args' parameter
        def query2opt(query):
            args = shlex.split(query['args'][0])
            return __main__.get_opt(args)

        # parse url, extracting path and query portions
        _, _, path, _, query, _ = urlparse.urlparse(self.path)
        query = urlparse.parse_qs(query)

        # query to root is redirected to default session 0
        if path=='/':
            self.send_response(301) # permanent
            self.send_header('Location', '/0')

        # open a new view in a new window
        # expects command-line arg string in url parameter "args"
        # parse off the query, open new session based on that, then redirect to bare session url
        elif path=='/open':
            opt = query2opt(query) # parse url "args" parameter
            ses = Ses(opt, server=True) # new session
            self.send_response(302) # temporary redirect
            self.send_header('Location', ses.path)

        # top-level page: return the container, which includes
        #   progress message area - loaded via /ses/progress url in progress phase (below)
        #   content area - loaded via /ses/content url in content phase (below)
        elif path in Ses.sessions:
            ses = Ses.sessions[path]
            self.prepare(ses)
            html.container(ses)

        # info for a given time t
        elif path.endswith('/info'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.info(ses, t)

        # raw info for a given time t
        elif path.endswith('/raw'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.raw(ses, t, kind='raw')

        elif path.endswith('/metadata'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.raw(ses, t, kind='metadata')

        # progress phase: load the data in preparation for generating content
        # while emitting progress messages. We also accept new view parameters to open
        # new view in current window as command-line arg string in url parameter "args"
        elif path.endswith('/progress'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            if 'args' in query:
                ses.opt = query2opt(query) # parse url "args" parameter
            self.prepare(ses)
            html.load(ses)

        # content phase: generate actual html view from graph data loadedin progress phase
        elif path.endswith('/content'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            self.prepare(ses)
            html.page(ses)

        # a window closed
        elif '/close/' in path:
            if Handler.exit_on_close:
                path, _, what = path.rsplit('/', 2)
                util.msg('closing', path, '('+what+')')
                if path in Ses.sessions:
                    del Ses.sessions[path]
                if not Ses.sessions:
                    util.msg('all sessions closed, exiting')
                    os._exit(0)

        # otherwise not found
        else:
            self.send_response(404) # not found
示例#4
0
    def do_GET(self):

        # parse command-line args passed in url query string as an 'args' parameter
        def query2opt(query):
            args = shlex.split(query['args'][0])
            return __main__.get_opt(args)

        # parse url, extracting path and query portions
        _, _, path, _, query, _ = urlparse.urlparse(self.path)
        query = urlparse.parse_qs(query)

        # query to root is redirected to default session 0
        if path == '/':
            self.send_response(301)  # permanent
            self.send_header('Location', '/0')

        # open a new view in a new window
        # expects command-line arg string in url parameter "args"
        # parse off the query, open new session based on that, then redirect to bare session url
        elif path == '/open':
            opt = query2opt(query)  # parse url "args" parameter
            ses = Ses(opt, server=True)  # new session
            self.send_response(302)  # temporary redirect
            self.send_header('Location', ses.path)

        # top-level page: return the container, which includes
        #   progress message area - loaded via /ses/progress url in progress phase (below)
        #   content area - loaded via /ses/content url in content phase (below)
        elif path in Ses.sessions:
            ses = Ses.sessions[path]
            self.prepare(ses)
            html.container(ses)

        # info for a given time t
        elif path.endswith('/info'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.info(ses, t)

        # raw info for a given time t
        elif path.endswith('/raw'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.raw(ses, t, kind='raw')

        elif path.endswith('/metadata'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            t = float(query['t'][0])
            self.prepare(ses)
            html.raw(ses, t, kind='metadata')

        # progress phase: load the data in preparation for generating content
        # while emitting progress messages. We also accept new view parameters to open
        # new view in current window as command-line arg string in url parameter "args"
        elif path.endswith('/progress'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            if 'args' in query:
                ses.opt = query2opt(query)  # parse url "args" parameter
            self.prepare(ses)
            html.load(ses)

        # content phase: generate actual html view from graph data loadedin progress phase
        elif path.endswith('/content'):
            path = path.rsplit('/', 1)[0]
            ses = Ses.sessions[path]
            self.prepare(ses)
            html.page(ses)

        # a window closed
        elif '/close/' in path:
            if Handler.exit_on_close:
                path, _, what = path.rsplit('/', 2)
                util.msg('closing', path, '(' + what + ')')
                if path in Ses.sessions:
                    del Ses.sessions[path]
                if not Ses.sessions:
                    util.msg('all sessions closed, exiting')
                    os._exit(0)

        # otherwise not found
        else:
            self.send_response(404)  # not found