Skip to content

Commit

Permalink
Use contextlib.closing around ftplib object
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 29, 2018
1 parent 71125a9 commit 90907f6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.8.0'
_release = '1604'
_commit = '3c6b504'
_release = '1605'
_commit = '71125a9'
16 changes: 6 additions & 10 deletions src/pywws/service/ftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@

from __future__ import absolute_import

from contextlib import contextmanager
from contextlib import closing, contextmanager
from datetime import timedelta
import ftplib
import logging
Expand Down Expand Up @@ -87,16 +87,12 @@ def __init__(self, context, check_params=True):

@contextmanager
def session(self):
logger.info("Uploading to web site with FTP")
session = ftplib.FTP()
session.connect(self.params['site'], self.params['port'])
logger.debug('welcome message\n' + session.getwelcome())
session.login(self.params['user'], self.params['password'])
session.cwd(self.params['directory'])
try:
with closing(ftplib.FTP()) as session:
session.connect(self.params['site'], self.params['port'])
logger.debug('welcome message\n' + session.getwelcome())
session.login(self.params['user'], self.params['password'])
session.cwd(self.params['directory'])
yield session
finally:
session.close()

def upload_file(self, session, path):
target = os.path.basename(path)
Expand Down

0 comments on commit 90907f6

Please sign in to comment.