# Path: spyder_terminal/server/logic/term_manager.py
# class TermManager(TermManagerBase):
#     """Wrapper around pexpect to execute local commands."""
# 
#     def __init__(self, shell_command, **kwargs):
#         """Create a new terminal handler instance."""
#         super().__init__(shell_command, **kwargs)
#         self.consoles = {}
# 
#     def new_terminal(self, **kwargs):
#         """Make a new terminal, return a :class:`PtyReader` instance."""
#         options = self.term_settings.copy()
#         options['shell_command'] = self.shell_command
#         options.update(kwargs)
#         argv = options['shell_command']
#         env = self.make_term_env(**options)
#         cwd = options.get('cwd', None)
# 
#         # Remove environment variables introduced by Spyder
#         for var in REMOVE_ENV:
#             env.pop(var, None)
# 
#         return PtyReader(argv, env, cwd)
# 
#     @tornado.gen.coroutine
#     def client_disconnected(self, pid, socket):
#         """Send terminal SIGHUP when client disconnects."""
#         self.log.info("Websocket closed, sending SIGHUP to terminal.")
#         term = self.consoles[pid]
#         term.clients.remove(socket)
#         try:
#             if WINDOWS:
#                 term.kill()
#                 self.pty_read(term.ptyproc.fd)
#                 return
#             term.killpg(signal.SIGHUP)
#         except Exception:
#             pass
#         del self.consoles[pid]
# 
#     @tornado.gen.coroutine
#     def create_term(self, rows, cols, cwd=None):
#         """Create a new virtual terminal."""
#         pid = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()[0:6]
#         # We need to do percent decoding for reading the cwd through a cookie
#         # For further information see spyder-ide/spyder-terminal#225
#         cwd = unquote(cwd)
#         pty = self.new_terminal(cwd=cwd, height=rows, width=cols)
#         pty.resize_to_smallest(rows, cols)
#         self.consoles[pid] = pty
#         return pid
# 
#     @tornado.gen.coroutine
#     def start_term(self, pid, socket):
#         """Start reading a virtual terminal."""
#         term = self.consoles[pid]
#         self.start_reading(term)
#         term.clients.append(socket)
# 
#     @tornado.gen.coroutine
#     def execute(self, pid, cmd):
#         """Write characters to terminal."""
#         term = self.consoles[pid]
#         term.ptyproc.write(cmd)
# 
#     @tornado.gen.coroutine
#     def resize_term(self, pid, rows, cols):
#         """Resize terminal."""
#         term = self.consoles[pid]
#         term.resize_to_smallest(rows, cols)


import tornado
import os.path
import spyder_terminal.server.routes as routes
from spyder_terminal.server.logic.term_manager import TermManager

"""General server constants and utillty functions."""



def create_app(shell, close_future=None):
    """Create and return a tornado Web Application instance."""
    settings = {"static_path": os.path.join(
        os.path.dirname(__file__), "static")}
    application = tornado.web.Application(routes.gen_routes(close_future),
                                          debug=True,
                                          serve_traceback=True,
                                          autoreload=True, **settings)

# Path: spyder_terminal/server/logic/term_manager.py
# class TermManager(TermManagerBase):
#     """Wrapper around pexpect to execute local commands."""
# 
#     def __init__(self, shell_command, **kwargs):
#         """Create a new terminal handler instance."""
#         super().__init__(shell_command, **kwargs)
#         self.consoles = {}
# 
#     def new_terminal(self, **kwargs):
#         """Make a new terminal, return a :class:`PtyReader` instance."""
#         options = self.term_settings.copy()
#         options['shell_command'] = self.shell_command
#         options.update(kwargs)
#         argv = options['shell_command']
#         env = self.make_term_env(**options)
#         cwd = options.get('cwd', None)
# 
#         # Remove environment variables introduced by Spyder
#         for var in REMOVE_ENV:
#             env.pop(var, None)
# 
#         return PtyReader(argv, env, cwd)
# 
#     @tornado.gen.coroutine
#     def client_disconnected(self, pid, socket):
#         """Send terminal SIGHUP when client disconnects."""
#         self.log.info("Websocket closed, sending SIGHUP to terminal.")
#         term = self.consoles[pid]
#         term.clients.remove(socket)
#         try:
#             if WINDOWS:
#                 term.kill()
#                 self.pty_read(term.ptyproc.fd)
#                 return
#             term.killpg(signal.SIGHUP)
#         except Exception:
#             pass
#         del self.consoles[pid]
# 
#     @tornado.gen.coroutine
#     def create_term(self, rows, cols, cwd=None):
#         """Create a new virtual terminal."""
#         pid = hashlib.md5(str(time.time()).encode('utf-8')).hexdigest()[0:6]
#         # We need to do percent decoding for reading the cwd through a cookie
#         # For further information see spyder-ide/spyder-terminal#225
#         cwd = unquote(cwd)
#         pty = self.new_terminal(cwd=cwd, height=rows, width=cols)
#         pty.resize_to_smallest(rows, cols)
#         self.consoles[pid] = pty
#         return pid
# 
#     @tornado.gen.coroutine
#     def start_term(self, pid, socket):
#         """Start reading a virtual terminal."""
#         term = self.consoles[pid]
#         self.start_reading(term)
#         term.clients.append(socket)
# 
#     @tornado.gen.coroutine
#     def execute(self, pid, cmd):
#         """Write characters to terminal."""
#         term = self.consoles[pid]
#         term.ptyproc.write(cmd)
# 
#     @tornado.gen.coroutine
#     def resize_term(self, pid, rows, cols):
#         """Resize terminal."""
#         term = self.consoles[pid]
#         term.resize_to_smallest(rows, cols)


# Path: spyder_terminal/server/common.py
import tornado
import os.path
import spyder_terminal.server.routes as routes
from spyder_terminal.server.logic.term_manager import TermManager


"""General server constants and utillty functions."""



def create_app(shell, close_future=None):
    """Create and return a tornado Web Application instance."""
    settings = {"static_path": os.path.join(
        os.path.dirname(__file__), "static")}
    application = tornado.web.Application(routes.gen_routes(close_future),
                                          debug=True,
                                          serve_traceback=True,
                                          autoreload=True, **settings)

    application.term_manager = TermManager([shell])