]> Frank Brehm's Git Trees - my-stuff/py-logrotate.git/commitdiff
Mit LogRotateHandler begonnen
authorFrank Brehm <frank@brehm-online.com>
Tue, 19 Apr 2011 12:23:47 +0000 (12:23 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 19 Apr 2011 12:23:47 +0000 (12:23 +0000)
git-svn-id: http://svn.brehm-online.com/svn/my-stuff/python/PyLogrotate/trunk@205 ec8d2aa5-1599-4edb-8739-2b3a1bc399aa

LogRotateHandler.py [new file with mode: 0755]
logrotate.py

diff --git a/LogRotateHandler.py b/LogRotateHandler.py
new file mode 100755 (executable)
index 0000000..4638fc1
--- /dev/null
@@ -0,0 +1,200 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# $Id$
+# $URL$
+
+'''
+@author: Frank Brehm
+@contact: frank@brehm-online.com
+@license: GPL3
+@copyright: (c) 2010-2011 by Frank Brehm, Berlin
+@version: 0.0.1
+@summary: Application handler module for Python logrotating
+'''
+
+# Für Terminal-Dinge: http://code.activestate.com/recipes/475116/
+
+import re
+import sys
+import gettext
+import logging
+import pprint
+
+revision = '$Revision$'
+revision = re.sub( r'\$', '', revision )
+revision = re.sub( r'Revision: ', r'r', revision )
+revision = re.sub( r'\s*$', '', revision )
+
+__author__    = 'Frank Brehm'
+__copyright__ = '(C) 2011 by Frank Brehm, Berlin'
+__contact__    = 'frank@brehm-online.com'
+__version__    = '0.0.1 ' + revision
+__license__    = 'GPL3'
+
+
+#========================================================================
+
+class LogrotateHandlerError(Exception):
+    '''
+    Base class for exceptions in this module.
+    '''
+
+#========================================================================
+
+class LogrotateHandler(object):
+    '''
+    Class for application handler for Python logrotating
+
+    @author: Frank Brehm
+    @contact: frank@brehm-online.com
+    '''
+
+    #-------------------------------------------------------
+    def __init__( self, config_file,
+                        test       = False,
+                        verbose    = 0,
+                        force      = False,
+                        state_file = None,
+                        mail_cmd   = None,
+                        local_dir  = None,
+    ):
+        '''
+        Costructor.
+
+        @param config_file: the configuration file to use
+        @type config_file:  str
+        @param prog:        testmode, no real actions are made
+        @type prog:         bool
+        @param verbose:     verbosity (debug) level
+        @type verbose:      int
+        @param force:       Force file rotation
+        @type force:        bool
+        @param state_file:  Path of state file (different to configuration)
+        @type state_file:   str or None
+        @param mail_cmd:    command to send mail (instead of using
+                            the Phyton email package)
+        @type mail_cmd:     str or None
+        @param local_dir:   The directory, where the i18n-files (*.mo)
+                            are located. If None, then system default
+                            (/usr/share/locale) is used.
+        @type local_dir:    str or None
+
+        @return: None
+        '''
+
+        self.local_dir = local_dir
+        '''
+        @ivar: The directory, where the i18n-files (*.mo) are located.
+        @type: str or None
+        '''
+
+        self.t = gettext.translation(
+            'LogRotateHandler',
+            local_dir,
+            fallback = True
+        )
+        '''
+        @ivar: a gettext translation object
+        @type: gettext.translation
+        '''
+
+        _ = self.t.lgettext
+
+        self.verbose = verbose
+        '''
+        @ivar: verbosity level (0 - 9)
+        @type: int
+        '''
+
+        self.test = test
+        '''
+        @ivar: testmode, no real actions are made
+        @type: bool
+        '''
+
+        self.force = force
+        '''
+        @ivar: Force file rotation
+        @type: bool
+        '''
+
+        self.state_file = state_file
+        '''
+        @ivar: Path of state file (from commandline or from configuration)
+        @type: str
+        '''
+
+        self.mail_cmd = mail_cmd
+        '''
+        @ivar: command to send mail (instead of using the Phyton email package)
+        @type: str or None
+        '''
+
+        self.config_file = config_file
+        '''
+        @ivar: the initial configuration file to use
+        @type: str
+        '''
+
+        #################################################
+        # Create a logger object
+        self.logger = logging.getLogger('pylogrotate')
+        '''
+        @ivar: logger object
+        @type: logging.getLogger
+        '''
+
+        self.logger.setLevel(logging.DEBUG)
+
+        pp = pprint.PrettyPrinter(indent=4)
+        # create console handler and set level to debug
+        ch = logging.StreamHandler()
+        #ch.setLevel(logging.DEBUG)
+        if verbose:
+            ch.setLevel(logging.DEBUG)
+        else:
+            ch.setLevel(logging.INFO)
+
+        # create formatter
+        formatter = logging.Formatter('[%(asctime)s]: %(name)s %(levelname)-8s - %(message)s')
+
+        # add formatter to ch
+        ch.setFormatter(formatter)
+
+        # add ch to logger
+        self.logger.addHandler(ch)
+
+        self.logger.debug( _("Logrotating initialised") )
+
+    #------------------------------------------------------------
+    def __str__(self):
+        '''
+        Typecasting function for translating object structure
+        into a string
+
+        @return: structure as string
+        @rtype:  str
+        '''
+
+        pp = pprint.PrettyPrinter(indent=4)
+        structure = {
+            'config_file': self.config_file,
+            'force':       self.force,
+            'local_dir':   self.local_dir,
+            'mail_cmd':    self.mail_cmd,
+            'state_file':  self.state_file,
+            'test':        self.test,
+            'verbose':     self.verbose,
+        }
+        return pp.pformat(structure)
+
+#========================================================================
+
+if __name__ == "__main__":
+    pass
+
+
+#========================================================================
+
+# vim: fileencoding=utf-8 filetype=python ts=4 expandtab
index 7f6387df58a7576bb7789972fcc0791f30fb4ee4..de434588b609d697458946e922776f928e404268 100755 (executable)
@@ -22,6 +22,9 @@ import os.path
 from LogRotateGetopts import LogrotateOptParser
 from LogRotateGetopts import LogrotateOptParserError
 
+from LogRotateHandler import LogrotateHandler
+from LogRotateHandler import LogrotateHandlerError
+
 revision = '$Revision$'
 revision = re.sub( r'\$', '', revision )
 revision = re.sub( r'Revision: ', r'r', revision )
@@ -29,7 +32,7 @@ revision = re.sub( r'Revision: ', r'r', revision )
 __author__    = 'Frank Brehm'
 __copyright__ = '(C) 2011 by Frank Brehm, Berlin'
 __contact__    = 'frank@brehm-online.com'
-__version__    = '0.2.0 ' + revision
+__version__    = '0.2.1 ' + revision
 __license__    = 'GPL3'
 
 
@@ -60,9 +63,33 @@ def main():
         opt_parser.parser.print_help(sys.stderr)
         sys.exit(1)
 
-    print _("Options") + ": " + pp.pformat(opt_parser.options)
-    print _("Arguments") + ": " + pp.pformat(opt_parser.args)
+    if opt_parser.options.verbose > 2:
+        print _("Options") + ": " + pp.pformat(opt_parser.options)
+        print _("Arguments") + ": " + pp.pformat(opt_parser.args)
+
+    testmode = False
+    if opt_parser.options.test or opt_parser.options.configcheck:
+        testmode = True
+
+    verbose_level = opt_parser.options.verbose
+
+    if opt_parser.options.debug:
+        testmode = True
+        if verbose_level < 1:
+            verbose_level = 1
+
+    lr_handler = LogrotateHandler(
+        opt_parser.args[0],
+        test       = testmode,
+        verbose    = verbose_level,
+        force      = opt_parser.options.force,
+        state_file = opt_parser.options.statefile,
+        mail_cmd   = opt_parser.options.mailcmd,
+        local_dir  = local_dir,
+    )
 
+    if opt_parser.options.verbose > 2:
+        print _("Handler object structure") + ': ' + str(lr_handler)
 
 #========================================================================