def test_iterative_dict_key(self): ct = category_tree(data={u"a/b": u"c", u"a/c": [u"d", u"e"]}, type='iterative') self.assertEqual(json.loads(str(ct)), {u'a': {u'c': {u'e': {}, u'd': {}}, u'b': {u'c': {}}}})
def test_recursive_string_key(self): ct = category_tree(data='a/b/c', type='recursive') self.assertEqual(json.loads(str(ct)), {u"a": {u"b": {u"c": {}}}})
def test_iterative_list_key(self): ct = category_tree(data=["a/b", "a/c/d"], type='iterative') self.assertEqual(json.loads(str(ct)), {u"a": {u"c": {u"d": {}}, u"b": {}}})
class CategoryTreeHandler(BaseHTTPRequestHandler): web = path.join(path.dirname(path.realpath(__file__)), '../web/') statics = { '.html': 'text/html', '.htm': 'text/html', '.js': 'application/javascript', '.css': 'text/css' } tree = category_tree(type=impl) def serve_static(self, file, mime): ''' Serves static files with given mime-type. ''' try: with open(path.join(self.web, file), 'r') as served: self.send_response(200) self.send_header('Content-type', mime) self.end_headers() self.wfile.write(served.read()) except IOError: self.send_error(404, 'File Not Found: %s' % self.path) def fail(self): ''' Sends internal server error message. ''' self.send_error(500, 'Don\'t know what to do with: %s' % self.path) def do_GET(self): ''' Handles HTTP GET requests. ''' url = urlparse(self.path) extension = path.splitext(url.path)[1] or '.html' if self.path == '/': self.serve_static('index.html', self.statics['.html']) elif extension.lower() in self.statics: self.serve_static(url.path[1:], self.statics[extension.lower()]) else: self.fail() def send_preamble(self): ''' Starts sending success message. ''' self.send_response(200) self.send_header('Content-type', 'application/json') self.end_headers() def do_POST(self): ''' Hanles AJAX requests. ''' length = int(self.headers['content-length']) data = self.rfile.read(length) if self.path == '/categories/get': self.send_preamble() self.wfile.write(str(self.tree)) elif self.path == '/categories/put': self.tree.add(data) self.wfile.write(str(self.tree)) else: self.fail()
def test_recursive_string_key(self): ct = category_tree(data="a/b/c", type="recursive") self.assertEqual(json.loads(str(ct)), {u"a": {u"b": {u"c": {}}}})
def test_iterative_dict_key(self): ct = category_tree(data={u"a/b": u"c", u"a/c": [u"d", u"e"]}, type="iterative") self.assertEqual(json.loads(str(ct)), {u"a": {u"c": {u"e": {}, u"d": {}}, u"b": {u"c": {}}}})
def test_iterative_list_key(self): ct = category_tree(data=["a/b", "a/c/d"], type="iterative") self.assertEqual(json.loads(str(ct)), {u"a": {u"c": {u"d": {}}, u"b": {}}})