-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathPyArgParser.java
More file actions
131 lines (110 loc) · 2.82 KB
/
PyArgParser.java
File metadata and controls
131 lines (110 loc) · 2.82 KB
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
126
127
128
129
130
131
/*
* Jython Database Specification API 2.0
*
*
* Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
*
*/
package com.ziclix.python.sql.util;
import org.python.core.Py;
import org.python.core.PyObject;
import java.util.HashMap;
import java.util.Map;
/**
* Parse the args and kws for a method call.
*
* @author brian zimmer
*/
public class PyArgParser extends Object {
/** Field keywords. */
protected Map<String, PyObject> keywords;
/** Field arguments. */
protected PyObject[] arguments;
/**
* Construct a parser with the arguments and keywords.
*/
public PyArgParser(PyObject[] args, String[] kws) {
keywords = new HashMap<String, PyObject>();
arguments = null;
parse(args, kws);
}
/**
* Method parse
*
* @param args
* @param kws
*/
protected void parse(PyObject[] args, String[] kws) {
// walk backwards through the kws and build the map
int largs = args.length;
if (kws != null) {
for (int i = kws.length - 1; i >= 0; i--) {
keywords.put(kws[i], args[--largs]);
}
}
arguments = new PyObject[largs];
System.arraycopy(args, 0, arguments, 0, largs);
}
/**
* How many keywords?
*/
public int numKw() {
return keywords.keySet().size();
}
/**
* Does the keyword exist?
*/
public boolean hasKw(String kw) {
return keywords.containsKey(kw);
}
/**
* Return the value for the keyword, raise a KeyError if the keyword does
* not exist.
*/
public PyObject kw(String kw) {
if (!hasKw(kw)) {
throw Py.KeyError(kw);
}
return keywords.get(kw);
}
/**
* Return the value for the keyword, return the default if the keyword does
* not exist.
*/
public PyObject kw(String kw, PyObject def) {
if (!hasKw(kw)) {
return def;
}
return keywords.get(kw);
}
/**
* Get the array of keywords.
*/
public String[] kws() {
return keywords.keySet().toArray(new String[0]);
}
/**
* Get the number of arguments.
*/
public int numArg() {
return arguments.length;
}
/**
* Return the argument at the given index, raise an IndexError if out of range.
*/
public PyObject arg(int index) {
if (index >= 0 && index <= arguments.length - 1) {
return arguments[index];
}
throw Py.IndexError("index out of range");
}
/**
* Return the argument at the given index, or the default if the index is out of range.
*/
public PyObject arg(int index, PyObject def) {
if (index >= 0 && index <= arguments.length - 1) {
return arguments[index];
}
return def;
}
}