<commit_before># -*- coding: utf-8 -*-
"""
Display if a file or directory exists.

Configuration parameters:
    cache_timeout: how often to run the check (default 10)
    format: format of the output. (default '{icon} {format_path}')
    format_path: format of the path output. (default '{basename}')
    format_path_separator: show separator if more than one (default ' ')
    icon_available: icon to display when available (default '●')
    icon_unavailable: icon to display when unavailable (default '■')
    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
    color_good: File or directory exists

Format placeholders:
    {format_path} paths of matching files
    {icon} icon for the current availability

format_path path placeholders:
    {basename} basename of matching files
    {fullpath} fullpath of matching files

Examples:
```
# check files with wildcard, or contain user path, full paths
file_status {
    path = ['/tmp/test*', '~user/test1']
    format_path = '{fullpath}'
}
```

@author obb, <NAME>, <NAME> (@cyrinux)

SAMPLE OUTPUT
{'color': '#00FF00', 'full_text': u'\u25cf test.py'}

missing
{'color': '#FF0000', 'full_text': u'\u25a0'}
"""

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

ERR_NO_PATH = 'no path given'


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    format = u'{icon} {format_path}'
    format_path = u'{basename}'
    format_path_separator = u' '
    icon_available = u'●'
    icon_unavailable = u'■'
    path = None

    class Meta:
        deprecated = {
            'rename': [
                {
                    'param': 'format_available',
                    'new': 'icon_available',
                    'msg': 'obsolete parameter use `icon_available`'
                },
                {
                    'param': 'format_unavailable',
                    'new': 'icon_unavailable',
                    'msg': 'obsolete parameter use `icon_unavailable`'
                },
            ],
        }

    def post_config_hook(self):
        if self.path:
            # backward compatibility, str to list
            if not isinstance(self.path, list):
                self.path = [self.path]
            # expand user paths
            self.path = list(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,
            }

        # init data
        file_status_data = {}
        # expand glob from paths
        paths = list(map(glob, self.path))
        # merge list of paths
        paths = [x for x in chain.from_iterable(paths)]

        # fill data
        file_status_data['icon'] = self.icon_unavailable
        color = self.py3.COLOR_BAD

        if paths:
            file_status_data['icon'] = self.icon_available
            color = self.py3.COLOR_GOOD

        # format paths
        if self.format_path:
            format_path = {}

            format_path_separator = self.py3.safe_format(
                self.format_path_separator)

            format_path['basename'] = self.py3.composite_join(
                format_path_separator, map(basename, paths))

            format_path['full'] = self.py3.composite_join(
                format_path_separator, paths)

            file_status_data['format_path'] = self.py3.safe_format(
                self.format_path, format_path)

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            'full_text': self.py3.safe_format(self.format, file_status_data),
            'color': color
        }

        return response


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)
<commit_msg>lasers recommandations, propose new format instead of legacy by default<commit_after># -*- coding: utf-8 -*-
"""
Display if a files or directories exists.

Configuration parameters:
    cache_timeout: how often to run the check (default 10)
    format: format of the output. (default '{icon} {format_path}')
    format: format of the output. (default '\?if=files ● {format_path}|■')
    format_path: format of the path output. (default '{basename}')
    format_path_separator: show separator if more than one (default ' ')
    icon_available: icon to display when available (default '●')
    icon_unavailable: icon to display when unavailable (default '■')
    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
    color_good: File or directory exists

Format placeholders:
    {format_path} paths of matching files
    {icon} icon for the current availability

format_path path placeholders:
    {basename} basename of matching files
    {fullpath} fullpath of matching files

Examples:
```
# check files with wildcard, or contain user path, full paths
file_status {
    path = ['/tmp/test*', '~user/test1']
    format = u'\?if=paths ● {format_path}|■ no files found'
    format_path = '{fullpath}'
}
```

@author obb, <NAME>, <NAME> (@cyrinux)

SAMPLE OUTPUT
{'color': '#00FF00', 'full_text': u'\u25cf test.py'}

missing
{'color': '#FF0000', 'full_text': u'\u25a0'}
"""

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

ERR_NO_PATH = 'no path given'


class Py3status:
    """
    """
    # available configuration parameters
    cache_timeout = 10
    format = u'\?if=paths ● {format_path}|■'
    format_path = u'{basename}'
    format_path_separator = u' '
    icon_available = u'●'
    icon_unavailable = u'■'
    path = None

    class Meta:
        deprecated = {
            'rename': [
                {
                    'param': 'format_available',
                    'new': 'icon_available',
                    'msg': 'obsolete parameter use `icon_available`'
                },
                {
                    'param': 'format_unavailable',
                    'new': 'icon_unavailable',
                    'msg': 'obsolete parameter use `icon_unavailable`'
                },
            ],
        }

    def post_config_hook(self):
        self.placeholders = set(
            self.py3.get_placeholders_list(self.format_path))

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

            self.format_path_separator = self.py3.safe_format(
                self.format_path_separator)

    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,
            }

        # init data
        file_status_data = {}
        # expand glob from paths
        paths = list(map(glob, self.path))
        # merge list of paths
        paths = sorted([files for path in self.path for files in glob(path)])
        file_status_data['paths'] = len(paths)

        # fill data, legacy stuff
        file_status_data['icon'] = self.icon_unavailable
        color = self.py3.COLOR_BAD
        if paths:
            file_status_data['icon'] = self.icon_available
            color = self.py3.COLOR_GOOD

        # format paths
        if set(['basename', 'fullname']) & self.placeholders:
            format_path = {}

            if set(['basename']) & self.placeholders:
                format_path['basename'] = map(basename, paths)

            if set(['fullpath']) & self.placeholders:
                format_path['fullpath'] = paths

            for x in self.placeholders:
                format_path[x] = self.py3.composite_join(
                    self.format_path_separator, format_path[x])

            file_status_data['format_path'] = self.py3.safe_format(
                self.format_path, format_path)

        response = {
            'cached_until': self.py3.time_in(self.cache_timeout),
            'full_text': self.py3.safe_format(self.format, file_status_data),
            'color': color
        }

        return response


if __name__ == "__main__":
    """
    Run module in test mode.
    """
    from py3status.module_test import module_test
    module_test(Py3status)

{'fraction_non_alphanumeric': 0.10508786034225336, 'fraction_numerical': 0.007580107959113357, 'mean_word_length': 2.2002940095553107, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3846019', 'n_tokens_mistral': 2593, 'n_tokens_neox': 2486, 'n_words': 697}