Gen3SchemaDev 2.0.11__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.
@@ -0,0 +1,18 @@
1
+ # Expose main submodules for convenient import
2
+ from .schema import *
3
+ from .validators import *
4
+ from .cli import *
5
+ from .utils import *
6
+ from .converter import *
7
+ from .ddvis import *
8
+
9
+ try:
10
+ from importlib.metadata import version, PackageNotFoundError
11
+ except ImportError:
12
+ from importlib_metadata import version, PackageNotFoundError # for Python<3.8
13
+
14
+ try:
15
+ from importlib.metadata import version
16
+ __version__ = version('gen3schemadev')
17
+ except Exception:
18
+ __version__ = 'unknown'
gen3schemadev/cli.py ADDED
@@ -0,0 +1,215 @@
1
+ import argparse
2
+ import logging
3
+ import sys
4
+ import os
5
+
6
+ from gen3schemadev.schema.gen3_template import (
7
+ get_metaschema,
8
+ generate_gen3_template,
9
+ generate_def_template,
10
+ generate_setting_template,
11
+ generate_terms_template,
12
+ generate_core_metadata_template,
13
+ )
14
+ from gen3schemadev.utils import write_yaml, load_yaml, bundle_yamls, write_json
15
+ from gen3schemadev.schema.input_schema import DataModel
16
+ from gen3schemadev.converter import get_node_names, populate_template
17
+ from gen3schemadev.utils import bundled_schema_to_list_dict, resolve_schema
18
+ from gen3schemadev.validators.metaschema_validator import validate_schema_with_metaschema
19
+ from importlib.metadata import version
20
+ from gen3schemadev.ddvis import visualise_with_docker
21
+
22
+ # def get_version():
23
+ # return "2.0.6"
24
+
25
+
26
+ def main():
27
+ version_parser = argparse.ArgumentParser(add_help=False)
28
+ version_parser.add_argument(
29
+ '--version',
30
+ action='version',
31
+ version=f"gen3schemadev {version('gen3schemadev')}"
32
+ )
33
+
34
+ version_parser.parse_known_args()
35
+
36
+ parser = argparse.ArgumentParser(
37
+ description="Gen3 Schema Development Tool"
38
+ )
39
+ parser.add_argument(
40
+ '--version',
41
+ action='version',
42
+ version=f"%(prog)s {version('gen3schemadev')}"
43
+ )
44
+
45
+ subparsers = parser.add_subparsers(dest="command", required=False)
46
+
47
+ # Create 'generate' subcommand
48
+ generate_parser = subparsers.add_parser(
49
+ "generate",
50
+ help="Generate schemas"
51
+ )
52
+ generate_parser.add_argument(
53
+ "-i", "--input",
54
+ required=True,
55
+ help="Input YAML file"
56
+ )
57
+ generate_parser.add_argument(
58
+ "-o", "--output",
59
+ required=True,
60
+ help="Output directory"
61
+ )
62
+ generate_parser.add_argument(
63
+ "--debug",
64
+ action="store_true",
65
+ help="Set logging level to DEBUG"
66
+ )
67
+
68
+ # Create 'bundle' subcommand
69
+ bundle_parser = subparsers.add_parser(
70
+ "bundle",
71
+ help="Bundle YAML files"
72
+ )
73
+ bundle_parser.add_argument(
74
+ "-i", "--input",
75
+ required=True,
76
+ help="Input directory"
77
+ )
78
+ bundle_parser.add_argument(
79
+ "-f", "--filename",
80
+ required=True,
81
+ help="Output Filename"
82
+ )
83
+ bundle_parser.add_argument(
84
+ "--debug",
85
+ action="store_true",
86
+ help="Set logging level to DEBUG"
87
+ )
88
+
89
+ # Create 'validate' subcommand
90
+ validate_parser = subparsers.add_parser(
91
+ "validate",
92
+ help="Validate schemas"
93
+ )
94
+ validate_parser.add_argument(
95
+ "-b", "--bundled",
96
+ required=False,
97
+ help="Bundled JsonSchema file"
98
+ )
99
+ validate_parser.add_argument(
100
+ "-y", "--yamls",
101
+ required=False,
102
+ help="Directory of Gen3 Yaml files"
103
+ )
104
+ validate_parser.add_argument(
105
+ "--debug",
106
+ action="store_true",
107
+ help="Set logging level to DEBUG"
108
+ )
109
+
110
+ # Create 'visualise' subcomand
111
+ visualise_parser = subparsers.add_parser(
112
+ "visualise",
113
+ help="Visualise schemas"
114
+ )
115
+ visualise_parser.add_argument(
116
+ "-i", "--input",
117
+ required=True,
118
+ help="Path to Bundled Gen3 JsonSchema file"
119
+ )
120
+ visualise_parser.add_argument(
121
+ "--debug",
122
+ action="store_true",
123
+ help="Set logging level to DEBUG"
124
+ )
125
+
126
+ args = parser.parse_args()
127
+
128
+ # Handle case where no command is provided
129
+ if args.command is None:
130
+ parser.print_help(sys.stderr)
131
+ sys.exit(0)
132
+
133
+ # Set up basic logging configuration
134
+ # If the subcommand has --debug, set to DEBUG, else INFO
135
+ log_level = logging.ERROR
136
+ if hasattr(args, "debug") and getattr(args, "debug", False):
137
+ log_level = logging.DEBUG
138
+ logging.basicConfig(
139
+ level=log_level,
140
+ format="%(asctime)s [%(levelname)s] %(message)s"
141
+ )
142
+ logger = logging.getLogger(__name__)
143
+
144
+
145
+ if args.command == "generate":
146
+ print("Starting schema generation process...")
147
+ metaschema = get_metaschema()
148
+ converter_template = generate_gen3_template(metaschema)
149
+ print(f"Loading input YAML from: {args.input}")
150
+ data = load_yaml(args.input)
151
+ print("Validating input data model...")
152
+ validated_model = DataModel.model_validate(data)
153
+ node_names = get_node_names(validated_model)
154
+ print(f"Found nodes: {node_names}")
155
+
156
+ for node in node_names:
157
+ print(f"Populating template for node: '{node}'")
158
+ out_template = populate_template(node, validated_model, converter_template)
159
+ print(f"Writing output YAML to: {args.output}/{node}.yaml")
160
+ write_yaml(out_template, f"{args.output}/{node}.yaml")
161
+
162
+ print('Writing auxiliary files')
163
+ write_yaml(generate_def_template(), f"{args.output}/_definitions.yaml")
164
+ setting_dict = generate_setting_template()
165
+ setting_dict['_dict_version'] = validated_model.version
166
+ write_yaml(setting_dict, f"{args.output}/_settings.yaml")
167
+ write_yaml(generate_terms_template(), f"{args.output}/_terms.yaml")
168
+ write_yaml(generate_core_metadata_template(), f"{args.output}/core_metadata_collection.yaml")
169
+
170
+ print("Schema generation process complete.")
171
+
172
+ elif args.command == "bundle":
173
+ print(f"Bundling YAML files from directory: {args.input}")
174
+ bundle_dict = bundle_yamls(args.input)
175
+ print(f"Writing bundled schema to file: {args.filename}")
176
+ write_json(bundle_dict, args.filename)
177
+ print("Bundling process complete.")
178
+
179
+ elif args.command == "validate":
180
+ print("Starting validation process...")
181
+ metaschema = get_metaschema()
182
+
183
+ resolve_schema_obj = None
184
+ if args.bundled:
185
+ print(f"Resolving schema from bundled file: {args.bundled}")
186
+ resolve_schema_obj = resolve_schema(schema_path=args.bundled)
187
+ elif args.yamls:
188
+ print(f"Bundling and resolving schemas from directory: {args.yamls}")
189
+ resolve_schema_obj = resolve_schema(schema_dir=args.yamls)
190
+
191
+ if resolve_schema_obj is None:
192
+ logger.error("You must provide either --bundled or --yamls for validation.")
193
+ sys.exit(1)
194
+
195
+ schema_list = bundled_schema_to_list_dict(resolve_schema_obj)
196
+ print(f"Found {len(schema_list)} schemas to validate.")
197
+
198
+ for schema in schema_list:
199
+ schema_id = schema.get('id', '<no id>')
200
+ print(f"Validating schema: {schema_id}")
201
+ validate_schema_with_metaschema(
202
+ schema,
203
+ metaschema=metaschema,
204
+ verbose=True
205
+ )
206
+ print(f"Successfully validated schema: {schema_id}")
207
+
208
+ print("Validation process complete.")
209
+
210
+ elif args.command == "visualise":
211
+ print(f"Visualising schema from file: {args.input}")
212
+ visualise_with_docker(args.input)
213
+
214
+ if __name__ == "__main__":
215
+ main()