Downloader: only import requests during setup

This commit is contained in:
Paulus Schoutsen 2014-01-23 23:26:25 -08:00
parent 097a51abc6
commit 3618181bce

View File

@ -8,8 +8,6 @@ import os
import logging import logging
import re import re
import requests
import homeassistant.util as util import homeassistant.util as util
DOMAIN = "downloader" DOMAIN = "downloader"
@ -22,11 +20,18 @@ def setup(bus, download_path):
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
try:
import requests
except ImportError:
logger.exception(("Failed to import requests. "
"Did you maybe not execute 'pip install requests'?"))
return False
if not os.path.isdir(download_path): if not os.path.isdir(download_path):
logger.error( logger.error(
("FileDownloader:" ("Download path {} does not exist. File Downloader not active.").
"Download path {} does not exist. File Downloader not active.").
format(download_path)) format(download_path))
return False return False
@ -66,7 +71,7 @@ def setup(bus, download_path):
final_path = path + "_{}".format(tries) + ext final_path = path + "_{}".format(tries) + ext
logger.info("FileDownloader:{} -> {}".format( logger.info("{} -> {}".format(
service.data['url'], final_path)) service.data['url'], final_path))
with open(final_path, 'wb') as fil: with open(final_path, 'wb') as fil:
@ -74,7 +79,7 @@ def setup(bus, download_path):
fil.write(chunk) fil.write(chunk)
except requests.exceptions.ConnectionError: except requests.exceptions.ConnectionError:
logger.exception("FileDownloader:ConnectionError occured for {}". logger.exception("ConnectionError occured for {}".
format(service.data['url'])) format(service.data['url']))
bus.register_service(DOMAIN, SERVICE_DOWNLOAD_FILE, bus.register_service(DOMAIN, SERVICE_DOWNLOAD_FILE,