-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspell_check_cli.py
executable file
·87 lines (72 loc) · 2.13 KB
/
spell_check_cli.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
#!/usr/bin/env python3
import re
import getopt, sys, socket
EXIT_SUCCESS=0
EXIT_USAGE_ERROR=1
DEFAULT_PORT=11111
def usage(exec_name):
print("usage: %s -p port -s server_name -f file_name" % (exec_name))
def words_from_file(filename):
try:
f = open(filename, "r")
words = re.split(r"[,.;:?\s]+", f.read())
f.close()
return words
except IOError:
print("Error opening %s for reading. Quitting" % (filename))
exit()
def main():
verbose=False
filename=None
hostname="localhost"
port=DEFAULT_PORT
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:s:p:v",
["help", "server=", "file=", "port="])
except getopt.GetoptError as err:
print("unrecognized option")
usage(sys.argv[0])
sys.exit(EXIT_USAGE_ERROR)
output = None
verbose = False
for o, a in opts:
if o == "-v":
verbose = True
elif o in ("-h", "--help"):
usage(sys.argv[0])
sys.exit()
elif o in ("-f", "--file"):
filename=a
print("file name:", filename)
elif o in ("-s", "--server"):
host=a
print("host:", host)
elif o in ("-p", "--port"):
try:
port=int(a)
print("port:", port)
except ValueError:
print("port should be an integer")
sys.exit(EXIT_USAGE_ERROR)
else:
assert False, "unhandled option"
if verbose:
print("using host %s:%d to spell check %s ..." % (hostname, port, filename))
if filename==None:
usage(sys.argv[0])
sys.exit(EXIT_USAGE_ERROR)
words = words_from_file(filename)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
inf=s.makefile(mode="r")
outf=s.makefile(mode="w")
for word in words:
outf.write("%s\n" % (word))
outf.flush()
line = inf.readline()
print("%s" % (line))
outf.close()
inf.close()
return EXIT_SUCCESS
if __name__ == "__main__":
sys.exit(main())