def __sort(cls, descriptors): """ Sort descriptors based on defined dependencies. Dependencies defined by [main].requires @param descriptors: A list of descriptor tuples (name,descriptor) @type descriptors: list @return: The sorted list @rtype: list """ index = {} for d in descriptors: index[d[0]] = d L = DepList() for n,d in descriptors: r = (n, d.__requires()) L.add(r) sorted = [] for name in [x[0] for x in L.sort()]: d = index[name] sorted.append(d) return sorted
def test_sort(self): a = ('a', ('x',)) b = ('b', ('a',)) c = ('c', ('a', 'b')) d = ('d', ('c',)) e = ('e', ('d', 'a')) f = ('f', ('e', 'c', 'd', 'a')) x = ('x', ()) g = ('g', ('G',)) dl = DepList() dl.add(c, e, d, b, f, a, x, g) self.assertEqual( dl.sort(), [ ('x', ()), ('a', ('x',)), ('b', ('a',)), ('c', ('a', 'b')), ('d', ('c',)), ('e', ('d', 'a')), ('f', ('e', 'c', 'd', 'a')), ('g', ('G',)) ])
def test_add(self): items = [(1, 2), (3, 4)] dl = DepList() dl.add(*items) self.assertEqual(dl.unsorted, items) self.assertEqual(dl.index, {1: (1, 2), 3: (3, 4)})