python.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

  • getBinSubDir()

    • binary sub directory for executables in an OpendTect installation

    • returns information on the release type

  • getODargs()

    • get dict containing information about the Opendtect executable, project database path and current survey name

Example

>>> import odpy.common as odcommon
>>> odcommon.getODSoftwareDir()
    'C:\PROGRA~1\OPENDT~1\new\6683E8~1.0'
>>> odcommon.getBinSubDir()
    'Release'
>>> 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']}

Module Contents

Classes

Timer

Repeated timer

Functions

sTimeUnitString(ismilli=False, abbr=True)

OpendTect-like time stamp

initLogging(args)

odpy Logger initialization

set_log_file(filenm, logger)

Sets log file with the handler based on the filename

get_log_logger()

Returns logger

get_std_logger()

Returns module logger

mergeArgs(a, b=None, c=None, d=None, e=None, f=None)

Concatenates input strings and objects if more than one as single string

std_msg(a, b=None, c=None, d=None, e=None, f=None)

Print to odpy standard logger

log_msg(a, b=None, c=None, d=None, e=None, f=None)

Print to odpy processing logger

has_file_handlers(logger)

To check if a log file has a file handler

has_stdlog_file()

Checks if module log has a file handler

has_log_file()

Checks if log (odproclog) has a file handler

get_handler_stream(logger)

Gets log handler stream

get_handler_filename(logger)

Gets logger filename

get_std_stream()

Gets current module stream handler

get_log_stream()

Gets odpy.commom log stream handler

get_stdlog_file()

Full log file path for current module

get_log_file()

Full log file path for odpy.common

reset_log_file(keeplines=0)

Log file reset

redirect_stdout()

Stdout-stderr redirection

restore_stdout()

Stdout-stderr restore

isWin()

Is platform Windows?

isLux()

Is platform Linux?

isMac()

Is platform Mac?

getPlfSubDir()

Platform sub-directory

getBinSubDir()

Binary sub-directory

getODSoftwareDir(args=None)

OpendTect sofware directory

getODBinaryDir(srcpath)

Get the binary directory from the source directory

getExecPlfDir(args=None)

OpendTect executables directory

get_settings_dir()

Directory with the OpendTect user settings

get_settings_filename(filenm='settings')

Get file path to an OpendTect settings file

get_base_datadir()

Get the OpendTect Survey Data Root directory

get_surveydir()

Get the OpendTect Current Survey directory

get_data_dir()

Full path to the current OpendTect Survey directory

add_user_dtectdata(args=None)

Returns the OpendTect Survey Data Root in a dictionary

add_user_survey(args=None)

Returns the OpendTect Survey Directory in a dictionary

getODArgs(args=None)

OpendTect arguments dictionary

getIconFp(nm, args=None)

Path to an OpendTect icon file

tail(fp, lines=1, strip_empty=False, _buffer=4098)

Returns the last line(s) from a file

batchIsFinished(logfile)

OpendTect batch processing status

writeFile(fnm, content)

Creates a new file with contents

Attributes

logconfig

syslog_logger

proclog_logger

handler

handler

storedstdout

storedstderr

python.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'
python.odpy.common.logconfig
python.odpy.common.syslog_logger
python.odpy.common.proclog_logger
python.odpy.common.handler
python.odpy.common.handler
python.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

python.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

python.odpy.common.get_log_logger()

Returns logger

python.odpy.common.get_std_logger()

Returns module logger

python.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.

python.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.

python.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

python.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

python.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

python.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

python.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

python.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

python.odpy.common.get_std_stream()

Gets current module stream handler

Returns:
  • str: log stream handler name

python.odpy.common.get_log_stream()

Gets odpy.commom log stream handler

Returns:
  • str: log stream handler name

python.odpy.common.get_stdlog_file()

Full log file path for current module

Returs:
  • str: Path to log file for current module

python.odpy.common.get_log_file()

Full log file path for odpy.common

Returs:
  • str: Path to log file for odpy.common

python.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.

python.odpy.common.storedstdout
python.odpy.common.storedstderr
python.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.

python.odpy.common.restore_stdout()

Stdout-stderr restore

Undo operation of redirect_stdout

python.odpy.common.isWin()

Is platform Windows?

Returns:
  • True if running on any Windows platform

python.odpy.common.isLux()

Is platform Linux?

Returns:
  • True if running on any Linux platform

python.odpy.common.isMac()

Is platform Mac?

Returns:
  • True if running on any Mac-OS platform

python.odpy.common.getPlfSubDir()

Platform sub-directory

Platform specific sub-directory as existing in an OpendTect software installation

Returns:
  • str: string like: ‘lux64’, ‘win64’, ‘mac’. None if the platform is not supported by OpendTect

python.odpy.common.getBinSubDir()

Binary sub-directory

Sub-directory containing the executables in an OpendTect installation

Returns:
  • str: string like: ‘Debug’, ‘Release’, ‘RelWithDebInfo’. None if no such sub-directory is found.

Notes:

Assumes that the OpendTect installation contains the executable ‘od_FileBrowser’, and that this installation can be located by the function getODSoftwareDir()

python.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:
  • Retrieved from either the input dictionary or from the current environment by reading the ‘DTECT_APPL’ or ‘DTECT_WINAPPL’ variables which are set by OpendTect at runtime.

  • Neither dictionary nor environment variables need to be set if the current module is placed within an OpendTect installation

Examples:
>>> getODSoftwareDir()
'C:\Program Files\OpendTect\6.6.0'
python.odpy.common.getODBinaryDir(srcpath)
Get the binary directory from the source directory

Only useful for development environments

srcpathFull path to a directory

Path to the OpendTect source directory, configured with cmake

Full path to OpendTect binary directory

python.odpy.common.getExecPlfDir(args=None)

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()
'C:\Program Files\OpendTect\6.6.0\bin\win64\Release'
python.odpy.common.get_settings_dir()

Directory with the OpendTect user settings

Can be overruled by setting the environment variable DTECT_PERSONAL_DIR

Full path to $HOME/.od

python.odpy.common.get_settings_filename(filenm='settings')

Get file path to an OpendTect settings file

  • filenm (string, optional): base file name inside .od folder

default is ‘settings’ Will be influenced by the environment variable DTECT_USER if set.

Full path to $HOME/.od/filenm[.DTECT_USER]

python.odpy.common.get_base_datadir()

Get the OpendTect Survey Data Root directory

Can be overruled by setting the environment variable DTECT_DATA

Full path to the Opendtect Survey Data Root as written inside from get_settings_filename()

python.odpy.common.get_surveydir()

Get the OpendTect Current Survey directory

Directory name of the current survey as written inside get_settings_filename( ‘survey’ )

python.odpy.common.get_data_dir()

Full path to the current OpendTect Survey directory

Full path to the current survey as retrieved by get_base_datadir() and get_surveydir()

python.odpy.common.add_user_dtectdata(args=None)

Returns the OpendTect Survey Data Root in a dictionary

  • args (dict, optional) Dictionary where the returned value is added/updated

Dictionary with the member ‘dtectdata’. The value for that member should point to the return of get_base_datadir()

python.odpy.common.add_user_survey(args=None)

Returns the OpendTect Survey Directory in a dictionary

  • args (dict, optional) Dictionary where the returned value is added/updated

Dictionary with the member ‘survey’. The value for that member should point to the return of get_surveydir()

python.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

python.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'
python.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):

python.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.

python.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 python.odpy.common.Timer(interval, function, args=None, kwargs=None)

Bases: threading.Timer

Repeated timer

Timer that restarts automatically after each interval

run(self)

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.