Skip to content

Latest commit

 

History

History
140 lines (100 loc) · 6.75 KB

File metadata and controls

140 lines (100 loc) · 6.75 KB

Configuration System – pmp_manip.config

This module handles centralized configuration for the pmp_manip project. It allows setting and validating all configuration parameters exactly once, after which they are deeply frozen to prevent further modification.

For field-specific documentation, refer to the table at the bottom.


Overview

The configuration system is composed of several modular configuration dataclasses:

  • ExtInfoGenConfig
  • ValidationConfig
  • PlatformMetaConfig

These are composed into a single root config: MasterConfig, which is initialized and accessed globally throughout the project.


Initialization

init_config(config: MasterConfig) -> None

Initializes the global configuration with a fully populated MasterConfig. This function must only be called once, ideally at application startup.

  • Validates the full config tree via .validate()
  • Sets all sub-configs and the master config as immutable
  • Subsequent calls to init_config() will raise an error

Raises:
ConfigurationError – if config has already been initialized


Accessing Configuration

get_config() -> MasterConfig

Returns the globally set, frozen configuration. Should be used by all internal modules to access config values.

Raises:
ConfigurationError – if config hasn't been initialized yet


Default Configuration

get_default_config() -> MasterConfig

Returns a pre-built MasterConfig with safe, reasonable defaults. This can be used:

  • As a base for testing
  • To bootstrap the system with minimal input
  • For default-based override logic

Configuration Fields

ExtInfoGenConfig

argument type purpose default
gen_opcode_info_dir directory path str directory used to store the generated
extension opcode info files in
"gen_ext_opcode_info"
js_fetch_interval datetime.timedelta if the extension is accessed through a link,
it will only be fetched again after this interval has passed
timedelta(days=3)
node_js_exec_timeout float (secs) if the extension code is directly executed,
the timeout secs of the Node.js suprocess
2.0 (secs)
is_trusted_extension_origin_handler Callable(str -> bool) or None takes the ext source and returns wether an unknown extension source can be trusted and
if it will therefore be directly executed
None

ValidationConfig

argument type purpose default
raise_if_monitor_position_outside_stage bool during validation an ValidationError will be raised
if a monitor's position is outside the stage edges
True
raise_if_monitor_bigger_then_stage bool during validation an ValidationError will be raised
if a list monitor's size is bigger then the stage
True

PlatformMetaConfig

argument type purpose default
scratch_semver version no. str holds up to date version of Scratch
for project meta generation
"3.0.0"
scratch_vm version no. str holds up to date version of the Scratch VM.
"
"11.1.0"
penguinmod_vm version no. str holds up to date version of the PenguinMod VM.
"
"0.2.0"

Example Usage


Method 1: Override from Default Config (Recommended)

from pmp_manip import (
    init_config, get_config, get_default_config,
)

# Start from defaults
cfg = get_default_config()

# Override only the fields you want to change
cfg.ext_info_gen.gen_opcode_info_dir = "my/overridden/path"
cfg.validation.raise_if_monitor_position_outside_stage = False


# Initialize with modified config
init_config(cfg)

# ... Use the pmp_manip module however you want from here

Method 2: Full Manual Initialization

from pmp_manip import (
    init_config, get_config,
    ExtInfoGenConfig, ValidationConfig,
    PlatformMetaConfig, MasterConfig,
)
from datetime import timedelta

# Initialize once, early in main
init_config(MasterConfig(
    ext_info_gen=ExtInfoGenConfig(
        gen_opcode_info_dir="my/output/dir",
        js_fetch_interval=timedelta(days=2),
        node_js_exec_timeout=3.5,
        is_trusted_extension_origin_handler=lambda source: source.startswith("https://example.com/"),
    ),
    validation=ValidationConfig(
        raise_if_monitor_position_outside_stage=True,
        raise_if_monitor_bigger_then_stage=False,
    ),
    platform_meta=PlatformMetaConfig(
        scratch_semver="3.0.0",
        scratch_vm="11.1.0",
        penguinmod_vm="0.2.0",
    ),
))

# ... Use the pmp_manip module however you want from here

References