From 46060d64167c1f700a8af5b89744ac0909d53c5a Mon Sep 17 00:00:00 2001 From: Mark McLoughlin Date: Wed, 5 Sep 2012 12:21:36 +0100 Subject: [PATCH] Sync misc changes from openstack-common Syncs the following changes from stable/folsom: 769ec65 Don't trap then re-raise ImportError. 202b8b7 Fix spelling typos 01b4f31 Support for marshalling datetime while preserving microseconds. Change-Id: I6ed5e71ed18cdf3c528351713a42645f31fd8965 --- cinder/openstack/common/excutils.py | 4 ++-- cinder/openstack/common/importutils.py | 2 +- cinder/openstack/common/timeutils.py | 22 ++++++++++++++++++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/cinder/openstack/common/excutils.py b/cinder/openstack/common/excutils.py index 67c9fa951..5dd483017 100644 --- a/cinder/openstack/common/excutils.py +++ b/cinder/openstack/common/excutils.py @@ -30,14 +30,14 @@ def save_and_reraise_exception(): """Save current exception, run some code and then re-raise. In some cases the exception context can be cleared, resulting in None - being attempted to be reraised after an exception handler is run. This + being attempted to be re-raised after an exception handler is run. This can happen when eventlet switches greenthreads or when running an exception handler, code raises and catches an exception. In both cases the exception context will be cleared. To work around this, we save the exception state, run handler code, and then re-raise the original exception. If another exception occurs, the - saved exception is logged and the new exception is reraised. + saved exception is logged and the new exception is re-raised. """ type_, value, tb = sys.exc_info() try: diff --git a/cinder/openstack/common/importutils.py b/cinder/openstack/common/importutils.py index 2fbb0291a..f45372b4d 100644 --- a/cinder/openstack/common/importutils.py +++ b/cinder/openstack/common/importutils.py @@ -29,7 +29,7 @@ def import_class(import_str): try: __import__(mod_str) return getattr(sys.modules[mod_str], class_str) - except (ImportError, ValueError, AttributeError), exc: + except (ValueError, AttributeError), exc: raise ImportError('Class %s cannot be found (%s)' % (class_str, traceback.format_exception(*sys.exc_info()))) diff --git a/cinder/openstack/common/timeutils.py b/cinder/openstack/common/timeutils.py index 4416a3b19..c4f6cf049 100644 --- a/cinder/openstack/common/timeutils.py +++ b/cinder/openstack/common/timeutils.py @@ -93,16 +93,34 @@ def set_time_override(override_time=datetime.datetime.utcnow()): def advance_time_delta(timedelta): - """Advance overriden time using a datetime.timedelta.""" + """Advance overridden time using a datetime.timedelta.""" assert(not utcnow.override_time is None) utcnow.override_time += timedelta def advance_time_seconds(seconds): - """Advance overriden time by seconds.""" + """Advance overridden time by seconds.""" advance_time_delta(datetime.timedelta(0, seconds)) def clear_time_override(): """Remove the overridden time.""" utcnow.override_time = None + + +def marshall_now(now=None): + """Make an rpc-safe datetime with microseconds. + + Note: tzinfo is stripped, but not required for relative times.""" + if not now: + now = utcnow() + return dict(day=now.day, month=now.month, year=now.year, hour=now.hour, + minute=now.minute, second=now.second, + microsecond=now.microsecond) + + +def unmarshall_time(tyme): + """Unmarshall a datetime dict.""" + return datetime.datetime(day=tyme['day'], month=tyme['month'], + year=tyme['year'], hour=tyme['hour'], minute=tyme['minute'], + second=tyme['second'], microsecond=tyme['microsecond']) -- 2.45.2