public class PrePy
extends java.lang.Object
PyObject
, for example when creating
the first interpreter. When preparing an application (from the command line options, say) for
creation of the first interpreter, it useful to defer type system creation until pre-Python
configuration is complete. See PEP 432 for further rationale.
Creation of the type system may happen as a side effect of referring using (almost) any object
from a class that statically refers to a PyObject
, for example Py
or
PySystemState
. The present class is intended to hold utility methods and configuration
useful in the pre-Python phase.
Modifier and Type | Field and Description |
---|---|
static int |
COMMENT
Options.verbose level providing detail during correct functioning. |
static int |
DEBUG
Options.verbose level providing detail in support of debugging or tracing. |
static int |
ERROR
Options.verbose level indicating an error that prevents correct results. |
static int |
MESSAGE
Options.verbose level for messages that confirm correct functioning. |
static int |
WARNING
Options.verbose level indicating an unexpected event, still working correctly. |
Constructor and Description |
---|
PrePy() |
Modifier and Type | Method and Description |
---|---|
static java.lang.String |
getCommandResult(java.lang.String... command)
Run a command as a sub-process and return as the result the first line of output that
consists of more than white space.
|
static java.lang.String |
getDefaultBinDir()
Infers the usual Jython bin-dir from the position of the jar-file returned by
getJarFileName() byr replacing the file name with "bin". |
static java.lang.String |
getDefaultExecutableName()
Infers the usual Jython executable name from the position of the jar-file returned by
getJarFileName() by replacing the file name with "bin/jython". |
static java.lang.String |
getJarFileName()
Utility-method to obtain the name (including absolute path) of the currently used
jython-jar-file.
|
static java.lang.String |
getJarFileNameFromURL(java.net.URL url)
Return the path in the file system (as a string) of a JAR located using the URL of a class
file that it contains.
|
static java.util.logging.Level |
getLoggingLevel()
Convenience function to get the effective level of Logger "org.python".
|
static java.util.Properties |
getSystemProperties()
Get the System properties if we are allowed to.
|
static java.lang.String |
getSystemProperty(java.lang.String key,
java.lang.String defaultValue)
Get a System property if it is defined, not null, and we are allowed to access it, otherwise
return the given default.
|
static boolean |
haveConsole()
Return
true iff the console is accessible through System.console(). |
static void |
increaseLoggingLevel(int n)
Adjust the level of the Jython logger "org.python" using the traditional "verbosity" system:
the bigger the number, the lower the logging threshold.
|
static boolean |
isInteractive()
Determine whether standard input is an interactive stream.
|
static boolean |
isInteractive(java.io.InputStream fp,
java.lang.String filename)
Check whether an input stream is interactive.
|
static java.util.logging.Level |
levelFromVerbosity(int verbosity)
Translate from the traditional "verbosity" system to JUL Level.
|
static void |
maybeWrite(java.lang.String type,
java.lang.String msg,
int verbosity)
Log a message at a specified level (if that level is not below the threshold).
|
static java.util.logging.Level |
setLoggingLevel(java.util.logging.Level newLevel)
Set the level of the Jython logger "org.python" using the standard
java.util.logging
scale. |
static int |
verbosityFromLevel(java.util.logging.Level level)
Translate from JUL Level to equivalent in the traditional "verbosity" system.
|
static void |
writeComment(java.lang.String type,
java.lang.String msg)
Submit a message to logging at the severity level COMMENT.
|
static void |
writeDebug(java.lang.String type,
java.lang.String msg)
Submit a message to logging at the severity level DEBUG.
|
static void |
writeError(java.lang.String type,
java.lang.String msg)
Submit a message to logging at the severity level ERROR.
|
static void |
writeMessage(java.lang.String type,
java.lang.String msg)
Submit a message to logging at the severity level MESSAGE.
|
static void |
writeWarning(java.lang.String type,
java.lang.String msg)
Submit a message to logging at the severity level WARNING.
|
public static final int ERROR
Options.verbose
level indicating an error that prevents correct results.public static final int WARNING
Options.verbose
level indicating an unexpected event, still working correctly.public static final int MESSAGE
Options.verbose
level for messages that confirm correct functioning.public static final int COMMENT
Options.verbose
level providing detail during correct functioning.public static final int DEBUG
Options.verbose
level providing detail in support of debugging or tracing.public static java.util.logging.Level levelFromVerbosity(int verbosity)
public static int verbosityFromLevel(java.util.logging.Level level)
FINER
returns 4 and ALL
returns 6 ).level
- java.util.logging.Level
to translate.INFO
= 1public static java.util.logging.Level getLoggingLevel()
public static java.util.logging.Level setLoggingLevel(java.util.logging.Level newLevel)
java.util.logging
scale. For backward compatibility with the traditional "verbosity" system, make a
corresponding setting of Options.verbose
.newLevel
- to setpublic static void increaseLoggingLevel(int n)
java.util.logging
scale.n
- increment on the scale 1=INFO, 2=CONFIG, 3=FINE, ...
public static void maybeWrite(java.lang.String type, java.lang.String msg, int verbosity)
public static void writeError(java.lang.String type, java.lang.String msg)
public static void writeWarning(java.lang.String type, java.lang.String msg)
public static void writeMessage(java.lang.String type, java.lang.String msg)
public static void writeComment(java.lang.String type, java.lang.String msg)
public static void writeDebug(java.lang.String type, java.lang.String msg)
public static java.util.Properties getSystemProperties()
-Dprop=value
to the java command will be found here. If a security manager prevents
access, we will return a new (empty) object instead.System
properties or a new Properties
objectpublic static java.lang.String getSystemProperty(java.lang.String key, java.lang.String defaultValue)
key
- of the entry to returndefaultValue
- to return if null or disallowedpublic static boolean isInteractive()
python.launcher.tty
is defined and equal to true
or false
, then that
provides the result. This property is normally supplied by the launcher. In the absence of
this certainty, we use haveConsole()
.public static boolean haveConsole()
true
iff the console is accessible through System.console().public static boolean isInteractive(java.io.InputStream fp, java.lang.String filename)
Py_FdIsInteractive
within the constraints of pure Java. The input stream is
considered ``interactive'' if either
System.in
and isInteractive()
is true
, or-i
flag was given (Options.interactive
=true
), and the
filename associated with it is null
or "<stdin>"
or "???"
.fp
- stream (tested only for System.in
)filename
- public static java.lang.String getDefaultExecutableName()
getJarFileName()
by replacing the file name with "bin/jython". This is intended as
an easy fallback for cases where sys.executable
is None
due to direct
launching via the java executable.
Note that this does not necessarily return the actual executable, but instead infers the
place where it is usually expected to be. Use sys.executable
to get the actual
executable (may be None
.
public static java.lang.String getDefaultBinDir()
getJarFileName()
byr replacing the file name with "bin". This is intended as an easy
fallback for cases where sys.executable
is null
due to direct launching via
the java executable.
Note that this does not necessarily return the actual bin-directory, but instead infers the place where it is usually expected to be.
public static java.lang.String getJarFileName()
null
if not
available.public static java.lang.String getJarFileNameFromURL(java.net.URL url)
Three protocols are supported, Java JAR-file protocol, and two JBoss protocols "vfs" and "vfszip".
The JAR-file protocol URL, which must be a jar:file:
reference to a contained element
(that is, it has a "!/" part) is able to identify an actual JAR in a file system that may
then be opened using jarFile = new JarFile(jarFileName)
. The path to the JAR is
returned. If the JAR is accessed by another mechanism (http:
say) this will fail.
The JBoss URL must be a reference to a class in vfs:<JAR>/org/python/core/
, or the
same thing using the vfszip:
protocol, where <JAR> stands for the absolute path
to the Jython JAR in VFS. There is no "!/" marker: in JBoss VFS a JAR is treated just like a
directory and can no longer be opened as a JAR. The method essentially just swaps a VFS
protocol for the Java file:
protocol. The path returned will be correct only if this
naive swap is valid.
url
- into the JARnull
in the event of a detectable errorpublic static java.lang.String getCommandResult(java.lang.String... command)
command
- as strings (as for ProcessBuilder
)