Merge branch 'master' of ansgar.seatribe.se:/usr/local/src/pdm
[pdm.git] / pdm-repl
1 #!/usr/bin/python3
2
3 import sys, getopt, readline
4 import pdm.cli
5
6 def usage(out):
7     out.write("usage: pdm-repl [-h] SOCKET\n")
8
9 opts, args = getopt.getopt(sys.argv[1:], "h")
10 for o, a in opts:
11     if o == "-h":
12         usage(sys.stdout)
13         sys.exit(0)
14 if len(args) < 1:
15     usage(sys.stderr)
16     sys.exit(1)
17 try:
18     cl = pdm.cli.replclient(args[0])
19 except Exception as e:
20     sys.stderr.write("%s: %s\n" % (args[0], e))
21     sys.exit(1)
22
23 buf = ""
24 while True:
25     try:
26         if buf != "":
27             line = input("  ")
28         else:
29             line = input("% ")
30     except EOFError:
31         break
32     if line == "":
33         sys.stdout.write(cl.run(buf))
34         buf = ""
35     else:
36         if buf == "":
37             try:
38                 compile(line, "Nought", "eval")
39             except:
40                 pass
41             else:
42                 sys.stdout.write(cl.run(line))
43                 continue
44         buf += line + "\n"