Class PrePy
- java.lang.Object
-
- org.python.core.PrePy
-
- Direct Known Subclasses:
Py
public class PrePy extends java.lang.ObjectThis class is part of the Jython run-time system, and contains only "pre-Python" data and methods that may safely be used before the type system is ready. The Jython type system springs into existence in response to a program's first use of anyPyObject, 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 examplePyorPySystemState. The present class is intended to hold utility methods and configuration useful in the pre-Python phase.
-
-
Field Summary
Fields Modifier and Type Field Description static intCOMMENTOptions.verboselevel providing detail during correct functioning.static intDEBUGOptions.verboselevel providing detail in support of debugging or tracing.static intERROROptions.verboselevel indicating an error that prevents correct results.static intMESSAGEOptions.verboselevel for messages that confirm correct functioning.static intWARNINGOptions.verboselevel indicating an unexpected event, still working correctly.
-
Constructor Summary
Constructors Constructor Description PrePy()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringgetCommandResult(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.StringgetDefaultBinDir()Infers the usual Jython bin-dir from the position of the jar-file returned bygetJarFileName()byr replacing the file name with "bin".static java.lang.StringgetDefaultExecutableName()Infers the usual Jython executable name from the position of the jar-file returned bygetJarFileName()by replacing the file name with "bin/jython".static java.lang.StringgetJarFileName()Utility-method to obtain the name (including absolute path) of the currently used jython-jar-file.static java.lang.StringgetJarFileNameFromURL(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.LevelgetLoggingLevel()Convenience function to get the effective level of Logger "org.python".static java.util.PropertiesgetSystemProperties()Get the System properties if we are allowed to.static java.lang.StringgetSystemProperty(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 booleanhaveConsole()Returntrueiff the console is accessible through System.console().static voidincreaseLoggingLevel(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 booleanisInteractive()Determine whether standard input is an interactive stream.static booleanisInteractive(java.io.InputStream fp, java.lang.String filename)Check whether an input stream is interactive.static java.util.logging.LevellevelFromVerbosity(int verbosity)Translate from the traditional "verbosity" system to JUL Level.static voidmaybeWrite(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.LevelsetLoggingLevel(java.util.logging.Level newLevel)Set the level of the Jython logger "org.python" using the standardjava.util.loggingscale.static intverbosityFromLevel(java.util.logging.Level level)Translate from JUL Level to equivalent in the traditional "verbosity" system.static voidwriteComment(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level COMMENT.static voidwriteDebug(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level DEBUG.static voidwriteError(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level ERROR.static voidwriteMessage(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level MESSAGE.static voidwriteWarning(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level WARNING.
-
-
-
Field Detail
-
ERROR
public static final int ERROR
Options.verboselevel indicating an error that prevents correct results.- See Also:
- Constant Field Values
-
WARNING
public static final int WARNING
Options.verboselevel indicating an unexpected event, still working correctly.- See Also:
- Constant Field Values
-
MESSAGE
public static final int MESSAGE
Options.verboselevel for messages that confirm correct functioning.- See Also:
- Constant Field Values
-
COMMENT
public static final int COMMENT
Options.verboselevel providing detail during correct functioning.- See Also:
- Constant Field Values
-
DEBUG
public static final int DEBUG
Options.verboselevel providing detail in support of debugging or tracing.- See Also:
- Constant Field Values
-
-
Method Detail
-
levelFromVerbosity
public static java.util.logging.Level levelFromVerbosity(int verbosity)
-
verbosityFromLevel
public static int verbosityFromLevel(java.util.logging.Level level)
Translate from JUL Level to equivalent in the traditional "verbosity" system. We return Jython verbosity values beyond the conventional range, enough to enumerate the Java standard levels (e.gFINERreturns 4 andALLreturns 6 ).- Parameters:
level-java.util.logging.Levelto translate.- Returns:
- integer verbosity, where the runtime default
INFO= 1
-
getLoggingLevel
public static java.util.logging.Level getLoggingLevel()
Convenience function to get the effective level of Logger "org.python".
-
setLoggingLevel
public static java.util.logging.Level setLoggingLevel(java.util.logging.Level newLevel)
Set the level of the Jython logger "org.python" using the standardjava.util.loggingscale. For backward compatibility with the traditional "verbosity" system, make a corresponding setting ofOptions.verbose.- Parameters:
newLevel- to set- Returns:
- previous logging level
-
increaseLoggingLevel
public 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. This is primarily for the command-line Jython, where each "-v" increases the verbosity by one, on thejava.util.loggingscale.- Parameters:
n- increment on the scale1=INFO, 2=CONFIG, 3=FINE, ...
-
maybeWrite
public 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).
-
writeError
public static void writeError(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level ERROR.
-
writeWarning
public static void writeWarning(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level WARNING.
-
writeMessage
public static void writeMessage(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level MESSAGE.
-
writeComment
public static void writeComment(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level COMMENT.
-
writeDebug
public static void writeDebug(java.lang.String type, java.lang.String msg)Submit a message to logging at the severity level DEBUG.
-
getSystemProperties
public static java.util.Properties getSystemProperties()
Get the System properties if we are allowed to. Configuration values set via-Dprop=valueto the java command will be found here. If a security manager prevents access, we will return a new (empty) object instead.- Returns:
Systemproperties or a newPropertiesobject
-
getSystemProperty
public 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.- Parameters:
key- of the entry to returndefaultValue- to return if null or disallowed- Returns:
- property value or given default
-
isInteractive
public static boolean isInteractive()
Determine whether standard input is an interactive stream. If the Java system propertypython.launcher.ttyis defined and equal totrueorfalse, then that provides the result. This property is normally supplied by the launcher. In the absence of this certainty, we usehaveConsole().- Returns:
- true if (we think) standard input is an interactive stream
-
haveConsole
public static boolean haveConsole()
Returntrueiff the console is accessible through System.console().
-
isInteractive
public static boolean isInteractive(java.io.InputStream fp, java.lang.String filename)Check whether an input stream is interactive. This emulates CPythonPy_FdIsInteractivewithin the constraints of pure Java. The input stream is considered ``interactive'' if either- it is
System.inandisInteractive()istrue, or - the
-iflag was given (Options.interactive=true), and the filename associated with it isnullor"<stdin>"or"???".
- Parameters:
fp- stream (tested only forSystem.in)filename-- Returns:
- true iff thought to be interactive
- it is
-
getDefaultExecutableName
public static java.lang.String getDefaultExecutableName()
Infers the usual Jython executable name from the position of the jar-file returned bygetJarFileName()by replacing the file name with "bin/jython". This is intended as an easy fallback for cases wheresys.executableisNonedue 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.executableto get the actual executable (may beNone.- Returns:
- usual Jython-executable as absolute path
-
getDefaultBinDir
public static java.lang.String getDefaultBinDir()
Infers the usual Jython bin-dir from the position of the jar-file returned bygetJarFileName()byr replacing the file name with "bin". This is intended as an easy fallback for cases wheresys.executableisnulldue 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.
- Returns:
- usual Jython bin-dir as absolute path
-
getJarFileName
public static java.lang.String getJarFileName()
Utility-method to obtain the name (including absolute path) of the currently used jython-jar-file. Usually this is jython.jar, but can also be jython-dev.jar or jython-standalone.jar or something custom.- Returns:
- the full name of the jar file containing this class,
nullif not available.
-
getJarFileNameFromURL
public 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. Classes in Java can be asked for the URL of their associated resources (including their own class definition), and so the caller of this method may present such a URL as the basis for locating the JAR from which it came.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 usingjarFile = 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 thevfszip: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 Javafile:protocol. The path returned will be correct only if this naive swap is valid.- Parameters:
url- into the JAR- Returns:
- the file path or
nullin the event of a detectable error
-
getCommandResult
public 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. It returns "" on any kind of error.- Parameters:
command- as strings (as forProcessBuilder)- Returns:
- the first line with content, or ""
-
-