-
Notifications
You must be signed in to change notification settings - Fork 0
/
new_creature.py
executable file
·125 lines (89 loc) · 3.07 KB
/
new_creature.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
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
#!/usr/bin/python
import cgi
import os
import sys
import getopt
import traceback
import string
import logging
import Location
import Picklable
import Experiment
import UsageError
def usage( msg=None ):
if msg:
sys.stderr.write( "ERROR: %s\n" % (msg) )
sys.stderr.write( "Usage: %s [--debug] -e exp [-p parent]\n" % (os.path.basename(sys.argv[0])) )
sys.stderr.write( " exp: The name of the experiment in which to create the creature.\n")
sys.stderr.write( " parent: The name of the parent creature to mutate. If not given, a branch new creature is created.\n")
sys.exit(-1)
def main():
try:
Experiment.initLogging(os.environ.has_key("GATEWAY_INTERFACE"))
if os.environ.has_key("GATEWAY_INTERFACE"):
# CGI
(odict, args) = getCGIOptions()
else:
# Command line. All the output is still CGI-ish, though. Sorry.
(odict, args) = getOptions()
(odict, args) = processOptions(odict, args)
data = newCreature(odict, odict["e"], odict.get("p"))
data = Location.getJsonModule().dumps(data, indent=2)
print "Content-type: application/json"
print "Content-length: %s" % (len(data))
print
print data
except:
msg = string.join(apply( traceback.format_exception, sys.exc_info() ), "")
if (msg[-1] == "\n"):
msg = msg[:-1]
logging.getLogger().warning(msg)
data = "Huh?\n%s" % (msg)
print "Status: 500 Internal Server Error"
print "Content-type: text/plain"
print "Content-length: %s" % (len(data))
print
print data
def newCreature(odict, exp, parent=None):
exp = Experiment.Experiment(exp)
creature = exp.newCreature(parent)
return creature.getInfo()
def getCGIOptions():
odict = cgi.parse()
args = []
if not odict.has_key("e"):
raise UsageError.UsageError("No experiment specified")
odict["e"] = odict["e"][0]
if odict.has_key("p"):
odict["p"] = odict["p"][0]
return (odict, args)
def getOptions():
try:
(tt, args) = getopt.getopt( sys.argv[1:], "he:p:", ["help", "debug"] )
except getopt.error:
usage( str(sys.exc_info()[1]) )
odict = {}
for t in tt:
s = t[0]
while (s[0] == "-"): # Strip leading '-'s
s = s[1:]
if not odict.has_key( s ):
odict[s] = []
odict[s] = t[1]
if (odict.has_key("h") or odict.has_key("help")):
usage()
if not odict.has_key("e"):
usage()
return odict, args
def processOptions(odict, args):
if (len(args) > 0):
usage()
if not Picklable.isValidID(odict["e"]):
raise Picklable.InvalidID(odict["e"])
if odict.has_key("p") and not Picklable.isValidID(odict["p"]):
raise Picklable.InvalidID(odict["p"])
if odict.has_key("debug"):
logging.getLogger().setLevel(logging.DEBUG)
return (odict, args)
if __name__ == '__main__':
main()