Skip to content

Commit 85e3c54

Browse files
committed
feat: allow passing an image file to 'file' option
refs: #275
1 parent 296e00f commit 85e3c54

File tree

5 files changed

+40
-9
lines changed

5 files changed

+40
-9
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ dependencies = [
2323
"numpy>=1.20",
2424
"pillow>=9.2.0",
2525
"materialyoucolor>=2.0.8",
26+
"python-magic>=0.4.27",
2627
]
2728

2829
[project.scripts]

src/kde_material_you_colors/data/sample_config.conf

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
# Default is 0
1111
monitor = 0
1212

13-
# File containing absolute path of an image (Takes precedence over automatic wallpaper detection)
13+
# Custom file for color generation
14+
# Can take a image file or a plain text file containing absolute path of the image
15+
# Takes precedence over automatic wallpaper detection
1416
# Commented by default
1517
#file = /tmp/000_eDP-1_current_wallpaper
1618

17-
# Custom color used to generate M3 color scheme (Takes precedence over automatic wallpaper detection, monitor and file options)
19+
# Custom color used to generate M3 color scheme
20+
# Takes precedence over automatic wallpaper detection, monitor and file options
1821
# Accepted values are hex (e.g #ff0000) and rgb (e.g 255,0,0) colors (rgb is converted to hex)
1922
#color = 255,0,1
2023

src/kde_material_you_colors/main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def main():
4242
"--file",
4343
"-f",
4444
type=str,
45-
help="Text file that contains wallpaper absolute path (Takes precedence over automatic wallpaper detection and --color options)",
45+
help="Image or plain text file containing the absolute path of an image (Takes precedence over automatic wallpaper detection and --color options)",
4646
default=None,
4747
metavar="<filename>",
4848
)

src/kde_material_you_colors/utils/file_utils.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import PIL
2-
import PIL.Image
31
import os
42
import hashlib
3+
import PIL
4+
import PIL.Image
5+
import magic
56

67

78
def get_last_modification(file):
@@ -64,3 +65,20 @@ def get_file_sha1(file_path):
6465
return file_sha1
6566
except Exception as e:
6667
print(f"Error:\n{e}")
68+
69+
70+
def is_plain_text(file_path):
71+
"""Check if a file is plain text
72+
73+
Args:
74+
file_path (str): Absolute path of file
75+
Returns:
76+
bool: True if plain text, False otherwise
77+
"""
78+
if file_path is not None:
79+
if os.path.exists(file_path):
80+
try:
81+
return magic.from_file(file_path, mime=True) == "text/plain"
82+
except Exception as e:
83+
print(f"Error:\n{e}")
84+
return False

src/kde_material_you_colors/utils/wallpaper_utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from kde_material_you_colors.utils import math_utils
99
from kde_material_you_colors.utils import kwin_utils
1010
from kde_material_you_colors.config import Configs
11+
from kde_material_you_colors.utils.file_utils import is_plain_text
1112

1213

1314
class WallpaperReader:
@@ -54,15 +55,23 @@ def validate_file(self):
5455
# TODO: detect image file too
5556
self._type = "image"
5657
if os.path.exists(self._file):
57-
with open(self._file, encoding="utf-8") as text_file:
58-
wallpaper = str(text_file.read()).replace("file://", "").strip()
59-
if wallpaper:
60-
self._source = wallpaper
58+
if is_plain_text(self._file):
59+
with open(self._file, encoding="utf-8") as text_file:
60+
self._source = (
61+
str(text_file.read()).replace("file://", "").strip()
62+
)
63+
else:
64+
self._source = self._file
6165
else:
6266
error = f"File '{self._file}' does not exist"
6367
logging.error(error)
6468
self._error = error
6569

70+
if self._source and not os.path.exists(self._source):
71+
error = f"Resolved file '{self._source}' does not exist"
72+
logging.error(error)
73+
self._error = error
74+
6675
@property
6776
def plugin(self):
6877
return self._plugin

0 commit comments

Comments
 (0)