Commits

edgimar committed dcd8ece

- fix issue with UTF-8 strings and added unit-test

Comments (0)

Files changed (2)

 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 """
 michel-orgmode -- a script to push/pull an org-mode text file to/from a google
                   tasks list.
             #indentations = '\t' * level
             # add number of asterisks corresponding to depth of task, followed
             # by "DONE" if the task is marked as completed.
-            done_string = ""
+            done_string = u""
             if (subtask.status is not None) and (subtask.status == "completed"):
-                done_string = " DONE"
+                done_string = u" DONE"
             indentations = '*' * (level+1) + done_string + " "
             res.append(indentations + subtask.title)
             if subtask.notes is not None:
         """
         # always add a trailing "\n" because text-files normally include a "\n"
         # at the end of the last line of the file.
-        return '\n'.join(self._lines(0)) + "\n"
+        return u'\n'.join(self._lines(0)) + u"\n"
+
+    def _print(self):
+        print(self.__str__())
+
+    def write_to_orgfile(self, fname):
+        f = open(fname, 'wb')
+        f.write(self.__str__().encode('utf-8'))
+        f.close()
 
 def database_read(key):
     """Fetch an object from the persistent database stored under *key*.
     
     """
     tasks_tree = get_gtask_list_as_tasktree(profile, list_name)
-    print(tasks_tree)
-    
+    tasks_tree._print()
+
 def write_todolist(orgfile_path, profile, list_name=None):
     """Create an orgmode-formatted file representing a google tasks list.
     
     
     """
     tasks_tree = get_gtask_list_as_tasktree(profile, list_name)
-    f = open(orgfile_path, 'wb')
-    f.write(str(tasks_tree))
-    f.close()
+    tasks_tree.write_to_orgfile(orgfile_path)
 
 def erase_todolist(profile, list_id):
     """Erases the todo list of given id"""
 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 """
 Suite of unit-tests for testing Michel
 """
             * DONE    Headline 2
             ** Headline 2.1
             """)
-        tasktree = m.parse_text(org_text)
+        tasktree = m.parse_text_to_tree(org_text)
         self.assertEqual(str(tasktree), org_text)
 
+    def test_unicode_print_and_dump_to_file(self):
+        """
+        Test ability to print unicode text, and pull unicode into orgfile
+        """
+        #tasks_tree = m.get_gtask_list_as_tasktree("__default", "_DEFAULT_")
+        tasks_tree = m.TasksTree()
+        tasks_tree.add_subtask(u"Title", 1, None, u'viele Grüße', 'status')
+
+        tasks_tree._print()
+        import tempfile
+        fname = tempfile.NamedTemporaryFile().name
+        tasks_tree.write_to_orgfile(fname)
+
     def test_initial_non_headline_text(self):
         """
         Test the case where the first lines of an org-mode file are not
             ** Headline 2.1
             """)
 
-        tasktree = m.parse_text(org_text)
+        tasktree = m.parse_text_to_tree(org_text)
         # a dummy headline will be added to contain the initial text
         self.assertEqual(str(tasktree), "* \n" + org_text)
 
         org_text2 = "" # empty file
 
         for org_text in [org_text1, org_text2]:
-            tasktree = m.parse_text(org_text)
+            tasktree = m.parse_text_to_tree(org_text)
             # a dummy headline will be added to contain the initial text
             self.assertEqual(str(tasktree), "* \n" + org_text)
 
             ** Headline B1.1
             * Headline B2
             """)
-        tree1 = m.parse_text(org_text1)
-        tree2 = m.parse_text(org_text2)
+        tree1 = m.parse_text_to_tree(org_text1)
+        tree2 = m.parse_text_to_tree(org_text2)
         
         # test tree concatenation
         target_tree = m.concatenate_trees(tree1, tree2)
             * Headline B2 modified
             New B2 body text.
             """)
-        tree0 = m.parse_text(org_text0) # original tree
-        tree1 = m.parse_text(org_text1) # modified tree 1
-        tree2 = m.parse_text(org_text2) # modified tree 2
+        tree0 = m.parse_text_to_tree(org_text0) # original tree
+        tree1 = m.parse_text_to_tree(org_text1) # modified tree 1
+        tree2 = m.parse_text_to_tree(org_text2) # modified tree 2
         
         merged_tree, had_conflict = m.treemerge(tree1, tree0, tree2)
         self.assertTrue(had_conflict)