Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
JCWasmx86 committed Jun 1, 2024
1 parent bff219c commit 28995c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 46 deletions.
12 changes: 2 additions & 10 deletions netbox/templates/extras/objectchange.html
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,8 @@ <h5 class="card-header d-flex justify-content-between">
<h5 class="card-header">{% trans "Pre-Change Data" %}</h5>
<div class="card-body">
{% if object.prechange_data %}
{% spaceless %}
<pre class="change-data">{% for text, changed in text_before %}{% spaceless %}
<span{% if changed %} class="removed"{% endif %}>{{ text }}</span>
{% endspaceless %}{% endfor %}
<pre class="change-data">{% for text, changed in text_before %}<span{% if changed %} class="removed"{% endif %}>{{ text }}<br></span>{% endfor %}
</pre>
{% endspaceless %}
{% elif non_atomic_change %}
{% trans "Warning: Comparing non-atomic change to previous change record" %} (<a href="{% url 'extras:objectchange' pk=prev_change.pk %}">{{ prev_change.pk }}</a>)
{% else %}
Expand All @@ -129,12 +125,8 @@ <h5 class="card-header">{% trans "Pre-Change Data" %}</h5>
<h5 class="card-header">{% trans "Post-Change Data" %}</h5>
<div class="card-body">
{% if object.postchange_data %}
{% spaceless %}
<pre class="change-data">{% for text,changed in text_after %}{% spaceless %}
<span{% if changed %} class="added"{% endif %}>{{ text }}</span>
{% endspaceless %}{% endfor %}
<pre class="change-data">{% for text,changed in text_after %}<span{% if changed %} class="added"{% endif %}>{{ text }}<br></span>{% endfor %}
</pre>
{% endspaceless %}
{% else %}
<span class="text-muted">{% trans "None" %}</span>
{% endif %}
Expand Down
69 changes: 33 additions & 36 deletions netbox/utilities/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,18 +132,20 @@ def drange(start, end, step=decimal.Decimal(1)):
#
# String utilities
#


def regular_line_ending(s: str):
if s.endswith(","):
if s.endswith(','):
return True
return not (s.endswith("{") or s.endswith("["))
return not (s.endswith('{') or s.endswith('['))


def begin_of_complex(s: str):
return s.endswith("[") or s.endswith("{")
return s.endswith('[') or s.endswith('{')


def has_indent(s: str, indent: str):
return not s.removeprefix(indent).startswith(" ")
return not s.removeprefix(indent).startswith(' ')


def extract_key(s: str):
Expand All @@ -153,6 +155,7 @@ def extract_key(s: str):
substr = s[:idx].replace('"', '').strip()
return substr


def make_diff(old: str, new: str):
old_lines = old.splitlines(False)
new_lines = new.splitlines(False)
Expand All @@ -166,13 +169,13 @@ def make_diff(old: str, new: str):
if old_idx == len(old_lines):
while new_idx != len(new_lines):
new_list.append((new_lines[new_idx], True))
old_list.append(("", False))
old_list.append(('', False))
new_idx += 1
break
if new_idx == len(new_lines):
while old_idx != len(old_lines):
old_list.append((old_lines[old_idx], True))
new_list.append(("", False))
new_list.append(('', False))
old_idx += 1
break
old_s = old_lines[old_idx]
Expand All @@ -183,20 +186,17 @@ def make_diff(old: str, new: str):
new_idx += 1
# Handle additions of keys
if old_k is not None and new_k is not None and old_k != new_k:
print("Found mismatch:", old_k, new_k)
found_it = False
for old_idx2 in range(old_idx, len(old_lines)):
old_s2 = old_lines[old_idx2]
old_k2 = extract_key(old_s2)
print(">>", old_k2, new_k)
if old_k2 == new_k:
print("Found it", old_k2, old_idx2, new_k)
old_list.append((old_s, False))
new_list.append((" <O>", False))
old_s = "" if old_idx >= len(old_lines) else old_lines[old_idx]
new_list.append(('', False))
old_s = '' if old_idx >= len(old_lines) else old_lines[old_idx]
for _ in range(old_idx + 1, old_idx2 + 1):
old_s = "" if old_idx >= len(old_lines) else old_lines[old_idx]
new_list.append((" <<OLDFILLER>", False))
old_s = '' if old_idx >= len(old_lines) else old_lines[old_idx]
new_list.append(('', False))
old_list.append((old_s, True))
old_idx += 1
old_list.append((old_s, False))
Expand All @@ -209,15 +209,13 @@ def make_diff(old: str, new: str):
for new_idx2 in range(new_idx, len(new_lines)):
new_s2 = new_lines[new_idx2]
new_k2 = extract_key(new_s2)
print(">>22", old_k, new_k2)
if new_k2 == old_k:
print("NEW Found it", new_k2, new_idx2, old_k)
old_list.append((" <N>", False))
old_list.append(('', False))
new_list.append((new_s, False))
new_s = "" if new_idx >= len(new_lines) else new_lines[new_idx]
new_s = '' if new_idx >= len(new_lines) else new_lines[new_idx]
for _ in range(new_idx + 1, new_idx2 + 1):
new_s = "" if new_idx >= len(new_lines) else new_lines[new_idx]
old_list.append((" <<NEWFILLER>", False))
new_s = '' if new_idx >= len(new_lines) else new_lines[new_idx]
old_list.append(('', False))
new_idx += 1
new_list.append((new_s, True))
old_list.append((old_s, False))
Expand All @@ -226,28 +224,28 @@ def make_diff(old: str, new: str):
found_it = True
break
continue
if "{" == old_s.strip() and "{" == new_s.strip():
old_list.append(("{", False))
new_list.append(("{", False))
if '{' == old_s.strip() and '{' == new_s.strip():
old_list.append(('{', False))
new_list.append(('{', False))
continue
if "}" == old_s.strip() and "}" == new_s.strip():
old_list.append(("}", False))
new_list.append(("}", False))
if '}' == old_s.strip() and '}' == new_s.strip():
old_list.append(('}', False))
new_list.append(('}', False))
continue
# Handle:
# "foo": null "foo": [
# "bar",
# "baz",
# "]"
if regular_line_ending(old_s) and begin_of_complex(new_s):
indent_of_new = new_s.replace(new_s.strip(), "")
indent_of_new = new_s.replace(new_s.strip(), '')
new_list.append((new_s, True))
old_list.append((old_s, True))
while not has_indent(new_lines[new_idx], indent_of_new):
old_list.append(("", True))
old_list.append(('', True))
new_list.append((new_lines[new_idx], True))
new_idx += 1
old_list.append(("", True))
old_list.append(('', True))
new_list.append((new_lines[new_idx], True))
new_idx += 1
continue
Expand All @@ -256,20 +254,20 @@ def make_diff(old: str, new: str):
# "foo": "bar"
# }
if begin_of_complex(old_s) and regular_line_ending(new_s):
indent_of_old = old_s.replace(old_s.strip(), "")
indent_of_old = old_s.replace(old_s.strip(), '')
old_list.append((old_s, True))
new_list.append((new_s, True))
while not has_indent(old_lines[old_idx], indent_of_old):
new_list.append(("", True))
new_list.append(('', True))
old_list.append((old_lines[old_idx], True))
old_idx += 1
new_list.append(("", True))
new_list.append(('', True))
old_list.append((old_lines[old_idx], True))
old_idx += 1
continue
if begin_of_complex(old_s) and begin_of_complex(new_s):
indent_of_old = old_s.replace(old_s.strip(), "")
indent_of_new = new_s.replace(new_s.strip(), "")
indent_of_old = old_s.replace(old_s.strip(), '')
indent_of_new = new_s.replace(new_s.strip(), '')
old_list.append((old_s, False))
new_list.append((new_s, False))
old_tmp = []
Expand All @@ -280,7 +278,7 @@ def make_diff(old: str, new: str):
while not has_indent(new_lines[new_idx], indent_of_new):
new_tmp.append(new_lines[new_idx])
new_idx += 1
a, b = make_diff("\n".join(old_tmp), "\n".join(new_tmp))
a, b = make_diff('\n'.join(old_tmp), '\n'.join(new_tmp))
assert len(a) == len(b)
old_list += a
new_list += b
Expand All @@ -299,6 +297,5 @@ def make_diff(old: str, new: str):
old_list.append((old_s, True))
new_list.append((new_s, True))
continue
print(len(old_list), len(new_list))
assert len(old_list) == len(new_list)
return old_list, new_list
return old_list, new_list

0 comments on commit 28995c9

Please sign in to comment.