|
| 1 | +# Copyright The IETF Trust 2007, All Rights Reserved |
| 2 | +# -*- check-flake8 -*- |
| 3 | +from __future__ import unicode_literals, print_function |
| 4 | + |
| 5 | +from django.db.models import Q |
| 6 | +from django.template.loader import render_to_string |
| 7 | + |
| 8 | +from ietf.doc.models import RelatedDocument |
| 9 | + |
| 10 | + |
| 11 | +class Edge(object): |
| 12 | + def __init__(self, relateddocument): |
| 13 | + self.relateddocument = relateddocument |
| 14 | + |
| 15 | + def __hash__(self): |
| 16 | + return hash("|".join([str(hash(nodename(self.relateddocument.source.name))), |
| 17 | + str(hash(nodename(self.relateddocument.target.document.name))), |
| 18 | + self.relateddocument.relationship.slug])) |
| 19 | + |
| 20 | + def __eq__(self, other): |
| 21 | + return self.__hash__() == other.__hash__() |
| 22 | + |
| 23 | + def sourcename(self): |
| 24 | + return nodename(self.relateddocument.source.name) |
| 25 | + |
| 26 | + def targetname(self): |
| 27 | + return nodename(self.relateddocument.target.document.name) |
| 28 | + |
| 29 | + def styles(self): |
| 30 | + |
| 31 | + # Note that the old style=dotted, color=red styling is never used |
| 32 | + |
| 33 | + if self.relateddocument.is_downref(): |
| 34 | + return { 'color': 'red', 'arrowhead': 'normalnormal' } |
| 35 | + else: |
| 36 | + styles = { 'refnorm' : { 'color': 'blue' }, |
| 37 | + 'refinfo' : { 'color': 'green' }, |
| 38 | + 'refold' : { 'color': 'orange' }, |
| 39 | + 'refunk' : { 'style': 'dashed' }, |
| 40 | + 'replaces': { 'color': 'pink', 'style': 'dashed', 'arrowhead': 'diamond' }, |
| 41 | + } |
| 42 | + return styles[self.relateddocument.relationship.slug] |
| 43 | + |
| 44 | + |
| 45 | +def nodename(name): |
| 46 | + return name.replace('-', '_') |
| 47 | + |
| 48 | + |
| 49 | +def get_node_styles(node, group): |
| 50 | + |
| 51 | + styles = dict() |
| 52 | + |
| 53 | + # Shape and style (note that old diamond shape is never used |
| 54 | + |
| 55 | + styles['style'] = 'filled' |
| 56 | + |
| 57 | + if node.get_state('draft').slug == 'rfc': |
| 58 | + styles['shape'] = 'box' |
| 59 | + elif node.get_state('draft-iesg') and not node.get_state('draft-iesg').slug in ['watching', 'dead']: |
| 60 | + styles['shape'] = 'parallelogram' |
| 61 | + elif node.get_state('draft').slug == 'expired': |
| 62 | + styles['shape'] = 'house' |
| 63 | + styles['style'] = 'solid' |
| 64 | + styles['peripheries'] = 3 |
| 65 | + elif node.get_state('draft').slug == 'repl': |
| 66 | + styles['shape'] = 'ellipse' |
| 67 | + styles['style'] = 'solid' |
| 68 | + styles['peripheries'] = 3 |
| 69 | + else: |
| 70 | + pass # quieter form of styles['shape'] = 'ellipse' |
| 71 | + |
| 72 | + # Color (note that the old 'Flat out red' is never used |
| 73 | + if node.group.acronym == 'none': |
| 74 | + styles['color'] = '"#FF800D"' # orangeish |
| 75 | + elif node.group == group: |
| 76 | + styles['color'] = '"#0AFE47"' # greenish |
| 77 | + else: |
| 78 | + styles['color'] = '"#9999FF"' # blueish |
| 79 | + |
| 80 | + # Label |
| 81 | + label = node.name |
| 82 | + if label.startswith('draft-'): |
| 83 | + if label.startswith('draft-ietf-'): |
| 84 | + label = label[11:] |
| 85 | + else: |
| 86 | + label = label[6:] |
| 87 | + try: |
| 88 | + t = label.index('-') |
| 89 | + label = r"%s\n%s" % (label[:t], label[t+1:]) |
| 90 | + except: |
| 91 | + pass |
| 92 | + if node.group.acronym != 'none' and node.group != group: |
| 93 | + label = "(%s) %s" % (node.group.acronym, label) |
| 94 | + if node.get_state('draft').slug == 'rfc': |
| 95 | + label = "%s\\n(%s)" % (label, node.canonical_name()) |
| 96 | + styles['label'] = '"%s"' % label |
| 97 | + |
| 98 | + return styles |
| 99 | + |
| 100 | + |
| 101 | +def make_dot(group): |
| 102 | + references = Q(source__group=group, source__type='draft', relationship__slug__startswith='ref') |
| 103 | + both_rfcs = Q(source__states__slug='rfc', target__document__states__slug='rfc') |
| 104 | + inactive = Q(source__states__slug__in=['expired', 'repl']) |
| 105 | + attractor = Q(target__name__in=['rfc5000', 'rfc5741']) |
| 106 | + removed = Q(source__states__slug__in=['auth-rm', 'ietf-rm']) |
| 107 | + relations = ( RelatedDocument.objects.filter(references).exclude(both_rfcs) |
| 108 | + .exclude(inactive).exclude(attractor).exclude(removed) ) |
| 109 | + |
| 110 | + edges = set() |
| 111 | + for x in relations: |
| 112 | + target_state = x.target.document.get_state_slug('draft') |
| 113 | + if target_state != 'rfc' or x.is_downref(): |
| 114 | + edges.add(Edge(x)) |
| 115 | + |
| 116 | + replacements = RelatedDocument.objects.filter(relationship__slug='replaces', |
| 117 | + target__document__in=[x.relateddocument.target.document for x in edges]) |
| 118 | + |
| 119 | + for x in replacements: |
| 120 | + edges.add(Edge(x)) |
| 121 | + |
| 122 | + nodes = set([x.relateddocument.source for x in edges]).union([x.relateddocument.target.document for x in edges]) |
| 123 | + |
| 124 | + for node in nodes: |
| 125 | + node.nodename = nodename(node.name) |
| 126 | + node.styles = get_node_styles(node, group) |
| 127 | + |
| 128 | + return render_to_string('group/dot.txt', |
| 129 | + dict( nodes=nodes, edges=edges ) |
| 130 | + ) |
| 131 | + |
| 132 | + |
0 commit comments