]>
git.proxmox.com Git - ceph.git/blob - ceph/src/ceph-volume/ceph_volume/decorators.py
3 from ceph_volume
import terminal
, exceptions
4 from functools
import wraps
9 Check for super user privileges on functions/methods. Raise
10 ``SuperUserError`` with a nice message.
13 def is_root(*a
, **kw
):
14 if not os
.getuid() == 0:
15 raise exceptions
.SuperUserError()
20 def catches(catch
=None, handler
=None, exit
=True):
22 Very simple decorator that tries any of the exception(s) passed in as
23 a single exception class or tuple (containing multiple ones) returning the
24 exception message and optionally handling the problem if it rises with the
25 handler if it is provided.
27 So instead of douing something like this::
33 except TypeError, exc:
34 print "Error while handling some call: %s" % exc
37 You would need to decorate it like this to have the same effect::
44 If multiple exceptions need to be catched they need to be provided as a
47 @catches((TypeError, AttributeError))
52 catch
= catch
or Exception
57 def newfunc(*a
, **kw
):
62 logger
= logging
.getLogger('ceph_volume')
63 logger
.exception('exception caught by decorator')
64 if os
.environ
.get('CEPH_VOLUME_DEBUG'):
69 sys
.stderr
.write(make_exception_message(e
))
81 def make_exception_message(exc
):
83 An exception is passed in and this function
84 returns the proper string depending on the result
85 so it is readable enough.
88 return '%s %s: %s\n' % (terminal
.red_arrow
, exc
.__class
__.__name
__, exc
)
90 return '%s %s\n' % (terminal
.red_arrow
, exc
.__class
__.__name
__)