odpy.common
Common tools for odpy package
- Copyright (C) dGB Beheer B.V.; (LICENSE) http://opendtect.org/OpendTect_license.txt
AUTHOR : A. Huck
DATE : July 2018
Module Summary
odpy.common is the main important module in odpy. Allows for basic interactions with the OpendTect software and database
KEY methods
getODsoftwareDir()
gets the root directory of the Opendtect installation
getODargs()
get dict containing information about the Opendtect executable, project database path and current survey name
Example
>>> import odpy.common as odcommon
>>> odcommon.getODSoftwareDir()
Windows: 'C:\Program Files\OpendTect\7.0.0'
Linux: '/home/user/OpendTect/7.0.0'
MacOS: '/Applications/OpendTect/OpendTect 7.0.0.app/Contents'
>>> odcommon.getODArgs()
{'dtectexec': ['C:\PROGRA~1\OPENDT~1\new\6683E8~1.0\bin\win64\Release'],
'dtectdata': ['C:\Users\OLAWALE IBRAHIM\DTECT_DATA'],
'survey': ['F3_Demo_2020']}
Attributes
Classes
Create a collection of name/value pairs. |
|
Repeated timer |
Functions
|
OpendTect-like time stamp |
|
odpy Logger initialization |
|
Sets log file with the handler based on the filename |
Returns logger |
|
Returns module logger |
|
|
Concatenates input strings and objects if more than one as single string |
|
Print to odpy standard logger |
|
Print to odpy processing logger |
|
To check if a log file has a file handler |
Checks if module log has a file handler |
|
Checks if log (odproclog) has a file handler |
|
|
Gets log handler stream |
|
Gets logger filename |
Gets current module stream handler |
|
Gets odpy.commom log stream handler |
|
Full log file path for current module |
|
Full log file path for odpy.common |
|
|
Log file reset |
Stdout-stderr redirection |
|
Stdout-stderr restore |
|
|
Is platform Windows? |
|
Is platform Linux? |
|
Is platform Mac? |
|
|
|
OpendTect sofware directory |
|
OpendTect executables directory |
|
OpendTect link libraries directory |
Directory with the OpendTect user settings |
|
|
Get file path to an OpendTect settings file |
Get the OpendTect Survey Data Root directory |
|
Get the OpendTect Current Survey directory |
|
Full path to the current OpendTect Survey directory |
|
|
Returns the OpendTect Survey Data Root in a dictionary |
|
Returns the OpendTect Survey Directory in a dictionary |
|
OpendTect arguments dictionary |
|
Path to an OpendTect icon file |
|
Returns the last line(s) from a file |
|
OpendTect batch processing status |
|
Creates a new file with contents |
Module Contents
- odpy.common.sTimeUnitString(ismilli=False, abbr=True)
OpendTect-like time stamp
- Parameters:
ismilli (bool, optional): Include millisecond (default is False)
abbr (bool, optional): Abbreviated (default is True)
- Returns:
str: Time stamp string formatted like done by OpendTect
- Examples:
>>> sTimeUnitString() 'Mon 20 Apr 2020, 13:59:54'
>>> sTimeUnitString( True ) 'Mon 20 Apr 2020, 13:59:54.001245'
>>> sTimeUnitString( True, True ) 'Mon 20 Apr 2020, 13:59:54.001245'
>>> sTimeUnitString( True, False ) 'Monday 20 April 2020, 13:59:54'
- odpy.common.logconfig = None
- odpy.common.syslog_logger
- odpy.common.proclog_logger
- odpy.common.handler
- odpy.common.handler
- odpy.common.initLogging(args)
odpy Logger initialization
- Parameters:
args (dict): The members ‘logfile’ and ‘sysout’ from the input dictionary are supposed to contain existing filenames which are used to setup the module loggers proclog_logger and syslog_logger
- odpy.common.set_log_file(filenm, logger)
Sets log file with the handler based on the filename
- Parameters:
filenm (str): log file name
logger (object): log object
- Returns:
Nothing. Removes past handlers if any and sets new log handler type based on file name
- odpy.common.get_log_logger()
Returns logger
- odpy.common.get_std_logger()
Returns module logger
- odpy.common.mergeArgs(a, b=None, c=None, d=None, e=None, f=None)
Concatenates input strings and objects if more than one as single string
- Parameters:
a (object or string): Message to be printed
b-f (object or string, optional): Message to be printed
- Returns:
str: Concatenated string
- Notes:
All objects are formatted to strings using the str() function
All outputs are automatically separated by spaces.
- odpy.common.std_msg(a, b=None, c=None, d=None, e=None, f=None)
Print to odpy standard logger
- Parameters:
a (object or string): Message to be printed
b-f (object or string, optional): Message to be printed
- Prints:
str: Concatenated string
- Notes:
All objects are formatted to strings using the str() function
All outputs are automatically separated by spaces.
Reserved for standard logging information.
- odpy.common.log_msg(a, b=None, c=None, d=None, e=None, f=None)
Print to odpy processing logger
- Parameters:
a (object or string): Message to be printed
b-f (object or string, optional): Message to be printed
- Returns:
str: Concatenated string
- Notes:
All objects are formatted to strings using the str() function
All outputs are automatically separated by spaces.
Reserved for processing logging information,
i.e. to report progress on a task
- odpy.common.has_file_handlers(logger)
To check if a log file has a file handler
- Parameters:
logger (object): Logger file object
- Returns:
bool: True if logger is a file handler logger
- odpy.common.has_stdlog_file()
Checks if module log has a file handler
- Returns
bool: True if module log file has a File handler and False if otherwise
- odpy.common.has_log_file()
Checks if log (odproclog) has a file handler
Returns: * bool: True if log file (odproclog) has a File handler and False if otherwise
- odpy.common.get_handler_stream(logger)
Gets log handler stream
- Parameters:
logger (object): log file object
- Returns:
str: where log info streams to if log handler exist,
returns None if handler level is not set
- odpy.common.get_handler_filename(logger)
Gets logger filename
- Parameters:
logger (object): log file object
- Returns:
str: Log file name if any, None if not
- odpy.common.get_std_stream()
Gets current module stream handler
- Returns:
str: log stream handler name
- odpy.common.get_log_stream()
Gets odpy.commom log stream handler
- Returns:
str: log stream handler name
- odpy.common.get_stdlog_file()
Full log file path for current module
- Returs:
str: Path to log file for current module
- odpy.common.get_log_file()
Full log file path for odpy.common
- Returs:
str: Path to log file for odpy.common
- odpy.common.reset_log_file(keeplines=0)
Log file reset
- Parameters:
keeplines (int, optional): Number of lines from the top of the file to keep (default is 0)
Empty the log file pointed at by the processing logger, for instance before starting a new task.
- odpy.common.storedstdout = None
- odpy.common.storedstderr = None
- odpy.common.redirect_stdout()
Stdout-stderr redirection
Forces stdout to point to odpy.proclog_logger
Forces stderr to point to odpy.syslog_logger
- Notes:
Changes the value of sys.stdout and sys.stderr
Should be avoided as much as possible: To be used only when one cannot redirect in any other way.
- odpy.common.restore_stdout()
Stdout-stderr restore
Undo operation of redirect_stdout
- odpy.common.isWin()
Is platform Windows?
- Returns:
True if running on any Windows platform
- odpy.common.isLux()
Is platform Linux?
- Returns:
True if running on any Linux platform
- odpy.common.isMac()
Is platform Mac?
- Returns:
True if running on any Mac-OS platform
- odpy.common.isPossibleODSoftwareDir(curdir)
- odpy.common.getODSoftwareDir(args=None)
OpendTect sofware directory
- Parameters:
args (dict, optional): Dictionary with the member ‘dtectexec’. The value for that member should point to the executables folder of the requested application
- Returns:
str: Full path to the OpendTect software installation
- Notes:
First search method: retrieved from the input dictionary.
Second search method: if odpy is located within an OpendTect installation, the path to this installation will be returned.
Third search method: retrieved by reading the ‘DTECT_APPL’ variables which are set by OpendTect at runtime.
Fourth search method: If all everything else fails, the PATH variable will be inspected to detect available OpendTect installations.
- Examples:
>>> getODSoftwareDir() Windows: 'C:\Program Files\OpendTect\7.0.0' Linux: '/home/user/OpendTect/7.0.0' MacOS: '/Applications/OpendTect/OpendTect 7.0.0.app/Contents'
- odpy.common.findODSoftwarePath()
- class odpy.common.BuildConfig(*args, **kwds)
Bases:
enum.EnumCreate a collection of name/value pairs.
Example enumeration:
>>> class Color(Enum): ... RED = 1 ... BLUE = 2 ... GREEN = 3
Access them by:
attribute access:
>>> Color.RED <Color.RED: 1>
value lookup:
>>> Color(1) <Color.RED: 1>
name lookup:
>>> Color['RED'] <Color.RED: 1>
Enumerations can be iterated over, and know how many members they have:
>>> len(Color) 3
>>> list(Color) [<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]
Methods can be added to enumerations, and members can have their own attributes – see the documentation for details.
- AUTO = 0
- Release = 1
- Debug = 2
- odpy.common.getExecPlfDir(args=None, config=BuildConfig.AUTO)
OpendTect executables directory
- Parameters:
args (dict, optional): Dictionary with the member ‘dtectexec’. The value for that member should point to the executables folder of the requested application
- Returns:
str: Full path to the binaries of an OpendTect installation
- Notes:
Assumes that the software installation can be located with getODSoftwareDir() and that python is running on a platform supported by OpendTect.
- Examples:
>>> getExecPlfDir() Windows: 'C:\Program Files\OpendTect\7.0.0\bin\win64\Release' Linux: '/home/user/OpendTect/7.0.0/bin/lux64/Release' MacOS: '/Applications/OpendTect/OpendTect 7.0.0.app/Contents/MacOS'
- odpy.common.getLibPlfDir(args=None, config=BuildConfig.AUTO)
OpendTect link libraries directory
- Parameters:
args (dict, optional): Dictionary with the member ‘dtectexec’. The value for that member should point to the executables folder of the requested application
- Returns:
str: Full path to the libraries of an OpendTect installation
- Examples:
>>> getLibPlfDir() Windows: 'C:\Program Files\OpendTect\7.0.0\bin\win64\Release' Linux: '/home/user/OpendTect/7.0.0/bin/lux64/Release' MacOS: '/Applications/OpendTect/OpendTect 7.0.0.app/Contents/Frameworks'
- odpy.common.get_settings_dir()
Directory with the OpendTect user settings
Parameters
Can be overruled by setting the environment variable DTECT_PERSONAL_DIR
Returns
Full path to $HOME/.od
- odpy.common.get_settings_filename(filenm='settings')
Get file path to an OpendTect settings file
Parameters
filenm (string, optional): base file name inside .od folder
default is ‘settings’ Will be influenced by the environment variable DTECT_USER if set.
Returns
Full path to $HOME/.od/filenm[.DTECT_USER]
- odpy.common.get_base_datadir()
Get the OpendTect Survey Data Root directory
Parameters
Can be overruled by setting the environment variable DTECT_DATA
Returns
Full path to the Opendtect Survey Data Root as written inside from get_settings_filename()
- odpy.common.get_surveydir()
Get the OpendTect Current Survey directory
Returns
Directory name of the current survey as written inside get_settings_filename( ‘survey’ )
- odpy.common.get_data_dir()
Full path to the current OpendTect Survey directory
Returns
Full path to the current survey as retrieved by get_base_datadir() and get_surveydir()
- odpy.common.add_user_dtectdata(args=None)
Returns the OpendTect Survey Data Root in a dictionary
Parameters
args (dict, optional) Dictionary where the returned value is added/updated
Returns
Dictionary with the member ‘dtectdata’. The value for that member should point to the return of get_base_datadir()
- odpy.common.add_user_survey(args=None)
Returns the OpendTect Survey Directory in a dictionary
Parameters
args (dict, optional) Dictionary where the returned value is added/updated
Returns
Dictionary with the member ‘survey’. The value for that member should point to the return of get_surveydir()
- odpy.common.getODArgs(args=None)
OpendTect arguments dictionary
Create a dictionary that contains typical OpendTect command line arguments and the files from this module’ loggers
- Parameters:
args (dict, optional): Dictionary with the member ‘dtectexec’. The value for that member should point to the executables folder of the requested application
- Returns:
- dict: A dictionary with the following key-values:
‘dtectexec’ : Full path to the OpendTect installation (see getExecPlfDir)
‘dtectdata’ : The root projects directory name
‘survey’ : The survey directory name
‘proclog’ : The log file from proclog_logger if applicable
‘syslog’ : The log file frol syslog_logger if applicable
- odpy.common.getIconFp(nm, args=None)
Path to an OpendTect icon file
- Parameters:
nm (string): Icon file basename (without extension)
args (dict, optional) Dictionary with the member ‘dtectexec’. The value for that member should point to the executables folder of the requested application
- Returns:
str: Full path to the icon folder of the OpendTect installation
- Notes:
Assumes that the software installation can be located with getODSoftwareDir()
- Examples:
>>> getIconFp( 'dgbpro' ) 'C:\Program Files\OpendTect\6.6.0\data\icons.Default\dgbpro.png'
- odpy.common.tail(fp, lines=1, strip_empty=False, _buffer=4098)
Returns the last line(s) from a file
- Parameters:
fp (object): opened log file
lines (int): number of last lines in a file to be printed
strip_empty (bool): removes empty new lines if True buffer (int):
- odpy.common.batchIsFinished(logfile)
OpendTect batch processing status
Checks if an OpendTect batch process reached completion by parsing its log file.
- Parameters:
logfile (str): Full path to an existing OpendTect log file.
- Returns:
bool: True if the file exists and contains ‘Finished batch processing’ at its tail.
- Notes:
Instantaneous status. To monitor if the processing finishes, the user must repeatedly call this function.
- odpy.common.writeFile(fnm, content)
Creates a new file with contents
- Parameters:
fnm (str): name of file to be created/written to content (str): content to be added to the file
- Returns:
bool: True if file has been successfully created False if file isn’t created due to an error
- class odpy.common.Timer(interval, function, args=None, kwargs=None)
Bases:
threading.TimerRepeated timer
Timer that restarts automatically after each interval
- run()
Method representing the thread’s activity.
You may override this method in a subclass. The standard run() method invokes the callable object passed to the object’s constructor as the target argument, if any, with sequential and keyword arguments taken from the args and kwargs arguments, respectively.