Skip to content

Commit

Permalink
Remove deprecated Monitor class
Browse files Browse the repository at this point in the history
  • Loading branch information
Crozzers committed Apr 3, 2024
1 parent 8880c65 commit bd35b2e
Showing 1 changed file with 1 addition and 195 deletions.
196 changes: 1 addition & 195 deletions screen_brightness_control/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ def is_active(self) -> bool:
return True
except Exception as e:
self._logger.error(
f'Monitor.is_active: {self.get_identifier()} failed get_brightness call'
f'Display.is_active: {self.get_identifier()} failed get_brightness call'
f' - {format_exc(e)}'
)
return False
Expand Down Expand Up @@ -556,200 +556,6 @@ def set_brightness(self, value: Percentage, force: bool = False):
self.method.set_brightness(value, display=self.index)


class Monitor(Display):
'''
Legacy class for managing displays.
.. warning:: Deprecated
Deprecated for removal in v0.23.0. Please use the new `Display` class instead
'''

def __init__(self, display: Union[int, str, dict]):
'''
Args:
display (.types.DisplayIdentifier or dict): the display you
wish to control. Is passed to `filter_monitors`
to decide which display to use.
Example:
```python
import screen_brightness_control as sbc
# create a class for the primary display and then a specifically named monitor
primary = sbc.Monitor(0)
benq_monitor = sbc.Monitor('BenQ GL2450H')
# check if the benq monitor is the primary one
if primary.serial == benq_monitor.serial:
print('BenQ GL2450H is the primary display')
else:
print('The primary display is', primary.name)
```
'''
warnings.warn(
(
'`Monitor` is deprecated for removal in v0.23.0.'
' Please use the new `Display` class instead'
),
DeprecationWarning
)

monitors_info = list_monitors_info(allow_duplicates=True)
if isinstance(display, dict):
if display in monitors_info:
info = display
else:
info = filter_monitors(
display=self.get_identifier(display)[1],
haystack=monitors_info
)[0]
else:
info = filter_monitors(display=display, haystack=monitors_info)[0]

# make a copy so that we don't alter the dict in-place
info = info.copy()

kw = [i.name for i in fields(Display) if i.init]
super().__init__(**{k: v for k, v in info.items() if k in kw})

# this assigns any extra info that is returned to this class
# eg: the 'interface' key in XRandr monitors on Linux
for key, value in info.items():
if key not in kw and value is not None:
setattr(self, key, value)

def get_identifier(self, monitor: Optional[dict] = None) -> Tuple[str, DisplayIdentifier]:
'''
Returns the `.types.DisplayIdentifier` for this display.
Will iterate through the EDID, serial, name and index and return the first
value that is not equal to None
Args:
monitor: extract an identifier from this dict instead of the monitor class
Returns:
A tuple containing the name of the property returned and the value of said
property. EG: `('serial', '123abc...')` or `('name', 'BenQ GL2450H')`
Example:
```python
import screen_brightness_control as sbc
primary = sbc.Monitor(0)
print(primary.get_identifier()) # eg: ('serial', '123abc...')
secondary = sbc.list_monitors_info()[1]
print(primary.get_identifier(monitor=secondary)) # eg: ('serial', '456def...')
# you can also use the class uninitialized
print(sbc.Monitor.get_identifier(secondary)) # eg: ('serial', '456def...')
```
'''
if monitor is None:
if isinstance(self, dict):
monitor = self
else:
return super().get_identifier()

for key in ('edid', 'serial', 'name'):
value = monitor[key]
if value is not None:
return key, value
return 'index', self.index

def set_brightness(
self,
value: Percentage,
no_return: bool = True,
force: bool = False
) -> Optional[IntPercentage]:
'''
Wrapper for `Display.set_brightness`
Args:
value: see `Display.set_brightness`
no_return: do not return the new brightness after it has been set
force: see `Display.set_brightness`
'''
# refresh display info, in case another display has been unplugged or something
# which would change the index of this display
self.get_info()
super().set_brightness(value, force)
if no_return:
return None
return self.get_brightness()

def get_brightness(self) -> IntPercentage:
# refresh display info, in case another display has been unplugged or something
# which would change the index of this display
self.get_info()
return super().get_brightness()

def fade_brightness(
self,
*args,
blocking: bool = True,
**kwargs
) -> Union[threading.Thread, IntPercentage]:
'''
Wrapper for `Display.fade_brightness`
Args:
*args: see `Display.fade_brightness`
blocking: run this function in the current thread and block until
it completes. If `False`, the fade will be run in a new daemonic
thread, which will be started and returned
**kwargs: see `Display.fade_brightness`
'''
if blocking:
super().fade_brightness(*args, **kwargs)
return self.get_brightness()

thread = threading.Thread(
target=super().fade_brightness, args=args, kwargs=kwargs, daemon=True)
thread.start()
return thread

@classmethod
def from_dict(cls, display) -> 'Monitor':
return cls(display)

def get_info(self, refresh: bool = True) -> dict:
'''
Returns all known information about this monitor instance
Args:
refresh: whether to refresh the information
or to return the cached version
Example:
```python
import screen_brightness_control as sbc
# initialize class for primary display
primary = sbc.Monitor(0)
# get the info
info = primary.get_info()
```
'''
def vars_self():
return {k: v for k, v in vars(self).items() if not k.startswith('_')}

if not refresh:
return vars_self()

identifier = self.get_identifier()

if identifier is not None:
# refresh the info we have on this monitor
info = filter_monitors(
display=identifier[1], method=self.method.__name__)[0]
for key, value in info.items():
if value is not None:
setattr(self, key, value)

return vars_self()


@config.default_params
def filter_monitors(
display: Optional[DisplayIdentifier] = None,
Expand Down

0 comments on commit bd35b2e

Please sign in to comment.