Skip to content

Commit

Permalink
Enable reading site config directly from text
Browse files Browse the repository at this point in the history
  • Loading branch information
eivindjahren committed Sep 23, 2024
1 parent 711d664 commit 0ead796
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 335 deletions.
11 changes: 11 additions & 0 deletions src/ert/config/ert_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
init_forward_model_schema,
init_site_config_schema,
init_user_config_schema,
parse_contents,
)
from .parsing import (
parse as parse_config,
Expand Down Expand Up @@ -162,6 +163,12 @@ def from_file(cls, user_config_file: str) -> Self:
cls.apply_config_content_defaults(user_config_dict, config_dir)
return cls.from_dict(user_config_dict)

@classmethod
def from_file_contents(cls, user_config_file_contents: str) -> Self:
user_config_dict = cls.read_user_config_contents(user_config_file_contents)
cls.apply_config_content_defaults(user_config_dict, ".")
return cls.from_dict(user_config_dict)

@classmethod
def from_dict(cls, config_dict) -> Self:
substitution_list = _substitution_list_from_dict(config_dict)
Expand Down Expand Up @@ -382,6 +389,10 @@ def read_site_config(cls) -> ConfigDict:
def read_user_config(cls, user_config_file: str) -> ConfigDict:
return parse_config(user_config_file, schema=init_user_config_schema())

@classmethod
def read_user_config_contents(cls, user_config: str) -> ConfigDict:
return parse_contents(user_config, schema=init_user_config_schema())

@classmethod
def _merge_user_and_site_config(
cls, user_config_dict: ConfigDict, site_config_dict: ConfigDict
Expand Down
3 changes: 2 additions & 1 deletion src/ert/config/parsing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from .forward_model_schema import init_forward_model_schema
from .history_source import HistorySource
from .hook_runtime import HookRuntime
from .lark_parser import parse
from .lark_parser import parse, parse_contents
from .queue_system import QueueSystem, QueueSystemWithGeneric
from .schema_item_type import SchemaItemType
from .types import MaybeWithContext
Expand Down Expand Up @@ -42,4 +42,5 @@
"init_workflow_job_schema",
"init_workflow_schema",
"parse",
"parse_contents",
]
14 changes: 11 additions & 3 deletions src/ert/config/parsing/lark_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ def parse(
)


def parse_contents(
contents: str,
schema: SchemaItemDict,
pre_defines: Optional[List[Tuple[str, str]]] = None,
) -> ConfigDict:
return _transform_tree(
_parse_contents(contents, "./config.ert"), "./config.ert", schema, pre_defines
)


def _transform_tree(
tree: Tree[Instruction],
file: str,
Expand All @@ -518,11 +528,9 @@ def _transform_tree(
# add to this list
_handle_includes(tree, pre_defines.copy(), filepath)

config_dict = _tree_to_dict(
return _tree_to_dict(
config_file=file,
pre_defines=pre_defines,
tree=tree,
schema=schema,
)

return config_dict
Loading

0 comments on commit 0ead796

Please sign in to comment.