From: Frank Brehm Date: Mon, 29 Nov 2021 10:24:12 +0000 (+0100) Subject: Adding translation markers to lib/pp_admintools/pidfile.py X-Git-Tag: 0.4.1~5^2~18^2~7 X-Git-Url: https://git.uhu-banane.org/?a=commitdiff_plain;h=f0010ee6c5a8af3100721add321c9f8b531020bc;p=pixelpark%2Fpp-admin-tools.git Adding translation markers to lib/pp_admintools/pidfile.py --- diff --git a/lib/pp_admintools/pidfile.py b/lib/pp_admintools/pidfile.py index c21a088..99ab05b 100644 --- a/lib/pp_admintools/pidfile.py +++ b/lib/pp_admintools/pidfile.py @@ -3,7 +3,7 @@ """ @author: Frank Brehm @contact: frank.brehm@pixelpark.com -@copyright: © 2018 by Frank Brehm, Berlin +@copyright: © 2021 by Frank Brehm, Berlin @summary: A module for a pidfile object. It provides methods to define, check, create and remove a pidfile. @@ -28,10 +28,15 @@ from fb_tools.errors import ReadTimeoutError from fb_tools.common import to_utf8 from fb_tools.obj import FbBaseObjectError, FbBaseObject -__version__ = '0.3.0' +from .xlate import XLATOR + +__version__ = '0.3.1' LOG = logging.getLogger(__name__) +_ = XLATOR.gettext + + # ============================================================================= class PidFileError(FbBaseObjectError): """Base error class for all exceptions happened during @@ -64,9 +69,10 @@ class InvalidPidFileError(PidFileError): msg = None if self.reason: - msg = "Invalid pidfile {!r} given: {}".format(self.pidfile, self.reason) + msg = _("Invalid pidfile {f!r} given: {r}").format( + f=self.pidfile, r=self.reason) else: - msg = "Invalid pidfile {!r} given.".format(self.pidfile) + msg = _("Invalid pidfile {!r} given.").format(self.pidfile) return msg @@ -95,8 +101,9 @@ class PidFileInUseError(PidFileError): def __str__(self): """Typecasting into a string for error output.""" - msg = "The pidfile {!r} is currently in use by the application with the PID {}.".format( - self.pidfile, self.pid) + msg = _( + "The pidfile {f!r} is currently in use by the application with the PID {p}.").format( + f=self.pidfile, p=self.pid) return msg @@ -164,7 +171,8 @@ class PidFile(FbBaseObject): ) if not filename: - raise ValueError('No filename given on initializing PidFile object.') + raise ValueError(_( + 'No filename given on initializing {} object.').format('PidFile')) self._filename = os.path.abspath(str(filename)) """ @@ -281,24 +289,24 @@ class PidFile(FbBaseObject): if not os.path.exists(self.filename): if self.verbose > 3: - LOG.debug("Pidfile {!r} doesn't exists, not removing.".format(self.filename)) + LOG.debug(_("Pidfile {!r} doesn't exists, not removing.").format(self.filename)) return if not self.auto_remove: if self.verbose > 3: - LOG.debug("Auto removing disabled, don't deleting {!r}.".format(self.filename)) + LOG.debug(_("Auto removing disabled, don't deleting {!r}.").format(self.filename)) return if self.verbose > 1: - LOG.debug("Removing pidfile {!r} ...".format(self.filename)) + LOG.debug(_("Removing pidfile {!r} ...").format(self.filename)) if self.simulate: if self.verbose > 1: - LOG.debug("Just kidding ..") + LOG.debug(_("Just kidding ..")) return try: os.remove(self.filename) except OSError as e: - LOG.err("Could not delete pidfile {!r}: {}".format(self.filename, e)) + LOG.err(_("Could not delete pidfile {f!r}: {e}").format(f=self.filename, e=e)) except Exception as e: self.handle_error(str(e), e.__class__.__name__, True) @@ -317,16 +325,17 @@ class PidFile(FbBaseObject): if pid: pid = int(pid) if pid <= 0: - msg = "Invalid PID {} for creating pidfile {!r} given.".format(pid, self.filename) + msg = _("Invalid PID {p} for creating pidfile {f!r} given.").format( + p=pid, f=self.filename) raise PidFileError(msg) else: pid = os.getpid() if self.check(): - LOG.info("Deleting pidfile {!r} ...".format(self.filename)) + LOG.info(_("Deleting pidfile {!r} ...").format(self.filename)) if self.simulate: - LOG.debug("Just kidding ..") + LOG.debug(_("Just kidding ..")) else: try: os.remove(self.filename) @@ -334,10 +343,10 @@ class PidFile(FbBaseObject): raise InvalidPidFileError(self.filename, str(e)) if self.verbose > 1: - LOG.debug("Trying opening {!r} exclusive ...".format(self.filename)) + LOG.debug(_("Trying opening {!r} exclusive ...").format(self.filename)) if self.simulate: - LOG.debug("Simulation mode - don't real writing in {!r}.".format(self.filename)) + LOG.debug(_("Simulation mode - don't real writing in {!r}.").format(self.filename)) self._created = True return @@ -347,11 +356,11 @@ class PidFile(FbBaseObject): self.filename, os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o644) except OSError as e: error_tuple = sys.exc_info() - msg = "Error on creating pidfile {!r}: {}".format(self.filename, e) + msg = _("Error on creating pidfile {f!r}: {e}").format(f=self.filename, e=e) reraise(PidFileError, msg, error_tuple[2]) if self.verbose > 2: - LOG.debug("Writing {} into {!r} ...".format(pid, self.filename)) + LOG.debug(_("Writing {p} into {f!r} ...").format(p=pid, f=self.filename)) out = to_utf8("%d\n" % (pid)) try: @@ -373,22 +382,23 @@ class PidFile(FbBaseObject): """ if not self.created: - msg = "Calling recreate() on a not self created pidfile." + msg = _("Calling {} on a not self created pidfile.").format('recreate()') raise PidFileError(msg) if pid: pid = int(pid) if pid <= 0: - msg = "Invalid PID {} for creating pidfile {!r} given.".format(pid, self.filename) + msg = "Invalid PID {p} for creating pidfile {f!r} given.".format( + p=pid, f=self.filename) raise PidFileError(msg) else: pid = os.getpid() if self.verbose > 1: - LOG.debug("Trying opening {!r} for recreate ...".format(self.filename)) + LOG.debug(_("Trying opening {!r} for recreate ...").format(self.filename)) if self.simulate: - LOG.debug("Simulation mode - don't real writing in {!r}.".format(self.filename)) + LOG.debug(_("Simulation mode - don't real writing in {!r}.").format(self.filename)) return fh = None @@ -396,11 +406,11 @@ class PidFile(FbBaseObject): fh = open(self.filename, 'w', **self.open_args) except OSError as e: error_tuple = sys.exc_info() - msg = "Error on recreating pidfile {!r}: {}".format(self.filename, e) + msg = _("Error on recreating pidfile {f!r}: {e}").format(f=self.filename, e=e) reraise(PidFileError, msg, error_tuple[2]) if self.verbose > 2: - LOG.debug("Writing {} into {!r} ...".format(pid, self.filename)) + LOG.debug(_("Writing {p} into {f!r} ...").format(p=pid, f=self.filename)) try: fh.write("%d\n" % (pid)) @@ -429,22 +439,22 @@ class PidFile(FbBaseObject): if not os.path.exists(self.filename): if not os.path.exists(self.parent_dir): - reason = "Pidfile parent directory {!r} doesn't exists.".format( + reason = _("Pidfile parent directory {!r} doesn't exists.").format( self.parent_dir) raise InvalidPidFileError(self.filename, reason) if not os.path.isdir(self.parent_dir): - reason = "Pidfile parent directory {!r} is not a directory.".format( + reason = _("Pidfile parent directory {!r} is not a directory.").format( self.parent_dir) raise InvalidPidFileError(self.filename, reason) if not os.access(self.parent_dir, os.X_OK): - reason = "No write access to pidfile parent directory {!r}.".format( + reason = _("No write access to pidfile parent directory {!r}.").format( self.parent_dir) raise InvalidPidFileError(self.filename, reason) return False if not os.path.isfile(self.filename): - reason = "It is not a regular file." + reason = _("It is not a regular file.") raise InvalidPidFileError(self.filename, self.parent_dir) # --------- @@ -461,7 +471,7 @@ class PidFile(FbBaseObject): return ReadTimeoutError(self.timeout, self.filename) if self.verbose > 1: - LOG.debug("Reading content of pidfile {!r} ...".format(self.filename)) + LOG.debug(_("Reading content of pidfile {!r} ...").format(self.filename)) signal.signal(signal.SIGALRM, pidfile_read_alarm_caller) signal.alarm(self.timeout) @@ -486,25 +496,26 @@ class PidFile(FbBaseObject): if match: pid = int(match.group(1)) else: - msg = "No useful information found in pidfile {!r}: {!r}".format(self.filename, line) + msg = _("No useful information found in pidfile {f!r}: {z!r}").format( + f=self.filename, z=line) return True if self.verbose > 1: - LOG.debug("Trying check for process with PID {} ...".format(pid)) + LOG.debug(_("Trying check for process with PID {} ...").format(pid)) try: os.kill(pid, 0) except OSError as err: if err.errno == errno.ESRCH: - LOG.info("Process with PID {} anonymous died.".format(pid)) + LOG.info(_("Process with PID {} anonymous died.").format(pid)) return True elif err.errno == errno.EPERM: error_tuple = sys.exc_info() - msg = "No permission to signal the process {} ...".format(pid) + msg = _("No permission to signal the process {} ...").format(pid) reraise(PidFileError, msg, error_tuple[2]) else: error_tuple = sys.exc_info() - msg = "Got a {}: {}.".format(err.__class__.__name__, err) + msg = _("Got a {c}: {e}.").format(err.__class__.__name__, err) reraise(PidFileError, msg, error_tuple[2]) else: raise PidFileInUseError(self.filename, pid)