File tree Expand file tree Collapse file tree 5 files changed +40
-9
lines changed
src/kde_material_you_colors Expand file tree Collapse file tree 5 files changed +40
-9
lines changed Original file line number Diff line number Diff 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 ]
Original file line number Diff line number Diff line change 1010# Default is 0
1111monitor = 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
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 1- import PIL
2- import PIL .Image
31import os
42import hashlib
3+ import PIL
4+ import PIL .Image
5+ import magic
56
67
78def 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
Original file line number Diff line number Diff line change 88from kde_material_you_colors .utils import math_utils
99from kde_material_you_colors .utils import kwin_utils
1010from kde_material_you_colors .config import Configs
11+ from kde_material_you_colors .utils .file_utils import is_plain_text
1112
1213
1314class 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
You can’t perform that action at this time.
0 commit comments