Skip to content

Commit

Permalink
Use the new decorator and remove repeated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
lndbrg committed Aug 21, 2014
1 parent 84299d3 commit dc63250
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions paramiko/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
MIN_PACKET_SIZE = 1024

def requires_open_channel(func):
"""This decorator make sures that the channel is open else raises an
exception"""
"""This decorator makes sure that the channel is open, else it raises an
SSHException."""
@wraps(func)
def _check(self, *args, **kwds):
if self.closed or self.eof_received or self.eof_sent or not self.active:
Expand Down Expand Up @@ -132,6 +132,7 @@ def __repr__(self):
out += '>'
return out

@requires_open_channel
def get_pty(self, term='vt100', width=80, height=24, width_pixels=0,
height_pixels=0):
"""
Expand All @@ -150,8 +151,6 @@ def get_pty(self, term='vt100', width=80, height=24, width_pixels=0,
:raises SSHException:
if the request was rejected or the channel was closed
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand All @@ -167,6 +166,7 @@ def get_pty(self, term='vt100', width=80, height=24, width_pixels=0,
self.transport._send_user_message(m)
self._wait_for_event()

@requires_open_channel
def invoke_shell(self):
"""
Request an interactive shell session on this channel. If the server
Expand All @@ -183,8 +183,6 @@ def invoke_shell(self):
:raises SSHException: if the request was rejected or the channel was
closed
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand All @@ -194,6 +192,7 @@ def invoke_shell(self):
self.transport._send_user_message(m)
self._wait_for_event()

@requires_open_channel
def exec_command(self, command):
"""
Execute a command on the server. If the server allows it, the channel
Expand All @@ -209,8 +208,6 @@ def exec_command(self, command):
:raises SSHException: if the request was rejected or the channel was
closed
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand All @@ -221,6 +218,7 @@ def exec_command(self, command):
self.transport._send_user_message(m)
self._wait_for_event()

@requires_open_channel
def invoke_subsystem(self, subsystem):
"""
Request a subsystem on the server (for example, ``sftp``). If the
Expand All @@ -235,8 +233,6 @@ def invoke_subsystem(self, subsystem):
:raises SSHException:
if the request was rejected or the channel was closed
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand All @@ -247,6 +243,7 @@ def invoke_subsystem(self, subsystem):
self.transport._send_user_message(m)
self._wait_for_event()

@requires_open_channel
def resize_pty(self, width=80, height=24, width_pixels=0, height_pixels=0):
"""
Resize the pseudo-terminal. This can be used to change the width and
Expand All @@ -260,8 +257,6 @@ def resize_pty(self, width=80, height=24, width_pixels=0, height_pixels=0):
:raises SSHException:
if the request was rejected or the channel was closed
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand Down Expand Up @@ -323,7 +318,8 @@ def send_exit_status(self, status):
m.add_boolean(False)
m.add_int(status)
self.transport._send_user_message(m)


@requires_open_channel
def request_x11(self, screen_number=0, auth_protocol=None, auth_cookie=None,
single_connection=False, handler=None):
"""
Expand Down Expand Up @@ -364,8 +360,6 @@ def request_x11(self, screen_number=0, auth_protocol=None, auth_cookie=None,
an optional handler to use for incoming X11 connections
:return: the auth_cookie used
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')
if auth_protocol is None:
auth_protocol = 'MIT-MAGIC-COOKIE-1'
if auth_cookie is None:
Expand All @@ -386,6 +380,7 @@ def request_x11(self, screen_number=0, auth_protocol=None, auth_cookie=None,
self.transport._set_x11_handler(handler)
return auth_cookie

@requires_open_channel
def request_forward_agent(self, handler):
"""
Request for a forward SSH Agent on this channel.
Expand All @@ -398,9 +393,6 @@ def request_forward_agent(self, handler):
:raises: SSHException in case of channel problem.
"""
if self.closed or self.eof_received or self.eof_sent or not self.active:
raise SSHException('Channel is not open')

m = Message()
m.add_byte(cMSG_CHANNEL_REQUEST)
m.add_int(self.remote_chanid)
Expand Down

0 comments on commit dc63250

Please sign in to comment.