1 package org.python.core; 2 3 17 18 public class ArgParser { 19 private String funcname; 21 22 private PyObject[] args; 24 25 private String [] kws; 27 28 private String [] params = null; 30 31 private static Object required = new Object (); 33 private static String [] emptyKws = new String [0]; 34 35 37 private ArgParser(String funcname, PyObject[] args, String [] kws) { 38 this.funcname = funcname; 39 this.args = args; 40 if (kws == null) 41 kws = emptyKws; 42 this.kws = kws; 43 } 44 45 52 public ArgParser(String funcname, PyObject[] args, String [] kws, 53 String p0) { 54 this(funcname, args, kws); 55 this.params = new String [] { p0 }; 56 check(); 57 } 58 59 69 public ArgParser(String funcname, PyObject[] args, String [] kws, 70 String p0, String p1) { 71 this(funcname, args, kws); 72 this.params = new String [] { p0, p1 }; 73 check(); 74 } 75 76 88 public ArgParser(String funcname, PyObject[] args, String [] kws, 89 String p0, String p1, String p2) { 90 this(funcname, args, kws); 91 this.params = new String [] { p0, p1, p2 }; 92 check(); 93 } 94 95 103 public ArgParser(String funcname, PyObject[] args, String [] kws, 104 String [] paramnames) { 105 this(funcname, args, kws); 106 this.params = paramnames; 107 check(); 108 } 109 110 public ArgParser(String funcname, PyObject[] args, String [] kws, 111 String [] paramnames, int minargs) { 112 this(funcname, args, kws); 113 this.params = paramnames; 114 check(); 115 if (!PyBuiltinFunction.DefaultInfo.check(args.length,minargs,this.params.length)) 116 throw PyBuiltinFunction.DefaultInfo.unexpectedCall(args.length, false, funcname, 117 minargs, this.params.length); 118 } 119 120 121 126 public String getString(int pos) { 127 return (String ) getArg(pos, String .class, "string"); 128 } 129 130 135 public String getString(int pos, String def) { 136 return (String ) getArg(pos, String .class, "string", def); 137 } 138 139 140 145 public int getInt(int pos) { 146 return ((PyInteger)getRequiredArg(pos).__int__()).getValue(); 147 } 148 149 154 public int getInt(int pos, int def) { 155 PyObject value = getOptionalArg(pos); 156 if (value == null) 157 return def; 158 return ((PyInteger)value.__int__()).getValue(); 159 } 160 161 162 167 public PyObject getPyObject(int pos) { 168 return getRequiredArg(pos); 169 } 170 171 176 public PyObject getPyObject(int pos, PyObject def) { 177 PyObject value = getOptionalArg(pos); 178 if (value == null) 179 value = def; 180 return value; 181 } 182 183 188 public PyObject getList(int pos) { 189 int kws_start = args.length - kws.length; 190 if (pos < kws_start) { 191 PyObject[] ret = new PyObject[kws_start - pos]; 192 System.arraycopy(args, pos, ret, 0, kws_start - pos); 193 return new PyTuple(ret); 194 } 195 return Py.EmptyTuple; 196 } 197 198 199 private void check() { 200 int nargs = args.length - kws.length; 201 l1: 202 for (int i = 0; i < kws.length; i++) { 203 for (int j = 0; j < params.length; j++) { 204 if (kws[i].equals(params[j])) { 205 if (j < nargs) { 206 throw Py.TypeError("keyword parameter '" + params[j] + 207 "' was given by position and by name"); 208 } 209 continue l1; 210 } 211 } 212 throw Py.TypeError("'" + kws[i] + "' is an invalid keyword " + 213 "argument for this function"); 214 } 215 } 216 217 private PyObject getRequiredArg(int pos) { 218 PyObject ret = getOptionalArg(pos); 219 if (ret == null) 220 throw Py.TypeError(funcname + ": The " + ordinal(pos) + 221 " argument is required"); 222 return ret; 223 } 224 225 private PyObject getOptionalArg(int pos) { 226 int kws_start = args.length - kws.length; 227 if (pos < kws_start) 228 return args[pos]; 229 for (int i = 0; i < kws.length; i++) { 230 if (kws[i].equals(params[pos])) 231 return args[kws_start + i]; 232 } 233 return null; 234 } 235 236 237 private Object getArg(int pos, Class clss, String classname) { 238 return getArg(pos, clss, classname, required); 239 } 240 241 private Object getArg(int pos, Class clss, String classname, Object def) { 242 PyObject value = null; 243 if (def == required) 244 value = getRequiredArg(pos); 245 else { 246 value = getOptionalArg(pos); 247 if (value == null) 248 return def; 249 } 250 251 Object ret = value.__tojava__(clss); 252 if (ret == Py.NoConversion) 253 throw Py.TypeError("argument " + (pos+1) + ": expected " + 254 classname + ", " + Py.safeRepr(value) + 255 " found"); 256 return ret; 257 } 258 259 260 private static String ordinal(int n) { 261 switch(n+1) { 262 case 1: 263 return "1st"; 264 case 2: 265 return "2nd"; 266 case 3: 267 return "3rd"; 268 default: 269 return Integer.toString(n+1)+"th"; 270 } 271 } 272 } 273 | Popular Tags |