Vega strike Python Modules doc  0.5.1
Documentation of the " Modules " folder of Vega strike
 All Data Structures Namespaces Files Functions Variables
custom Namespace Reference

Data Structures

class  IOmessageWriter
 

Functions

def add
 
def generateID
 
def splitArgs
 
def joinArgs
 
def putFunction
 
def getFunction
 
def run
 
def respond
 
def processMessage
 

Variables

dictionary procedures
 
dictionary running_cmds = {}
 
int maxid = 2
 

Function Documentation

def custom.add (   name,
  proc 
)

Definition at line 8 of file custom.py.

8 
9 def add(name, proc):
10  procedures[name.lower()] = proc
def custom.generateID ( )

Definition at line 20 of file custom.py.

References locale.str().

20 
21 def generateID():
22  global maxid;
23  maxid=maxid+1
24  return str(maxid)
def custom.getFunction (   id,
  cp 
)

Definition at line 76 of file custom.py.

References locale.str().

76 
77 def getFunction(id, cp):
78  key = str(cp)+","+id
79  if running_cmds.has_key(key):
80  func = running_cmds[key]
81  del running_cmds[key]
82  return func
83  return None
84 
85 # custom.run should be the last thing that happens in a function/
86 # it might either be synchronous or asynchronous (this could be considered a bug)
def custom.joinArgs (   arglist)

Definition at line 53 of file custom.py.

References pydoc.replace(), and locale.str().

53 
54 def joinArgs(arglist):
55  ret = ''
56  for arg in arglist:
57  if ret:
58  ret += ' '
59  arg = str(arg).replace('\'','') # Remove all single-quotes
60  space = arg.find(' ')
61  quote = arg.find('"')
62  newstr = arg.replace('"','""')
63  if not newstr or space!=-1 or quote!=-1:
64  ret += '"' + newstr + '"'
65  else:
66  ret += newstr
67  return ret
def custom.processMessage (   local,
  cmd,
  argstr,
  id 
)

Definition at line 116 of file custom.py.

References getFunction(), traceback.print_exc(), server.processMessage(), putFunction(), respond(), run(), splitArgs(), and locale.str().

117 def processMessage(local, cmd, argstr, id):
118  cp = VS.getCurrentPlayer();
119  cmd = cmd.lower()
120  print "======= Processing message "+str(id)+" ======="
121  try:
122  args = splitArgs(argstr)
123  print "Command: "+cmd
124  for arg in args:
125  print arg
126  if cmd=='reloadlib' and local and len(args)>=1:
127  reload(__import__(args[0]))
128  VS.IOmessage(0, "game", "p"+str(cp), "Reloaded "+str(args[0]))
129  elif cmd=='local':
130  # simple way of bouncing back message to client....
131  if id:
132  def localresponse(args):
133  respond(args, None, id, cp)
134  else:
135  localresponse = None
136  run(args[0], args[1:], localresponse, id, cp)
137  elif (cmd=='response'):
138  func = getFunction(id, cp)
139  if func:
140  ret = func(args)
141  if ret and isinstance(ret, tuple) and len(ret)==2:
142  respond(ret[0], ret[1], id, cp)
143  elif ret==True:
144  putFunction(func, id, cp)
145  elif ret:
146  respond(ret, None, id, cp)
147  elif procedures.has_key(cmd):
148  ret = procedures[cmd](local, cmd, args, id)
149  if ret and isinstance(ret, tuple) and len(ret)==2:
150  respond(ret[0], ret[1], id, cp)
151  elif ret:
152  respond(ret, None, id, cp)
153  elif VS.isserver():
154  import server
155  server.processMessage(cp, local, cmd, args, id)
156  else:
157  print "Command "+repr(cmd)+" does not exist. Available functions:"
158  print procedures.keys()
159  except:
160  if id or cp<0:
161  writer = sys.stderr
162  else:
163  writer = IOmessageWriter(cp)
164  writer.write("An error occurred when processing custom command: \n"
165  + str(cmd)+" "+argstr + "\n")
166  traceback.print_exc(file=writer)
167  print "-------------------------- " +str(id)+" -------"
168 
def custom.putFunction (   continuation,
  id,
  cp 
)

Definition at line 68 of file custom.py.

References generateID(), and locale.str().

68 
69 def putFunction(continuation, id, cp):
70  global running_cmds;
71  if not id:
72  id = generateID()
73  key = str(cp)+","+id
74  running_cmds[key] = continuation
75  return id
def custom.respond (   args,
  continuation,
  id,
  cp = -1 
)

Definition at line 98 of file custom.py.

References run().

98 
99 def respond(args, continuation, id, cp=-1):
100  run("response", args, continuation, id, cp)
def custom.run (   cmd,
  args,
  continuation,
  id = None,
  cp = -1 
)

Definition at line 87 of file custom.py.

References joinArgs(), putFunction(), and locale.str().

87 
88 def run(cmd, args, continuation, id=None, cp=-1):
89  if -1==cp:
90  cp = VS.getCurrentPlayer()
91  if continuation:
92  id = putFunction(continuation, id, cp)
93  if not isinstance(id,str):
94  id = "null"
95  print "running: "+cmd+", "+str(args)+"; id: "+id
96  VS.sendCustom(cp, cmd, joinArgs(args), id)
97  return id
def custom.splitArgs (   argstr)

Definition at line 25 of file custom.py.

25 
26 def splitArgs(argstr):
27  ret=[]
28  while argstr:
29  arg = ''
30  qadd = ''
31  empty = True
32  while argstr and argstr[0]=='"':
33  end=argstr.find('"', 1)
34  if end!=-1:
35  arg += qadd + argstr[1:end]
36  qadd='"'
37  argstr = argstr[end+1:]
38  empty = False
39  else:
40  arg = argstr[1:]
41  empty = False
42  argstr = ''
43  space = argstr.find(' ')
44  if space != -1:
45  arg += argstr[:space]
46  argstr = argstr[space+1:]
47  else:
48  arg += argstr
49  argstr = ''
50  if arg or not empty:
51  ret.append(arg)
52  return ret

Variable Documentation

int maxid = 2

Definition at line 19 of file custom.py.

dictionary procedures
Initial value:
1 = {
2  }

Definition at line 5 of file custom.py.

dictionary running_cmds = {}

Definition at line 17 of file custom.py.