From 852c09a1854cc37fffedbeade7c8c519a52d01b4 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Mon, 15 Apr 2024 16:16:58 +0200 Subject: [PATCH] Adding method check_output_file() to class BaseDPXApplication --- lib/pp_admintools/app/__init__.py | 47 ++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/pp_admintools/app/__init__.py b/lib/pp_admintools/app/__init__.py index 5412f95..8a71eed 100644 --- a/lib/pp_admintools/app/__init__.py +++ b/lib/pp_admintools/app/__init__.py @@ -10,12 +10,14 @@ from __future__ import absolute_import # Standard modules import logging +import os import re import shutil import sys import termios from collections import namedtuple from io import UnsupportedOperation +from pathlib import Path # Third party modules from fb_tools.cfg_app import FbConfigApplication @@ -25,6 +27,7 @@ from fb_tools.multi_config import BaseMultiConfig from .. import DEFAULT_CONFIG_DIR from .. import DEFAULT_TERMINAL_HEIGHT, DEFAULT_TERMINAL_WIDTH from .. import __version__ as GLOBAL_VERSION +from ..errors import DpxFileError from ..xlate import DOMAIN, LOCALE_DIR, XLATOR from ..xlate import __base_dir__ as __xlate_base_dir__ from ..xlate import __lib_dir__ as __xlate_lib_dir__ @@ -36,7 +39,7 @@ LOG = logging.getLogger(__name__) _ = XLATOR.gettext ngettext = XLATOR.ngettext -__version__ = '0.9.3' +__version__ = '0.10.0' # ============================================================================= @@ -211,6 +214,48 @@ class BaseDPXApplication(FbConfigApplication): print('.', end='', flush=True) self.cur_dots += 1 + # ------------------------------------------------------------------------- + def check_output_file(self, ofile, must_exists=False, must_absolute=False): + """Check all nessacary thinks about an output file, before it can be used.""" + if ofile is None: + msg = _('The output file to chack must not be None.') + raise TypeError(msg) + + ofile = Path(ofile) + if must_absolute: + msg = _('The path {!r} must be an absolute path.').format(ofile) + raise DpxFileError(msg) + + if ofile.exists(): + + if not ofile.is_file(): + msg = _('The given path {!r} exists, but is not a regular file.').format( + str(ofile)) + raise DpxFileError(msg) + + if not os.access(str(ofile), os.R_OK): + msg = _('The given file {!r} is not readable.').format(str(ofile)) + raise DpxFileError(msg) + + if not os.access(str(ofile), os.W_OK): + msg = _('The given file {!r} is not writeable.').format(str(ofile)) + raise DpxFileError(msg) + + return + + parent_dir = ofile.parent + if not parent_dir.exists(): + msg = _('Directory {!r} does not exists.').format(str(parent_dir)) + raise DpxFileError(msg) + + if not parent_dir.is_dir(): + msg = _('Path {!r} exists, bu is not a directory.').format(str(parent_dir)) + raise DpxFileError(msg) + + if not os.access(str(parent_dir), os.W_OK): + msg = _('The directory {!r} is not writeable.').format(str(parent_dir)) + raise DpxFileError(msg) + # ============================================================================= -- 2.39.5