mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-23 04:03:00 -05:00
Improve validate() function for plugin options (#1323)
* Add `validateConfigParameter` and `validateConfigParameterFromArray` functions to validate user-provided parameters from py-config * Add units tests for `validateConfigParameter` and `validateConfigParameterFromArray` --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,28 +1,15 @@
|
||||
import type { PyScriptApp } from '../main';
|
||||
import type { AppConfig } from '../pyconfig';
|
||||
import { Plugin } from '../plugin';
|
||||
import { UserError, ErrorCode } from '../exceptions';
|
||||
import { Plugin, validateConfigParameterFromArray } from '../plugin';
|
||||
import { getLogger } from '../logger';
|
||||
import { type Stdio } from '../stdio';
|
||||
import { InterpreterClient } from '../interpreter_client';
|
||||
|
||||
type AppConfigStyle = AppConfig & { terminal?: boolean | 'auto'; docked?: boolean | 'docked' };
|
||||
|
||||
const logger = getLogger('py-terminal');
|
||||
|
||||
const validate = (config: AppConfigStyle, name: string, default_: string) => {
|
||||
const value = config[name] as undefined | boolean | string;
|
||||
if (value !== undefined && value !== true && value !== false && value !== default_) {
|
||||
const got = JSON.stringify(value);
|
||||
throw new UserError(
|
||||
ErrorCode.BAD_CONFIG,
|
||||
`Invalid value for config.${name}: the only accepted` +
|
||||
`values are true, false and "${default_}", got "${got}".`,
|
||||
);
|
||||
}
|
||||
if (value === undefined) {
|
||||
config[name] = default_;
|
||||
}
|
||||
type AppConfigStyle = AppConfig & {
|
||||
terminal?: string | boolean;
|
||||
docked?: string | boolean;
|
||||
};
|
||||
|
||||
export class PyTerminalPlugin extends Plugin {
|
||||
@@ -35,8 +22,18 @@ export class PyTerminalPlugin extends Plugin {
|
||||
|
||||
configure(config: AppConfigStyle) {
|
||||
// validate the terminal config and handle default values
|
||||
validate(config, 'terminal', 'auto');
|
||||
validate(config, 'docked', 'docked');
|
||||
validateConfigParameterFromArray({
|
||||
config: config,
|
||||
name: 'terminal',
|
||||
possibleValues: [true, false, 'auto'],
|
||||
defaultValue: 'auto',
|
||||
});
|
||||
validateConfigParameterFromArray({
|
||||
config: config,
|
||||
name: 'docked',
|
||||
possibleValues: [true, false, 'docked'],
|
||||
defaultValue: 'docked',
|
||||
});
|
||||
}
|
||||
|
||||
beforeLaunch(config: AppConfigStyle) {
|
||||
|
||||
Reference in New Issue
Block a user