From 3040bcdf32bbaf106db6fb22a94adabf372651b7 Mon Sep 17 00:00:00 2001 From: Artem Bityutskiy Date: Fri, 13 Sep 2013 12:55:14 +0300 Subject: [PATCH] TransRead: accept the logger object In order to improve user experience, I would like to print warnings when we cannot open the URL for some time. This requires the TransRead object to accept the logger object, or take the global one. Change-Id: I4fc9f3d6544a2a699b6476dddf362aa127e81550 Signed-off-by: Artem Bityutskiy --- bmaptool | 10 +++++----- bmaptools/TransRead.py | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bmaptool b/bmaptool index 50eeea1..3c607e8 100755 --- a/bmaptool +++ b/bmaptool @@ -95,7 +95,7 @@ def copy_command_open_blkdev(path, log): return NamedFile(file_obj, path) -def find_and_open_bmap(image_path): +def find_and_open_bmap(image_path, log): """ When the user does not specify the bmap file, we try to find it at the same place where the image file is located. We search for a file with the same @@ -113,7 +113,7 @@ def find_and_open_bmap(image_path): bmap_path = image_path + ".bmap" try: - bmap_obj = TransRead.TransRead(bmap_path) + bmap_obj = TransRead.TransRead(bmap_path, logger=log) bmap_obj.close() return bmap_path except TransRead.Error: @@ -140,7 +140,7 @@ def copy_command_open_all(args, log): # Open the image file using the TransRead module, which will automatically # recognize whether it is compressed or whether file path is an URL, etc. try: - image_obj = TransRead.TransRead(args.image) + image_obj = TransRead.TransRead(args.image, logger=log) except TransRead.Error as err: log.error("cannot open image: %s" % str(err)) raise SystemExit(1) @@ -149,7 +149,7 @@ def copy_command_open_all(args, log): # was not specified. bmap_path = args.bmap if not bmap_path and not args.nobmap: - bmap_path = find_and_open_bmap(args.image) + bmap_path = find_and_open_bmap(args.image, log) if bmap_path: log.info("discovered bmap file '%s'" % bmap_path) @@ -158,7 +158,7 @@ def copy_command_open_all(args, log): try: # The current implementation of BmapCopy requires a local file for # the bmap file. - bmap_obj = TransRead.TransRead(bmap_path, local = True) + bmap_obj = TransRead.TransRead(bmap_path, local=True, logger=log) except TransRead.Error as err: log.error("cannot open bmap file '%s': %s" % (bmap_path, str(err))) raise SystemExit(1) diff --git a/bmaptools/TransRead.py b/bmaptools/TransRead.py index 45c243d..49c7b59 100644 --- a/bmaptools/TransRead.py +++ b/bmaptools/TransRead.py @@ -19,6 +19,7 @@ them on-the-fly if needed. Remote files are read using urllib2 (except of import os import errno import urlparse +import logging # Disable the following pylint errors and recommendations: # * Instance of X has no member Y (E1101), because it produces @@ -218,7 +219,7 @@ class TransRead: this class are file-like objects which you can read and seek only forward. """ - def __init__(self, filepath, local=False): + def __init__(self, filepath, local=False, logger=None): """ Class constructor. The 'filepath' argument is the full path to the file to read transparently. If 'local' is True, then the file-like object is @@ -226,8 +227,15 @@ class TransRead: if the source file is compressed and/or an URL, then it will first be copied to an temporary local file, and then all the subsequent operations will be done with the uncompresed local copy. + + The "logger" argument is the logger object to use for printing + messages. """ + self._logger = logger + if self._logger is None: + self._logger = logging.getLogger(__name__) + self.name = filepath # Size of the file (in uncompressed form), may be 'None' if the size is # unknown -- 2.7.4