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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
|
# -*- coding: utf-8 -*-
import codecs
import sys
def sort_items(input_filename, output_filename):
input_file = codecs.open(input_filename, "r", encoding="utf-8")
lines = [line.rstrip("\r\n") for line in input_file]
input_file.close()
def line_type(line):
if line == "" or line.strip() == "":
return "empty"
if line.startswith("#"):
return "comment"
if "\t" in line:
return "entry"
raise ValueError("Invalid dictionary line: " + line)
parsed = []
for line in lines:
parsed.append({"type": line_type(line), "content": line})
entry_lines = [i for i, p in enumerate(parsed) if p["type"] == "entry"]
if not entry_lines:
header_blocks = []
current = []
for p in parsed:
if p["type"] == "comment":
current.append(p["content"])
elif p["type"] == "empty":
if current:
header_blocks.append(list(current))
current = []
if current:
header_blocks.append(list(current))
output_file = open(output_filename, "wb")
for idx, block in enumerate(header_blocks):
for line in block:
output_file.write((line + "\n").encode("utf-8"))
if idx < len(header_blocks) - 1:
output_file.write(b"\n")
if header_blocks:
output_file.write(b"\n")
output_file.close()
return
first_entry = entry_lines[0]
last_entry = entry_lines[-1]
header_end = -1
for i in range(first_entry - 1, -1, -1):
if parsed[i]["type"] == "empty":
header_end = i
break
header_blocks = []
current = []
for i in range(0, header_end + 1):
if parsed[i]["type"] == "comment":
current.append(parsed[i]["content"])
elif parsed[i]["type"] == "empty":
if current:
header_blocks.append(list(current))
current = []
if current:
header_blocks.append(list(current))
footer_blocks = []
current = []
for i in range(last_entry + 1, len(parsed)):
if parsed[i]["type"] == "comment":
current.append(parsed[i]["content"])
elif parsed[i]["type"] == "empty":
if current:
footer_blocks.append(list(current))
current = []
if current:
footer_blocks.append(list(current))
annotated_entries = []
floating_blocks = []
current = []
entry_index = 0
for i in range(header_end + 1, last_entry + 1):
p = parsed[i]
if p["type"] == "comment":
current.append(p["content"])
continue
if p["type"] == "empty":
if current:
floating_blocks.append({"anchor": entry_index, "lines": list(current)})
current = []
continue
if p["type"] == "entry":
attached = None
if current:
has_empty = False
for j in range(i - 1, -1, -1):
if parsed[j]["type"] == "entry":
break
if parsed[j]["type"] == "empty":
has_empty = True
break
if has_empty:
floating_blocks.append({"anchor": entry_index, "lines": list(current)})
else:
attached = list(current)
current = []
key, value = p["content"].split("\t", 1)
annotated_entries.append(
{
"key": key,
"value": value,
"attached": attached,
"original_index": entry_index,
}
)
entry_index += 1
if current:
floating_blocks.append({"anchor": entry_index, "lines": list(current)})
annotated_entries.sort(key=lambda e: e["key"])
index_map = {e["original_index"]: i for i, e in enumerate(annotated_entries)}
for block in floating_blocks:
if block["anchor"] in index_map:
block["anchor"] = index_map[block["anchor"]]
else:
block["anchor"] = len(annotated_entries)
floating_by_anchor = {}
for block in floating_blocks:
floating_by_anchor.setdefault(block["anchor"], []).append(block["lines"])
output_file = open(output_filename, "wb")
for idx, block in enumerate(header_blocks):
for line in block:
output_file.write((line + "\n").encode("utf-8"))
if idx < len(header_blocks) - 1:
output_file.write(b"\n")
if header_blocks and annotated_entries:
output_file.write(b"\n")
for i, entry in enumerate(annotated_entries):
for block in floating_by_anchor.get(i, []):
output_file.write(b"\n")
for line in block:
output_file.write((line + "\n").encode("utf-8"))
output_file.write(b"\n")
if entry["attached"]:
for line in entry["attached"]:
output_file.write((line + "\n").encode("utf-8"))
output_file.write(
(entry["key"] + "\t" + entry["value"] + "\n").encode("utf-8")
)
for block in floating_by_anchor.get(len(annotated_entries), []):
output_file.write(b"\n")
for line in block:
output_file.write((line + "\n").encode("utf-8"))
if footer_blocks:
if annotated_entries:
output_file.write(b"\n")
for idx, block in enumerate(footer_blocks):
for line in block:
output_file.write((line + "\n").encode("utf-8"))
if idx < len(footer_blocks) - 1:
output_file.write(b"\n")
output_file.close()
def reverse_items(input_filename, output_filename):
input_file = codecs.open(input_filename, "r", encoding="utf-8")
dic = {}
for line in input_file:
stripped = line.strip()
if not stripped or stripped.startswith("#"):
continue
key, value = line.split("\t")
while value[-1] == "\n" or value[-1] == "\r":
value = value[:-1]
value_list = value.split(" ")
for value in value_list:
if value in dic:
dic[value].append(key)
else:
dic[value] = [key]
input_file.close()
output_file = open(output_filename, "wb")
for key in sorted(dic.keys()):
line = key + "\t" + " ".join(dic[key]) + "\n"
output_file.write(line.encode('utf-8'))
output_file.close()
def find_target_items(input_filename, keyword):
input_file = codecs.open(input_filename, "r", encoding="utf-8")
for line in input_file:
stripped = line.strip()
if not stripped or stripped.startswith("#"):
continue
key, value = line.split("\t")
while value[-1] == "\n" or value[-1] == "\r":
value = value[:-1]
value_list = value.split(" ")
for value in value_list:
if keyword in value:
sys.stdout.write(line)
input_file.close()
|