src/hdalab/utils.py
changeset 319 fef4317b915f
parent 293 b33caeba7faa
child 345 7bc38c7d6cf9
equal deleted inserted replaced
318:25c28dd19898 319:fef4317b915f
     3 Created on Mar 13, 2012
     3 Created on Mar 13, 2012
     4 
     4 
     5 @author: ymh and tc
     5 @author: ymh and tc
     6 '''
     6 '''
     7 from django.core.cache import cache
     7 from django.core.cache import cache
     8 from django.http.response import Http404
       
     9 from django.utils.encoding import smart_str
     8 from django.utils.encoding import smart_str
    10 import md5
     9 import md5
    11 import re
    10 import re
    12 
    11 
    13 import logging
    12 import logging
    18     cache_key = re.sub(r'\s+', '-', key)
    17     cache_key = re.sub(r'\s+', '-', key)
    19     cache_key = smart_str(cache_key)
    18     cache_key = smart_str(cache_key)
    20     if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33):
    19     if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33):
    21         cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest()
    20         cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest()
    22     return cache_key
    21     return cache_key
    23 
       
    24 
       
    25 
       
    26 class NodePlacer():
       
    27     
       
    28     cat_nb_nodes = {}
       
    29     
       
    30     def init(self, cat_nb_nodes_initial):
       
    31         raise NotImplementedError( "Should have implemented get_place" )
       
    32     
       
    33     def get_place(self, category):
       
    34         if not category or category not in self.cat_nb_nodes:
       
    35             raise Http404
       
    36         return self.cat_nb_nodes[category].pop(0)
       
    37 
       
    38 
       
    39 
       
    40 class LineNodePlacer(NodePlacer):
       
    41     
       
    42     max_length = 0
       
    43     
       
    44     def init(self, cat_nb_nodes_initial):
       
    45         for c in cat_nb_nodes_initial:
       
    46             nb = cat_nb_nodes_initial[c]
       
    47             if isinstance(cat_nb_nodes_initial[c], tuple):
       
    48                 _, nb = nb
       
    49             self.max_length = nb if nb > self.max_length else self.max_length
       
    50         for i_cat,c in enumerate(cat_nb_nodes_initial):
       
    51             self.cat_nb_nodes[c] = []
       
    52             order = i_cat
       
    53             nb = cat_nb_nodes_initial[c]
       
    54             if isinstance(cat_nb_nodes_initial[c], tuple):
       
    55                 order, nb = nb
       
    56             offset = float(self.max_length - nb) / 2
       
    57             for i in xrange(nb):
       
    58                 self.cat_nb_nodes[c].append({ "x": order*300, "y": 100*(i+offset) })
       
    59         #logger.debug(self.cat_nb_nodes)
       
    60 
       
    61 
       
    62 
       
    63 class CircleNodePlacer(NodePlacer):
       
    64     #TODO: Does not work. Meant to be a real circle placer
       
    65     def init(self, cat_nb_nodes_initial):
       
    66         for c in cat_nb_nodes_initial:
       
    67             self.max_length = cat_nb_nodes_initial[c] if cat_nb_nodes_initial[c] > self.max_length else self.max_length
       
    68         for i_cat,c in enumerate(cat_nb_nodes_initial):
       
    69             self.cat_nb_nodes[c] = []
       
    70             offset = float(self.max_length - cat_nb_nodes_initial[c]) / 2
       
    71             for i in xrange(cat_nb_nodes_initial[c]):
       
    72                 self.cat_nb_nodes[c].append({ "x": i_cat*400, "y": 200*(i+offset) })
       
    73         #logger.debug(self.cat_nb_nodes)
       
    74         
       
    75                 
       
    76     
       
    77         
    22         
    78         
    23