Antoine Nguyen avatar Antoine Nguyen committed 3a44f70

* Fixing addfilter (see #16)
* Quote input strings if necessary (see #17)

Comments (0)

Files changed (2)


 class FiltersSet(object):
-    def __init__(self, name, filter_name_pretext="# Filter:", filter_desc_pretext="# Description:"):
+    def __init__(self, name, filter_name_pretext="# Filter: ", filter_desc_pretext="# Description: "):
         """Represents a set of one or more filters
         :param name: the filterset's name
         reqcmd.check_next_arg("stringlist", self.requires)
         return reqcmd
+    def __quote_if_necessary(self, value):
+        """Add double quotes to the given string if necessary
+        :param value: the string to check
+        :return: the string between quotes
+        """
+        if not value.startswith(('"', "'")):
+            return u'"%s"' % value
+        return value
     def __create_filter(self, conditions, actions, matchtype="anyof"):
         """Create a new filter
                 cmd = get_command_instance("header", ifcontrol)
                 cmd.check_next_arg("tag", c[1])
-                cmd.check_next_arg("string", c[0])
-                cmd.check_next_arg("string", c[2])
+                cmd.check_next_arg("string", self.__quote_if_necessary(c[0]))
+                cmd.check_next_arg("string", self.__quote_if_necessary(c[2]))
             mtypeobj.check_next_arg("test", cmd)
         ifcontrol.check_next_arg("test", mtypeobj)
             action = get_command_instance(actdef[0], ifcontrol, False)
             for arg in actdef[1:]:
-                action.check_next_arg("string", arg)
+                action.check_next_arg("string", self.__quote_if_necessary(arg))
         return ifcontrol
         for f in self.filters:
-            print "Filter Name:%s" % f["name"]
-            print "Filter Description:%s" % f["description"]
+            print "Filter Name: %s" % f["name"]
+            print "Filter Description: %s" % f["description"]
     def tosieve(self, target=sys.stdout):
         for f in self.filters:
             print >> target, self.filter_name_pretext + f["name"]
-            if len(f["description"]) > 0:
+            if "description" in f and len(f["description"]):
                 print >> target, self.filter_desc_pretext + f["description"]


+# coding: utf-8
+import unittest
+import cStringIO
+from sievelib.factory import FiltersSet
+class FactoryTestCase(unittest.TestCase):
+    def setUp(self):
+        self.fs = FiltersSet("test")
+    def test_add_filter(self):
+        output = cStringIO.StringIO()
+        self.fs.addfilter("rule1",
+                          [('Sender', ":is", ''),],
+                          [("fileinto", 'Toto'),])
+        self.fs.tosieve(output)
+        self.assertEqual(output.getvalue(), """require ["fileinto"];
+# Filter: rule1
+if anyof (header :is "Sender" "") {
+    fileinto "Toto";
+        output.close()
+if __name__ == "__main__":
+    unittest.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.