<commit_before>#!/usr/bin/env python

import logging
import tornado.ioloop
import tornado.web
import os.path
import os
import json
import uuid
import multiprocessing

import common

from tornado import template

#from pyjade.ext.tornado import patch_tornado
#patch_tornado()

# TODO: make it configurable
TRANSFER_SIZE_LIMIT = 128 * 1024

class RunHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        """
        sessions
            dict: session_id->session_status
        """
        self.out_queue = out_queue
        self.sessions = sessions
        self.working_dir = working_dir

    def post(self):

        test_id = self.get_argument("test", uuid.uuid4().hex)
        breakpoint = self.get_argument("break", "finish")
        session_id = uuid.uuid4().hex
        config = self.request.body

        self.set_header("Content-type", "application/json")

        #Check existing sessions
        running_session=None
        conflict_session=None
        for s in self.sessions:
            if s['status'] not in ['success','failed']:
                running_session=s
            if s['test']==test_id:
                conflict_session=s

        # 409 if session with this test_id is already running
        if conflict_session is not None:
                self.set_status(409)
                self.finish(json.dumps(conflict_session))
                return

        # 409 if finished test with this test_id exists
        test_status_file=os.path.join(self.working_dir, test_id, 'status.json')
        if os.path.exists(test_status_file):
            self.set_status(409)
            self.finish(open(test_status_file).read())
            return

        # 503 if any running session exists (but no test_id conflict)
        if running_session is not None:
                self.set_status(503)
                self.finish(json.dumps(running_session))
                return

        #Remember that such session exists
        self.sessions[session_id]={'status':'starting',
                              'break':breakpoint,
                              'test': test_id}

        # Post run command to manager queue
        self.out_queue.put({'session':session_id,
                       'cmd':'run',
                       'break':breakpoint,
                       'test':test_id,
                       'config':config})

        self.set_status(200)
        self.finish(json.dumps({
                               "test": test_id,
                               "session": session_id,
                               }))

    def get(self):
        breakpoint = self.get_argument("break", "finish")
        session_id = self.get_argument("session")
        self.set_header("Content-type", "application/json")

        # 400 if invalid breakpoint
        if not breakpoint in common.test_stage_order:
            self.set_status(400)
            self.finish(json.dumps({'reason':'Invalid break point',
                                    'hint':
                                        {'breakpoints':common.test_stage_order}
                                   }))
            return

        # 404 if no such session
        if not session_id in self.sessions:
            self.set_status(404)
            self.finish(json.dumps({'reason':'No session with this ID'}))
            return
        status_dict=self.sessions[session_id]

        # 500 if failed
        if status_dict['status'] == 'failed':
            self.set_status(500)
            self.finish(json.dumps(status_dict))
            return

        # 418 if in higher state or not running
        if status_dict['status'] == 'success' or common.is_A_earlier_than_B(breakpoint,status_dict['break']):
            reply={'reason':'I am a teapot! I know nothing of time-travel!',
                    'hint': {'breakpoints':common.test_stage_order} }
            reply.update(status_dict)
            self.set_status(418)
            self.finish(json.dumps(reply))
            return

        # Post run command to manager queue
        self.out_queue.put({'session':session_id,
                       'cmd':'run',
                       'break':breakpoint})

        self.set_status(200)

class StopHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions

    def get(self):
        session_id = self.get_argument("session")
        if session_id:
            if session_id in self.sessions:
                if self.sessions[session_id]['status'] not in ['success','failed']:
                    self.out_queue.put({
                        'cmd': 'stop',
                        'session': session_id,
                    })
                    self.set_status(200)
                    return
                else:
                    self.set_status(409)
                    self.finish(json.dumps(
                        {
                            'reason': 'This session is already stopped',
                            'session': session_id,
                        }
                    ))
                    return
            else:
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No session with this ID found',
                    'session': session_id,
                }))
                return
        else:
            self.set_status(400)
            self.finish(json.dumps(
                {'reason': 'Specify an ID of a session you want to stop'}
            ))
            return

class StatusHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions

    def get(self):
        print self.request.arguments

        session_id = self.get_argument("session",default=None)
        self.set_header("Content-type", "application/json")
        if session_id:
            if session_id in self.sessions:
                self.set_status(200)
                self.finish(json.dumps(
                    self.sessions[session_id]
                ))
            else:
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No session with this ID found',
                    'session': session_id,
                }))
        else:
            self.set_status(200)
            self.finish(json.dumps(
                self.sessions
            ))

class ArtifactHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions
        self.working_dir = working_dir

    def get(self):
        test_id = self.get_argument("test")
        filename = self.get_argument("filename",None)

        # look for status.json (any test should have it)
        if not os.path.exists(os.path.join(self.working_dir, test_id, 'status.json')):
            self.set_header("Content-type", "application/json")
            self.set_status(404)
            self.finish(json.dumps({
                'reason': 'No test with this ID found',
                'test': test_id,
            }))
            return

        if filename:
            filepath = os.path.join(self.working_dir, test_id, filename)
            if os.path.exists(filepath):
                file_size = os.stat(filepath)

                if file_size > TRANSFER_SIZE_LIMIT and any(s['status'] not in ['success','failed'] for s in self.sessions):
                    self.set_header("Content-type", "application/json")
                    self.set_status(503)
                    self.finish(json.dumps({
                        'reason': 'File is too large and test is running',
                        'test': test_id,
                        'filename': filename,
                        'filesize': file_size,
                        'limit': TRANSFER_SIZE_LIMIT,
                    }))
                    return
                else:
                    self.set_header("Content-type", "application/octet-stream")
                    with open(filepath, 'rb') as f:
                        data = f.read()
                        self.write(data)
                    self.finish()
                    return
            else:
                self.set_header("Content-type", "application/json")
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No such file',
                    'test': test_id,
                    'filename': filename,
                }))
                return
        else:
            basepath = os.path.join(self.working_dir, test_id)
            onlyfiles = [
                f for f in os.listdir(basepath)
                if os.path.isfile(os.path.join(basepath, f))
            ]
            self.set_header("Content-type", "application/json")
            self.set_status(200)
            self.finish(json.dumps(onlyfiles))

class ApiServer(object):
    def __init__(self, in_queue, out_queue, working_dir):
        self.in_queue = in_queue
        self.out_queue = out_queue
        self.working_dir = working_dir
        self.sessions = {}
        handler_params = dict(
            out_queue=self.out_queue,
            sessions=self.sessions,
            working_dir=self.working_dir,
        )
        self.app = tornado.web.Application(
            [
                (r"/run", RunHandler, handler_params),
                (r"/stop", StopHandler, handler_params),
                (r"/status", StatusHandler, handler_params),
                (r"/artifact", ArtifactHandler, handler_params),
            ],
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )

    def update_status(self):
        try:
           while True:
               message = self.in_queue.get_nowait()
               session_id=message.get('session')
               del message['session']
               #Test ID and break are always present in message from Manager
               self.sessions[session_id] = message
        except multiprocessing.queues.Empty:
            pass

    def serve(self):
        self.app.listen(8888)
        ioloop = tornado.ioloop.IOLoop.instance()
        update_cb = tornado.ioloop.PeriodicCallback(self.update_status, 100, ioloop)
        update_cb.start()
        ioloop.start()

def main(webserver_queue, manager_queue, test_directory):
    """Target for webserver process.
    The only function ever used by the Manager.

    webserver_queue
        Read statuses from Manager here.

    manager_queue
        Write commands for Manager there.

    test_directory
        Directory where tests are

    """
    ApiServer(webserver_queue, manager_queue, test_directory).serve()
<commit_msg>Fixed typo (self.sessions->self.sessions.values() )<commit_after>#!/usr/bin/env python

import logging
import tornado.ioloop
import tornado.web
import os.path
import os
import json
import uuid
import multiprocessing

import common

from tornado import template

#from pyjade.ext.tornado import patch_tornado
#patch_tornado()

# TODO: make it configurable
TRANSFER_SIZE_LIMIT = 128 * 1024

class RunHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        """
        sessions
            dict: session_id->session_status
        """
        self.out_queue = out_queue
        self.sessions = sessions
        self.working_dir = working_dir

    def post(self):

        test_id = self.get_argument("test", uuid.uuid4().hex)
        breakpoint = self.get_argument("break", "finish")
        session_id = uuid.uuid4().hex
        config = self.request.body

        self.set_header("Content-type", "application/json")

        #Check existing sessions
        running_session=None
        conflict_session=None
        for s in self.sessions.values():
            if s['status'] not in ['success','failed']:
                running_session=s
            if s['test']==test_id:
                conflict_session=s

        # 409 if session with this test_id is already running
        if conflict_session is not None:
                self.set_status(409)
                self.finish(json.dumps(conflict_session))
                return

        # 409 if finished test with this test_id exists
        test_status_file=os.path.join(self.working_dir, test_id, 'status.json')
        if os.path.exists(test_status_file):
            self.set_status(409)
            self.finish(open(test_status_file).read())
            return

        # 503 if any running session exists (but no test_id conflict)
        if running_session is not None:
                self.set_status(503)
                self.finish(json.dumps(running_session))
                return

        #Remember that such session exists
        self.sessions[session_id]={'status':'starting',
                              'break':breakpoint,
                              'test': test_id}

        # Post run command to manager queue
        self.out_queue.put({'session':session_id,
                       'cmd':'run',
                       'break':breakpoint,
                       'test':test_id,
                       'config':config})

        self.set_status(200)
        self.finish(json.dumps({
                               "test": test_id,
                               "session": session_id,
                               }))

    def get(self):
        breakpoint = self.get_argument("break", "finish")
        session_id = self.get_argument("session")
        self.set_header("Content-type", "application/json")

        # 400 if invalid breakpoint
        if not breakpoint in common.test_stage_order:
            self.set_status(400)
            self.finish(json.dumps({'reason':'Invalid break point',
                                    'hint':
                                        {'breakpoints':common.test_stage_order}
                                   }))
            return

        # 404 if no such session
        if not session_id in self.sessions:
            self.set_status(404)
            self.finish(json.dumps({'reason':'No session with this ID'}))
            return
        status_dict=self.sessions[session_id]

        # 500 if failed
        if status_dict['status'] == 'failed':
            self.set_status(500)
            self.finish(json.dumps(status_dict))
            return

        # 418 if in higher state or not running
        if status_dict['status'] == 'success' or common.is_A_earlier_than_B(breakpoint,status_dict['break']):
            reply={'reason':'I am a teapot! I know nothing of time-travel!',
                    'hint': {'breakpoints':common.test_stage_order} }
            reply.update(status_dict)
            self.set_status(418)
            self.finish(json.dumps(reply))
            return

        # Post run command to manager queue
        self.out_queue.put({'session':session_id,
                       'cmd':'run',
                       'break':breakpoint})

        self.set_status(200)

class StopHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions

    def get(self):
        session_id = self.get_argument("session")
        if session_id:
            if session_id in self.sessions:
                if self.sessions[session_id]['status'] not in ['success','failed']:
                    self.out_queue.put({
                        'cmd': 'stop',
                        'session': session_id,
                    })
                    self.set_status(200)
                    return
                else:
                    self.set_status(409)
                    self.finish(json.dumps(
                        {
                            'reason': 'This session is already stopped',
                            'session': session_id,
                        }
                    ))
                    return
            else:
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No session with this ID found',
                    'session': session_id,
                }))
                return
        else:
            self.set_status(400)
            self.finish(json.dumps(
                {'reason': 'Specify an ID of a session you want to stop'}
            ))
            return

class StatusHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions

    def get(self):
        print self.request.arguments

        session_id = self.get_argument("session",default=None)
        self.set_header("Content-type", "application/json")
        if session_id:
            if session_id in self.sessions:
                self.set_status(200)
                self.finish(json.dumps(
                    self.sessions[session_id]
                ))
            else:
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No session with this ID found',
                    'session': session_id,
                }))
        else:
            self.set_status(200)
            self.finish(json.dumps(
                self.sessions
            ))

class ArtifactHandler(tornado.web.RequestHandler):
    def initialize(self, out_queue, sessions, working_dir):
        self.out_queue = out_queue
        self.sessions = sessions
        self.working_dir = working_dir

    def get(self):
        test_id = self.get_argument("test")
        filename = self.get_argument("filename",None)

        # look for status.json (any test should have it)
        if not os.path.exists(os.path.join(self.working_dir, test_id, 'status.json')):
            self.set_header("Content-type", "application/json")
            self.set_status(404)
            self.finish(json.dumps({
                'reason': 'No test with this ID found',
                'test': test_id,
            }))
            return

        if filename:
            filepath = os.path.join(self.working_dir, test_id, filename)
            if os.path.exists(filepath):
                file_size = os.stat(filepath)

                if file_size > TRANSFER_SIZE_LIMIT and any(s['status'] not in ['success','failed'] for s in self.sessions.values()):
                    self.set_header("Content-type", "application/json")
                    self.set_status(503)
                    self.finish(json.dumps({
                        'reason': 'File is too large and test is running',
                        'test': test_id,
                        'filename': filename,
                        'filesize': file_size,
                        'limit': TRANSFER_SIZE_LIMIT,
                    }))
                    return
                else:
                    self.set_header("Content-type", "application/octet-stream")
                    with open(filepath, 'rb') as f:
                        data = f.read()
                        self.write(data)
                    self.finish()
                    return
            else:
                self.set_header("Content-type", "application/json")
                self.set_status(404)
                self.finish(json.dumps({
                    'reason': 'No such file',
                    'test': test_id,
                    'filename': filename,
                }))
                return
        else:
            basepath = os.path.join(self.working_dir, test_id)
            onlyfiles = [
                f for f in os.listdir(basepath)
                if os.path.isfile(os.path.join(basepath, f))
            ]
            self.set_header("Content-type", "application/json")
            self.set_status(200)
            self.finish(json.dumps(onlyfiles))

class ApiServer(object):
    def __init__(self, in_queue, out_queue, working_dir):
        self.in_queue = in_queue
        self.out_queue = out_queue
        self.working_dir = working_dir
        self.sessions = {}
        handler_params = dict(
            out_queue=self.out_queue,
            sessions=self.sessions,
            working_dir=self.working_dir,
        )
        self.app = tornado.web.Application(
            [
                (r"/run", RunHandler, handler_params),
                (r"/stop", StopHandler, handler_params),
                (r"/status", StatusHandler, handler_params),
                (r"/artifact", ArtifactHandler, handler_params),
            ],
            template_path=os.path.join(os.path.dirname(__file__), "templates"),
            static_path=os.path.join(os.path.dirname(__file__), "static"),
            debug=True,
        )

    def update_status(self):
        try:
           while True:
               message = self.in_queue.get_nowait()
               session_id=message.get('session')
               del message['session']
               #Test ID and break are always present in message from Manager
               self.sessions[session_id] = message
        except multiprocessing.queues.Empty:
            pass

    def serve(self):
        self.app.listen(8888)
        ioloop = tornado.ioloop.IOLoop.instance()
        update_cb = tornado.ioloop.PeriodicCallback(self.update_status, 100, ioloop)
        update_cb.start()
        ioloop.start()

def main(webserver_queue, manager_queue, test_directory):
    """Target for webserver process.
    The only function ever used by the Manager.

    webserver_queue
        Read statuses from Manager here.

    manager_queue
        Write commands for Manager there.

    test_directory
        Directory where tests are

    """
    ApiServer(webserver_queue, manager_queue, test_directory).serve()
<|endoftext|>


<commit_before>from collections import namedtuple
from flask import Response
from flask import request
from functools import wraps
from geventwebsocket.handler import WebSocketHandler
import inspect
import logging
from socketio import socketio_manage
from socketio.server import SocketIOHandler
from socketio.server import SocketIOServer

from model.graph import Attr_Diff, Topo_Diff
from rz_api_websocket import WebSocket_Graph_NS
from rz_kernel import RZ_Kernel
from rz_req_handling import make_response__http__empty

log = logging.getLogger('rhizi')

class RZ_WebSocket_Server(SocketIOServer):
    """
    Rhizi customized SocketIOServer:
        - allow response header injection on websocket connections
    """

    class RZ_SocketIOHandler(SocketIOHandler):

        def __init__(self, config, *args, **kwargs):
            SocketIOHandler.__init__(self, config, *args, **kwargs)

        def start_response(self, status, headers, exc_info=None):
            # headers['Access-Control-Allow-Origin'] = '*'
            return SocketIOHandler.start_response(self, status, headers, exc_info)

        def handle_disconnect_request(self):
            SocketIOHandler.handle_disconnect_request(self)

    class RZ_WebSocketHandler(WebSocketHandler):

        def __init__(self, socket, address, server, rfile=None):
            WebSocketHandler.__init__(self, socket, address, server, rfile=rfile)

        def upgrade_connection(self):
            return WebSocketHandler.upgrade_connection(self)

        def upgrade_websocket(self):
            return WebSocketHandler.upgrade_websocket(self)

    def __init__ (self, cfg, wsgi_app):
        self.wsgi_app = wsgi_app
        SocketIOServer.__init__(self,
                                (cfg.listen_address, cfg.listen_port),
                                wsgi_app,
                                resource='socket.io',  # URL prefix for socket.io requests
                                policy_server=False,
                                handler_class=RZ_WebSocket_Server.RZ_SocketIOHandler,
                                ws_handler_class=RZ_WebSocket_Server.RZ_WebSocketHandler)

    def log_multicast(self, msg_name):
        multicast_size = len(self.sockets) - 1  # subtract self socket
        log.info('ws: multicast: msg: \'%s\', cast-size ~= %d' % (msg_name, multicast_size))  # ~=: as race conditions apply

def init_ws_interface(cfg, kernel, flask_webapp):
    """
    Initialize websocket interface:
       - apply websocket route handlers

    @return: an initialized RZ_WebSocket_Server object
    """

    # init websocket-env
    ws_env = namedtuple('RZ_websocket_env', ['kernel'])
    ws_env.kernel = kernel

    def decorator__ws_multicast(ws_srv, f, f_multicast):
        """
        Emit multicast on topo_diff, attr_diff

        @param f: [!] wrapped function, name used to derive socket message name
        """

        def _prep_for_serialization(obj):
            # handle special serialization cases
            if isinstance(obj, Topo_Diff):
                return obj.to_json_dict()
            return obj

        @wraps(f)
        def wrapped_function(*args, **kw):

            f_ret = f(*args, **kw)
            assert type(f_ret) in [list, tuple]  # expect (X_Diff, X_Diff.Commit_Result_Type)

            #
            # Identify f caller
            #
            # TODO: avoid stack inspection if possible
            #
            try:
                stack = inspect.stack()
                stack_frame = stack[1][0]

                obj_instance = stack_frame.f_locals.get('self')
                if None != obj_instance:
                    # [!] no need emit broadcast if call originated from a websocket as
                    # WebSocket_Graph_NS#on_diff_commit__xxx emit their own self-excluding multicast
                    # we still asser call class == WebSocket_Graph_NS

                    caller_class = obj_instance.__class__
                    assert WebSocket_Graph_NS == caller_class, 'decorator__ws_multicast: unknown callpath: not from WebSocket_Graph_NS'

                    return f_ret

            except Exception as e:
                log.exception('decorator__ws_multicast: failed to detect REST/Websocket call via stack inspection')  # exception derived from stack

            rzdoc = rzdoc_from_f_args_extractor(args)

            pkt_data = map(_prep_for_serialization, f_ret)

            msg_name = f.__name__
            pkt = dict(type="event",
                       name=msg_name,
                       args=pkt_data,
                       endpoint='/graph')

            ws_srv.log_multicast(msg_name)

            ws_broadcast_to_rzdoc_readers(ws_srv, pkt, rzdoc)

            return f_ret

        return wrapped_function

    def rzdoc_from_f_args_extractor(f_args):  # extract rzdoc from req ctx
        if len(f_args) < 2 or not isinstance(f_args[1], Req_Context):
            return
        req_ctx = f_args[1]
        return req_ctx.rzdoc

    def socketio_route_handler(url_path):

        # FIXME: ws upgrade process for non socketio clients
        if None == request.environ.get('socketio'):
            header__upgrade = request.headers.get('Upgrade')
            if 'websocket' == header__upgrade:

                remote_addr = request.environ['REMOTE_ADDR']
                remote_port = request.environ['REMOTE_PORT']

                log.debug('ws: \'Upgrade: websocket\' header detected, serving \'101\': remote-socket-addr: %s:%s' % (remote_addr, remote_port))
                resp = make_response__http__empty(101)  # 'switching protocols' HTTP status code
                resp.headers['Upgrade'] = 'websocket'
                resp.headers['Connection'] = 'Upgrade'
                return resp
            else:
                raise Exception('ws: failed to obtain socketio object from WSGI environment')

        try:
            socketio_manage(request.environ, {'/graph': WebSocket_Graph_NS}, ws_env) # connect socketio manager
        except:
            log.exception("ws: exception while handling connection", exc_info=True)
            return make_response__json(status=HTTP_STATUS__500_INTERNAL_SERVER_ERROR)

    def ws_broadcast_to_rzdoc_readers(ws_srv, pkt, rzdoc):
        """
        Cast update messege to subscribed readers
        """

        cast_set = []
        rzdoc_r_set = kernel.rzdoc__reader_set_from_rzdoc(rzdoc)
        for r_assoc in rzdoc_r_set:
            cast_set.append(r_assoc)

        log.debug('reader diff cast: rzdoc: %s, cast size: %d' % (rzdoc.name, len(cast_set)))
        for r_assoc in cast_set:
            try:
                r_assoc.socket.send_packet(pkt)
            except Exception as e:
                r_assoc.err_count__IO += 1

    # connect socketio route
    route_dec = flask_webapp.route('/socket.io/<path:url_path>')
    f = route_dec(socketio_route_handler)
    flask_webapp.f = f

    # init ws server
    ws_srv = RZ_WebSocket_Server(cfg, flask_webapp)

    kernel.diff_commit__topo = decorator__ws_multicast(ws_srv,
                                                       kernel.diff_commit__topo,
                                                       f_multicast=WebSocket_Graph_NS.on_diff_commit__topo)
    kernel.diff_commit__attr = decorator__ws_multicast(ws_srv,
                                                       kernel.diff_commit__attr,
                                                       f_multicast=WebSocket_Graph_NS.on_diff_commit__attr)

    return ws_srv;
<commit_msg>rz_mesh: decorator__ws_multicast(): return on reflective caller detection failure<commit_after>from collections import namedtuple
from flask import Response
from flask import request
from functools import wraps
from geventwebsocket.handler import WebSocketHandler
import inspect
import logging
from socketio import socketio_manage
from socketio.server import SocketIOHandler
from socketio.server import SocketIOServer

from model.graph import Attr_Diff, Topo_Diff
from rz_api_websocket import WebSocket_Graph_NS
from rz_kernel import RZ_Kernel
from rz_req_handling import make_response__http__empty

log = logging.getLogger('rhizi')

class RZ_WebSocket_Server(SocketIOServer):
    """
    Rhizi customized SocketIOServer:
        - allow response header injection on websocket connections
    """

    class RZ_SocketIOHandler(SocketIOHandler):

        def __init__(self, config, *args, **kwargs):
            SocketIOHandler.__init__(self, config, *args, **kwargs)

        def start_response(self, status, headers, exc_info=None):
            # headers['Access-Control-Allow-Origin'] = '*'
            return SocketIOHandler.start_response(self, status, headers, exc_info)

        def handle_disconnect_request(self):
            SocketIOHandler.handle_disconnect_request(self)

    class RZ_WebSocketHandler(WebSocketHandler):

        def __init__(self, socket, address, server, rfile=None):
            WebSocketHandler.__init__(self, socket, address, server, rfile=rfile)

        def upgrade_connection(self):
            return WebSocketHandler.upgrade_connection(self)

        def upgrade_websocket(self):
            return WebSocketHandler.upgrade_websocket(self)

    def __init__ (self, cfg, wsgi_app):
        self.wsgi_app = wsgi_app
        SocketIOServer.__init__(self,
                                (cfg.listen_address, cfg.listen_port),
                                wsgi_app,
                                resource='socket.io',  # URL prefix for socket.io requests
                                policy_server=False,
                                handler_class=RZ_WebSocket_Server.RZ_SocketIOHandler,
                                ws_handler_class=RZ_WebSocket_Server.RZ_WebSocketHandler)

    def log_multicast(self, msg_name):
        multicast_size = len(self.sockets) - 1  # subtract self socket
        log.info('ws: multicast: msg: \'%s\', cast-size ~= %d' % (msg_name, multicast_size))  # ~=: as race conditions apply

def init_ws_interface(cfg, kernel, flask_webapp):
    """
    Initialize websocket interface:
       - apply websocket route handlers

    @return: an initialized RZ_WebSocket_Server object
    """

    # init websocket-env
    ws_env = namedtuple('RZ_websocket_env', ['kernel'])
    ws_env.kernel = kernel

    def decorator__ws_multicast(ws_srv, f, f_multicast):
        """
        Emit multicast on topo_diff, attr_diff

        @param f: [!] wrapped function, name used to derive socket message name
        """

        def _prep_for_serialization(obj):
            # handle special serialization cases
            if isinstance(obj, Topo_Diff):
                return obj.to_json_dict()
            return obj

        @wraps(f)
        def wrapped_function(*args, **kw):

            f_ret = f(*args, **kw)
            assert type(f_ret) in [list, tuple]  # expect (X_Diff, X_Diff.Commit_Result_Type)

            #
            # Identify f caller
            #
            # TODO: avoid stack inspection if possible
            #
            try:
                stack = inspect.stack()
                stack_frame = stack[1][0]

                obj_instance = stack_frame.f_locals.get('self')
                if None != obj_instance:
                    # [!] no need emit broadcast if call originated from a websocket as
                    # WebSocket_Graph_NS#on_diff_commit__xxx emit their own self-excluding multicast
                    # we still asser call class == WebSocket_Graph_NS

                    caller_class = obj_instance.__class__
                    assert WebSocket_Graph_NS == caller_class, 'decorator__ws_multicast: unknown callpath: not from WebSocket_Graph_NS'

                    return f_ret

            except Exception as e:
                log.exception('decorator__ws_multicast: failed to detect REST/Websocket call via stack inspection')  # exception derived from stack
                return  # f caller unidentified, abort cast

            rzdoc = rzdoc_from_f_args_extractor(args)

            pkt_data = map(_prep_for_serialization, f_ret)

            msg_name = f.__name__
            pkt = dict(type="event",
                       name=msg_name,
                       args=pkt_data,
                       endpoint='/graph')

            ws_srv.log_multicast(msg_name)

            ws_broadcast_to_rzdoc_readers(ws_srv, pkt, rzdoc)

            return f_ret

        return wrapped_function

    def rzdoc_from_f_args_extractor(f_args):  # extract rzdoc from req ctx
        if len(f_args) < 2 or not isinstance(f_args[1], Req_Context):
            return
        req_ctx = f_args[1]
        return req_ctx.rzdoc

    def socketio_route_handler(url_path):

        # FIXME: ws upgrade process for non socketio clients
        if None == request.environ.get('socketio'):
            header__upgrade = request.headers.get('Upgrade')
            if 'websocket' == header__upgrade:

                remote_addr = request.environ['REMOTE_ADDR']
                remote_port = request.environ['REMOTE_PORT']

                log.debug('ws: \'Upgrade: websocket\' header detected, serving \'101\': remote-socket-addr: %s:%s' % (remote_addr, remote_port))
                resp = make_response__http__empty(101)  # 'switching protocols' HTTP status code
                resp.headers['Upgrade'] = 'websocket'
                resp.headers['Connection'] = 'Upgrade'
                return resp
            else:
                raise Exception('ws: failed to obtain socketio object from WSGI environment')

        try:
            socketio_manage(request.environ, {'/graph': WebSocket_Graph_NS}, ws_env) # connect socketio manager
        except:
            log.exception("ws: exception while handling connection", exc_info=True)
            return make_response__json(status=HTTP_STATUS__500_INTERNAL_SERVER_ERROR)

    def ws_broadcast_to_rzdoc_readers(ws_srv, pkt, rzdoc):
        """
        Cast update messege to subscribed readers
        """

        cast_set = []
        rzdoc_r_set = kernel.rzdoc__reader_set_from_rzdoc(rzdoc)
        for r_assoc in rzdoc_r_set:
            cast_set.append(r_assoc)

        log.debug('reader diff cast: rzdoc: %s, cast size: %d' % (rzdoc.name, len(cast_set)))
        for r_assoc in cast_set:
            try:
                r_assoc.socket.send_packet(pkt)
            except Exception as e:
                r_assoc.err_count__IO += 1

    # connect socketio route
    route_dec = flask_webapp.route('/socket.io/<path:url_path>')
    f = route_dec(socketio_route_handler)
    flask_webapp.f = f

    # init ws server
    ws_srv = RZ_WebSocket_Server(cfg, flask_webapp)

    kernel.diff_commit__topo = decorator__ws_multicast(ws_srv,
                                                       kernel.diff_commit__topo,
                                                       f_multicast=WebSocket_Graph_NS.on_diff_commit__topo)
    kernel.diff_commit__attr = decorator__ws_multicast(ws_srv,
                                                       kernel.diff_commit__attr,
                                                       f_multicast=WebSocket_Graph_NS.on_diff_commit__attr)

    return ws_srv;
<|endoftext|>


<commit_before># yaranullin/network/server.py
#
# Copyright (c) 2012 Marco Scopesi <marco.scopesi@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import socket
import asyncore
import logging

LOGGER = logging.getLogger(__name__)

from yaranullin.event_system import connect
from yaranullin.network.base import EndPoint

class ServerEndPoint(EndPoint):

    """End point wrapper for the server"""

    def __init__(self, sock):
        EndPoint.__init__(self, sock)
        self._connect_handlers()

    def _connect_handlers(self):
        connect('game-event-update', self.post)
        connect('game-event-pawn-next', self.post)
        connect('game-event-pawn-updated', self.post)
        connect('game-event-board-change', self.post)
        connect('resource-update', self.post)

class Server(asyncore.dispatcher):

    """Handle server and create end points"""

    def __init__(self, server_address):
        asyncore.dispatcher.__init__(self)
        # XXX Remember IPv6
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(server_address)
        LOGGER.debug('Server listening on port %d', server_address[1])
        self.listen(5)

    def log_info(self, message, type='info'):
        try:
            log = getattr(LOGGER, type)
        except AttributeError:
            pass
        else:
            log(message)

    def handle_accept(self):
        client_info = self.accept()
        if client_info is None:
            return
        LOGGER.debug('Accept connection from %s', client_info[1])
        ServerEndPoint(sock=client_info[0])
<commit_msg>yaranullin/network/server.py: purge _connect_handlers()<commit_after># yaranullin/network/server.py
#
# Copyright (c) 2012 Marco Scopesi <marco.scopesi@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

import socket
import asyncore
import logging

LOGGER = logging.getLogger(__name__)

from yaranullin.event_system import connect
from yaranullin.network.base import EndPoint

class ServerEndPoint(EndPoint):

    """End point wrapper for the server"""

    def __init__(self, sock):
        EndPoint.__init__(self, sock)
        connect('game-event-update', self.post)
        connect('game-event-pawn-next', self.post)
        connect('game-event-pawn-updated', self.post)
        connect('game-event-board-change', self.post)
        connect('resource-update', self.post)

class Server(asyncore.dispatcher):

    """Handle server and create end points"""

    def __init__(self, server_address):
        asyncore.dispatcher.__init__(self)
        # XXX Remember IPv6
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(server_address)
        LOGGER.debug('Server listening on port %d', server_address[1])
        self.listen(5)

    def log_info(self, message, type='info'):
        try:
            log = getattr(LOGGER, type)
        except AttributeError:
            pass
        else:
            log(message)

    def handle_accept(self):
        client_info = self.accept()
        if client_info is None:
            return
        LOGGER.debug('Accept connection from %s', client_info[1])
        ServerEndPoint(sock=client_info[0])
<|endoftext|>


<commit_before>import abc
from nalaf.structures.data import Edge
from itertools import product, chain

class EdgeGenerator(object):
    """
    Abstract class for generating edges between two entities. Each edge represents
    a possible relationship between the two entities
    Subclasses that inherit this class should:
    * Be named [Name]EdgeGenerator
    * Implement the abstract method generate
    * Append new items to the list field "edges" of each Part in the dataset
    """

    @abc.abstractmethod
    def generate(self, dataset):
        """
        :type dataset: nalaf.structures.data.Dataset
        """
        return

class SentenceDistanceEdgeGenerator(EdgeGenerator):
    """
    Simple implementation of generating edges between the two entities
    if they are #`distance` sentences away (always same part).

    As a special case, if `distance` is None, the distance is disregarded,
    consequently creating edges for all entities within a part (paragraph or what not).
    """

    def __init__(self, entity1_class, entity2_class, relation_type, distance, use_predicted_entities=True):
        # Note: would be nice to implement the word filter too here -- see below

        self.entity1_class = entity1_class
        self.entity2_class = entity2_class
        self.relation_type = relation_type
        self.distance = distance
        self.use_predicted_entities = use_predicted_entities

        self.part_entities = (lambda part: chain(part.annotations, part.predicted_annotations) if self.use_predicted_entities else part.annotations)

    def generate(self, dataset):

        for part in dataset.parts():
            part.edges = []

            for e_1, e_2 in product(
                    (e for e in self.part_entities(part) if e.class_id == self.entity1_class),
                    (e for e in self.part_entities(part) if e.class_id == self.entity2_class)):

                s1_index = part.get_sentence_index_for_annotation(e_1)
                s2_index = part.get_sentence_index_for_annotation(e_2)

                if s2_index < s1_index:
                    s1_index, s2_index = s2_index, s1_index

                if e_2.offset < e_1.offset:
                    e_1, e_2 = e_2, e_1

                pair_distance = s2_index - s1_index
                assert pair_distance >= 0  # Because they must be sorted

                if pair_distance == self.distance or self.distance is None:
                    edge = Edge(self.relation_type, e_1, e_2, part, part, s1_index, s2_index)
                    part.edges.append(edge)

class WordFilterEdgeGenerator(EdgeGenerator):
    """
    Simple implementation of generating edges between the two entities
    if they are contained in the same sentence AND the sentence
    contains one of the trigger-like given words

    **It only uses the _gold_ annotations**

    """

    def __init__(self, entity1_class, entity2_class, relation_type, words):
        self.entity1_class = entity1_class
        self.entity2_class = entity2_class
        self.relation_type = relation_type
        self.words = words

    def generate(self, dataset):
        from itertools import product

        for part in dataset.parts():
            part.edges = []

            for ann_1, ann_2 in product(
                    (ann for ann in part.annotations if ann.class_id == self.entity1_class),
                    (ann for ann in part.annotations if ann.class_id == self.entity2_class)):

                index_1 = part.get_sentence_index_for_annotation(ann_1)
                index_2 = part.get_sentence_index_for_annotation(ann_2)

                if index_1 == index_2 and index_1 is not None:

                    for token in part.sentences[index_1]:

                        if token.word in self.words:
                            edge = Edge(self.relation_type, ann_1, ann_2, part, part, index_1, index_2)

                            part.edges.append(edge)

                            break
<commit_msg>minor<commit_after>import abc
from nalaf.structures.data import Edge
from itertools import product, chain

class EdgeGenerator(object):
    """
    Abstract class for generating edges between two entities. Each edge represents
    a possible relationship between the two entities
    Subclasses that inherit this class should:
    * Be named [Name]EdgeGenerator
    * Implement the abstract method generate
    * Append new items to the list field "edges" of each Part in the dataset
    """

    @abc.abstractmethod
    def generate(self, dataset):
        """
        :type dataset: nalaf.structures.data.Dataset
        """
        return

class SentenceDistanceEdgeGenerator(EdgeGenerator):
    """
    Simple implementation of generating edges between the two entities
    if they are #`distance` sentences away (always same part).

    As a special case, if `distance` is None, the distance is disregarded,
    consequently creating edges for all entities within a part (paragraph or what not).
    """

    def __init__(self, entity1_class, entity2_class, relation_type, distance, use_predicted_entities=True):
        # Note: would be nice to implement the word filter too here -- see below

        self.entity1_class = entity1_class
        self.entity2_class = entity2_class
        self.relation_type = relation_type
        self.distance = distance
        self.use_predicted_entities = use_predicted_entities

        self.part_entities = (lambda part: chain(part.annotations, part.predicted_annotations) if self.use_predicted_entities else part.annotations)

    def generate(self, dataset):

        for part in dataset.parts():
            part.edges = []

            for e_1, e_2 in product(
                    (e for e in self.part_entities(part) if e.class_id == self.entity1_class),
                    (e for e in self.part_entities(part) if e.class_id == self.entity2_class)):

                s1_index = part.get_sentence_index_for_annotation(e_1)
                s2_index = part.get_sentence_index_for_annotation(e_2)

                if s2_index < s1_index:
                    s1_index, s2_index = s2_index, s1_index

                if e_2.offset < e_1.offset:
                    e_1, e_2 = e_2, e_1

                pair_distance = s2_index - s1_index
                assert pair_distance >= 0  # Because they must be sorted

                if pair_distance == self.distance or self.distance is None:
                    edge = Edge(self.relation_type, e_1, e_2, part, part, s1_index, s2_index)
                    part.edges.append(edge)

class WordFilterEdgeGenerator(EdgeGenerator):
    """
    Simple implementation of generating edges between the two entities
    if they are contained in the same sentence AND the sentence
    contains one of the trigger-like given words

    **It only uses the _gold_ annotations**

    """

    def __init__(self, entity1_class, entity2_class, relation_type, words):
        self.entity1_class = entity1_class
        self.entity2_class = entity2_class
        self.relation_type = relation_type
        self.words = words

    def generate(self, dataset):
        from itertools import product

        for part in dataset.parts():
            part.edges = []

            for ann_1, ann_2 in product(
                    (ann for ann in part.annotations if ann.class_id == self.entity1_class),
                    (ann for ann in part.annotations if ann.class_id == self.entity2_class)):

                index_1 = part.get_sentence_index_for_annotation(ann_1)
                index_2 = part.get_sentence_index_for_annotation(ann_2)

                if index_1 == index_2 and index_1 is not None:

                    for token in part.sentences[index_1]:

                        if token.word in self.words:
                            edge = Edge(self.relation_type, ann_1, ann_2, part, part, index_1, index_2)

                            part.edges.append(edge)

                            break
<|endoftext|>


<commit_before>"""Base objects to be exported for use in Controllers"""
from paste.registry import StackedObjectProxy

from pylons.config import config
from pylons.legacy import h, jsonify, Controller, Response

__all__ = ['c', 'g', 'cache', 'request', 'response', 'session', 'jsonify',
           'Controller', 'Response']

c = StackedObjectProxy(name="C")
g = StackedObjectProxy(name="G")

cache = StackedObjectProxy(name="Cache")
request = StackedObjectProxy(name="Request")
response = StackedObjectProxy(name="Response")
session = StackedObjectProxy(name="Session")

buffet = StackedObjectProxy(name="Buffet")
translator = StackedObjectProxy(name="Translator")
<commit_msg>[svn] Added version as pylons.__version__ for scripts that need revision information.<commit_after>"""Base objects to be exported for use in Controllers"""
from paste.registry import StackedObjectProxy

from pylons.config import config
from pylons.legacy import h, jsonify, Controller, Response

__all__ = ['c', 'g', 'cache', 'request', 'response', 'session', 'jsonify',
           'Controller', 'Response']

def __figure_version():
    try:
        from pkg_resources import require
        import os
        # NOTE: this only works when the package is either installed,
        # or has an .egg-info directory present (i.e. wont work with raw SVN checkout)
        info = require('pylons')[0]
        if os.path.dirname(os.path.dirname(__file__)) == info.location:
            return info.version
        else:
            return '(not installed)'
    except:
        return '(not installed)'

__version__ = __figure_version()

c = StackedObjectProxy(name="C")
g = StackedObjectProxy(name="G")

cache = StackedObjectProxy(name="Cache")
request = StackedObjectProxy(name="Request")
response = StackedObjectProxy(name="Response")
session = StackedObjectProxy(name="Session")

buffet = StackedObjectProxy(name="Buffet")
translator = StackedObjectProxy(name="Translator")
<|endoftext|>


<commit_before>"""Paster Commands, for use with paster in your project

The command(s) listed here are for use with Paste to enable easy creation of
various core Pylons templates.

Currently available commands are::

    controller, restcontroller, shell
"""
import os
import sys

import paste.fixture
import paste.registry
import paste.deploy.config
from paste.deploy import loadapp, appconfig
from paste.script.command import Command, BadCommand
from paste.script.filemaker import FileOp
from paste.script.pluginlib import find_egg_info_dir

import pylons.util as util

def can_import(name):
    """Attempt to __import__ the specified package/module, returning True when
    succeeding, otherwise False"""
    try:
        __import__(name)
        return True
    except ImportError:
        return False

def validate_name(name):
    """Validate that the name for the controller isn't present on the
    path already"""
    if not name:
        # This happens when the name is an existing directory
        raise BadCommand('Please give the name of a controller.')
    # 'setup' is a valid controller name, but when paster controller is ran
    # from the root directory of a project, importing setup will import the
    # project's setup.py causing a sys.exit(). Blame relative imports
    if name != 'setup' and can_import(name):
        raise BadCommand(
            "\n\nA module named '%s' is already present in your "
            "PYTHON_PATH.\nChoosing a conflicting name will likely cause "
            "import problems in\nyour controller at some point. It's "
            "suggested that you choose an\nalternate name, and if you'd "
            "like that name to be accessible as\n'%s', add a route "
            "to your projects config/routing.py file similar\nto:\n"
            "    map.connect('%s', controller='my_%s')" \
            % (name, name, name, name))
    return True

class ControllerCommand(Command):
    """Create a Controller and accompanying functional test

    The Controller command will create the standard controller template
    file and associated functional test to speed creation of controllers.

    Example usage::

        yourproj% paster controller comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster controller admin/trackback
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackback.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackback.py
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 1
    max_args = 1
    group_name = 'pylons'

    default_verbosity = 3

    parser = Command.standard_parser(simulate=True)
    parser.add_option('--no-test',
                      action='store_true',
                      dest='no_test',
                      help="Don't create the test; just the controller")

    def command(self):
        """Main command to create controller"""
        try:
            file_op = FileOp(source_dir=os.path.join(
                os.path.dirname(__file__), 'templates'))
            try:
                name, directory = file_op.parse_path_name_args(self.args[0])
            except:
                raise BadCommand('No egg_info directory was found')

            # Check the name isn't the same as the package
            base_package = file_op.find_dir('controllers', True)[0]
            if base_package.lower() == name.lower():
                raise BadCommand(
                    'Your controller name should not be the same as '
                    'the package name %r.'% base_package
            )
            # Validate the name
            name = name.replace('-', '_')
            validate_name(name)

            # Check to see if PROJ.lib.base exists
            try:
                __import__(base_package + '.lib.base')
                importstatement = "from %s.lib.base import *" % base_package
            except ImportError:
                # Assume its the minimal template
                importstatement = "from %s.controllers import *" % base_package
            except:
                # lib.base exists but throws an error
                importstatement = "from %s.lib.base import *" % base_package

            # Setup the controller
            fullname = os.path.join(directory, name)
            controller_name = util.class_name_from_module_name(
                name.split('/')[-1])
            if not fullname.startswith(os.sep):
                fullname = os.sep + fullname
            testname = fullname.replace(os.sep, '_')[1:]
            file_op.template_vars.update({'name': controller_name,
                                          'fname': os.path.join(directory, name),
                                          'importstatement': importstatement})
            file_op.copy_file(template='controller.py_tmpl',
                         dest=os.path.join('controllers', directory),
                         filename=name)
            if not self.options.no_test:
                file_op.copy_file(template='test_controller.py_tmpl',
                             dest=os.path.join('tests', 'functional'),
                             filename='test_'+testname)
        except BadCommand, e:
            raise BadCommand('An error occurred. %s' % e)
        except:
            msg = str(sys.exc_info()[1])
            raise BadCommand('An unknown error occurred. %s' % msg)

class RestControllerCommand(Command):
    """Create a REST Controller and accompanying functional test

    The RestController command will create a REST-based Controller file for use
    with the map.resource REST-based dispatching. This template includes the
    methods that map.resource dispatches to in addition to doc strings for
    clarification on when the methods will be called.

    The first argument should be the singular form of the REST resource. The
    second argument is the plural form of the word. If its a nested controller,
    put the directory information in front as shown in the second example
    below.

    Example usage::

        yourproj% paster restcontroller comment comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster restcontroller admin/tracback admin/trackbacks
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackbacks.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackbacks.py
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 2
    max_args = 2
    group_name = 'pylons'

    default_verbosity = 3

    parser = Command.standard_parser(simulate=True)
    parser.add_option('--no-test',
                      action='store_true',
                      dest='no_test',
                      help="Don't create the test; just the controller")

    def command(self):
        """Main command to create controller"""
        try:
            file_op = FileOp(source_dir=os.path.join(
                os.path.dirname(__file__), 'templates'))
            try:
                singularname, singulardirectory = \
                    file_op.parse_path_name_args(self.args[0])
                pluralname, pluraldirectory = \
                    file_op.parse_path_name_args(self.args[1])
            except:
                raise BadCommand('No egg_info directory was found')

            # Check the name isn't the same as the package
            base_package = file_op.find_dir('controllers', True)[0]
            if base_package.lower() == pluralname.lower():
                raise BadCommand(
                    'Your controller name should not be the same as '
                    'the package name %r.'% base_package
            )
            # Validate the name
            for name in [singularname, pluralname]:
                name = name.replace('-', '_')
                validate_name(name)

            # Setup the controller
            fullname = os.path.join(pluraldirectory, pluralname)
            controller_name = util.class_name_from_module_name(
                pluralname.split('/')[-1])
            if not fullname.startswith(os.sep):
                fullname = os.sep + fullname
            testname = fullname.replace(os.sep, '_')[1:]

            nameprefix = ''
            if pluraldirectory:
                nameprefix = pluraldirectory.replace(os.path.sep, '_') + '_'

            controller_c = ''
            if nameprefix:
                controller_c = ", controller='%s', \n\t" % \
                    '/'.join([pluraldirectory, pluralname])
                controller_c += "path_prefix='/%s', name_prefix='%s_'" % \
                    (pluraldirectory, pluraldirectory)
            command = "map.resource('%s', '%s'%s)\n" % \
                (singularname, pluralname, controller_c)

            file_op.template_vars.update(
                {'classname': controller_name,
                 'pluralname': pluralname,
                 'singularname': singularname,
                 'name': controller_name,
                 'nameprefix': nameprefix,
                 'resource_command': command.replace('\n\t', '\n%s#%s' % \
                                                         (' '*4, ' '*9)),
                 'fname': os.path.join(pluraldirectory, pluralname)}
            )

            resource_command = ("\nTo create the appropriate RESTful mapping, "
                                "add a map statement to your\n")
            resource_command += ("config/routing.py file near the top like "
                                 "this:\n\n")
            resource_command += command
            file_op.copy_file(template='restcontroller.py_tmpl',
                         dest=os.path.join('controllers', pluraldirectory),
                         filename=pluralname)
            if not self.options.no_test:
                file_op.copy_file(template='test_controller.py_tmpl',
                             dest=os.path.join('tests', 'functional'),
                             filename='test_'+testname)
            print resource_command
        except BadCommand, e:
            raise BadCommand('An error occurred. %s' % e)
        except:
            msg = str(sys.exc_info()[1])
            raise BadCommand('An unknown error occurred. %s' % msg)

class ShellCommand(Command):
    """Open an interactive shell with the Pylons app loaded

    The optional CONFIG_FILE argument specifies the config file to use for
    the interactive shell. CONFIG_FILE defaults to 'development.ini'.

    This allows you to test your mapper, models, and simulate web requests
    using ``paste.fixture``.

    Example::

        $ paster shell my-development.ini
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 0
    max_args = 1
    group_name = 'pylons'

    parser = Command.standard_parser(simulate=True)
    parser.add_option('-d', '--disable-ipython',
                      action='store_true',
                      dest='disable_ipython',
                      help="Don't use IPython if it is available")

    def command(self):
        """Main command to create a new shell"""
        self.verbose = 3
        if len(self.args) == 0:
            # Assume the .ini file is ./development.ini
            config_file = 'development.ini'
            if not os.path.isfile(config_file):
                raise BadCommand('%sError: CONFIG_FILE not found at: .%s%s\n'
                                 'Please specify a CONFIG_FILE' % \
                                 (self.parser.get_usage(), os.path.sep,
                                  config_file))
        else:
            config_file = self.args[0]

        config_name = 'config:%s' % config_file
        here_dir = os.getcwd()
        locs = dict(__name__="pylons-admin")

        # Load app config into paste.deploy to simulate request config
        conf = appconfig(config_name, relative_to=here_dir)
        conf = dict(app_conf=conf.local_conf,
                    global_conf=conf.global_conf)
        paste.deploy.config.CONFIG.push_thread_config(conf)

        # Load locals and populate with objects for use in shell
        sys.path.insert(0, here_dir)

        # Load the wsgi app first so that everything is initialized right
        wsgiapp = loadapp(config_name, relative_to=here_dir)
        test_app = paste.fixture.TestApp(wsgiapp)

        # Query the test app to setup the environment
        tresponse = test_app.get('/_test_vars')
        request_id = int(tresponse.body)

        # Disable restoration during test_app requests
        test_app.pre_request_hook = lambda self: \
            paste.registry.restorer.restoration_end()
        test_app.post_request_hook = lambda self: \
            paste.registry.restorer.restoration_begin(request_id)

        # Restore the state of the Pylons special objects
        # (StackedObjectProxies)
        paste.registry.restorer.restoration_begin(request_id)

        # Determine the package name from the .egg-info top_level.txt.
        egg_info = find_egg_info_dir(here_dir)
        f = open(os.path.join(egg_info, 'top_level.txt'))
        packages = [l.strip() for l in f.readlines()
                    if l.strip() and not l.strip().startswith('#')]
        f.close()

        # Start the rest of our imports now that the app is loaded
        found_base = False
        for pkg_name in packages:
            # Import all objects from the base module
            base_module = pkg_name + '.lib.base'
            found_base = can_import(base_module)
            if not found_base:
                # Minimal template
                base_module = pkg_name + '.controllers'
                found_base = can_import(base_module)

            if found_base:
                models_package = pkg_name + '.models'
                # The Minimal template lacks an official models package
                has_models = can_import(models_package)
                break

        if not found_base:
            raise ImportError("Could not import base module. Are you sure this "
                              "is a Pylons app?")

        base = sys.modules[base_module]
        base_public = [__name for __name in dir(base) if not \
                       __name.startswith('_') or __name == '_']
        for name in base_public:
            locs[name] = getattr(base, name)
        locs.update(
            dict(
                mapper=tresponse.pylons_config.map,
                wsgiapp=wsgiapp,
                app=test_app,
            )
        )
        if has_models:
            locs['model'] = sys.modules[models_package]

        banner = "  All objects from %s are available\n" % base_module
        banner += "  Additional Objects:\n"
        banner += "  %-10s -  %s\n" % ('mapper', 'Routes mapper object')
        if has_models:
            banner += "  %-10s -  %s\n" % ('model',
                                           'Models from models package')
        banner += "  %-10s -  %s\n" % ('wsgiapp',
            'This projects WSGI App instance')
        banner += "  %-10s -  %s\n" % ('app',
            'paste.fixture wrapped around wsgiapp')

        try:
            if self.options.disable_ipython:
                raise ImportError()

            # try to use IPython if possible
            from IPython.Shell import IPShellEmbed

            shell = IPShellEmbed(user_ns=locs)
            shell.set_banner(shell.IP.BANNER + '\n\n' + banner)
            try:
                shell()
            finally:
                paste.registry.restorer.restoration_end()
        except ImportError:
            import code

            class CustomShell(code.InteractiveConsole):
                """Custom shell class to handle raw input"""
                def raw_input(self, *args, **kw):
                    """Capture raw input in exception wrapping"""
                    try:
                        return code.InteractiveConsole.raw_input(
                            self, *args, **kw)
                    except EOFError:
                        # In the future, we'll put our own override as needed
                        # to save models, TG style
                        raise EOFError

            newbanner = "Pylons Interactive Shell\nPython %s\n\n" % sys.version
            banner = newbanner + banner
            shell = CustomShell(locals=locs)
            try:
                import readline
            except ImportError:
                pass
            try:
                shell.interact(banner)
            finally:
                paste.registry.restorer.restoration_end()

__all__ = ['ControllerCommand', 'RestControllerCommand', 'ShellCommand']
<commit_msg>[svn] an ImportError raised during the lib.base import might be nested: only assume there's no lib.base when the ImportError contains "No module named lib.base"<commit_after>"""Paster Commands, for use with paster in your project

The command(s) listed here are for use with Paste to enable easy creation of
various core Pylons templates.

Currently available commands are::

    controller, restcontroller, shell
"""
import os
import sys

import paste.fixture
import paste.registry
import paste.deploy.config
from paste.deploy import loadapp, appconfig
from paste.script.command import Command, BadCommand
from paste.script.filemaker import FileOp
from paste.script.pluginlib import find_egg_info_dir

import pylons.util as util

def can_import(name):
    """Attempt to __import__ the specified package/module, returning True when
    succeeding, otherwise False"""
    try:
        __import__(name)
        return True
    except ImportError:
        return False

def validate_name(name):
    """Validate that the name for the controller isn't present on the
    path already"""
    if not name:
        # This happens when the name is an existing directory
        raise BadCommand('Please give the name of a controller.')
    # 'setup' is a valid controller name, but when paster controller is ran
    # from the root directory of a project, importing setup will import the
    # project's setup.py causing a sys.exit(). Blame relative imports
    if name != 'setup' and can_import(name):
        raise BadCommand(
            "\n\nA module named '%s' is already present in your "
            "PYTHON_PATH.\nChoosing a conflicting name will likely cause "
            "import problems in\nyour controller at some point. It's "
            "suggested that you choose an\nalternate name, and if you'd "
            "like that name to be accessible as\n'%s', add a route "
            "to your projects config/routing.py file similar\nto:\n"
            "    map.connect('%s', controller='my_%s')" \
            % (name, name, name, name))
    return True

class ControllerCommand(Command):
    """Create a Controller and accompanying functional test

    The Controller command will create the standard controller template
    file and associated functional test to speed creation of controllers.

    Example usage::

        yourproj% paster controller comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster controller admin/trackback
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackback.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackback.py
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 1
    max_args = 1
    group_name = 'pylons'

    default_verbosity = 3

    parser = Command.standard_parser(simulate=True)
    parser.add_option('--no-test',
                      action='store_true',
                      dest='no_test',
                      help="Don't create the test; just the controller")

    def command(self):
        """Main command to create controller"""
        try:
            file_op = FileOp(source_dir=os.path.join(
                os.path.dirname(__file__), 'templates'))
            try:
                name, directory = file_op.parse_path_name_args(self.args[0])
            except:
                raise BadCommand('No egg_info directory was found')

            # Check the name isn't the same as the package
            base_package = file_op.find_dir('controllers', True)[0]
            if base_package.lower() == name.lower():
                raise BadCommand(
                    'Your controller name should not be the same as '
                    'the package name %r.'% base_package
            )
            # Validate the name
            name = name.replace('-', '_')
            validate_name(name)

            # Check to see if PROJ.lib.base exists
            minimal_template = False
            try:
                __import__(base_package + '.lib.base')
            except ImportError, ie:
                if 'No module named lib.base' in str(ie):
                    minimal_template = True
            except:
                # lib.base exists but throws an error
                pass

            if minimal_template:
                importstatement = "from %s.controllers import *" % base_package
            else:
                importstatement = "from %s.lib.base import *" % base_package

            # Setup the controller
            fullname = os.path.join(directory, name)
            controller_name = util.class_name_from_module_name(
                name.split('/')[-1])
            if not fullname.startswith(os.sep):
                fullname = os.sep + fullname
            testname = fullname.replace(os.sep, '_')[1:]
            file_op.template_vars.update({'name': controller_name,
                                          'fname': os.path.join(directory, name),
                                          'importstatement': importstatement})
            file_op.copy_file(template='controller.py_tmpl',
                         dest=os.path.join('controllers', directory),
                         filename=name)
            if not self.options.no_test:
                file_op.copy_file(template='test_controller.py_tmpl',
                             dest=os.path.join('tests', 'functional'),
                             filename='test_'+testname)
        except BadCommand, e:
            raise BadCommand('An error occurred. %s' % e)
        except:
            msg = str(sys.exc_info()[1])
            raise BadCommand('An unknown error occurred. %s' % msg)

class RestControllerCommand(Command):
    """Create a REST Controller and accompanying functional test

    The RestController command will create a REST-based Controller file for use
    with the map.resource REST-based dispatching. This template includes the
    methods that map.resource dispatches to in addition to doc strings for
    clarification on when the methods will be called.

    The first argument should be the singular form of the REST resource. The
    second argument is the plural form of the word. If its a nested controller,
    put the directory information in front as shown in the second example
    below.

    Example usage::

        yourproj% paster restcontroller comment comments
        Creating yourproj/yourproj/controllers/comments.py
        Creating yourproj/yourproj/tests/functional/test_comments.py

    If you'd like to have controllers underneath a directory, just include
    the path as the controller name and the necessary directories will be
    created for you::

        yourproj% paster restcontroller admin/tracback admin/trackbacks
        Creating yourproj/controllers/admin
        Creating yourproj/yourproj/controllers/admin/trackbacks.py
        Creating yourproj/yourproj/tests/functional/test_admin_trackbacks.py
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 2
    max_args = 2
    group_name = 'pylons'

    default_verbosity = 3

    parser = Command.standard_parser(simulate=True)
    parser.add_option('--no-test',
                      action='store_true',
                      dest='no_test',
                      help="Don't create the test; just the controller")

    def command(self):
        """Main command to create controller"""
        try:
            file_op = FileOp(source_dir=os.path.join(
                os.path.dirname(__file__), 'templates'))
            try:
                singularname, singulardirectory = \
                    file_op.parse_path_name_args(self.args[0])
                pluralname, pluraldirectory = \
                    file_op.parse_path_name_args(self.args[1])
            except:
                raise BadCommand('No egg_info directory was found')

            # Check the name isn't the same as the package
            base_package = file_op.find_dir('controllers', True)[0]
            if base_package.lower() == pluralname.lower():
                raise BadCommand(
                    'Your controller name should not be the same as '
                    'the package name %r.'% base_package
            )
            # Validate the name
            for name in [singularname, pluralname]:
                name = name.replace('-', '_')
                validate_name(name)

            # Setup the controller
            fullname = os.path.join(pluraldirectory, pluralname)
            controller_name = util.class_name_from_module_name(
                pluralname.split('/')[-1])
            if not fullname.startswith(os.sep):
                fullname = os.sep + fullname
            testname = fullname.replace(os.sep, '_')[1:]

            nameprefix = ''
            if pluraldirectory:
                nameprefix = pluraldirectory.replace(os.path.sep, '_') + '_'

            controller_c = ''
            if nameprefix:
                controller_c = ", controller='%s', \n\t" % \
                    '/'.join([pluraldirectory, pluralname])
                controller_c += "path_prefix='/%s', name_prefix='%s_'" % \
                    (pluraldirectory, pluraldirectory)
            command = "map.resource('%s', '%s'%s)\n" % \
                (singularname, pluralname, controller_c)

            file_op.template_vars.update(
                {'classname': controller_name,
                 'pluralname': pluralname,
                 'singularname': singularname,
                 'name': controller_name,
                 'nameprefix': nameprefix,
                 'resource_command': command.replace('\n\t', '\n%s#%s' % \
                                                         (' '*4, ' '*9)),
                 'fname': os.path.join(pluraldirectory, pluralname)}
            )

            resource_command = ("\nTo create the appropriate RESTful mapping, "
                                "add a map statement to your\n")
            resource_command += ("config/routing.py file near the top like "
                                 "this:\n\n")
            resource_command += command
            file_op.copy_file(template='restcontroller.py_tmpl',
                         dest=os.path.join('controllers', pluraldirectory),
                         filename=pluralname)
            if not self.options.no_test:
                file_op.copy_file(template='test_controller.py_tmpl',
                             dest=os.path.join('tests', 'functional'),
                             filename='test_'+testname)
            print resource_command
        except BadCommand, e:
            raise BadCommand('An error occurred. %s' % e)
        except:
            msg = str(sys.exc_info()[1])
            raise BadCommand('An unknown error occurred. %s' % msg)

class ShellCommand(Command):
    """Open an interactive shell with the Pylons app loaded

    The optional CONFIG_FILE argument specifies the config file to use for
    the interactive shell. CONFIG_FILE defaults to 'development.ini'.

    This allows you to test your mapper, models, and simulate web requests
    using ``paste.fixture``.

    Example::

        $ paster shell my-development.ini
    """
    summary = __doc__.splitlines()[0]
    usage = '\n' + __doc__

    min_args = 0
    max_args = 1
    group_name = 'pylons'

    parser = Command.standard_parser(simulate=True)
    parser.add_option('-d', '--disable-ipython',
                      action='store_true',
                      dest='disable_ipython',
                      help="Don't use IPython if it is available")

    def command(self):
        """Main command to create a new shell"""
        self.verbose = 3
        if len(self.args) == 0:
            # Assume the .ini file is ./development.ini
            config_file = 'development.ini'
            if not os.path.isfile(config_file):
                raise BadCommand('%sError: CONFIG_FILE not found at: .%s%s\n'
                                 'Please specify a CONFIG_FILE' % \
                                 (self.parser.get_usage(), os.path.sep,
                                  config_file))
        else:
            config_file = self.args[0]

        config_name = 'config:%s' % config_file
        here_dir = os.getcwd()
        locs = dict(__name__="pylons-admin")

        # Load app config into paste.deploy to simulate request config
        conf = appconfig(config_name, relative_to=here_dir)
        conf = dict(app_conf=conf.local_conf,
                    global_conf=conf.global_conf)
        paste.deploy.config.CONFIG.push_thread_config(conf)

        # Load locals and populate with objects for use in shell
        sys.path.insert(0, here_dir)

        # Load the wsgi app first so that everything is initialized right
        wsgiapp = loadapp(config_name, relative_to=here_dir)
        test_app = paste.fixture.TestApp(wsgiapp)

        # Query the test app to setup the environment
        tresponse = test_app.get('/_test_vars')
        request_id = int(tresponse.body)

        # Disable restoration during test_app requests
        test_app.pre_request_hook = lambda self: \
            paste.registry.restorer.restoration_end()
        test_app.post_request_hook = lambda self: \
            paste.registry.restorer.restoration_begin(request_id)

        # Restore the state of the Pylons special objects
        # (StackedObjectProxies)
        paste.registry.restorer.restoration_begin(request_id)

        # Determine the package name from the .egg-info top_level.txt.
        egg_info = find_egg_info_dir(here_dir)
        f = open(os.path.join(egg_info, 'top_level.txt'))
        packages = [l.strip() for l in f.readlines()
                    if l.strip() and not l.strip().startswith('#')]
        f.close()

        # Start the rest of our imports now that the app is loaded
        found_base = False
        for pkg_name in packages:
            # Import all objects from the base module
            base_module = pkg_name + '.lib.base'
            found_base = can_import(base_module)
            if not found_base:
                # Minimal template
                base_module = pkg_name + '.controllers'
                found_base = can_import(base_module)

            if found_base:
                models_package = pkg_name + '.models'
                # The Minimal template lacks an official models package
                has_models = can_import(models_package)
                break

        if not found_base:
            raise ImportError("Could not import base module. Are you sure this "
                              "is a Pylons app?")

        base = sys.modules[base_module]
        base_public = [__name for __name in dir(base) if not \
                       __name.startswith('_') or __name == '_']
        for name in base_public:
            locs[name] = getattr(base, name)
        locs.update(
            dict(
                mapper=tresponse.pylons_config.map,
                wsgiapp=wsgiapp,
                app=test_app,
            )
        )
        if has_models:
            locs['model'] = sys.modules[models_package]

        banner = "  All objects from %s are available\n" % base_module
        banner += "  Additional Objects:\n"
        banner += "  %-10s -  %s\n" % ('mapper', 'Routes mapper object')
        if has_models:
            banner += "  %-10s -  %s\n" % ('model',
                                           'Models from models package')
        banner += "  %-10s -  %s\n" % ('wsgiapp',
            'This projects WSGI App instance')
        banner += "  %-10s -  %s\n" % ('app',
            'paste.fixture wrapped around wsgiapp')

        try:
            if self.options.disable_ipython:
                raise ImportError()

            # try to use IPython if possible
            from IPython.Shell import IPShellEmbed

            shell = IPShellEmbed(user_ns=locs)
            shell.set_banner(shell.IP.BANNER + '\n\n' + banner)
            try:
                shell()
            finally:
                paste.registry.restorer.restoration_end()
        except ImportError:
            import code

            class CustomShell(code.InteractiveConsole):
                """Custom shell class to handle raw input"""
                def raw_input(self, *args, **kw):
                    """Capture raw input in exception wrapping"""
                    try:
                        return code.InteractiveConsole.raw_input(
                            self, *args, **kw)
                    except EOFError:
                        # In the future, we'll put our own override as needed
                        # to save models, TG style
                        raise EOFError

            newbanner = "Pylons Interactive Shell\nPython %s\n\n" % sys.version
            banner = newbanner + banner
            shell = CustomShell(locals=locs)
            try:
                import readline
            except ImportError:
                pass
            try:
                shell.interact(banner)
            finally:
                paste.registry.restorer.restoration_end()

__all__ = ['ControllerCommand', 'RestControllerCommand', 'ShellCommand']
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Convert fitpot parameters to in.params.Morse for pmd.

Usage:
  fp2morse.py [options] FITPOT_VAR_FILE

Options:
  -h, --help  Show this message and exit.
"""
from __future__ import print_function

from docopt import docopt

__author__ = "RYO KOBAYASHI"
__version__ = ""

if __name__ == "__main__":

    args = docopt(__doc__)
    infname = args['FITPOT_VAR_FILE']

    ds = []
    alps = []
    rs = []
    with open(infname,'r') as f:
        lines = f.readlines()
        ndat = int(lines[0].split()[0])
        n = 0
        for i in range(1,len(lines)):
            n += 1
            if n > ndat: break
            ds.append(float(lines[3*(i-1)+1].split()[0]))
            n += 1
            if n > ndat: break
            alps.append(float(lines[3*(i-1)+2].split()[0]))
            n += 1
            if n > ndat: break
            rs.append(float(lines[3*(i-1)+3].split()[0]))

    # print(' ds  = ',ds)
    # print(' alps= ',alps)
    # print(' rs  = ',rs)
    with open('in.params.Morse','w') as f:
        f.write('# is, js,  D,      alpha,  rmin\n')
        for l in range(len(ds)):
            f.write(' {0:3d} {1:3d}'.format(1,l+2))
            f.write(' {0:7.3f} {1:7.3f} {2:7.3f}\n'.format(ds[l],alps[l],rs[l]))

    print(' Check in.params.Morse.')

<commit_msg>modified fpvars2morse.py<commit_after>#!/usr/bin/env python
"""
Convert fitpot parameters to in.params.Morse for pmd.

Usage:
  fp2morse.py [options] FITPOT_VAR_FILE

Options:
  -h, --help  Show this message and exit.
  --pairs PAIRS
              Specify pairs used in FITPOT_VAR_FILE in the format hyphen-connected
              and comma-separated, e.g.) 1-1,2-1. [default: all]
  --bvs       Specify BVS parameter set. This sets pairs as 1-1,1-2,...,1-nspeics.
"""
from __future__ import print_function

from docopt import docopt

__author__ = "RYO KOBAYASHI"
__version__ = ""

def ndat2nsp(ndat):
    #...Detect number of species assumed in FITPOT_VAR_FILE
    nd3 = ndat/3
    if nd3 == 1:
        nsp = 1
    elif nd3 == 3:
        nsp = 2
    elif nd3 == 6:
        nsp = 3
    elif nd3 == 10:
        nsp = 4
    elif nd3 == 15:
        nsp = 5
    elif nd3 == 21:
        nsp = 6
    elif nd3 == 28:
        nsp = 7
    elif nd3 == 36:
        nsp = 8
    elif nd3 == 45:
        nsp = 9
    else:
        raise ValueError(' NSP cannot be determined from NDAT.')
    return nsp

if __name__ == "__main__":

    args = docopt(__doc__)
    infname = args['FITPOT_VAR_FILE']
    pairs = args['--pairs']
    bvs = args['--bvs']
    if bvs:
        msg = ' BVS parameters are to be extracted, which means only pairs ' \
              +'including oxygen are used.'
        print(msg)
    elif pairs == 'all':
        print(' All the pairs are to be extracted.')
    else:
        pairs = [ (pair.split('-')[0],pair.split('-')[1])
                  for pair in pairs.split(',') ]
        print(' Pairs to be extracted:')
        for pair in pairs:
            print('   {0:d}-{1:d}'.format(pair[0],pair[1]))

    ds = []
    alps = []
    rs = []
    with open(infname,'r') as f:
        lines = f.readlines()
        ndat = int(lines[0].split()[0])
        if bvs:
            pairs = []
            nsp = ndat/3 +1
            for j in range(nsp):
                ja = j + 1
                if ja == 1:
                    continue
                pairs.append((1,ja))
        elif pairs == 'all':
            nsp = ndat2nsp(ndat)
            pairs = []
            for i in range(nsp):
                ia = i + 1
                for j in range(i,nsp):
                    ja = j + 1
                    pairs.append((ia,ja))
        n = 0
        for i in range(1,len(lines)):
            n += 1
            if n > ndat: break
            ds.append(float(lines[3*(i-1)+1].split()[0]))
            n += 1
            if n > ndat: break
            alps.append(float(lines[3*(i-1)+2].split()[0]))
            n += 1
            if n > ndat: break
            rs.append(float(lines[3*(i-1)+3].split()[0]))

    # print(' ds  = ',ds)
    # print(' alps= ',alps)
    # print(' rs  = ',rs)
    with open('in.params.Morse','w') as f:
        f.write('# is, js,  D,      alpha,  rmin\n')
        for l in range(len(ds)):
            f.write(' {0:3d} {1:3d}'.format(pairs[l][0],pairs[l][1]))
            f.write(' {0:7.3f} {1:7.3f} {2:7.3f}\n'.format(ds[l],alps[l],rs[l]))

    print(' Check in.params.Morse.')

<|endoftext|>


<commit_before>"""Utility classes for creating workable pylons controllers for unit testing."""
import gettext

import pylons
from pylons.controllers.util import Request, Response
from pylons.util import ContextObj, PylonsContext

class ControllerWrap(object):
    def __init__(self, controller):
        self.controller = controller

    def __call__(self, environ, start_response):
        app = self.controller()
        app.start_response = None
        return app(environ, start_response)

class SetupCacheGlobal(object):
    def __init__(self, app, environ, setup_g=True, setup_cache=False,
                 setup_session=False):
        if setup_g:
            g = type('G object', (object,), {})
            g.message = 'Hello'
            g.counter = 0
            g.pylons_config = type('App conf', (object,), {})
            g.pylons_config.app_conf = dict(cache_enabled='True')
            self.g = g
        self.app = app
        self.environ = environ
        self.setup_cache = setup_cache
        self.setup_session = setup_session
        self.setup_g = setup_g

    def __call__(self, environ, start_response):
        registry = environ['paste.registry']
        py_obj = PylonsContext()
        environ_config = environ.setdefault('pylons.environ_config', {})
        if self.setup_cache:
            py_obj.cache = environ['beaker.cache']
            registry.register(pylons.cache, environ['beaker.cache'])
            environ_config['cache'] = 'beaker.cache'
        if self.setup_session:
            py_obj.session = environ['beaker.session']
            registry.register(pylons.session, environ['beaker.session'])
            environ_config['session'] = 'beaker.session'
        if self.setup_g:
            py_obj.g = self.g
            registry.register(pylons.g, self.g)
        translator = gettext.NullTranslations()
        py_obj.translator = translator
        registry.register(pylons.translator, translator)

        # Update the environ
        environ.update(self.environ)
        py_obj.config = pylons.config._current_obj()
        py_obj.request = req = Request(environ)
        py_obj.response = resp = Response()
        py_obj.c = ContextObj()
        environ['pylons.pylons'] = py_obj
        registry.register(pylons.request, req)
        registry.register(pylons.response, resp)
        resp.content_type = 'text/html'
        resp.charset = 'utf-8'
        return self.app(environ, start_response)
<commit_msg>Updating doc string.<commit_after>"""Utility classes for creating workable pylons controllers for unit
testing.

These classes are used solely by Pylons for unit testing controller
functionality.

"""
import gettext

import pylons
from pylons.controllers.util import Request, Response
from pylons.util import ContextObj, PylonsContext

class ControllerWrap(object):
    def __init__(self, controller):
        self.controller = controller

    def __call__(self, environ, start_response):
        app = self.controller()
        app.start_response = None
        return app(environ, start_response)

class SetupCacheGlobal(object):
    def __init__(self, app, environ, setup_g=True, setup_cache=False,
                 setup_session=False):
        if setup_g:
            g = type('G object', (object,), {})
            g.message = 'Hello'
            g.counter = 0
            g.pylons_config = type('App conf', (object,), {})
            g.pylons_config.app_conf = dict(cache_enabled='True')
            self.g = g
        self.app = app
        self.environ = environ
        self.setup_cache = setup_cache
        self.setup_session = setup_session
        self.setup_g = setup_g

    def __call__(self, environ, start_response):
        registry = environ['paste.registry']
        py_obj = PylonsContext()
        environ_config = environ.setdefault('pylons.environ_config', {})
        if self.setup_cache:
            py_obj.cache = environ['beaker.cache']
            registry.register(pylons.cache, environ['beaker.cache'])
            environ_config['cache'] = 'beaker.cache'
        if self.setup_session:
            py_obj.session = environ['beaker.session']
            registry.register(pylons.session, environ['beaker.session'])
            environ_config['session'] = 'beaker.session'
        if self.setup_g:
            py_obj.g = self.g
            registry.register(pylons.g, self.g)
        translator = gettext.NullTranslations()
        py_obj.translator = translator
        registry.register(pylons.translator, translator)

        # Update the environ
        environ.update(self.environ)
        py_obj.config = pylons.config._current_obj()
        py_obj.request = req = Request(environ)
        py_obj.response = resp = Response()
        py_obj.c = ContextObj()
        environ['pylons.pylons'] = py_obj
        registry.register(pylons.request, req)
        registry.register(pylons.response, resp)
        resp.content_type = 'text/html'
        resp.charset = 'utf-8'
        return self.app(environ, start_response)
<|endoftext|>