001: // Copyright (c) Corporation for National Research Initiatives
002: package org.python.core;
003:
004: /**
005: * A class with static fields for each of the settable options. The options from
006: * registry and command line is copied into the fields here and the rest of
007: * Jyhton checks these fields.
008: */
009: public class Options {
010: // Jython options. Some of these can be set from the command line
011: // options, but all can be controlled through the JPython registry
012:
013: /**
014: * when an exception occurs in Java code, and it is not caught, should the
015: * interpreter print out the Java exception in the traceback?
016: */
017: public static boolean showJavaExceptions = false;
018:
019: /**
020: * When true, python exception raised in overriden methods will be shown on
021: * stderr. This option is remarkable usefull when python is used for
022: * implementing CORBA server. Some CORBA servers will turn python exception
023: * (say a NameError) into an anonymous user exception without any
024: * stacktrace. Setting this option will show the stacktrace.
025: */
026: public static boolean showPythonProxyExceptions = false;
027:
028: /**
029: * To force JIT compilation of Jython code -- should be unnecessary Setting
030: * this to true will cause jdk1.2rc1 to core dump on Windows
031: */
032: public static boolean skipCompile = true;
033:
034: /**
035: * Setting this to true will cause the console to poll standard in. This
036: * might be helpful on systems without system-level threads.
037: */
038: public static boolean pollStandardIn = false;
039:
040: /**
041: * If true, JPython respects Java the accessibility flag for fields,
042: * methods, and constructors. This means you can only access public members.
043: * Set this to false to access all members by toggling the accessible flag
044: * on the member.
045: */
046: public static boolean respectJavaAccessibility = true;
047:
048: /**
049: * When false the <code>site.py</code> will not be imported. This is only
050: * honored by the command line main class.
051: */
052: public static boolean importSite = true;
053:
054: /**
055: * Set verbosity to Py.ERROR, Py.WARNING, Py.MESSAGE, Py.COMMENT, or
056: * Py.DEBUG for varying levels of informative messages from Jython. Normally
057: * this option is set from the command line.
058: */
059: public static int verbose = Py.MESSAGE;
060:
061: /**
062: * Setting this to true will support old 1.0 style keyword+"_" names. This
063: * isn't needed any more due to improvements in the parser
064: */
065: public static boolean deprecatedKeywordMangling = false;
066:
067: /**
068: * A directory where the dynamicly generated classes are written. Nothing is
069: * ever read from here, it is only for debugging purposes.
070: */
071: public static String proxyDebugDirectory = null;
072:
073: /**
074: * If true, Jython will use the first module found on sys.path where java
075: * File.isFile() returns true. Setting this to true have no effect on
076: * unix-type filesystems. On Windows/HFS+ systems setting it to true will
077: * enable Jython-2.0 behaviour.
078: */
079: public static boolean caseok = false;
080:
081: /**
082: * If true, enable truedivision for the '/' operator.
083: */
084: public static boolean Qnew = false;
085:
086: /**
087: * Enable division warning. The value maps to the registry values of
088: * <ul>
089: * <li>old: 0</li>
090: * <li>warn: 1</li>
091: * <li>warnall: 2</li>
092: * </ul>
093: */
094: public static int divisionWarning = 0;
095:
096: //
097: // ####### END OF OPTIONS
098: //
099:
100: private Options() {
101: ;
102: }
103:
104: private static boolean getBooleanOption(String name,
105: boolean defaultValue) {
106: String prop = PySystemState.registry.getProperty("python."
107: + name);
108: if (prop == null) {
109: return defaultValue;
110: }
111: return prop.equalsIgnoreCase("true")
112: || prop.equalsIgnoreCase("yes");
113: }
114:
115: private static String getStringOption(String name,
116: String defaultValue) {
117: String prop = PySystemState.registry.getProperty("python."
118: + name);
119: if (prop == null) {
120: return defaultValue;
121: }
122: return prop;
123: }
124:
125: /**
126: * Initialize the static fields from the registry options.
127: */
128: public static void setFromRegistry() {
129: // Set the more unusual options
130: Options.showJavaExceptions = getBooleanOption(
131: "options.showJavaExceptions",
132: Options.showJavaExceptions);
133:
134: Options.showPythonProxyExceptions = getBooleanOption(
135: "options.showPythonProxyExceptions",
136: Options.showPythonProxyExceptions);
137:
138: Options.skipCompile = getBooleanOption("options.skipCompile",
139: Options.skipCompile);
140:
141: Options.deprecatedKeywordMangling = getBooleanOption(
142: "deprecated.keywordMangling",
143: Options.deprecatedKeywordMangling);
144:
145: Options.pollStandardIn = getBooleanOption("console.poll",
146: Options.pollStandardIn);
147:
148: Options.respectJavaAccessibility = getBooleanOption(
149: "security.respectJavaAccessibility",
150: Options.respectJavaAccessibility);
151:
152: Options.proxyDebugDirectory = getStringOption(
153: "options.proxyDebugDirectory",
154: Options.proxyDebugDirectory);
155:
156: // verbosity is more complicated:
157: String prop = PySystemState.registry
158: .getProperty("python.verbose");
159: if (prop != null) {
160: if (prop.equalsIgnoreCase("error")) {
161: Options.verbose = Py.ERROR;
162: } else if (prop.equalsIgnoreCase("warning")) {
163: Options.verbose = Py.WARNING;
164: } else if (prop.equalsIgnoreCase("message")) {
165: Options.verbose = Py.MESSAGE;
166: } else if (prop.equalsIgnoreCase("comment")) {
167: Options.verbose = Py.COMMENT;
168: } else if (prop.equalsIgnoreCase("debug")) {
169: Options.verbose = Py.DEBUG;
170: } else {
171: throw Py.ValueError("Illegal verbose option setting: '"
172: + prop + "'");
173: }
174: }
175:
176: Options.caseok = getBooleanOption("options.caseok",
177: Options.caseok);
178:
179: Options.Qnew = getBooleanOption("options.Qnew", Options.Qnew);
180:
181: prop = PySystemState.registry
182: .getProperty("python.divisionWarning");
183: if (prop != null) {
184: if (prop.equalsIgnoreCase("old")) {
185: Options.divisionWarning = 0;
186: } else if (prop.equalsIgnoreCase("warn")) {
187: Options.divisionWarning = 1;
188: } else if (prop.equalsIgnoreCase("warnall")) {
189: Options.divisionWarning = 2;
190: } else {
191: throw Py.ValueError("Illegal divisionWarning option "
192: + "setting: '" + prop + "'");
193: }
194: }
195: // additional initializations which must happen after the registry
196: // is guaranteed to be initialized.
197: JavaAccessibility.initialize();
198: }
199: }
|