Commits

jordilin committed f59c0eb

executor notification provided. Added license in some test files

Comments (0)

Files changed (9)

             help="displays a mark in bottom right corner of terminal")
     parser.add_option("--no-mail-silence", action="store_true", 
             dest="nomailsilence", help="silent mode but no specific notification")
+    parser.add_option("--executable", action="store_true", 
+            dest="executable", help="executes a program")
+
  
     (options,args) = parser.parse_args()
     # defaults 
     if options.cornermark:
         cornermark = notifications.CornerMark(options.cornermark)
         actions.append(cornermark)
+
+    if options.executable and properties:
+        executor = notifications.Executor(properties)
+        actions.append(executor)
     
     if options.remote:
         from log4tailer import SSHLogTailer

log4tailer/notifications.py

                 self.count = 0
                 self.flagged = False
             
+class Executor(object):
+    """Will execute a program if a certain condition is given"""
+    PlaceHolders = '%s'
+
+    def __init__(self, properties):
+        executable = properties.getValue('executor')
+        if not executable:
+            raise Exception("need to provide executor option")
+        self.executable = executable.split(' ')
+        self.full_trigger_active = False
+        if self.PlaceHolders in self.executable:
+            self.full_trigger_active = True
+
+    def __build_trigger(self, logtrace, logpath):
+        trigger = ' '.join(self.executable)
+        if self.full_trigger_active:
+            trigger = ' '.join(self.executable) % (logtrace, logpath)
+        return trigger
+
+    def notify(self, message, log):
+        logtrace, logpath = message.getPlainMessage()
+        trigger = self.__build_trigger(logtrace, logpath)
+        print trigger
+        os.system(trigger)
+        

test/test_colors.py

+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2010 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
+
 import unittest
 import os,sys
 sys.path.append('..')
 from log4tailer.Log import Log
 from log4tailer.Message import Message
-from log4tailer.Properties import Property
 from log4tailer.LogColors import LogColors
-#from log4tailer.Actions.PrintAction import PrintAction
 from log4tailer import notifications
 from log4tailer.TermColorCodes import TermColorCodes
 

test/test_executor.py

+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2010 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
+
+import unittest
+import sys
+import mocker
+sys.path.append('..')
+from log4tailer import notifications
+from log4tailer.Properties import Property
+from log4tailer.Message import Message
+from log4tailer.LogColors import LogColors
+from log4tailer.Log import Log
+
+CONFIG = 'aconfig.txt'
+
+class TestExecutor(unittest.TestCase):
+    def setUp(self):
+        self.mocker = mocker.Mocker()
+    
+    def testShouldReadExecutorFromConfigFile(self):
+        fh = open(CONFIG, 'w')
+        fh.write('executor = ls -l\n')
+        fh.close()
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        self.assertEquals(['ls', '-l'], executor.executable)
+
+    def testShouldRaiseIfExecutorNotProvided(self):
+        fh = open(CONFIG, 'w')
+        fh.write('anything = ls -l\n')
+        fh.close()
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        self.assertRaises(Exception, notifications.Executor, properties)
+
+    def testShouldProvideNotifyMethod(self):
+        fh = open(CONFIG, 'w')
+        fh.write('executor = ls -l\n')
+        fh.close()
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        self.assertTrue(hasattr(executor, 'notify'))
+
+    def testFullTriggerTrueTwoPlaceHoldersBasedOnConfig(self):
+        fh = open(CONFIG, 'w')
+        fh.write('executor = ls -l %s %s\n')
+        fh.close()
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        self.assertEqual(True, executor.full_trigger_active)
+
+    def testFullTriggerFalseBasedOnConfig(self):
+        fh = open(CONFIG, 'w')
+        fh.write('executor = ls -l\n')
+        fh.close()
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        self.assertEqual(False, executor.full_trigger_active)
+
+    def testShouldNotifyWithFullTrigger(self):
+        logcolor = LogColors()
+        message = Message(logcolor)
+        log = Log('anylog')
+        fh = open(CONFIG, 'w')
+        fh.write('executor = ls -l %s %s\n')
+        fh.close()
+        trigger = "ls -l this is a log trace anylog"
+        trace = "this is a log trace"
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        os_mock = self.mocker.replace('os')
+        os_mock.system(trigger)
+        self.mocker.result(True)
+        self.mocker.replay()
+        message.parse(trace, log)
+        executor.notify(message, log)
+    
+    def testShouldNotifyWithNoFullTrigger(self):
+        logcolor = LogColors()
+        message = Message(logcolor)
+        log = Log('anylog')
+        fh = open(CONFIG, 'w')
+        fh.write('executor = echo\n')
+        fh.close()
+        trace = "this is a log trace"
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        message.parse(trace, log)
+        executor.notify(message, log)
+
+    def testShouldNotifyAndContinueIfExecutorFails(self):
+        logcolor = LogColors()
+        message = Message(logcolor)
+        log = Log('anylog')
+        fh = open(CONFIG, 'w')
+        fh.write('executor = anycommand\n')
+        fh.close()
+        trace = "this is a log trace"
+        properties = Property(CONFIG)
+        properties.parseProperties()
+        executor = notifications.Executor(properties)
+        message.parse(trace, log)
+        executor.notify(message, log)
+        
+    def tearDown(self):
+        self.mocker.restore()
+        self.mocker.verify()
+
+if __name__ == '__main__':
+    unittest.main()
+

test/test_filternotifier.py

-#!/usr/bin/env python
+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2010 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
 
 import unittest
 import sys

test/test_inactivityAction.py

 # Log4Tailer: A multicolored python tailer for log4J logs
-# Copyright (C) 2008 Jordi Carrillo Bosch
+# Copyright (C) 2010 Jordi Carrillo Bosch
 
 # This file is part of Log4Tailer Project.
 #

test/test_properties.py

+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2010 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
+
+
 import unittest
 import os,sys
 

test/test_resume.py

+# Log4Tailer: A multicolored python tailer for log4J logs
+# Copyright (C) 2010 Jordi Carrillo Bosch
+
+# This file is part of Log4Tailer Project.
+#
+# Log4Tailer is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Log4Tailer 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 General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Log4Tailer.  If not, see <http://www.gnu.org/licenses/>.
+
+
 import unittest
 import os,sys
 import re

test/test_sshTailer.py

 # Log4Tailer: A multicolored python tailer for log4J logs
-# Copyright (C) 2008 Jordi Carrillo Bosch
+# Copyright (C) 2010 Jordi Carrillo Bosch
 
 # This file is part of Log4Tailer Project.
 #