annotate bin/addwhatis @ 12256:821155c00e34 draft

<fizzie> ` sed -e \'s|wisdom|bin|\' < ../bin/culprits > ../bin/cblprits; chmod a+x ../bin/cblprits
author HackEso <hackeso@esolangs.org>
date Sat, 07 Dec 2019 23:36:22 +0000
parents 0124f9ed4c49
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
12232
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
1 #!/usr/bin/python3
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
2 import sys, os, re, getopt
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
3 # allow options for future compatibility
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
4 opts, args = getopt.getopt(sys.argv[1:], "")
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
5 newd = dict()
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
6 def procnew(arg):
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
7 match = re.fullmatch(r"([^\x00\r\n()]+\([0-9A-Z_a-z]+\))"
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
8 r"(?: ?| - ([^\x00\r\n]*?))\r?\n?", arg)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
9 if match:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
10 key, val = match.group(1), match.group(2)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
11 if key in newd:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
12 print("addwhatis: duplicate key in input: %r" % (key,), file = sys.stderr)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
13 sys.exit(2)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
14 else:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
15 newd[key] = val
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
16 else:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
17 print("addwhatis: cannot parse input as whatis line or whatis key: %r" % (line,), file = sys.stderr)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
18 sys.exit(2)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
19 if args:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
20 for arg in args:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
21 procnew(arg)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
22 else:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
23 for line in sys.stdin:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
24 procnew(line)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
25 sharedir = os.environ.get("HACKENV", "/hackenv") + "/share"
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
26 inputfname = sharedir + "/whatis"
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
27 outputfname = sharedir + "/whatis.tmp"
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
28 with open(inputfname, errors = "surrogateescape", newline = "\n") as inputf, \
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
29 open(outputfname, "x", errors = "surrogatepass") as outputf:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
30 for line in inputf:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
31 keep = True
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
32 match = re.match(r"([^\x00\r\n()]+\([0-9A-Z_a-z]+\)) - ", line)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
33 if match:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
34 key = match.group(1)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
35 if key in newd:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
36 keep = False
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
37 if None == newd[key]:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
38 print("addwhatis: dropped %r" % (key,), file = sys.stderr)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
39 else:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
40 outputf.write("%s - %s\n" % (key, newd[key]))
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
41 print("addwhatis: replaced %r" % (key,), file = sys.stderr)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
42 del newd[key]
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
43 if keep:
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
44 outputf.write(line)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
45 for key, val in newd.items():
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
46 outputf.write("%s - %s\n" % (key, val))
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
47 print("addwhatis: added %r" % (key,), file = sys.stderr)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
48 os.rename(outputfname, inputfname)
0124f9ed4c49 <b_jonas> fetch /hackenv/bin/addwhatis https://hack.esolangs.org/get/bin/addwhatis
HackEso <hackeso@esolangs.org>
parents:
diff changeset
49