Skip to content

sessioninfo

Manage information about pyRevit sessions.

Attributes

mlogger = get_logger(__name__) module-attribute

RuntimeInfo = namedtuple('RuntimeInfo', ['pyrevit_version', 'engine_version', 'host_version']) module-attribute

Session runtime information tuple.

Parameters:

Name Type Description Default
pyrevit_version str

formatted pyRevit version

required
engine_version int

active IronPython engine version

required
host_version str

Current Revit version

required

Functions

setup_runtime_vars()

Setup runtime environment variables with session information.

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def setup_runtime_vars():
    """Setup runtime environment variables with session information."""
    # set pyrevit version
    pyrvt_ver = versionmgr.get_pyrevit_version().get_formatted()
    envvars.set_pyrevit_env_var(envvars.VERSION_ENVVAR, pyrvt_ver)

    # set app version env var
    if HOST_APP.is_newer_than(2017):
        envvars.set_pyrevit_env_var(envvars.APPVERSION_ENVVAR,
                                    HOST_APP.subversion)
    else:
        envvars.set_pyrevit_env_var(envvars.APPVERSION_ENVVAR,
                                    HOST_APP.version)

    # set ironpython engine version env var
    attachment = user_config.get_current_attachment()
    if attachment and attachment.Clone:
        envvars.set_pyrevit_env_var(envvars.CLONENAME_ENVVAR,
                                    attachment.Clone.Name)
        envvars.set_pyrevit_env_var(envvars.IPYVERSION_ENVVAR,
                                    str(attachment.Engine.Version))
    else:
        mlogger.debug('Can not determine attachment.')
        envvars.set_pyrevit_env_var(envvars.CLONENAME_ENVVAR, "Unknown")
        envvars.set_pyrevit_env_var(envvars.IPYVERSION_ENVVAR, "0")

    # set cpython engine version env var
    cpyengine = user_config.get_active_cpython_engine()
    if cpyengine:
        envvars.set_pyrevit_env_var(envvars.CPYVERSION_ENVVAR,
                                    str(cpyengine.Version))
    else:
        envvars.set_pyrevit_env_var(envvars.CPYVERSION_ENVVAR, "0")

    # set a list of important assemblies
    # this is required for dotnet script execution
    set_loaded_pyrevit_referenced_modules(
        runtime.get_references()
        )

get_runtime_info()

Return runtime information tuple.

Returns:

Type Description
RuntimeInfo

runtime info tuple

Examples:

sessioninfo.get_runtime_info()
Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def get_runtime_info():
    """Return runtime information tuple.

    Returns:
        (RuntimeInfo): runtime info tuple

    Examples:
        ```python
        sessioninfo.get_runtime_info()
        ```
    """
    # FIXME: add example output
    return RuntimeInfo(
        pyrevit_version=envvars.get_pyrevit_env_var(envvars.VERSION_ENVVAR),
        engine_version=envvars.get_pyrevit_env_var(envvars.IPYVERSION_ENVVAR),
        host_version=envvars.get_pyrevit_env_var(envvars.APPVERSION_ENVVAR)
        )

set_session_uuid(uuid_str)

Set session uuid on environment variable.

Parameters:

Name Type Description Default
uuid_str str

session uuid string

required
Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def set_session_uuid(uuid_str):
    """Set session uuid on environment variable.

    Args:
        uuid_str (str): session uuid string
    """
    envvars.set_pyrevit_env_var(envvars.SESSIONUUID_ENVVAR, uuid_str)

get_session_uuid()

Read session uuid from environment variable.

Returns:

Type Description
str

session uuid string

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def get_session_uuid():
    """Read session uuid from environment variable.

    Returns:
        (str): session uuid string
    """
    return envvars.get_pyrevit_env_var(envvars.SESSIONUUID_ENVVAR)

new_session_uuid()

Create a new uuid for a pyRevit session.

Returns:

Type Description
str

session uuid string

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def new_session_uuid():
    """Create a new uuid for a pyRevit session.

    Returns:
        (str): session uuid string
    """
    uuid_str = safe_strtype(coreutils.new_uuid())
    set_session_uuid(uuid_str)
    return uuid_str

get_loaded_pyrevit_assemblies()

Return list of loaded pyRevit assemblies from environment variable.

Returns:

Type Description
list[str]

list of loaded assemblies

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def get_loaded_pyrevit_assemblies():
    """Return list of loaded pyRevit assemblies from environment variable.

    Returns:
        (list[str]): list of loaded assemblies
    """
    # FIXME: verify and document return type
    loaded_assms_str = envvars.get_pyrevit_env_var(envvars.LOADEDASSMS_ENVVAR)
    if loaded_assms_str:
        return loaded_assms_str.split(coreutils.DEFAULT_SEPARATOR)
    else:
        return []

set_loaded_pyrevit_assemblies(loaded_assm_name_list)

Set the environment variable with list of loaded assemblies.

Parameters:

Name Type Description Default
loaded_assm_name_list list[str]

list of assembly names

required
Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def set_loaded_pyrevit_assemblies(loaded_assm_name_list):
    """Set the environment variable with list of loaded assemblies.

    Args:
        loaded_assm_name_list (list[str]): list of assembly names
    """
    envvars.set_pyrevit_env_var(
        envvars.LOADEDASSMS_ENVVAR,
        coreutils.DEFAULT_SEPARATOR.join(loaded_assm_name_list)
        )

get_loaded_pyrevit_referenced_modules()

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def get_loaded_pyrevit_referenced_modules():
    loaded_assms_str = envvars.get_pyrevit_env_var(envvars.REFEDASSMS_ENVVAR)
    if loaded_assms_str:
        return set(loaded_assms_str.split(coreutils.DEFAULT_SEPARATOR))
    else:
        return set()

set_loaded_pyrevit_referenced_modules(loaded_assm_name_list)

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def set_loaded_pyrevit_referenced_modules(loaded_assm_name_list):
    envvars.set_pyrevit_env_var(
        envvars.REFEDASSMS_ENVVAR,
        coreutils.DEFAULT_SEPARATOR.join(loaded_assm_name_list)
        )

update_loaded_pyrevit_referenced_modules(loaded_assm_name_list)

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def update_loaded_pyrevit_referenced_modules(loaded_assm_name_list):
    loaded_modules = get_loaded_pyrevit_referenced_modules()
    loaded_modules.update(loaded_assm_name_list)
    set_loaded_pyrevit_referenced_modules(loaded_modules)

report_env()

Report python version, home directory, config file, etc.

Source code in pyrevitlib/pyrevit/loader/sessioninfo.py
def report_env():
    """Report python version, home directory, config file, etc."""
    # run diagnostics
    system_diag()

    # get python version that includes last commit hash
    mlogger.info('pyRevit version: %s - </> with :growing_heart: in %s',
                 envvars.get_pyrevit_env_var(envvars.VERSION_ENVVAR),
                 about.get_pyrevit_about().madein)

    if user_config.rocket_mode:
        mlogger.info('pyRevit Rocket Mode enabled. :rocket:')

    mlogger.info('Host is %s pid: %s', HOST_APP.pretty_name, HOST_APP.proc_id)
    # ipy 2.7.10 has a new line in its sys.version :rolling-eyes-emoji:
    mlogger.info('Running on: %s', sys.version.replace('\n', ' '))
    mlogger.info('User is: %s', HOST_APP.username)
    mlogger.info('Home Directory is: %s', HOME_DIR)
    mlogger.info('Session uuid is: %s', get_session_uuid())
    mlogger.info('Runtime assembly is: %s', runtime.RUNTIME_ASSM_NAME)
    mlogger.info('Config file is (%s): %s',
                 user_config.config_type, user_config.config_file)