Datasets:
Download code/evaluation-code/ent400-a06a1576/apps/console/tools/generate_contract.py from VEHwang/cocogni4d: direct link, hf CLI and curl.
- Browser
- Download file 2.61 kB
-
https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/tools/generate_contract.py
- Command line
-
hf download hf://datasets/VEHwang/cocogni4d/code/evaluation-code/ent400-a06a1576/apps/console/tools/generate_contract.py
-
curl -L -o generate_contract.py https://huggingface.co/datasets/VEHwang/cocogni4d/resolve/main/code/evaluation-code/ent400-a06a1576/apps/console/tools/generate_contract.py
2.61 kB
| #!/usr/bin/env python3 | |
| """Generate TypeScript declarations from one schema.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| ROOT = Path(__file__).resolve().parents[1] | |
| def type_of(node): | |
| if '$ref' in node: | |
| return node['$ref'].split('/')[-1] | |
| if 'const' in node: | |
| return json.dumps(node['const']) | |
| if 'enum' in node: | |
| return ' | '.join(json.dumps(x) for x in node['enum']) | |
| if 'anyOf' in node or isinstance(node.get('type'), list): | |
| choices = node.get('anyOf', [{'type': t} for t in node.get('type', [])]) | |
| return ' | '.join(type_of(n) for n in choices) | |
| kind = node.get('type') | |
| if kind == 'array': | |
| item = type_of(node['items']) | |
| length = node.get('minItems') | |
| if length and length == node.get('maxItems'): | |
| return '[' + ', '.join([item] * length) + ']' | |
| return '(' + item + ')[]' | |
| if kind == 'object': | |
| if 'properties' not in node: | |
| return 'Record<string, ' + type_of(node.get('additionalProperties', {})) + '>' | |
| required = node.get('required', []) | |
| fields = [json.dumps(k) + ('' if k in required else '?') + ': ' + type_of(v) | |
| for k, v in node['properties'].items()] | |
| if node.get('additionalProperties') is True: | |
| fields.append('[key: string]: unknown') | |
| return '{ ' + '; '.join(fields) + ' }' | |
| return {'string': 'string', 'number': 'number', 'integer': 'number', 'boolean': 'boolean', 'null': 'null'}.get(kind, 'unknown') | |
| def outputs(): | |
| defs = json.loads((ROOT / 'server/contract.schema.json').read_text())['$defs'] | |
| ts = ['// Generated by tools/generate_contract.py; edit contract.schema.json.', ''] | |
| ts.extend('export type ' + name + ' = ' + type_of(node) + ';' for name, node in defs.items()) | |
| # NP run contracts are a frontend-only supplement, maintained separately | |
| # from the backend's segment-view schema. Keep regeneration reproducible. | |
| supplement = (ROOT / 'web/np-contract.ts').read_text(encoding='utf-8') | |
| return {ROOT / 'web/src/contract.ts': '\n'.join(ts) + '\n\n' + supplement} | |
| def main(): | |
| parser = argparse.ArgumentParser() | |
| parser.add_argument('--check', action='store_true') | |
| args = parser.parse_args() | |
| for path, source in outputs().items(): | |
| if args.check: | |
| if not path.is_file() or path.read_text() != source: | |
| raise SystemExit('contract drift: ' + str(path)) | |
| else: | |
| path.parent.mkdir(parents=True, exist_ok=True) | |
| path.write_text(source, encoding='utf-8') | |
| if __name__ == '__main__': | |
| main() | |