Skip to content

Commit

Permalink
[DESKTOP_ENVIRONMENT] Adds display server protocol to entry
Browse files Browse the repository at this point in the history
Co-Authored-By: Tobilike <80016610+Tobilike@users.noreply.github.com>
  • Loading branch information
HorlogeSkynet and Tobilike committed Jan 9, 2024
1 parent 7da714b commit ec479f1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ and this project (partially) adheres to [Semantic Versioning](https://semver.org
## [Unreleased]
### Added
- Official EndeavourOS distribution support
- Display server protocol to `DesktopEnvironment`

### Changed
- Allow `df` output to contain Unicode characters
- `DesktopEnvironment` API value format: now an object with `name` and `display_server_protocol` attributes

## [v4.14.2.0] - 2023-08-26
### Added
Expand Down
37 changes: 30 additions & 7 deletions archey/entries/desktop_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import os
import platform
import typing

from archey.entry import Entry
from archey.processes import Processes
Expand All @@ -19,6 +20,11 @@
"xfce4-session": "Xfce",
}

DSP_DICT = {
"x11": "X11",
"wayland": "Wayland",
}


class DesktopEnvironment(Entry):
"""
Expand All @@ -31,17 +37,34 @@ class DesktopEnvironment(Entry):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.value = {
"name": self._fetch_desktop_env_name(),
"display_server_protocol": DSP_DICT.get(os.getenv("XDG_SESSION_TYPE", "")),

}

def _fetch_desktop_env_name(self) -> typing.Optional[str]:
# macOS' desktop environment is called "Aqua",
# and could not be detected from processes list.
if platform.system() == "Darwin":
self.value = "Aqua"
return
return "Aqua"

processes = Processes().list
for de_id, de_name in DE_DICT.items():
if de_id in processes:
self.value = de_name
break
else:
# Let's rely on an environment variable if the loop above didn't `break`.
self.value = os.getenv("XDG_CURRENT_DESKTOP")
return de_name

# Let's rely on an environment variable if the loop above didn't `break`.
return os.getenv("XDG_CURRENT_DESKTOP")

def output(self, output) -> None:
# No DE could be detected.
if self.value["name"] is None:
output.append(self.name, self._default_strings.get("not_detected"))
return

text_output = self.value["name"]
if self.value["display_server_protocol"] is not None:
text_output += f" ({self.value['display_server_protocol']})"

output.append(self.name, text_output)
15 changes: 10 additions & 5 deletions archey/test/entries/test_archey_desktop_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class TestDesktopEnvironmentEntry(unittest.TestCase):
)
def test_match(self, _):
"""Simple list matching"""
self.assertEqual(DesktopEnvironment().value, "Cinnamon")
self.assertEqual(DesktopEnvironment().value["name"], "Cinnamon")

@patch(
"archey.entries.desktop_environment.Processes.list",
Expand All @@ -36,17 +36,22 @@ def test_match(self, _):
"coffee",
),
)
@patch("archey.entries.desktop_environment.os.getenv", return_value="DESKTOP ENVIRONMENT")
@patch(
"archey.entries.desktop_environment.os.getenv",
side_effect=["DESKTOP ENVIRONMENT", "wayland"],
)
def test_mismatch(self, _, __):
"""Simple list (mis-)-matching"""
self.assertEqual(DesktopEnvironment().value, "DESKTOP ENVIRONMENT")
desktop_environment = DesktopEnvironment()
self.assertEqual(desktop_environment.value["name"], "DESKTOP ENVIRONMENT")
self.assertEqual(desktop_environment.value["display_server_protocol"], "Wayland")

@patch("archey.entries.desktop_environment.platform.system")
def test_darwin_aqua_deduction(self, _, platform_system_mock):
"""Test "Aqua" deduction on Darwin systems"""
platform_system_mock.return_value = "Darwin" # Override module-wide mocked value.

self.assertEqual(DesktopEnvironment().value, "Aqua")
self.assertEqual(DesktopEnvironment().value["name"], "Aqua")

@patch(
"archey.entries.desktop_environment.Processes.list",
Expand All @@ -64,7 +69,7 @@ def test_darwin_aqua_deduction(self, _, platform_system_mock):
)
def test_non_detection(self, _, __):
"""Simple global non-detection"""
self.assertIsNone(DesktopEnvironment().value)
self.assertIsNone(DesktopEnvironment().value["name"])


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions archey/test/entries/test_archey_window_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def test_wmctrl(self, _, __):
"here",
),
)

def test_no_wmctrl_match(self, _, __):
"""Test basic detection based on a (fake) processes list"""
self.assertEqual(WindowManager().value, "Awesome")
Expand Down

0 comments on commit ec479f1

Please sign in to comment.