-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgraph.py
191 lines (144 loc) · 6.15 KB
/
graph.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
"""Module to manipulate history graphs for cjv."""
from textwrap import fill, wrap, indent
import networkx as nx
from colorama import Fore, Style
from rich.console import Console
from rich.themes import Theme
from rich.padding import Padding
from cityjson.versioning import VersionedCityJSON
class History:
"""Class to represent the history of versions of a versioned city model."""
def __init__(self, citymodel: VersionedCityJSON):
self._citymodel = citymodel
self._dag = nx.DiGraph()
def add_versions(self, version_name):
"""Adds the versions that lead to version_name."""
G = self._dag
next_key = version_name
next_ver = self._citymodel.versioning.versions[version_name]
G.add_node(next_key, **next_ver.data)
for parent in next_ver.parents:
if not G.has_node(parent):
self.add_versions(parent.name)
G.add_edge(parent.name, next_key)
@property
def citymodel(self):
"""Returns the versioned city model."""
return self._citymodel
@property
def dag(self):
"""Returns the dag object."""
return self._dag
class AbstractLog:
"""Abstract class that shares logic related to formatting of logs"""
def __init__(self, history: 'History'):
self._history = history
self._theme = Theme({
"header": "yellow",
"branch": "cyan",
"tag": "magenta",
"message": "white",
"main": "white"
})
def get_header_text(self, version):
"""Returns the header line of a version, formatted."""
header_line = f"[header]version {version.name}"
if len(version.branches) > 0:
branches_txt = self.get_refs_string(version.branches, "branch")
header_line += f" ({branches_txt})"
if len(version.tags) > 0:
tags_txt = self.get_refs_string(version.tags, "tag")
header_line += f" ({tags_txt})"
header_line += "[/header]"
return header_line
def get_refs_string(self, refs, ref_type):
"""Returns a string with comma-seperated names of the refs."""
return ", ".join([f"[{ref_type}]{ref}[/{ref_type}]"
for ref in refs])
class SimpleHistoryLog(AbstractLog):
"""Class to print a simple list log of a History."""
def print_all(self):
"""Prints the history as a list."""
dag = self._history.dag
sorted_keys = list(nx.topological_sort(dag))
sorted_keys.reverse()
for version_name in sorted_keys:
version = self._history.citymodel.versioning.versions[version_name]
self.print_version(version)
def print_version(self, version: 'Version', console=None):
"""Prints a given version."""
def text_wrap(text, width):
return '\n'.join(wrap(text, width))
console = Console(theme=self._theme, highlight=False)
header_line = self.get_header_text(version)
console.print(Padding(header_line, (0, 0)))
console.print(f"{'Author:':<7} {version.author}")
console.print(f"{'Date:':<7} {version.date}")
msg = f"[message]{text_wrap(version.message, 70)}[/message]"
console.print(Padding(msg, (1, 4)))
class GraphHistoryLog(AbstractLog):
"""Class to print a history with a graph."""
def print_all(self):
"""Prints the history as a graph."""
def get_branch_lines(num_lines, num_empty_lines):
"""
Returns the lines for the given branches
"""
txt = num_lines * "| " + num_empty_lines * " "
return txt
console = Console(theme = self._theme, highlight=False)
cm = self._history.citymodel
dag = self._history.dag
sorted_keys = list(nx.topological_sort(dag))
sorted_keys.reverse()
# Get the leafs of the graph
leafs = [x for x in dag.nodes() if dag.out_degree(x) == 0]
all_paths = []
for leaf in leafs:
version = cm.versioning.versions[leaf]
all_paths.extend(list(nx.shortest_simple_paths(dag,
sorted_keys[-1],
version.name)))
active_paths = [all_paths[0]]
for version_name in sorted_keys:
version = self._history.citymodel.versioning.versions[version_name]
found = False
header_line = ""
is_merge = dag.in_degree(version_name) > 1
is_fork = dag.out_degree(version_name) > 1
if is_merge:
for path in all_paths:
if version_name in path and path not in active_paths:
active_paths.append(path)
width = len(active_paths)
empty_lines = 0
# Find to which branch this version belongs
for path in active_paths:
# TODO: This set of conditional branches is awful!
if version_name in path:
if found:
if not is_fork:
header_line += " "
continue
else:
header_line += "* "
found = True
continue
header_line += "| "
header_line += self.get_header_text(version)
console.print(header_line)
if is_merge:
console.print("|\\")
if is_fork:
console.print("|/")
width -= 1
empty_lines += 1
if not (is_merge or is_fork):
console.print("".join(["| " for _ in active_paths]))
lines = get_branch_lines(width, empty_lines)
console.print(indent(f"{'Author:':<7} {version.author}", lines))
console.print(indent(f"{'Date:':<7} {version.date}", lines))
console.print(lines)
msg = indent(fill(version.message, 70, initial_indent="[message] ", subsequent_indent="[message] "), lines)
console.print(msg)
console.print(lines)