1 9 package com.ziclix.python.sql.util; 10 11 import org.python.core.Py; 12 import org.python.core.PyObject; 13 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 23 public class PyArgParser extends Object { 24 25 28 protected Map keywords; 29 30 33 protected PyObject[] arguments; 34 35 38 public PyArgParser(PyObject[] args, String [] kws) { 39 40 this.keywords = new HashMap (); 41 this.arguments = null; 42 43 parse(args, kws); 44 } 45 46 52 protected void parse(PyObject[] args, String [] kws) { 53 54 int largs = args.length; 56 57 if (kws != null) { 58 for (int i = kws.length - 1; i >= 0; i--) { 59 keywords.put(kws[i], args[--largs]); 60 } 61 } 62 63 this.arguments = new PyObject[largs]; 64 65 System.arraycopy(args, 0, this.arguments, 0, largs); 66 } 67 68 71 public int numKw() { 72 return this.keywords.keySet().size(); 73 } 74 75 78 public boolean hasKw(String kw) { 79 return this.keywords.containsKey(kw); 80 } 81 82 86 public PyObject kw(String kw) { 87 88 if (!hasKw(kw)) { 89 throw Py.KeyError(kw); 90 } 91 92 return (PyObject) this.keywords.get(kw); 93 } 94 95 99 public PyObject kw(String kw, PyObject def) { 100 101 if (!hasKw(kw)) { 102 return def; 103 } 104 105 return (PyObject) this.keywords.get(kw); 106 } 107 108 111 public String [] kws() { 112 return (String []) this.keywords.keySet().toArray(new String [0]); 113 } 114 115 118 public int numArg() { 119 return this.arguments.length; 120 } 121 122 125 public PyObject arg(int index) { 126 127 if ((index >= 0) && (index <= this.arguments.length - 1)) { 128 return this.arguments[index]; 129 } 130 131 throw Py.IndexError("index out of range"); 132 } 133 134 137 public PyObject arg(int index, PyObject def) { 138 139 if ((index >= 0) && (index <= this.arguments.length - 1)) { 140 return this.arguments[index]; 141 } 142 143 return def; 144 } 145 } 146 | Popular Tags |