Commits

Anonymous committed 0ac245b

Refactoring of DATESET.py. Added test_DATESET.py

Refactored DATESET functions into a single class allowing for cleaner client
code.

  • Participants
  • Parent commits e39a19a

Comments (0)

Files changed (2)

File modules/support/vectorlinux/DATESET.py

 #    You should have received a copy of the GNU General Public License
 #    along with VASM.  If not, see <http://www.gnu.org/licenses/>.
 
+
+
 __author__ = "Moises Henriquez"
 __author_email__ = "moc.liamg@xnl.E0M"[::-1]
 
-""" Support module for the time/date setting vasm plugin """
+
+"""Support module for the time/date setting vasm plugin
+
+"""
+
+
 from datetime import datetime
 import utils
 import logging
 
+
 log = logging.getLogger('vasm')
 
-def get_current_date():
-    """ return the current date """
-    now = datetime.now()
-    return (now.year, now.month, now.day)
 
-def get_current_time():
-    """ return the current time"""
-    now = datetime.now()
-    return (now.hour, now.minute, now.second)
+class DateTime:
+    """Manage the system date and time.
 
-def set_date(_date):
-    """ Set the system date to the values specified.
-    arguments:
-        _date = (year, month, day) tuple"""
-    year = _date[0]
-    # the month array starts with 0
-    month = _date[1] + 1
-    day = _date[2]
-    if month < 10:
-        month = "0%s"%month
-    if day < 10:
-        day = "0%s"%day
-    datestr = "%s%s%s"% (year, month, day)
-    cmd = ["/bin/date", "+%Y%m%d", "-s", datestr]
-    proc = utils.get_popen(cmd)
-    output = proc.communicate()[1]
-    rcode = proc.returncode
-    if rcode > 0:
-        _log = ("Unable to set the system date.", " ",
-            "Command: %s said \"%s\""% (" ".join(cmd),
-                "".join(output)))
-        log.fatal(" ".join(_log))
-    else:
-        log.debug("System date has been set to %s"% datestr)        
-    return (rcode, output)
+    """
+    def __init__(self, date_time=None):
 
-def set_time(_time):
-    """ Set the system time to the specified value
-    arguments:
-        _time is a (hour, minute, second) tuple"""
-    timestr = "%s:%s:%s"% (_time[0], _time[1], _time[2])
-    #proc = sp.Popen(["/bin/date", "+%T", "-s", timestr],
-    #    stdout=sp.PIPE, stderr=sp.STDOUT)
-    cmd = ["/bin/date", "+%T", "-s", timestr]
-    proc = utils.get_popen(cmd)
-    output = proc.communicate()[1]
-    rcode = proc.returncode
-    if rcode > 0:
-        _log = ("Unable to set the system time.", " ",
-        "Command: %s said \"%s\""% (" ".join(cmd), "".join(output)))
-        log.fatal(_log)
-    else:
-        log.debug("System time has been set to %s"% timestr)
+        if not date_time:
+            date_time = datetime.now()
 
-    return (rcode, output)
+        self.year = date_time.year
+        self.month = date_time.month
+        self.day = date_time.day
+        self.hour = date_time.hour
+        self.minute = date_time.minute
+        self.second = date_time.second
+        self._date_time = date_time
 
+    def set_date(self):
+        """ Set the system date to the values specified.
 
-    
+        """
+        if self.month < 10:
+            month = "0%s" % self.month
+        else:
+            month = self.month
+            
+        if self.day < 10:
+            day = "0%s" % self.day
+        else:
+            day = self.day
+            
+        datestr = "%s%s%s"% (self.year, month, day)
+        cmd = ["/bin/date", "+%Y%m%d", "-s", datestr]
+        proc = utils.get_popen(cmd)
+        output = proc.communicate()[1]
+        rcode = proc.returncode
 
+        if rcode > 0:
+            _log = ("Unable to set the system date.", " ",
+                "Command: %s said \"%s\""% (" ".join(cmd),
+                    "".join(output)))
+            log.fatal(" ".join(_log))
+            raise DateTimeError(_log)
+        else:
+            log.debug("System date has been set to %s"% datestr)        
 
+    def set_time(self):
+        """ Set the system time to the specified value
 
+        """
+        timestr = "%s:%s:%s" % (self.hour, self.minute, self.second)
+        cmd = ["/bin/date", "+%T", "-s", timestr]
+        proc = utils.get_popen(cmd)
+        output = proc.communicate()[1]
+        rcode = proc.returncode
+
+        if rcode > 0:
+            _log = ("Unable to set the system time.", " ",
+            "Command: %s said \"%s\""% (" ".join(cmd), "".join(output)))
+            log.fatal(_log)
+            raise DateTimeError(_log)
+        else:
+            log.debug("System time has been set to %s"% timestr)
+
+    def set_date_and_time(self):
+        """Set the system date and time.
+
+        """
+        self.set_date()
+        self.set_time()
+
+    def set_date_from_tuple(self, data):
+        """Update date attributes from a tuple. Convenience method for dealing
+        with UI widgets (gtk calendar returns a tuple, for example)
+        
+        """
+        self.year, self.month, self.day = data
+
+    def set_time_from_tuple(self, data):
+        """Update time attributes from a tuple. Convenience method for dealing
+        with UI widgets (gtk calendar returns a tuple, for example)
+        
+        """
+        self.hour, self.minute, self.second = data
+
+
+class DateTimeError(Exception):
+    """Raised when the system returns an error trying to set time or date
+
+    """
+    pass

File modules/support/vectorlinux/tests/test_DATESET.py

+#!/usr/bin/env python
+# -*- encoding: utf-8 -*-
+
+#    This file is part of VASM.
+#
+#    VASM is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License v3 as published by
+#    the Free Software Foundation.
+#
+#    VASM 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 VASM.  If not, see <http://www.gnu.org/licenses/>.
+
+
+import unittest
+import DATESET
+import os
+
+
+class DateTimeTest(unittest.TestCase):
+
+    def setUp(self):
+
+        self.date_time = DATESET.DateTime()
+
+        # save current time for restoring it later
+        self.now = self.date_time._date_time
+        self.date = self.now.year, self.now.month, self.now.day
+        self.time = self.now.hour, self.now.minute, self.now.second
+
+    def tearDown(self):
+
+        dt = self.date_time
+        dt.year, dt.month, dt.day = self.date
+        dt.hour, dt.minute, dt.second = self.time
+        self.date_time.set_date_and_time()
+
+    def testSetDate(self):
+
+        # test day and month < 10
+        self.date_time.year += 1
+        self.date_time.month = 2
+        self.date_time.day = 2
+        self.date_time.set_date()
+        current_date = DATESET.DateTime()
+        self.assertEqual(current_date.year, self.date[0] + 1)
+        self.assertEqual(self.date_time.year, current_date.year)
+        self.assertEqual(self.date_time.month, current_date.month)
+        self.assertEqual(self.date_time.day, current_date.day)
+
+        # test day and month > 10
+        self.date_time.month = 12
+        self.date_time.day = 24
+        self.date_time.set_date()
+        current_date = DATESET.DateTime()
+        self.assertEqual(current_date.year, self.date[0] + 1)
+        self.assertEqual(self.date_time.year, current_date.year)
+        self.assertEqual(self.date_time.month, current_date.month)
+        self.assertEqual(self.date_time.day, current_date.day)
+
+    def testSetTime(self):
+ 
+        self.date_time.hour += 1
+        self.date_time.minute += 1
+        self.date_time.second = 0
+        self.date_time.set_time()
+        current_time = DATESET.DateTime()
+        self.assertEqual(current_time.hour, self.time[0] + 1)
+        self.assertEqual(current_time.minute, self.time[1] + 1)
+        self.assertEqual(self.date_time.hour, current_time.hour)
+        self.assertEqual(self.date_time.minute, current_time.minute)
+        self.assertAlmostEqual(self.date_time.second, current_time.second)
+
+
+if __name__ == "__main__":
+    assert os.getuid() == 0, "You need to be root for running DateTime tests"
+    unittest.main()