fractal-task-tools 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of fractal-task-tools might be problematic. Click here for more details.

@@ -0,0 +1,5 @@
1
+ import logging
2
+
3
+ logging.basicConfig(level=logging.INFO)
4
+
5
+ __VERSION__ = "0.0.2"
@@ -0,0 +1,214 @@
1
+ import logging
2
+ import os
3
+ from collections import Counter
4
+ from pathlib import Path
5
+ from typing import Any
6
+ from typing import Callable
7
+ from typing import Optional
8
+
9
+ from docstring_parser import parse as docparse
10
+ from pydantic._internal import _generate_schema
11
+ from pydantic._internal import _typing_extra
12
+ from pydantic._internal._config import ConfigWrapper
13
+
14
+ from ._descriptions import _get_class_attrs_descriptions
15
+ from ._descriptions import _get_function_args_descriptions
16
+ from ._descriptions import _insert_class_attrs_descriptions
17
+ from ._descriptions import _insert_function_args_descriptions
18
+ from ._pydantic_generatejsonschema import CustomGenerateJsonSchema
19
+ from ._signature_constraints import _extract_function
20
+ from ._signature_constraints import _validate_function_signature
21
+ from ._titles import _include_titles
22
+
23
+
24
+ _Schema = dict[str, Any]
25
+
26
+
27
+ def _remove_attributes_from_descriptions(old_schema: _Schema) -> _Schema:
28
+ """
29
+ Keeps only the description part of the docstrings: e.g from
30
+ ```
31
+ 'Custom class for Omero-channel window, based on OME-NGFF v0.4.\\n'
32
+ '\\n'
33
+ 'Attributes:\\n'
34
+ 'min: Do not change. It will be set to `0` by default.\\n'
35
+ 'max: Do not change. It will be set according to bitdepth of the images\\n'
36
+ ' by default (e.g. 65535 for 16 bit images).\\n'
37
+ 'start: Lower-bound rescaling value for visualization.\\n'
38
+ 'end: Upper-bound rescaling value for visualization.'
39
+ ```
40
+ to `'Custom class for Omero-channel window, based on OME-NGFF v0.4.\\n'`.
41
+
42
+ Args:
43
+ old_schema: TBD
44
+ """
45
+ new_schema = old_schema.copy()
46
+ if "$defs" in new_schema:
47
+ for name, definition in new_schema["$defs"].items():
48
+ if "description" in definition.keys():
49
+ parsed_docstring = docparse(definition["description"])
50
+ new_schema["$defs"][name][
51
+ "description"
52
+ ] = parsed_docstring.short_description
53
+ elif "title" in definition.keys():
54
+ title = definition["title"]
55
+ new_schema["$defs"][name][
56
+ "description"
57
+ ] = f"Missing description for {title}."
58
+ else:
59
+ new_schema["$defs"][name][
60
+ "description"
61
+ ] = "Missing description"
62
+ logging.info("[_remove_attributes_from_descriptions] END")
63
+ return new_schema
64
+
65
+
66
+ def _create_schema_for_function(function: Callable) -> _Schema:
67
+ namespace = _typing_extra.add_module_globals(function, None)
68
+ gen_core_schema = _generate_schema.GenerateSchema(
69
+ ConfigWrapper(None), namespace
70
+ )
71
+ core_schema = gen_core_schema.generate_schema(function)
72
+ clean_core_schema = gen_core_schema.clean_schema(core_schema)
73
+ gen_json_schema = CustomGenerateJsonSchema()
74
+ json_schema = gen_json_schema.generate(
75
+ clean_core_schema, mode="validation"
76
+ )
77
+ return json_schema
78
+
79
+
80
+ def create_schema_for_single_task(
81
+ executable: str,
82
+ package: Optional[str] = None,
83
+ pydantic_models: Optional[list[tuple[str, str, str]]] = None,
84
+ task_function: Optional[Callable] = None,
85
+ verbose: bool = False,
86
+ ) -> _Schema:
87
+ """
88
+ Main function to create a JSON Schema of task arguments
89
+
90
+ This function can be used in two ways:
91
+
92
+ 1. `task_function` argument is `None`, `package` is set, and `executable`
93
+ is a path relative to that package.
94
+ 2. `task_function` argument is provided, `executable` is an absolute path
95
+ to the function module, and `package` is `None. This is useful for
96
+ testing.
97
+ """
98
+
99
+ DEFINITIONS_KEY = "$defs"
100
+
101
+ logging.info("[create_schema_for_single_task] START")
102
+ if task_function is None:
103
+ usage = "1"
104
+ # Usage 1 (standard)
105
+ if package is None:
106
+ raise ValueError(
107
+ "Cannot call `create_schema_for_single_task with "
108
+ f"{task_function=} and {package=}. Exit."
109
+ )
110
+ if os.path.isabs(executable):
111
+ raise ValueError(
112
+ "Cannot call `create_schema_for_single_task with "
113
+ f"{task_function=} and absolute {executable=}. Exit."
114
+ )
115
+ else:
116
+ usage = "2"
117
+ # Usage 2 (testing)
118
+ if package is not None:
119
+ raise ValueError(
120
+ "Cannot call `create_schema_for_single_task with "
121
+ f"{task_function=} and non-None {package=}. Exit."
122
+ )
123
+ if not os.path.isabs(executable):
124
+ raise ValueError(
125
+ "Cannot call `create_schema_for_single_task with "
126
+ f"{task_function=} and non-absolute {executable=}. Exit."
127
+ )
128
+
129
+ # Extract function from module
130
+ if usage == "1":
131
+ # Extract the function name (for the moment we assume the function has
132
+ # the same name as the module)
133
+ function_name = Path(executable).with_suffix("").name
134
+ # Extract the function object
135
+ task_function = _extract_function(
136
+ package_name=package,
137
+ module_relative_path=executable,
138
+ function_name=function_name,
139
+ verbose=verbose,
140
+ )
141
+ else:
142
+ # The function object is already available, extract its name
143
+ function_name = task_function.__name__
144
+
145
+ if verbose:
146
+ logging.info(f"[create_schema_for_single_task] {function_name=}")
147
+ logging.info(f"[create_schema_for_single_task] {task_function=}")
148
+
149
+ # Validate function signature against some custom constraints
150
+ _validate_function_signature(task_function)
151
+
152
+ # Create and clean up schema
153
+ schema = _create_schema_for_function(task_function)
154
+ schema = _remove_attributes_from_descriptions(schema)
155
+
156
+ # Include titles for custom-model-typed arguments
157
+ schema = _include_titles(
158
+ schema, definitions_key=DEFINITIONS_KEY, verbose=verbose
159
+ )
160
+
161
+ # Include main title
162
+ if schema.get("title") is None:
163
+
164
+ def to_camel_case(snake_str):
165
+ return "".join(
166
+ x.capitalize() for x in snake_str.lower().split("_")
167
+ )
168
+
169
+ schema["title"] = to_camel_case(task_function.__name__)
170
+
171
+ # Include descriptions of function. Note: this function works both
172
+ # for usages 1 or 2 (see docstring).
173
+ function_args_descriptions = _get_function_args_descriptions(
174
+ package_name=package,
175
+ module_path=executable,
176
+ function_name=function_name,
177
+ verbose=verbose,
178
+ )
179
+
180
+ schema = _insert_function_args_descriptions(
181
+ schema=schema, descriptions=function_args_descriptions
182
+ )
183
+
184
+ if pydantic_models is not None:
185
+ # Check that model names are unique
186
+ pydantic_models_names = [item[2] for item in pydantic_models]
187
+ duplicate_class_names = [
188
+ name
189
+ for name, count in Counter(pydantic_models_names).items()
190
+ if count > 1
191
+ ]
192
+ if duplicate_class_names:
193
+ pydantic_models_str = " " + "\n ".join(map(str, pydantic_models))
194
+ raise ValueError(
195
+ "Cannot parse docstrings for models with non-unique names "
196
+ f"{duplicate_class_names}, in\n{pydantic_models_str}"
197
+ )
198
+
199
+ # Extract model-attribute descriptions and insert them into schema
200
+ for package_name, module_relative_path, class_name in pydantic_models:
201
+ attrs_descriptions = _get_class_attrs_descriptions(
202
+ package_name=package_name,
203
+ module_relative_path=module_relative_path,
204
+ class_name=class_name,
205
+ )
206
+ schema = _insert_class_attrs_descriptions(
207
+ schema=schema,
208
+ class_name=class_name,
209
+ descriptions=attrs_descriptions,
210
+ definition_key=DEFINITIONS_KEY,
211
+ )
212
+
213
+ logging.info("[create_schema_for_single_task] END")
214
+ return schema
@@ -0,0 +1,72 @@
1
+ import argparse as ap
2
+ import sys
3
+
4
+ from fractal_task_tools._create_manifest import check_manifest_unchanged
5
+ from fractal_task_tools._create_manifest import create_manifest
6
+ from fractal_task_tools._create_manifest import write_manifest_to_file
7
+
8
+
9
+ main_parser = ap.ArgumentParser(
10
+ description="`fractal-manifest` command-line interface",
11
+ allow_abbrev=False,
12
+ )
13
+
14
+ subparsers = main_parser.add_subparsers(
15
+ title="Available commands",
16
+ dest="cmd",
17
+ )
18
+
19
+
20
+ create_manifest_parser = subparsers.add_parser(
21
+ "create",
22
+ description="Create new manifest file",
23
+ allow_abbrev=False,
24
+ )
25
+
26
+ check_manifest_parser = subparsers.add_parser(
27
+ "check",
28
+ description="Check existing manifest file",
29
+ allow_abbrev=False,
30
+ )
31
+
32
+
33
+ for subparser in (create_manifest_parser, check_manifest_parser):
34
+ subparser.add_argument(
35
+ "--package",
36
+ type=str,
37
+ help="Example: 'fractal_tasks_core'",
38
+ required=True,
39
+ )
40
+ subparser.add_argument(
41
+ "--task-list-path",
42
+ type=str,
43
+ help=(
44
+ "Dot-separated path to the `task_list.py` module, "
45
+ "relative to the package root (default value: 'dev.task_list')."
46
+ ),
47
+ default="dev.task_list",
48
+ required=False,
49
+ )
50
+
51
+
52
+ def main():
53
+ args = main_parser.parse_args(sys.argv[1:])
54
+ if args.cmd == "create":
55
+ manifest = create_manifest(
56
+ raw_package_name=args.package,
57
+ task_list_path=args.task_list_path,
58
+ )
59
+ write_manifest_to_file(
60
+ raw_package_name=args.package,
61
+ manifest=manifest,
62
+ )
63
+
64
+ elif args.cmd == "check":
65
+ manifest = create_manifest(
66
+ raw_package_name=args.package,
67
+ task_list_path=args.task_list_path,
68
+ )
69
+ check_manifest_unchanged(
70
+ raw_package_name=args.package,
71
+ manifest=manifest,
72
+ )
@@ -0,0 +1,190 @@
1
+ """
2
+ Generate JSON schemas for task arguments and combine them into a manifest.
3
+ """
4
+ import json
5
+ import logging
6
+ from importlib import import_module
7
+ from pathlib import Path
8
+ from typing import Any
9
+
10
+ from ._args_schemas import create_schema_for_single_task
11
+ from ._package_name_tools import normalize_package_name
12
+ from ._task_docs import create_docs_info
13
+ from ._task_docs import read_docs_info_from_file
14
+
15
+
16
+ ARGS_SCHEMA_VERSION = "pydantic_v2"
17
+ MANIFEST_FILENAME = "__FRACTAL_MANIFEST__.json"
18
+ MANIFEST_VERSION = "2"
19
+
20
+
21
+ def create_manifest(
22
+ *,
23
+ raw_package_name: str,
24
+ task_list_path: str,
25
+ ) -> dict[str, Any]:
26
+ """
27
+ Create the package manifest based on a `task_list.py` module
28
+
29
+ Arguments:
30
+ raw_package_name:
31
+ The name of the package. Note that this name must be importable
32
+ (after normalization).
33
+ task_list_path:
34
+ Relative path to the `task_list.py` module, with respect to the
35
+ package root (example `dev.task_list`).
36
+
37
+ Returns:
38
+ Task-package manifest.
39
+ """
40
+
41
+ # Preliminary validation
42
+ if "/" in task_list_path or task_list_path.endswith(".py"):
43
+ raise ValueError(
44
+ f"Invalid {task_list_path=} (valid example: `dev.task_list`)."
45
+ )
46
+
47
+ # Normalize package name
48
+ package_name = normalize_package_name(raw_package_name)
49
+
50
+ logging.info(f"Start generating a new manifest for {package_name}")
51
+
52
+ # Prepare an empty manifest
53
+ manifest = dict(
54
+ manifest_version=MANIFEST_VERSION,
55
+ has_args_schemas=True,
56
+ args_schema_version=ARGS_SCHEMA_VERSION,
57
+ task_list=[],
58
+ )
59
+
60
+ # Import the task-list module
61
+ task_list_module = import_module(f"{package_name}.{task_list_path}")
62
+
63
+ # Load TASK_LIST
64
+ TASK_LIST = getattr(task_list_module, "TASK_LIST")
65
+
66
+ # Load INPUT_MODELS
67
+ try:
68
+ INPUT_MODELS = getattr(task_list_module, "INPUT_MODELS")
69
+ except AttributeError:
70
+ INPUT_MODELS = []
71
+ logging.warning(
72
+ "No `INPUT_MODELS` found in task_list module. Setting it to `[]`."
73
+ )
74
+
75
+ # Load AUTHORS
76
+ try:
77
+ manifest["authors"] = getattr(task_list_module, "AUTHORS")
78
+ except AttributeError:
79
+ logging.warning("No `AUTHORS` found in task_list module.")
80
+
81
+ # Load DOCS_LINK
82
+ try:
83
+ DOCS_LINK = getattr(task_list_module, "DOCS_LINK")
84
+ except AttributeError:
85
+ DOCS_LINK = None
86
+ logging.warning("No `DOCS_LINK` found in task_list module.")
87
+
88
+ # Loop over TASK_LIST, and append the proper task dictionaries
89
+ # to manifest["task_list"]
90
+ for task_obj in TASK_LIST:
91
+ # Convert Pydantic object to dictionary
92
+ task_dict = task_obj.model_dump(
93
+ exclude={"meta_init", "executable_init", "meta", "executable"},
94
+ exclude_unset=True,
95
+ )
96
+
97
+ # Copy some properties from `task_obj` to `task_dict`
98
+ if task_obj.executable_non_parallel is not None:
99
+ task_dict[
100
+ "executable_non_parallel"
101
+ ] = task_obj.executable_non_parallel
102
+ if task_obj.executable_parallel is not None:
103
+ task_dict["executable_parallel"] = task_obj.executable_parallel
104
+ if task_obj.meta_non_parallel is not None:
105
+ task_dict["meta_non_parallel"] = task_obj.meta_non_parallel
106
+ if task_obj.meta_parallel is not None:
107
+ task_dict["meta_parallel"] = task_obj.meta_parallel
108
+
109
+ # Autogenerate JSON Schemas for non-parallel/parallel task arguments
110
+ for kind in ["non_parallel", "parallel"]:
111
+ executable = task_dict.get(f"executable_{kind}")
112
+ if executable is not None:
113
+ logging.info(f"[{executable}] START")
114
+ schema = create_schema_for_single_task(
115
+ executable,
116
+ package=package_name,
117
+ pydantic_models=INPUT_MODELS,
118
+ )
119
+ logging.info(f"[{executable}] END (new schema)")
120
+ task_dict[f"args_schema_{kind}"] = schema
121
+
122
+ # Compute and set `docs_info`
123
+ docs_info = task_dict.get("docs_info")
124
+ if docs_info is None:
125
+ docs_info = create_docs_info(
126
+ executable_non_parallel=task_obj.executable_non_parallel,
127
+ executable_parallel=task_obj.executable_parallel,
128
+ package=package_name,
129
+ )
130
+ elif docs_info.startswith("file:"):
131
+ docs_info = read_docs_info_from_file(
132
+ docs_info=docs_info,
133
+ task_list_path=task_list_module.__file__,
134
+ )
135
+ if docs_info is not None:
136
+ task_dict["docs_info"] = docs_info
137
+
138
+ # Set `docs_link`
139
+ if DOCS_LINK is not None:
140
+ task_dict["docs_link"] = DOCS_LINK
141
+
142
+ # Append task
143
+ manifest["task_list"].append(task_dict)
144
+ print()
145
+ return manifest
146
+
147
+
148
+ def write_manifest_to_file(
149
+ *,
150
+ raw_package_name: str,
151
+ manifest: str,
152
+ ) -> None:
153
+ """
154
+ Write manifest to file.
155
+
156
+ Arguments:
157
+ raw_package_name:
158
+ manifest: The manifest object
159
+ """
160
+ package_name = normalize_package_name(raw_package_name)
161
+ imported_package = import_module(package_name)
162
+ package_root_dir = Path(imported_package.__file__).parent
163
+ manifest_path = package_root_dir / MANIFEST_FILENAME
164
+ with manifest_path.open("w") as f:
165
+ json.dump(manifest, f, indent=2)
166
+ f.write("\n")
167
+ logging.info(f"Manifest stored in {manifest_path.as_posix()}")
168
+
169
+
170
+ def check_manifest_unchanged(
171
+ *,
172
+ raw_package_name: str,
173
+ manifest: str,
174
+ ) -> None:
175
+ """
176
+ Write manifest to file.
177
+
178
+ Arguments:
179
+ raw_package_name:
180
+ manifest: The manifest object
181
+ """
182
+ package_name = normalize_package_name(raw_package_name)
183
+ imported_package = import_module(package_name)
184
+ package_root_dir = Path(imported_package.__file__).parent
185
+ manifest_path = package_root_dir / MANIFEST_FILENAME
186
+ with manifest_path.open("f") as f:
187
+ old_manifest = json.load(f)
188
+ logging.info(f"Manifest read from {manifest_path.as_posix()}")
189
+ if manifest != old_manifest:
190
+ raise ValueError("New/old manifests differ")