Skip to content

Commit

Permalink
remove @active_prop decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasjuhrich committed Sep 28, 2023
1 parent 05edd6d commit 7203e1a
Showing 1 changed file with 0 additions and 123 deletions.
123 changes: 0 additions & 123 deletions sipa/model/fancy_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,129 +138,6 @@ def unsupported_prop(func):
return property(lambda self: UnsupportedProperty(name=func.__name__))


class active_prop(property):
"""A property-like class wrapping the getter with
:py:class:`ActiveProperty`
:py:class:`active_prop` automatically adds `edit` and `delete`
capabilities to the ActiveProperty object if `setter`/`deleter` is
invoked.
Example usage:
>>> class User:
... @active_prop
... def foo(self):
... return {'value': "Empty!!", 'empty': True, 'style': 'danger'}
...
... @active_prop
... def bar(self):
... return 0
...
... @bar.setter
... def bar(self):
... print("furiously setting things")
>>> User().foo
<ActiveProperty foo='Empty!!' [empty]>
>>> User().bar
<ActiveProperty bar='Nicht angegeben' [empty]>
>>> User().bar.capabilities
capabilities(edit=True, delete=False)
"""

import warnings

warnings.warn(
"active_prop is deprecated. directly return ActiveProperty instead.",
DeprecationWarning,
stacklevel=2,
)

def __init__(self, fget, fset=None, fdel=None, doc=None,
fake_setter=False):
"""Return a property object and wrap fget with `ActiveProperty`.
The first argument is the function given to `active_prop`
(perhaps as a decorator).
`fget` is always wrapped by `ActiveProperty`in a way depending
of the return value of `fget`. If it returns
- Something subscriptable to `'value'`: Pass `['value']` and
`['style']` (defaults to `None`) to `ActiveProperty`s
`__init__`, respectively.
- Something else: Pass it as `value` to `ActiveProperty`s
`__init__`.
"""
self.__raw_getter = fget
self.__fake_setter = fake_setter # only for the __repr__

@wraps(fget)
def wrapped_getter(*args, **kwargs):
result = fget(*args, **kwargs)
try:
value = result['value']
except TypeError:
# `KeyError` should not happen, since that means a
# dict would have been returned not including `value`,
# which would make no sense and likely is a mistake.
name = fget.__name__
value = result
raw_value = value
style = None
description_url = None
empty = None
tmp_readonly = False
else:
name = result.get('name', fget.__name__)
raw_value = result.get('raw_value', value)
style = result.get('style', None)
description_url = result.get('description_url', None)
empty = result.get('empty', None)
tmp_readonly = result.get('tmp_readonly', False)

return ActiveProperty(
name=name,
value=value,
raw_value=raw_value,
capabilities=Capabilities(
edit=(fset is not None or fake_setter),
delete=(fdel is not None),
) if not tmp_readonly else NO_CAPABILITIES,
style=style,
description_url=description_url,
empty=empty,
)

# Let `property` handle the initialization of `__get__`, `__set__` etc.
super().__init__(wrapped_getter, fset, fdel, doc)

def __repr__(self):
return "{}.{}({})".format(__name__, type(self).__name__, argstr(
fget=self.__raw_getter,
fset=self.fset,
fdel=self.fdel,
doc=self.__doc__,
fake_setter=self.__fake_setter,
))

def getter(self, func):
return type(self)(func, self.fset, self.fdel, self.__doc__)

def setter(self, func):
return type(self)(self.__raw_getter, func, self.fdel, self.__doc__)

def deleter(self, func):
return type(self)(self.__raw_getter, self.fset, func, self.__doc__)

def fake_setter(self):
return type(self)(self.__raw_getter, self.fset, self.fdel,
self.__doc__,
fake_setter=True)


def connection_dependent(func):
"""A decorator to “deactivate” the property if the user's not active.
"""
Expand Down

0 comments on commit 7203e1a

Please sign in to comment.