aboutsummaryrefslogtreecommitdiffstats
path: root/icebox
diff options
context:
space:
mode:
authorClifford Wolf <clifford@clifford.at>2018-06-15 17:58:58 +0200
committerGitHub <noreply@github.com>2018-06-15 17:58:58 +0200
commit492a337202ee25ae47b4a22c99343d7ae5e40e43 (patch)
tree6a08e0245cf7b1fa79f6fb7371116c1a4da4a02a /icebox
parent6596f00ae177d21edf33e0c2718c5df5c61e5772 (diff)
parentbb025493aae0ce64b0740d8aa8d297c541a04fce (diff)
downloadicestorm-492a337202ee25ae47b4a22c99343d7ae5e40e43.tar.gz
icestorm-492a337202ee25ae47b4a22c99343d7ae5e40e43.tar.bz2
icestorm-492a337202ee25ae47b4a22c99343d7ae5e40e43.zip
Merge pull request #160 from mithro/hlc-sort
icebox_hlcsort: Adding a tool for canonicalizing HLC files.
Diffstat (limited to 'icebox')
-rwxr-xr-xicebox/icebox_hlcsort.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/icebox/icebox_hlcsort.py b/icebox/icebox_hlcsort.py
new file mode 100755
index 0000000..8696449
--- /dev/null
+++ b/icebox/icebox_hlcsort.py
@@ -0,0 +1,41 @@
+#!/usr/bin/env python3
+import sys
+
+top_levels = []
+f = open(sys.argv[1])
+
+current_block_stack = [top_levels]
+while len(current_block_stack) > 0:
+ line = f.readline()
+ if not line:
+ break
+
+ if '#' in line:
+ continue
+
+ if '{' in line:
+ new_block = []
+ current_block_stack[-1].append(new_block)
+ current_block_stack.append(new_block)
+
+ if line.strip():
+ current_block_stack[-1].append(line)
+
+ if '}' in line or not line:
+ assert len(current_block_stack) > 1 or not line, current_block_stack
+ old_block = current_block_stack.pop(-1)
+ sorted_block = [old_block[0],] + sorted(old_block[1:-1], key=lambda x: repr(x)) + [old_block[-1],]
+ old_block.clear()
+ old_block.extend(sorted_block)
+
+top_levels = list(sorted(top_levels, key=lambda x: repr(x)))
+
+output_stack = [top_levels]
+while len(output_stack) > 0:
+ if len(output_stack[0]) == 0:
+ output_stack.pop(0)
+ continue
+ if type(output_stack[0][0]) == list:
+ output_stack.insert(0, output_stack[0].pop(0))
+ continue
+ print(output_stack[0].pop(0), end='')