def test_misc_14(): import collections import logging class MockLoggingHandler(logging.Handler): def __init__(self, *args, **kwargs): super(MockLoggingHandler, self).__init__(*args, **kwargs) self.messages = collections.defaultdict(list) def emit(self, record): self.messages[record.levelname].append(record.getMessage()) logger = logging.getLogger('gdal_logging_test') logger.setLevel(logging.DEBUG) logger.propagate = False handler = MockLoggingHandler(level=logging.DEBUG) logger.addHandler(handler) prev_debug = gdal.GetConfigOption("CPL_DEBUG") try: gdal.ConfigurePythonLogging(logger_name='gdal_logging_test', enable_debug=True) assert gdal.GetConfigOption("CPL_DEBUG") == "ON", "should have enabled debug" gdal.Debug("test1", "debug1") gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") expected = { 'DEBUG': ["test1: debug1", "FileIO: debug2"], 'INFO': ["AppDefined: info1"], 'WARNING': ["AssertionFailed: warning1"], 'ERROR': ["99999: error1"], } assert handler.messages == expected, "missing log messages" gdal.SetErrorHandler('CPLDefaultErrorHandler') handler.messages.clear() gdal.SetConfigOption('CPL_DEBUG', "OFF") gdal.ConfigurePythonLogging(logger_name='gdal_logging_test') assert gdal.GetConfigOption("CPL_DEBUG") == "OFF", \ "shouldn't have enabled debug" # these get suppressed by CPL_DEBUG gdal.Debug("test1", "debug3") # these don't gdal.Error(gdal.CE_Debug, gdal.CPLE_None, "debug4") assert handler.messages['DEBUG'] == ['debug4'], "unexpected log messages" finally: gdal.SetErrorHandler('CPLDefaultErrorHandler') gdal.SetConfigOption('CPL_DEBUG', prev_debug) logger.removeHandler(handler)
def test_misc_15(): messages0 = [] def handle0(ecls, ecode, emsg): messages0.append((ecls, ecode, emsg)) messages1 = [] def handle1(ecls, ecode, emsg): messages1.append((ecls, ecode, emsg)) prev_debug = gdal.GetConfigOption("CPL_DEBUG") try: gdal.SetErrorHandler(handle0) gdal.SetConfigOption('CPL_DEBUG', "ON") gdal.Debug('foo', 'bar') gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") expected0 = [ (gdal.CE_Debug, 0, 'foo: bar'), (gdal.CE_Debug, gdal.CPLE_FileIO, "debug2"), (gdal.CE_None, gdal.CPLE_AppDefined, "info1"), (gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1"), (gdal.CE_Failure, 99999, "error1"), ] assert expected0 == messages0, "SetErrorHandler: mismatched log messages" messages0[:] = [] # Check Push gdal.PushErrorHandler(handle1) gdal.SetConfigOption("CPL_DEBUG", "OFF") gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") assert len(messages0) == 0, "PushErrorHandler: unexpected log messages" assert len(messages1) == 4, "PushErrorHandler: missing log messages" # and pop restores original behaviour gdal.PopErrorHandler() messages1[:] = [] gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") assert len(messages0) == 4, "PopErrorHandler: missing log messages" assert len(messages1) == 0, "PopErrorHandler: unexpected log messages" finally: gdal.SetErrorHandler('CPLDefaultErrorHandler') gdal.SetConfigOption('CPL_DEBUG', prev_debug)
def run_download(args): """Run download blocks from input datasource""" gdal.SetConfigOption('GDAL_TIFF_OVR_BLOCKSIZE', str(args.tile_size)) conn = get_db(args) while True: cursor = conn.cursor() cursor.execute( "SELECT " "id, file_name, x, y, scale, block_size " "FROM task WHERE " "NOT complete " "ORDER BY last_access ASC " "LIMIT 1") row = cursor.fetchone() if row is None: break url = None file_hash = None wms_cache_path = os.path.join(args.output, 'wms_%d_%d_%d' % (row['scale'], row['x'], row['y'])) gdal.SetConfigOption('GDAL_DEFAULT_WMS_CACHE_PATH', wms_cache_path) gdal.SetErrorHandler('CPLQuietErrorHandler') input_ds = gdal.Open(args.input) try: complete = download_block(input_ds, args, row['file_name'], ImageBlock(row['x'], row['y'], row['scale'], row['block_size'])) except ValueError: complete = False input_ds = None if complete: file_hash = get_file_hash(os.path.join(args.output, row['file_name'])) if args.upload: url = upload_block(args, row['file_name']) cursor.execute('UPDATE task SET ' 'complete = ?, ' 'last_access = ?, ' 'file_url = ?, ' 'file_hash = ? ' 'WHERE id = ?', [complete, datetime.datetime.now(), url, file_hash, row['id']]) conn.commit() # Do not allow to grow cache infinitely. # Drop it after each success download if complete and not args.keep_cache: shutil.rmtree(wms_cache_path, ignore_errors=True) return 0
def misc_15(): messages0 = [] def handle0(ecls, ecode, emsg): messages0.append((ecls, ecode, emsg)) messages1 = [] def handle1(ecls, ecode, emsg): messages1.append((ecls, ecode, emsg)) prev_debug = gdal.GetConfigOption("CPL_DEBUG") try: gdal.SetErrorHandler(handle0) gdal.SetConfigOption('CPL_DEBUG', "ON") gdal.Debug('foo', 'bar') gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") expected0 = [ (gdal.CE_Debug, 0, 'foo: bar'), (gdal.CE_Debug, gdal.CPLE_FileIO, "debug2"), (gdal.CE_None, gdal.CPLE_AppDefined, "info1"), (gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1"), (gdal.CE_Failure, 99999, "error1"), ] if expected0 != messages0: gdaltest.post_reason("SetErrorHandler: mismatched log messages") return 'fail' messages0[:] = [] # Check Push gdal.PushErrorHandler(handle1) gdal.SetConfigOption("CPL_DEBUG", "OFF") gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") if len(messages0) != 0: gdaltest.post_reason("PushErrorHandler: unexpected log messages") return 'fail' if len(messages1) != 4: gdaltest.post_reason("PushErrorHandler: missing log messages") return 'fail' # and pop restores original behaviour gdal.PopErrorHandler() messages1[:] = [] gdal.Error(gdal.CE_Debug, gdal.CPLE_FileIO, "debug2") gdal.Error(gdal.CE_None, gdal.CPLE_AppDefined, "info1") gdal.Error(gdal.CE_Warning, gdal.CPLE_AssertionFailed, "warning1") gdal.Error(gdal.CE_Failure, 99999, "error1") if len(messages0) != 4: gdaltest.post_reason("PopErrorHandler: missing log messages") return 'fail' if len(messages1) != 0: gdaltest.post_reason("PopErrorHandler: unexpected log messages") return 'fail' finally: gdal.SetErrorHandler('CPLDefaultErrorHandler') gdal.SetConfigOption('CPL_DEBUG', prev_debug) return 'success'