tiramisu/tiramisu/option/filenameoption.py

86 lines
3 KiB
Python

# -*- coding: utf-8 -*-
# Copyright (C) 2017-2024 Team tiramisu (see AUTHORS for all contributors)
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# The original `Config` design model is unproudly borrowed from
# the rough pypy's guys: http://codespeak.net/svn/pypy/dist/pypy/config/
# the whole pypy projet is under MIT licence
# ____________________________________________________________
"""FilenameOption
"""
from pathlib import Path
from ..i18n import _
from ..error import display_list
from .stroption import StrOption
class FilenameOption(StrOption):
"""validate file or directory name"""
__slots__ = tuple()
_type = "file name"
_t_type = _("file name")
def __init__(
self,
name: str,
*args,
allow_relative=False,
test_existence=False,
types=["file", "directory"],
**kwargs,
):
if not isinstance(types, list):
raise ValueError(
_('types parameter must be a list, not "{0}" for "{1}"').format(
types, name
)
)
for typ in types:
if typ not in ["file", "directory"]:
raise ValueError(f'unknown type "{typ}" for "{name}"')
extra = {
"allow_relative": allow_relative,
"test_existence": test_existence,
"types": types,
}
super().__init__(name, *args, extra=extra, **kwargs)
def validate(
self,
value: str,
) -> None:
super().validate(value)
if not self.impl_get_extra("allow_relative") and not value.startswith("/"):
raise ValueError(_('must starts with "/"'))
if value is not None and self.impl_get_extra("test_existence"):
types = self.impl_get_extra("types")
file = Path(value)
found = False
if "file" in types and file.is_file():
found = True
if not found and "directory" in types and file.is_dir():
found = True
if not found:
translated_types = [
{"file": _("file"), "directory": _("directory")}.get(typ)
for typ in types
]
raise ValueError(
_("cannot find this {0}").format(
display_list(translated_types, separator="or"), value
)
)