Skip to content

Commit

Permalink
expand user at post_config_hook
Browse files Browse the repository at this point in the history
backward compat from str to list
handle glob wildcard
  • Loading branch information
cyrinux committed Jun 12, 2018
1 parent 1909b9e commit 769caaa
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions py3status/modules/file_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
format: format of the output. (default '{icon}')
icon_available: icon to display when available (default '●')
icon_unavailable: icon to display when unavailable (default '■')
path: the path to a file or dir to check if it exists (default None)
path: the path(s) to a file or dir to check if it exists, take a list (default None)
Color options:
color_bad: Error or file/directory does not exist
Expand All @@ -26,7 +26,8 @@
"""

from glob import glob
from os.path import exists, expanduser
from itertools import chain
from os.path import expanduser

ERR_NO_PATH = 'no path given'

Expand Down Expand Up @@ -57,26 +58,34 @@ class Meta:
],
}

def post_config_hook(self):
if self.path:
# backward compatibility, str to list
if isinstance(self.path, str):
self.path = [self.path]
# expand user paths
self.path = [*map(expanduser, self.path)]

def file_status(self):

if self.path is None:
return {
'color': self.py3.COLOR_ERROR or self.py3.COLOR_BAD,
'full_text': ERR_NO_PATH,
'cached_until': self.py3.CACHE_FOREVER,
}

paths = glob(expanduser(self.path))
if isinstance(paths, str):
paths = [paths]
# expand user to paths
paths = [*map(glob, self.path)]
# merge list of paths
paths = [x for x in chain.from_iterable(paths)]

icon = self.icon_unavailable
color = self.py3.COLOR_BAD

for path in paths:
if exists(path):
icon = self.icon_available
color = self.py3.COLOR_GOOD
break
if paths:
icon = self.icon_available
color = self.py3.COLOR_GOOD

response = {
'cached_until': self.py3.time_in(self.cache_timeout),
Expand Down

0 comments on commit 769caaa

Please sign in to comment.