[hamster-applet] moved figure_time out to stuff and added parse_activity_input func that parses arbitrary input into



commit d58b4548d0813372b7717a5b2461313a62f2d972
Author: Toms Bauģis <toms baugis gmail com>
Date:   Wed May 20 14:19:52 2009 +0100

    moved figure_time out to stuff and added parse_activity_input func that parses arbitrary input into an activity
---
 hamster/stuff.py    |   75 ++++++++++++++++++++++++++++++++++++++++++++++++++-
 hamster/widgets.py  |   29 +-------------------
 tests/stuff_test.py |   67 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 142 insertions(+), 29 deletions(-)

diff --git a/hamster/stuff.py b/hamster/stuff.py
index f43b35e..c12162a 100644
--- a/hamster/stuff.py
+++ b/hamster/stuff.py
@@ -22,15 +22,16 @@
 # cells, columns, trees and other
 
 import gtk
-from hamster import storage, SHARED_DATA_DIR
 import pango
 from pango import ELLIPSIZE_END
 
 import datetime as dt
+import re
 import locale
 import os
 
 def load_ui_file(name):
+    from hamster import SHARED_DATA_DIR
     ui = gtk.Builder()
     ui.add_from_file(os.path.join(SHARED_DATA_DIR, name))
     return ui 
@@ -169,3 +170,75 @@ def escape_pango(text):
     text = text.replace("<", "&lt;")
     text = text.replace(">", "&gt;")
     return text
+
+def figure_time(str_time):
+    if not str_time:
+        return None
+    
+    # strip everything non-numeric and consider hours to be first number
+    # and minutes - second number
+    numbers = re.split("\D", str_time)
+    numbers = filter(lambda x: x!="", numbers)
+    
+    hours, minutes = None, None
+    
+    if len(numbers) == 1 and len(numbers[0]) == 4:
+        hours, minutes = int(numbers[0][:2]), int(numbers[0][2:])
+    else:
+        if len(numbers) >= 1:
+            hours = int(numbers[0])
+        if len(numbers) >= 2:
+            minutes = int(numbers[1])
+        
+    if (hours == None or minutes == None) or hours > 24 or minutes > 60:
+        return None #no can do
+
+    return dt.datetime.now().replace(hour = hours, minute = minutes)
+
+def parse_activity_input(text):
+    """Currently pretty braindead function that tries to parse arbitrary input
+    into a activity"""
+    class InputParseResult(object):
+        def __init__(self):
+            self.activity_name = None
+            self.category_name = None
+            self.start_time = None
+            self.end_time = None
+            self.description = None
+            self.tags = []
+            
+    
+    res = InputParseResult()
+    
+    potential_time = text.split(" ")[0]
+    potential_end_time = None
+    if potential_time.find("-") > -1:
+        potential_time, potential_end_time = potential_time.split("-", 2)
+        res.end_time = figure_time(potential_end_time)
+    
+    res.start_time = figure_time(potential_time)
+    
+    #remove parts that worked
+    if potential_end_time and not res.end_time:
+        text = text[text.find("-"):]
+    elif res.start_time:
+        text = text[text.find(" ")+1:]
+    
+    #see if we have description of activity somewhere here (delimited by comma)
+    if text.find(",") > 0:
+        text, res.description = text.split(",", 1)
+        res.description = res.description.strip()
+
+    if text.find("@") > 0:
+        text, res.category_name = text.split("@", 1)
+        res.category_name = res.category_name.strip()
+
+    #only thing left now is the activity name itself
+    res.activity_name = text
+
+    #this is most essential
+    if (text.find("bbq") > -1 or text.find("barbeque") > -1
+        or text.find("barbecue") > -1)  and text.find("omg") > -1:
+        res.description = "[ponies = 1], [rainbows = 0]"
+
+    return res
diff --git a/hamster/widgets.py b/hamster/widgets.py
index 2c79a47..8082438 100644
--- a/hamster/widgets.py
+++ b/hamster/widgets.py
@@ -17,7 +17,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Project Hamster.  If not, see <http://www.gnu.org/licenses/>.
 
-from hamster.stuff import format_duration
+from hamster.stuff import format_duration, figure_time
 import gtk
 import datetime as dt
 import calendar
@@ -360,33 +360,6 @@ class TimeInput(gtk.Entry):
         
     def _on_button_press_event(self, button, event):
         self.popup.show()
-        
-        
-    def figure_time(self, str_time):
-        # strip everything non-numeric and consider hours to be first number
-        # and minutes - second number
-        numbers = re.split("\D", str_time)
-        numbers = filter(lambda x: x!="", numbers)
-        hours, minutes = None, None
-        
-        if len(numbers) >= 1:
-            hours = int(numbers[0])
-            
-        if len(numbers) >= 2:
-            minutes = int(numbers[1])
-            
-        if (hours == None and minutes == None) or hours > 24 or minutes > 60:
-            return None #no can do
-
-        """ this breaks 24 hour mode, when hours are given
-        #if hours specified in 12 hour mode, default to PM
-        #TODO - laame, show me how to do this better, pleease
-        am = dt.time(1, 00).strftime("%p")
-        if hours <= 11 and str_time.find(am) < 0:
-            hours += 12
-        """
-        
-        return dt.datetime(1900, 1, 1, hours, minutes)
 
 
     
\ No newline at end of file
diff --git a/tests/stuff_test.py b/tests/stuff_test.py
new file mode 100644
index 0000000..f4535c1
--- /dev/null
+++ b/tests/stuff_test.py
@@ -0,0 +1,67 @@
+import sys, os.path
+# a convoluted line to add hamster module to absolute path
+sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
+
+import unittest
+from hamster import stuff
+
+class TestActivityInputParsing(unittest.TestCase):
+    def test_plain_name(self):
+        # plain activity name
+        activity = stuff.parse_activity_input("just a simple case")
+        self.assertEquals(activity.activity_name, "just a simple case")
+        assert activity.category_name == None and activity.start_time == None \
+               and activity.end_time == None and activity.category_name == None\
+               and activity.description == None
+
+    def test_with_start_time(self):
+        # with time
+        activity = stuff.parse_activity_input("12:35 with start time")
+        self.assertEquals(activity.activity_name, "with start time")
+        self.assertEquals(activity.start_time.strftime("%H:%M"), "12:35")
+
+        #rest must be empty        
+        assert activity.category_name == None \
+               and activity.end_time == None and activity.category_name == None\
+               and activity.description == None
+
+    def test_with_start_and_end_time(self):
+        # with time
+        activity = stuff.parse_activity_input("12:35-14:25 with start-end time")
+        self.assertEquals(activity.activity_name, "with start-end time")
+        self.assertEquals(activity.start_time.strftime("%H:%M"), "12:35")
+        self.assertEquals(activity.end_time.strftime("%H:%M"), "14:25")
+
+        #rest must be empty        
+        assert activity.category_name == None \
+               and activity.category_name == None\
+               and activity.description == None
+
+    def test_category(self):
+        # plain activity name
+        activity = stuff.parse_activity_input("just a simple case hamster")
+        self.assertEquals(activity.activity_name, "just a simple case")
+        self.assertEquals(activity.category_name, "hamster")
+        assert activity.start_time == None \
+               and activity.end_time == None \
+               and activity.description == None
+
+    def test_description(self):
+        # plain activity name
+        activity = stuff.parse_activity_input("case, with added description")
+        self.assertEquals(activity.activity_name, "case")
+        self.assertEquals(activity.description, "with added description")
+        assert activity.category_name == None and activity.start_time == None \
+               and activity.end_time == None and activity.category_name == None
+
+    def test_full(self):
+        # plain activity name
+        activity = stuff.parse_activity_input("1225-1325 case cat, description")
+        self.assertEquals(activity.start_time.strftime("%H:%M"), "12:25")
+        self.assertEquals(activity.end_time.strftime("%H:%M"), "13:25")
+        self.assertEquals(activity.activity_name, "case")
+        self.assertEquals(activity.category_name, "cat")
+        self.assertEquals(activity.description, "description")
+
+if __name__ == '__main__':
+    unittest.main()



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]