KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > Options


1 // Copyright (c) Corporation for National Research Initiatives
2
package org.python.core;
3
4 /**
5  * A class with static fields for each of the settable options.
6  * The options from registry and command line is copied into
7  * the fields here and the rest of Jyhton checks these fields.
8  */

9 public class Options
10 {
11     // JPython options. Some of these can be set from the command line
12
// options, but all can be controlled through the JPython registry
13

14     /**
15      * when an exception occurs in Java code, and it is not caught, should
16      * the interpreter print out the Java exception in the traceback?
17      */

18     public static boolean showJavaExceptions = false;
19
20     /**
21      * When true, python exception raised in overriden methods will
22      * be shown on stderr. This option is remarkable usefull when
23      * python is used for implementing CORBA server. Some CORBA
24      * servers will turn python exception (say a NameError) into an
25      * anonymous user exception without any stacktrace. Setting this
26      * option will show the stacktrace.
27      */

28     public static boolean showPythonProxyExceptions = false;
29
30     /**
31      * To force JIT compilation of Jython code -- should be unnecessary
32      * Setting this to true will cause jdk1.2rc1 to core dump on Windows
33      */

34     public static boolean skipCompile = true;
35
36     /**
37      * Setting this to true will cause the console to poll standard in.
38      * This might be helpful on systems without system-level threads.
39      */

40     public static boolean pollStandardIn = false;
41
42     /**
43      * If true, JPython respects Java the accessibility flag for fields,
44      * methods, and constructors. This means you can only access public
45      * members. Set this to false to access all members by toggling the
46      * accessible flag on the member.
47      */

48     public static boolean respectJavaAccessibility = true;
49
50     /**
51      * When false the <code>site.py</code> will not be imported.
52      * This is only honored by the command line main class.
53      */

54     public static boolean importSite = true;
55
56     /**
57      * Set verbosity to Py.ERROR, Py.WARNING, Py.MESSAGE, Py.COMMENT,
58      * or Py.DEBUG for varying levels of informative messages from
59      * Jython. Normally this option is set from the command line.
60      */

61     public static int verbose = Py.MESSAGE;
62
63     /**
64      * Setting this to true will support old 1.0 style keyword+"_" names.
65      * This isn't needed any more due to improvements in the parser
66      */

67     public static boolean deprecatedKeywordMangling = false;
68
69     /**
70      * A directory where the dynamicly generated classes are written.
71      * Nothing is ever read from here, it is only for debugging
72      * purposes.
73      */

74     public static String JavaDoc proxyDebugDirectory = null;
75
76     /**
77      * If true, Jython will use the first module found on sys.path
78      * where java File.isFile() returns true. Setting this to true
79      * have no effect on unix-type filesystems. On Windows/HPS+
80      * systems setting it to true will enable Jython-2.0 behaviour.
81      */

82     public static boolean caseok = false;
83
84     /**
85      * If true, enable truedivision for the '/' operator.
86      */

87     public static boolean Qnew = false;
88
89     /**
90      * Enable division warning. The value maps to the registry values of
91      * <ul>
92      * <li>old: 0</li>
93      * <li>warn: 1</li>
94      * <li>warnall: 2</li>
95      * </ul>
96      */

97     public static int divisionWarning = 0;
98
99     //
100
// ####### END OF OPTIONS
101
//
102

103     private Options() { ; }
104
105     private static boolean getBooleanOption(String JavaDoc name, boolean defaultValue)
106     {
107         String JavaDoc prop = PySystemState.registry.getProperty("python."+name);
108         if (prop == null)
109             return defaultValue;
110         return prop.equalsIgnoreCase("true") || prop.equalsIgnoreCase("yes");
111     }
112
113     private static String JavaDoc getStringOption(String JavaDoc name, String JavaDoc defaultValue) {
114         String JavaDoc prop = PySystemState.registry.getProperty("python."+name);
115         if (prop == null)
116             return defaultValue;
117         return prop;
118     }
119
120
121     /**
122      * Initialize the static fields from the registry options.
123      */

124     public static void setFromRegistry() {
125         // Set the more unusual options
126
Options.showJavaExceptions =
127             getBooleanOption("options.showJavaExceptions",
128                              Options.showJavaExceptions);
129
130         Options.showPythonProxyExceptions =
131             getBooleanOption("options.showPythonProxyExceptions",
132                              Options.showPythonProxyExceptions);
133
134         Options.skipCompile =
135             getBooleanOption("options.skipCompile", Options.skipCompile);
136
137         Options.deprecatedKeywordMangling =
138             getBooleanOption("deprecated.keywordMangling",
139                              Options.deprecatedKeywordMangling);
140
141         Options.pollStandardIn =
142             getBooleanOption("console.poll", Options.pollStandardIn);
143
144         Options.respectJavaAccessibility =
145             getBooleanOption("security.respectJavaAccessibility",
146                              Options.respectJavaAccessibility);
147
148         Options.proxyDebugDirectory =
149             getStringOption("options.proxyDebugDirectory",
150                              Options.proxyDebugDirectory);
151
152         // verbosity is more complicated:
153
String JavaDoc prop = PySystemState.registry.getProperty("python.verbose");
154         if (prop != null) {
155             if (prop.equalsIgnoreCase("error"))
156                 Options.verbose = Py.ERROR;
157             else if (prop.equalsIgnoreCase("warning"))
158                 Options.verbose = Py.WARNING;
159             else if (prop.equalsIgnoreCase("message"))
160                 Options.verbose = Py.MESSAGE;
161             else if (prop.equalsIgnoreCase("comment"))
162                 Options.verbose = Py.COMMENT;
163             else if (prop.equalsIgnoreCase("debug"))
164                 Options.verbose = Py.DEBUG;
165             else
166                 throw Py.ValueError("Illegal verbose option setting: '"+
167                                     prop+"'");
168         }
169
170         Options.caseok =
171             getBooleanOption("options.caseok", Options.caseok);
172
173         Options.Qnew =
174             getBooleanOption("options.Qnew", Options.Qnew);
175
176         prop = PySystemState.registry.getProperty("python.divisionWarning");
177         if (prop != null) {
178             if (prop.equalsIgnoreCase("old"))
179                 Options.divisionWarning = 0;
180             else if (prop.equalsIgnoreCase("warn"))
181                 Options.divisionWarning = 1;
182             else if (prop.equalsIgnoreCase("warnall"))
183                 Options.divisionWarning = 2;
184             else
185                 throw Py.ValueError("Illegal divisionWarning option " +
186                                     "setting: '"+ prop+"'");
187         }
188         // additional initializations which must happen after the registry
189
// is guaranteed to be initialized.
190
JavaAccessibility.initialize();
191     }
192 }
193
Popular Tags