# sas.system package

## Subpackages

- sas.system.config package
  - Submodules
  - sas.system.config.config module
    - Configs
    - Brief Outline
    - What Belongs in a Config
    - Making Changes to the Config Class
    - Other Design Decisions made for the Config Class
    - `Config`
  - sas.system.config.config_meta module
    - `ConfigBase`
      - `ConfigBase.config_filename()`
      - `ConfigBase.defaults`
      - `ConfigBase.finalise()`
      - `ConfigBase.load()`
      - `ConfigBase.load_from_file_object()`
      - `ConfigBase.override_with_defaults()`
      - `ConfigBase.save()`
      - `ConfigBase.save_to_file_object()`
      - `ConfigBase.update()`
      - `ConfigBase.validate()`
    - `ConfigLocked`
    - `ConfigMeta`
    - `MalformedFile`
  - sas.system.config.schema_elements module
    - `CoercionError`
    - `SchemaBool`
      - `SchemaBool.coerce()`
      - `SchemaBool.schema_variable_type`
    - `SchemaElement`
      - `SchemaElement.coerce()`
      - `SchemaElement.validate()`
    - `SchemaError`
    - `SchemaFloat`
      - `SchemaFloat.coerce()`
      - `SchemaFloat.schema_variable_type`
    - `SchemaInt`
      - `SchemaInt.coerce()`
      - `SchemaInt.schema_variable_type`
    - `SchemaList`
      - `SchemaList.coerce()`
    - `SchemaNonSpecified`
      - `SchemaNonSpecified.coerce()`
    - `SchemaStr`
      - `SchemaStr.coerce()`
      - `SchemaStr.schema_variable_type`
    - `SchemaVariable`
      - `SchemaVariable.schema_variable_type`
    - `create_schema_element()`
    - `pairwise_schema_union()`
    - `schema_union()`
  - Module contents

## Submodules

## sas.system.console module

Windows console binding for SasView

class sas.system.console.Singleton

  Bases: `type`

  Metaclass indicating that all object instantiations should return the same instance.

  Usage:

  > class Stateful(metaclass=Singleton): …

  The init will only be triggered for the first instance, so you probably shouldn’t parameterize it, or only parameterize it during setup before any other instances are created.

class sas.system.console.WindowsConsole(*args, **kwargs)

  Bases: `object`

  Windows console object.

  This only creates the console when you read to it or write from it. This reduces flashing windows when using the app in a pipeline.

  This presents as an incomplete standard rw file interface.

  Unfortunately it does not regiister as a windows console stdio object so the cpython myreadline code does not call PyOS_InputHook during read. The practical consequence is that pyplot.ion() does not create an interactive plot, and you instead need to call pyplot.pause(0.1) to draw the figure. You can try tracing through myreadline.c to see what we need to do to get sys.stdin recognized as stdin here: https://github.com/python/cpython/blob/main/Parser/myreadline.c

  close_wait()

    Registered with atexit to give users a chance to see the output.

  flush()
  isatty()
  name()
  read(*args, **kwargs)
  readable()
  readline(*args, **kwargs)
  seekable()
  write(*args, **kwargs)
  writeable()

sas.system.console.attach_windows_console()

  Attach a console to a windows program that does not normally have one.

  Note: Uses a lazy import for win32console so you will to add pywin32 to requirements.txt and tell the installer to include win32.win32console

sas.system.console.demo()

sas.system.console.setup_console(stderr_as='console')

  Lazy redirect of stdio to windows console.

  Handling of stderr is defined by the caller:

  - console: create a console for stderr even if stdin/stdout are redirected.

  - stdout: redirect stderr to whereever stdout is going

  - null: redirect stderr to the NUL device (untested!!)

  - none: don’t redirect stderr; instead windows displays an error box with stderr contents

sas.system.console.setup_console_simple(stderr_to_stdout=True)

  Simple version of stdio redirection: always open a console, and don’t pause before closing.

## sas.system.legal module

class sas.system.legal.Legal

  Bases: `object`

## sas.system.lib module

Setup third-party libraries (e.g., qt, sasview, periodictable, bumps)

These functions are used to setup up the GUI and the scripting environment.

sas.system.lib.reset_sasmodels(sas_opencl)

  Trigger a reload of all sasmodels calculators using the new value of sas_opencl. The new value will be saved in the sasview configuration file.

sas.system.lib.setup_qt_env()

  Setup the Qt environment.

  The environment values are set by the user and managed by sasview config.

  This function does not import the Qt libraries so it is safe to use from a script.

sas.system.lib.setup_sasmodels()

  Initialize sasmodels settings from the sasview configuration.

## sas.system.log module

sas.system.log.development()

sas.system.log.print_config(msg='Logger config:')

  When debugging the logging configuration it is handy to see exactly how it is configured. To do so you will need to pip install the logging_tree package and add log.print_config() at choice points in the code.

sas.system.log.production()

sas.system.log.setup_logging(level=20)

## sas.system.resources module

Extract resources from a Python module

Python modules can have resources such as icons or source code within them. The resources will often be sitting beside the Python files in the source tree or when installed, with the exception of PEP 660 editable installs where the resources can be sitting inside a tree in site-packages, while the source code remains in the original source directory.

These utilities can transparently extract resources from the appropriate location.

The approach is:

> 1. Look in the distribution file manifest via importlib.metadata; these are the files that have been installed into site-packages and have been recorded in the dist-info/RECORD manifest. This code calls them “recorded” files.

> 2. Look at the files that are part of the module tree as sitting on disk via importlib.resources; in the case of an installed module, this will cover the same files as #1, but for an editable install, it will only include the files that are in the source tree and not the files in site-packages. This code calls them “adjacent” files.

The approach described above prioritises files that are installed (hence part of a wheel distribution, known to pip, and can be removed when uninstalling).

This module is extensible for future work to

  - remove path calculation of the location of resources

  - provide filehandles to resources rather than copies of resources

  - deduplicate some resources across the codebase

class sas.system.resources.ModuleResources(module: str)

  Bases: `object`

  Locate and extract resources in Python modules

  extract_resource(src: str | PurePath, dest: Path | str) → bool

    Extract a single resource from the module.

    Parameters:

      src: The source path for the file within the module (does not include the module name) dest: The destination path to which the resource will be copied (including filename).

    Returns: True if resource was extracted

  extract_resource_tree(src: str | PurePath, dest: Path | str) → bool

    Extract a tree of resources from the module.

    Parameters:

      src: The source path for the directory within the module (does not include the module name) dest: The destination path to which the resources will be copied recursively.

  path_to_resource(src: str | PurePath) → Path

    Provide the filesystem path to a file resource

    If the resource is already available on the filesystem, then provide the path to it directly; if it is not available, then a FileNotFoundError is raised and the caller should extract into a filesystem location that they can clean up after use.

  path_to_resource_directory(src: str | PurePath) → Path

    Provide the filesystem path to a file resource directory

    If the resource is already available on the filesystem, then provide the path to it directly; if it is not available, then a NotADirectoryError is raised and the caller should extract into a filesystem location that they can clean up after use.

    Note that completely empty directories might not be found by this method as they are not recorded in the dist-info/RECORD.

sas.system.resources.main(argv: list[str]) → bool

  Extract a resource from a Python module.

  Extracts resources such as config files, icons, data files that are part of a Python module.

  The Python module is specified via its importable name (not the distribution name).

  Resources shipped in the following ways are supported:

  - resources that are installed on-disk in a normal pip installed module; these are in site-packages and recorded within the dist-info.

  - resources that are still adjacent to the code and not in site-packages, such as in an editable install.

  - resources that are inside a wheel file (whl) that has been added to PYTHONPATH

  - resources that are inside a module shipped in zip-bundle form

## sas.system.user module

sas.system.user.copy_old_files_to_new_location()

  Only run at app startup A check to see if files in the old user locations exist and move them if they haven’t already been moved.

sas.system.user.copy_resources() → None

  Set up user environment with resource files

sas.system.user.create_user_files_if_needed() → None

  Create user documentation directories if necessary and copy built docs there.

sas.system.user.find_plugins_dir() → str

  A helper function that returns a string representation of the plugins directory as defined by sas.system.user.

sas.system.user.get_app_dir(create_if_nonexistent: bool = True) → Path

  The directory where the os-specific app data is stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_app_dir_versioned(create_if_nonexistent: bool = True) → Path

  The directory where the version-dependent, os-specific app data is stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_cache_dir(create_if_nonexistent: bool = True) → Path

  The directory where the os-specific cache is stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_config_dir(create_if_nonexistent: bool = True) → Path

  The directory where os-specific configurations are stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_dir_and_create_if_needed(path: Path | str | PathLike[str], create_if_nonexistent: bool = True) → Path

  Returns the requested directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_log_dir(create_if_nonexistent: bool = True) → Path

  The directory where the os-specific logs are stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

sas.system.user.get_plugin_dir(create_if_nonexistent: bool = True) → Path

  The directory where the os-specific cache for plugin models is stored.

  Returns the directory as a pathlib.Path object, creating the directory if it does not already exist.

## sas.system.version module

## sas.system.web module

class sas.system.web.WebLinks

  Bases: `object`

## sas.system.zenodo module

## Module contents