--- /dev/null
+#!/usr/bin/env python
+# vim: set ts=4 sts=4 sw=4 textwidth=112:
+
+class RarslaveMessage (object):
+ def __init__ (self, msg, printquiet):
+ self.msg = msg
+ self.printquiet = printquiet
+
+ def __repr__ (self):
+ return "%s" % self.msg
+
+ def isVerbose (self):
+ # A message is verbose only if we should
+ # print it when we are not printing quietly
+ return not self.printquiet
+
+class RarslaveLogger (object):
+ # This class will log multiple messages, one for each set that is operated on.
+ # It will then be able to print all of them out.
+
+ def __init__ (self):
+ self.__uniq_num = 0
+ self.__msg_dict = {}
+
+ def getUniqueID (self):
+ # Generate a unique id. This implementation is pretty simple,
+ # just generating an increasing set of integers.
+
+ # This function also sets up the dictionary which will hold the messages.
+ self.__uniq_num += 1
+ self.__msg_dict[self.__uniq_num] = []
+
+ return self.__uniq_num
+
+ def isValidID (self, id):
+ # Check if the id given is a valid one. This is done by checking if
+ # the id is in the dictionary.
+ return id in self.__msg_dict.keys()
+
+ def addMessage (self, id, msg, printquiet=True):
+ # Add a message to the dictionary, to be printed later.
+ # The printquiet variable controls whether the message will be printed
+ # normally, or only in verbose mode. The default is to print always.
+
+ # Check the id
+ assert self.isValidID (id)
+
+ # Get the list from the dictionary, then add the message to it
+ self.__msg_dict[id].append (RarslaveMessage (msg, printquiet))
+
+ def printAllMessages (self, verbose=False):
+ # Print all messages in groups by id
+ for k in self.__msg_dict.keys():
+ for msg in self.__msg_dict[k]:
+
+ # Skip verbose messages if we're not in verbose mode
+ if msg.isVerbose() and not verbose:
+ continue
+
+ print msg
+
+ print
+
+def main ():
+ pass
+
+if __name__ == '__main__':
+ main ()
+
import os, sys, unittest
from rarslave import *
+from RarslaveLogger import RarslaveLogger
+from RarslaveLogger import RarslaveMessage
class rarslavetest (unittest.TestCase):
return True
def setUp (self):
- pass
+ self.logger = RarslaveLogger ()
def tearDown (self):
- pass
+ self.logger = None
def testGetBasenameNone (self):
QNAME = '[something] some names.txt'
self.assertListEquals (find_deleteable_files (FILESN + FILESY), FILESY)
+ ### RarslaveMessage tests
+
+ def testRepr (self):
+ STR1 = "Hello World"
+ STR2 = "Goodbye, \nCruel World"
+
+ self.assertEquals (STR1, RarslaveMessage (STR1, True ).__repr__())
+ self.assertEquals (STR1, RarslaveMessage (STR1, False).__repr__())
+ self.assertEquals (STR2, RarslaveMessage (STR2, True ).__repr__())
+ self.assertEquals (STR2, RarslaveMessage (STR2, False).__repr__())
+
+ def testisVerboseMessage (self):
+ STR1 = "Hello World"
+ STR2 = "Goodbye, \nCruel World"
+
+ self.assertTrue (RarslaveMessage (STR1, False).isVerbose())
+ self.assertTrue (RarslaveMessage (STR2, False).isVerbose())
+ self.assertFalse (RarslaveMessage (STR1, True).isVerbose())
+ self.assertFalse (RarslaveMessage (STR2, True).isVerbose())
+
+ ### RarslaveLogger tests
+
+ def testgetUniqueID (self):
+ generated = []
+
+ for i in xrange(100):
+ id = self.logger.getUniqueID ()
+ if id in generated:
+ self.fail ("Already generated ID was generated again")
+
+ generated.append (id)
+
+ def testisValidID (self):
+ id1 = self.logger.getUniqueID ()
+ id2 = self.logger.getUniqueID ()
+
+ self.assertFalse (self.logger.isValidID (-100))
+ self.assertFalse (self.logger.isValidID (1000))
+
+ self.assertTrue (self.logger.isValidID (id1))
+ self.assertTrue (self.logger.isValidID (id2))
+
if __name__ == '__main__':
unittest.main ()