|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +This module contains routines to simplify front-end use. |
| 4 | +
|
| 5 | +In particular we provide: |
| 6 | +
|
| 7 | +* a class to create a Mathics session, |
| 8 | +* load the Mathics core settings files (written in WL), |
| 9 | +* read and set Mathics Settings. |
| 10 | +""" |
| 11 | + |
| 12 | +import os.path as osp |
| 13 | +from mathics.core.definitions import autoload_files |
1 | 14 | from mathics.core.parser import parse, MathicsSingleLineFeeder |
2 | 15 | from mathics.core.definitions import Definitions |
3 | 16 | from mathics.core.evaluation import Evaluation |
4 | 17 |
|
5 | 18 |
|
| 19 | +def load_default_settings_files( |
| 20 | + definitions: Definitions, load_cli_settings: bool = True |
| 21 | +): |
| 22 | + """ |
| 23 | + Loads the system default settings for Mathics core. |
| 24 | +
|
| 25 | + Other settings files may get loaded later and override these |
| 26 | + defaults. |
| 27 | + """ |
| 28 | + root_dir = osp.realpath(osp.dirname(__file__)) |
| 29 | + |
| 30 | + autoload_files(definitions, root_dir, "autoload", False) |
| 31 | + if load_cli_settings: |
| 32 | + autoload_files(definitions, root_dir, "autoload-cli", False) |
| 33 | + |
| 34 | + |
| 35 | +def get_settings_value(definitions: Definitions, setting_name: str): |
| 36 | + """Get a Mathics Settings` value with name "setting_name" from definitions.""" |
| 37 | + return definitions.get_ownvalue(setting_name).replace.to_python(string_quotes=False) |
| 38 | + |
| 39 | + |
| 40 | +def set_settings_value(definitions: Definitions, setting_name: str, value): |
| 41 | + """Set a Mathics Settings` with name "setting_name" from definitions to value |
| 42 | + "value". |
| 43 | + """ |
| 44 | + return definitions.set_ownvalue(setting_name, value) |
| 45 | + |
| 46 | + |
6 | 47 | class MathicsSession: |
| 48 | + """A session stores definitions or evaluation results. This class |
| 49 | + also simplifies the common activity of reading as string, parsing |
| 50 | + it and evaluating it in the context of the current session. |
| 51 | + """ |
| 52 | + |
7 | 53 | def __init__(self, add_builtin=True, catch_interrupt=False, form="InputForm"): |
8 | 54 | self.definitions = Definitions(add_builtin) |
9 | 55 | self.evaluation = Evaluation( |
|
0 commit comments