Skip to content

Commit

Permalink
Merge pull request #1369 from cyrinux/file_status-allow-glob
Browse files Browse the repository at this point in the history
file_status module: add support for several paths with new format options, by @cyrinux
  • Loading branch information
ultrabug authored Jul 5, 2018
2 parents 15b6ebf + 0bfe17a commit 4a11583
Showing 1 changed file with 106 additions and 39 deletions.
145 changes: 106 additions & 39 deletions py3status/modules/file_status.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
# -*- coding: utf-8 -*-
"""
Display if a file or directory exists.
Display if files or directories exists.
Configuration parameters:
cache_timeout: how often to run the check (default 10)
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)
Color options:
color_bad: Error or file/directory does not exist
color_good: File or directory exists
cache_timeout: refresh interval for this module (default 10)
format: display format for this module
(default '\?color=paths [\?if=paths ●|■]')
format_path: format for paths (default '{basename}')
format_path_separator: show separator if more than one (default ' ')
path: specify a string or a list of paths to check (default None)
thresholds: specify color thresholds to use
(default [(0, 'bad'), (1, 'good')])
Format placeholders:
{icon} icon for the current availability
{format_path} format for paths
{paths} number of paths, eg 1, 2, 3
@author obb, Moritz Lüdecke
format_path placeholders:
{basename} basename of pathname
{pathname} pathname
Color options:
color_bad: files or directories does not exist
color_good: files or directories exists
Color thresholds:
format:
paths: print a color based on the number of paths
Examples:
```
# add multiple paths with wildcard or with pathnames
file_status {
path = ['/tmp/test*', '~user/test1', '~/Videos/*.mp4']
}
# colorize basenames
file_status {
path = ['~/.config/i3/modules/*.py']
format = '{format_path}'
format_path = '\?color=good {basename}'
format_path_separator = ', '
}
```
@author obb, Moritz Lüdecke, Cyril Levis (@cyrinux)
SAMPLE OUTPUT
{'color': '#00FF00', 'full_text': u'\u25cf'}
Expand All @@ -25,20 +53,22 @@
{'color': '#FF0000', 'full_text': u'\u25a0'}
"""

from os.path import expanduser, exists
from glob import glob
from os.path import basename, expanduser

ERR_NO_PATH = 'no path given'
STRING_NO_PATH = 'missing path'


class Py3status:
"""
"""
# available configuration parameters
cache_timeout = 10
format = '{icon}'
icon_available = u''
icon_unavailable = u''
format = u'\?color=paths [\?if=paths \u25cf|\u25a0]'
format_path = u'{basename}'
format_path_separator = u' '
path = None
thresholds = [(0, 'bad'), (1, 'good')]

class Meta:
deprecated = {
Expand All @@ -57,32 +87,69 @@ class Meta:
}

def post_config_hook(self):
if self.path:
self.path = expanduser(self.path)
if not self.path:
raise Exception(STRING_NO_PATH)

# icon deprecation
on = getattr(self, 'icon_available', u'\u25cf')
off = getattr(self, 'icon_unavailable', u'\u25a0')
new_icon = u'\?color=paths [\?if=paths {}|{}]'.format(on, off)
self.format = self.format.replace('{icon}', new_icon)

# convert str to list + expand path
if not isinstance(self.path, list):
self.path = [self.path]
self.path = list(map(expanduser, self.path))

self.init = {'format_path': []}
if self.py3.format_contains(self.format, 'format_path'):
self.init['format_path'] = self.py3.get_placeholders_list(
self.format_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,
}

if exists(self.path):
icon = self.icon_available
color = self.py3.COLOR_GOOD
else:
icon = self.icon_unavailable
color = self.py3.COLOR_BAD

response = {
'cached_until': self.py3.time_in(self.cache_timeout),
'full_text': self.py3.safe_format(self.format, {'icon': icon}),
'color': color
# init datas
paths = sorted([files for path in self.path for files in glob(path)])
count_path = len(paths)
format_path = None

# format paths
if self.init['format_path']:
new_data = []
format_path_separator = self.py3.safe_format(
self.format_path_separator
)

for pathname in paths:
path = {}
for key in self.init['format_path']:
if key == 'basename':
value = basename(pathname)
elif key == 'pathname':
value = pathname
else:
continue
path[key] = self.py3.safe_format(value)
new_data.append(self.py3.safe_format(self.format_path, path))

format_path = self.py3.composite_join(
format_path_separator, new_data
)

if self.thresholds:
self.py3.threshold_get_color(count_path, 'paths')

return {
'cached_until':
self.py3.time_in(self.cache_timeout),
'full_text': self.py3.safe_format(
self.format, {
'paths': count_path,
'format_path': format_path
}
)
}

return response


if __name__ == "__main__":
"""
Expand Down

0 comments on commit 4a11583

Please sign in to comment.