2015-06-02 07:04:25 -05:00
|
|
|
#
|
|
|
|
# Copyright (C) 2015 FreeIPA Contributors see COPYING for license
|
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
|
|
|
Utilities.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
|
Use Python3-compatible dict method names
Python 2 has keys()/values()/items(), which return lists,
iterkeys()/itervalues()/iteritems(), which return iterators,
and viewkeys()/viewvalues()/viewitems() which return views.
Python 3 has only keys()/values()/items(), which return views.
To get iterators, one can use iter() or a for loop/comprehension;
for lists there's the list() constructor.
When iterating through the entire dict, without modifying the dict,
the difference between Python 2's items() and iteritems() is
negligible, especially on small dicts (the main overhead is
extra memory, not CPU time). In the interest of simpler code,
this patch changes many instances of iteritems() to items(),
iterkeys() to keys() etc.
In other cases, helpers like six.itervalues are used.
Reviewed-By: Christian Heimes <cheimes@redhat.com>
Reviewed-By: Jan Cholasta <jcholast@redhat.com>
2015-08-11 06:51:14 -05:00
|
|
|
import six
|
|
|
|
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
class from_(object):
|
|
|
|
"""
|
|
|
|
Wrapper for delegating to a subgenerator.
|
|
|
|
|
|
|
|
See `run_generator_with_yield_from`.
|
|
|
|
"""
|
|
|
|
__slots__ = ('obj',)
|
|
|
|
|
|
|
|
def __init__(self, obj):
|
|
|
|
self.obj = obj
|
|
|
|
|
|
|
|
|
|
|
|
def run_generator_with_yield_from(gen):
|
|
|
|
"""
|
|
|
|
Iterate over a generator object with subgenerator delegation.
|
|
|
|
|
|
|
|
This implements Python 3's ``yield from`` expressions, using Python 2
|
|
|
|
syntax:
|
|
|
|
|
|
|
|
>>> def subgen():
|
|
|
|
... yield 'B'
|
|
|
|
... yield 'C'
|
|
|
|
...
|
|
|
|
>>> def gen():
|
|
|
|
... yield 'A'
|
|
|
|
... yield from_(subgen())
|
|
|
|
... yield 'D'
|
|
|
|
...
|
|
|
|
>>> list(run_generator_with_yield_from(gen()))
|
|
|
|
['A', 'B', 'C', 'D']
|
|
|
|
|
|
|
|
Returning value from a subgenerator is not supported.
|
|
|
|
"""
|
|
|
|
|
|
|
|
exc_info = None
|
|
|
|
value = None
|
|
|
|
|
|
|
|
stack = [gen]
|
|
|
|
while stack:
|
|
|
|
prev_exc_info, exc_info = exc_info, None
|
|
|
|
prev_value, value = value, None
|
|
|
|
|
|
|
|
gen = stack[-1]
|
|
|
|
try:
|
|
|
|
if prev_exc_info is None:
|
|
|
|
value = gen.send(prev_value)
|
|
|
|
else:
|
|
|
|
value = gen.throw(*prev_exc_info)
|
|
|
|
except StopIteration:
|
|
|
|
stack.pop()
|
|
|
|
continue
|
|
|
|
except BaseException:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
stack.pop()
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
if isinstance(value, from_):
|
|
|
|
stack.append(value.obj)
|
|
|
|
value = None
|
|
|
|
continue
|
|
|
|
|
|
|
|
try:
|
|
|
|
value = (yield value)
|
|
|
|
except BaseException:
|
|
|
|
exc_info = sys.exc_info()
|
|
|
|
|
|
|
|
if exc_info is not None:
|
2015-08-12 07:06:54 -05:00
|
|
|
six.reraise(*exc_info)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
|
|
|
|
|
|
|
class InnerClassMeta(type):
|
2017-08-22 07:08:27 -05:00
|
|
|
# pylint: disable=no-value-for-parameter
|
2018-07-12 15:20:59 -05:00
|
|
|
def __new__(cls, name, bases, class_dict):
|
2015-06-02 07:04:25 -05:00
|
|
|
class_dict.pop('__outer_class__', None)
|
|
|
|
class_dict.pop('__outer_name__', None)
|
|
|
|
|
2018-07-12 15:20:59 -05:00
|
|
|
return super(InnerClassMeta, cls).__new__(cls, name, bases, class_dict)
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
def __get__(cls, obj, obj_type):
|
|
|
|
outer_class, outer_name = cls.__bind(obj_type)
|
2015-06-02 07:04:25 -05:00
|
|
|
if obj is None:
|
2016-06-03 06:43:21 -05:00
|
|
|
return cls
|
2015-06-02 07:04:25 -05:00
|
|
|
assert isinstance(obj, outer_class)
|
|
|
|
|
|
|
|
try:
|
|
|
|
return obj.__dict__[outer_name]
|
|
|
|
except KeyError:
|
2016-06-03 06:43:21 -05:00
|
|
|
inner = cls(obj)
|
2015-06-02 07:04:25 -05:00
|
|
|
try:
|
|
|
|
getter = inner.__get__
|
|
|
|
except AttributeError:
|
|
|
|
return inner
|
|
|
|
else:
|
|
|
|
return getter(obj, obj_type)
|
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
def __set__(cls, obj, value):
|
|
|
|
outer_class, outer_name = cls.__bind(obj.__class__)
|
2015-06-02 07:04:25 -05:00
|
|
|
assert isinstance(obj, outer_class)
|
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
inner = cls(obj)
|
2015-06-02 07:04:25 -05:00
|
|
|
try:
|
|
|
|
setter = inner.__set__
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
inner.__delete__
|
|
|
|
except AttributeError:
|
|
|
|
obj.__dict__[outer_name] = value
|
|
|
|
else:
|
|
|
|
raise AttributeError('__set__')
|
|
|
|
else:
|
|
|
|
setter(obj, value)
|
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
def __delete__(cls, obj):
|
|
|
|
outer_class, outer_name = cls.__bind(obj.__class__)
|
2015-06-02 07:04:25 -05:00
|
|
|
assert isinstance(obj, outer_class)
|
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
inner = cls(obj)
|
2015-06-02 07:04:25 -05:00
|
|
|
try:
|
|
|
|
deleter = inner.__delete__
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
inner.__set__
|
|
|
|
except AttributeError:
|
|
|
|
try:
|
|
|
|
del obj.__dict__[outer_name]
|
|
|
|
except KeyError:
|
|
|
|
raise AttributeError(outer_name)
|
|
|
|
else:
|
|
|
|
raise AttributeError('__delete__')
|
|
|
|
else:
|
|
|
|
deleter(obj)
|
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
def __bind(cls, obj_type):
|
2015-06-02 07:04:25 -05:00
|
|
|
try:
|
2016-06-03 06:43:21 -05:00
|
|
|
outer_class = cls.__dict__['__outer_class__']
|
|
|
|
name = cls.__dict__['__outer_name__']
|
2015-06-02 07:04:25 -05:00
|
|
|
except KeyError:
|
2016-06-03 06:43:21 -05:00
|
|
|
outer_class, name, value = None, None, None
|
|
|
|
for outer_class in obj_type.__mro__:
|
|
|
|
for name, value in six.iteritems(outer_class.__dict__):
|
|
|
|
if value is cls:
|
2015-06-02 07:04:25 -05:00
|
|
|
break
|
2016-06-03 06:43:21 -05:00
|
|
|
if value is cls:
|
2015-06-02 07:04:25 -05:00
|
|
|
break
|
2016-06-03 06:43:21 -05:00
|
|
|
assert value is cls
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
cls.__outer_class__ = outer_class
|
|
|
|
cls.__outer_name__ = name
|
|
|
|
cls.__name__ = '.'.join((outer_class.__name__, name))
|
|
|
|
cls.__qualname__ = cls.__name__
|
2015-06-02 07:04:25 -05:00
|
|
|
|
2016-06-03 06:43:21 -05:00
|
|
|
return outer_class, name
|