Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit caacc5d077373d857412311f9a834e0671ec2b94 @kushaldas committed Jul 3, 2012
Showing with 240 additions and 0 deletions.
  1. +7 −0 LICENSE
  2. 0 retask/__init__.py
  3. +106 −0 retask/queue.py
  4. +55 −0 retask/task.py
  5. +72 −0 tests.py
@@ -0,0 +1,7 @@
+Copyright (C) 2012, Kushal Das
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No changes.
@@ -0,0 +1,106 @@
+#Copyright (C) 2012, Kushal Das <kushaldas@gmail.com>
+
+#Permission is hereby granted, free of charge, to any person obtaining a copy of
+#this software and associated documentation files (the "Software"), to deal in
+#the Software without restriction, including without limitation the rights to
+#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+#of the Software, and to permit persons to whom the Software is furnished to do
+#so, subject to the following conditions:
+
+#The above copyright notice and this permission notice shall be included in all
+#copies or substantial portions of the Software.
+
+#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+#SOFTWARE.
+
+__author__ = 'Kushal Das <kushaldas@gmail.com'
+__copyright__ = 'Copyright (c) 2012 Kushal Das'
+__license__ = 'MIT'
+__status__ = 'Development'
+__version__ = '0.1'
+
+"""
+retask Queue implementation
+
+"""
+import redis
+import json
+from task import Task
+
+
+
+class Queue:
+ """
+ Generic Queue on Redis
+ """
+ def __init__(self, name, config = {}):
+ self.name = name
+ if not config:
+ self.config = {'host':'localhost', 'port':6379, 'db':0,\
+ 'password':None}
+ else:
+ self.config = config
+ self.rdb = None
+ self.connected = False
+
+ @property
+ def length(self):
+ """
+ Returns the length of the queue
+ """
+ if not self.connected:
+ return None
+
+ return self.rdb.llen(self.name)
+
+ def connect(self):
+ """
+ Checks the connectivity with the redis server
+
+ """
+ config = self.config
+ self.rdb = redis.Redis(config['host'], config['port'], config['db'],
+ config['password'])
+ try:
+ info = self.rdb.info()
+ self.connected = True
+ except redis.ConnectionError:
+ return False
+
+ return True
+
+ def dequeue(self):
+ """
+ Gets the first task from the Queue
+ """
+ if not self.connected:
+ return None
+
+ if self.rdb.llen(self.name) == 0:
+ return None
+
+ data = self.rdb.rpop(self.name)
+ task = Task(data, True)
+ return task
+
+ def enqueue(self, task):
+ """
+ Sets the given task in the queue
+
+ """
+ if not self.connected:
+ return False, 'Not connected'
+
+ if not task.data:
+ return False, 'No data'
+ try:
+ #We can set the value to the queue
+ self.rdb.lpush(self.name, task.rawdata)
+ except Exception, err:
+ return False, str(err)
+ return True, 'Pushed'
@@ -0,0 +1,55 @@
+#Copyright (C) 2012, Kushal Das <kushaldas@gmail.com>
+
+#Permission is hereby granted, free of charge, to any person obtaining a copy of
+#this software and associated documentation files (the "Software"), to deal in
+#the Software without restriction, including without limitation the rights to
+#use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
+#of the Software, and to permit persons to whom the Software is furnished to do
+#so, subject to the following conditions:
+
+#The above copyright notice and this permission notice shall be included in all
+#copies or substantial portions of the Software.
+
+#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+#SOFTWARE.
+
+__author__ = 'Kushal Das <kushaldas@gmail.com'
+__copyright__ = 'Copyright (c) 2012 Kushal Das'
+__license__ = 'MIT'
+__status__ = 'Development'
+__version__ = '0.1'
+
+"""
+Task Class
+"""
+import json
+
+class Task:
+ """
+ Generic Task class
+ """
+
+ def __init__(self, data=None, raw= False):
+ if not raw:
+ self._data = json.dumps(data)
+ else:
+ self._data = data
+
+ @property
+ def data(self):
+ """
+ Returns the data of the task
+ """
+ return json.loads(self._data)
+
+
+ @property
+ def rawdata(self):
+ return self._data
+
+
@@ -0,0 +1,72 @@
+import unittest
+import redis
+from mock import patch
+from retask.task import Task
+from retask.queue import Queue
+from pprint import pprint
+
+
+class ConnectTest(unittest.TestCase):
+ """
+ Test the connect method
+ """
+ def runTest(self):
+ queue = Queue('testqueue')
+ self.assertTrue(queue.connect())
+
+
+
+class LengthTest(unittest.TestCase):
+ """
+ Tests the length method of the Queue
+
+ """
+ @patch('redis.Redis')
+ def runTest(self, mock_redis):
+ m = mock_redis.return_value
+ m.llen.return_value = 2
+ queue = Queue('testqueue')
+ queue.connect()
+ self.assertEqual(queue.length, 2)
+
+
+class SetTest(unittest.TestCase):
+ """
+ Sets a task in the Queue
+
+ """
+ def runTest(self):
+ queue = Queue('testqueue')
+ queue.connect()
+ t = Task({'name':'kushal'})
+ self.assertTrue(queue.enqueue(t)[0])
+
+ def tearDown(self):
+ rdb = redis.Redis()
+ rdb.delete('testqueue')
+
+
+class GetTest(unittest.TestCase):
+ """
+ Gets a task in the Queue
+
+ """
+ def setUp(self):
+ queue = Queue('testqueue')
+ queue.connect()
+ t = Task({'name':'kushal'})
+ queue.enqueue(t)
+
+
+ def runTest(self):
+ queue = Queue('testqueue')
+ queue.connect()
+ task = queue.dequeue()
+ i = task.data
+ self.assertEqual(task.data['name'], u'kushal')
+
+
+if __name__ == '__main__':
+ unittest.main()
+
+

0 comments on commit caacc5d

Please sign in to comment.