source: revtreeplugin/0.11/revtree/optimizer.py

Last change on this file was 1908, checked in by Emmanuel Blot, 17 years ago

RevtreePlugin:

Bye bye ClearSilver: RevtreePlugin is now using Genshi

  • Fix up a lot of issues with revtree parameter management - and simplifies it
  • Remove custom jquery.js as it now comes with Trac core
  • Minor presentation fixes (CSS)
  • Add a (not-so) hidden URL parameter to clean-up the revtree data from the user session: ?reset=1
  • Backport fixes for Python2.3 from RevtreePlugin for milestone:0.10.3
  • Remove obsoleted test files
  • Property svn:eol-style set to native
File size: 2.3 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006-2007 Emmanuel Blot <emmanuel.blot@free.fr>
4# All rights reserved.
5#
6# This software is licensed as described in the file COPYING, which
7# you should have received as part of this distribution. The terms
8# are also available at http://trac.edgewall.com/license.html.
9#
10# This software consists of voluntary contributions made by many
11# individuals. For the exact contribution history, see the revision
12# history and logs, available at http://projects.edgewall.com/trac/.
13#
14
15from revtree.api import IRevtreeOptimizer
16from trac.core import *
17
18__all__ = ['DefaultRevtreeOptimizer']
19
20class DefaultRevtreeOptimizer(Component):
21    """Default optmizer"""
22   
23    implements(IRevtreeOptimizer)   
24       
25    def optimize(self, repos, branches):
26        """Computes the optimal placement of branches.
27       
28        Optimal placement is recommended to reduce the number of operation
29        links that cross each other on the rendered graphic.
30        This rudimentary example is FAR from providing optimal placements...
31        """
32        # FIXME: really stupid algorithm
33        graph = {}
34        for v in repos.branches().values():
35            k = v.name
36            src = v.source()
37            if src:
38                (rev, path) = src
39                if graph.has_key(path):
40                    graph[path].append(k)
41                else:
42                    graph[path] = [k]
43        density = []
44        for (p, v) in graph.items():
45            density.append((p,len(v)))
46        density.sort(lambda a,b: cmp(a[1],b[1]))
47        density.reverse()
48        order = []
49        cur = 0
50        for (branch, weight) in density:
51            order.insert(cur, branch)
52            if cur:
53                cur = 0
54            else:
55                cur = len(order)
56        nbranches = []
57        for br in graph.values():
58            nbranches.extend(br)
59        nbranches.extend([br.name for br in repos.branches().values() \
60                          if br.name not in nbranches])
61        for branch in nbranches:
62            if branch in order:
63                continue
64            order.insert(cur, branch)
65            if cur:
66                cur = 0
67            else:
68                cur = len(order)
69        obranches = [repos.branch(name) for name in order]
70        # FIXME: use filter()
71        return [b for b in obranches if b in branches]
Note: See TracBrowser for help on using the repository browser.