Skip to content

Instantly share code, notes, and snippets.

@Carreau
Created November 9, 2022 14:34
Show Gist options
  • Save Carreau/bb0b1ed5b981065c71893b92f4a18253 to your computer and use it in GitHub Desktop.
Save Carreau/bb0b1ed5b981065c71893b92f4a18253 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 138,
"id": "c8383e33",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 139,
"id": "e673b15a",
"metadata": {},
"outputs": [],
"source": [
"schemas = {}\n",
"\n",
"for p in Path('schema').glob('*.schema.json'):\n",
" schemas[p.name] = json.loads(p.read_text())"
]
},
{
"cell_type": "code",
"execution_count": 140,
"id": "9fd7c2c2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60 60\n"
]
}
],
"source": [
"defs = {}\n",
"i = 0\n",
"for root, val in schemas.items():\n",
" vx = {(root,name):v for (name,v) in val['$defs'].items()}\n",
" i += len(vx)\n",
" defs.update(vx)\n",
" \n",
"print(len(defs.keys()), i)"
]
},
{
"cell_type": "code",
"execution_count": 141,
"id": "1ccfab67",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"('StaticPhrasingContent',\n",
" {'anyOf': [{'$ref': 'commonmark.schema.json#/$defs/Text'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/HTML'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/EmphasisStatic'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/StrongStatic'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/InlineCode'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/Break'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/Image'},\n",
" {'$ref': 'commonmark.schema.json#/$defs/ImageReference'},\n",
" {'$ref': 'roles.schema.json#/$defs/Role'},\n",
" {'$ref': 'styles.schema.json#/$defs/SubscriptStatic'},\n",
" {'$ref': 'styles.schema.json#/$defs/SuperscriptStatic'},\n",
" {'$ref': 'styles.schema.json#/$defs/UnderlineStatic'},\n",
" {'$ref': 'math.schema.json#/$defs/InlineMath'}]})"
]
},
"execution_count": 141,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def _parse(path):\n",
" root, local = path.split('#/')\n",
" keys = local.split('/')\n",
" return root, keys\n",
"\n",
"\n",
" \n",
"\n",
"def resolve(schemas, path, current_root='?'):\n",
" root,keys = _parse(path)\n",
" if root == '':\n",
" root = current_root\n",
" s = schemas[root]\n",
" for k in keys:\n",
" s = s[k]\n",
" return k, s\n",
" \n",
" \n",
"\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 173,
"id": "d468118b",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"60 60\n",
"\n",
"class Math:\n",
" type = 'math'\n",
" enumerated : bool\n",
" enumerator : str\n",
" identifier : Any\n",
" label : Any\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class InlineMath:\n",
" type = 'inlineMath'\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Abbreviation:\n",
" type = 'abbreviation'\n",
" children : List[StaticPhrasingContent]\n",
" title : str\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Directive:\n",
"Refs ? {'required'}\n",
" type = 'mystDirective'\n",
" name : str\n",
" args : str\n",
" ?object\n",
" value : str\n",
" children : List[{'anyOf': [{'$ref': 'myst.schema.json#/$defs/FlowContent'}, {'$ref': 'myst.schema.json#/$defs/PhrasingContent'}]}]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Comment:\n",
" type = 'mystComment'\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Paragraph:\n",
" type = 'paragraph'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Heading:\n",
"Refs ? {'required'}\n",
" type = 'heading'\n",
" depth : int\n",
" enumerated : bool\n",
" enumerator : str\n",
" children : List[PhrasingContent]\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class ThematicBreak:\n",
" type = 'thematicBreak'\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Blockquote:\n",
" type = 'blockquote'\n",
" children : List[FlowContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class List:\n",
" type = 'list'\n",
" ordered : bool\n",
" start : int\n",
" spread : bool\n",
" children : List[ListContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class ListItem:\n",
" type = 'listItem'\n",
" spread : bool\n",
" children : List[{'anyOf': [{'$ref': 'myst.schema.json#/$defs/PhrasingContent'}, {'$ref': 'myst.schema.json#/$defs/FlowContent'}]}]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class HTML:\n",
" type = 'html'\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Code:\n",
"Refs ? {'if', 'then', 'else'}\n",
"Refs ? {'if', 'then', 'else'}\n",
" type = 'code'\n",
" lang : str\n",
" meta : str\n",
" class : str\n",
" showLineNumbers : bool\n",
" startingLineNumber : int\n",
" emphasizeLines : List[{'type': 'integer', 'minimum': 1}]\n",
" identifier : Any\n",
" label : Any\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Definition:\n",
" type = 'definition'\n",
" identifier : Any\n",
" label : Any\n",
" url : Any\n",
" title : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Text:\n",
" type = 'text'\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Emphasis:\n",
" type = 'emphasis'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class EmphasisStatic:\n",
" type = 'emphasis'\n",
" children : List[StaticPhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Strong:\n",
" type = 'strong'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class StrongStatic:\n",
" type = 'strong'\n",
" children : List[StaticPhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class InlineCode:\n",
" type = 'inlineCode'\n",
" value : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Break:\n",
" type = 'break'\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Link:\n",
" type = 'link'\n",
" children : List[StaticPhrasingContent]\n",
" url : Any\n",
" title : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Image:\n",
" type = 'image'\n",
" class : str\n",
" width : str\n",
" align : str\n",
" url : Any\n",
" title : Any\n",
" alt : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class LinkReference:\n",
" type = 'linkReference'\n",
" children : List[StaticPhrasingContent]\n",
" referenceType : Any\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class ImageReference:\n",
" type = 'imageReference'\n",
" referenceType : Any\n",
" identifier : Any\n",
" label : Any\n",
" alt : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Resource:\n",
" url : str\n",
" title : str\n",
"\n",
"class OptionalAssociation:\n",
" identifier : str\n",
" label : str\n",
"\n",
"class Association:\n",
"Refs ? {'required'}\n",
" identifier : Any\n",
"\n",
"class Alternative:\n",
" alt : str\n",
"\n",
"class Reference:\n",
" referenceType : str\n",
"\n",
"class Target:\n",
" type = 'mystTarget'\n",
" label : str\n",
" position : Any\n",
" data : Any\n",
"\n",
"class CrossReference:\n",
" type = 'crossReference'\n",
" kind : str\n",
" children : List[StaticPhrasingContent]\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"We likely have a union\n",
"FlowContent = Union[Paragraph,Definition,Heading,ThematicBreak,Blockquote,List,HTML,Code,Comment,Target,Directive,Admonition,Container,Math,Table,FootnoteDefinition]\n",
"\n",
"We likely have a union\n",
"ListContent = Union[ListItem]\n",
"\n",
"We likely have a union\n",
"PhrasingContent = Union[StaticPhrasingContent,Emphasis,Strong,Link,LinkReference,Subscript,Superscript,Underline,Abbreviation,CrossReference,FootnoteReference]\n",
"\n",
"We likely have a union\n",
"StaticPhrasingContent = Union[Text,HTML,EmphasisStatic,StrongStatic,InlineCode,Break,Image,ImageReference,Role,SubscriptStatic,SuperscriptStatic,UnderlineStatic,InlineMath]\n",
"\n",
"class Role:\n",
"Refs ? {'required'}\n",
" type = 'mystRole'\n",
" name : str\n",
" value : str\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Admonition:\n",
" type = 'admonition'\n",
" kind : str\n",
" class : str\n",
" children : List[{'anyOf': [{'$ref': '#/$defs/AdmonitionTitle'}, {'$ref': 'myst.schema.json#/$defs/FlowContent'}]}]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class AdmonitionTitle:\n",
" type = 'admonitionTitle'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class FootnoteReference:\n",
" type = 'footnoteReference'\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class FootnoteDefinition:\n",
" type = 'footnoteDefinition'\n",
" children : List[FlowContent]\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Table:\n",
" type = 'table'\n",
" align : str\n",
" children : List[TableRow]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class TableRow:\n",
" type = 'tableRow'\n",
" children : List[TableCell]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class TableCell:\n",
" type = 'tableCell'\n",
" header : bool\n",
" align : str\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class BlockBreak:\n",
" type = 'blockBreak'\n",
" meta : str\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Block:\n",
" type = 'block'\n",
" meta : str\n",
"{'description': 'Top-level children of myst document', 'anyOf': [{'type': 'array', 'items': {'$ref': 'myst.schema.json#/$defs/FlowContent'}}, {'type': 'array', 'items': {'$ref': 'myst.schema.json#/$defs/ListContent'}}, {'type': 'array', 'items': {'$ref': 'myst.schema.json#/$defs/PhrasingContent'}}]}\n",
" children: TBD\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Subscript:\n",
" type = 'subscript'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class SubscriptStatic:\n",
" type = 'subscript'\n",
" children : List[StaticPhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Superscript:\n",
" type = 'superscript'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class SuperscriptStatic:\n",
" type = 'superscript'\n",
" children : List[StaticPhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Underline:\n",
" type = 'underline'\n",
" children : List[PhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class UnderlineStatic:\n",
" type = 'underline'\n",
" children : List[StaticPhrasingContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Container:\n",
"Refs ? {'required'}\n",
"Refs ? {'if', 'then', 'else'}\n",
" type = 'container'\n",
" kind : str\n",
" class : str\n",
" enumerated : bool\n",
" enumerator : str\n",
" children : List[{'anyOf': [{'$ref': '#/$defs/Caption'}, {'$ref': '#/$defs/Legend'}, {'$ref': 'commonmark.schema.json#/$defs/Image'}, {'$ref': 'tables.schema.json#/$defs/Table'}]}]\n",
" identifier : Any\n",
" label : Any\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Caption:\n",
" type = 'caption'\n",
" children : List[FlowContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Legend:\n",
" type = 'legend'\n",
" children : List[FlowContent]\n",
" position : Any\n",
" data : Any\n",
"\n",
"class Node:\n",
" type : str\n",
" ?object\n",
" !!!{'description': 'location of node in source file; must not be present for generated nodes', '$ref': '#/$defs/Position'}\n",
"\n",
"class Literal:\n",
"Refs ? {'required'}\n",
" value : str\n",
"\n",
"class Parent:\n",
"Refs ? {'required'}\n",
" children : List[Node]\n",
"\n",
"class Point:\n",
" line : int\n",
" column : int\n",
" offset : int\n",
"\n",
"class Position:\n",
" !!!{'description': 'place of first character of parsed source region', '$ref': '#/$defs/Point'}\n",
" !!!{'description': 'place of first character after parsed source region, whether it exists or not', '$ref': '#/$defs/Point'}\n",
" indent : List[{'type': 'integer', 'minimum': 0}]\n"
]
}
],
"source": [
"defs = {}\n",
"i = 0\n",
"for root, val in schemas.items():\n",
" vx = {(root,name):v for (name,v) in val['$defs'].items()}\n",
" i += len(vx)\n",
" defs.update(vx)\n",
" \n",
"print(len(defs.keys()), i)\n",
"\n",
"\n",
"def _handle_prop(name, prop, current_root):\n",
" \n",
" prop = {k:v for k,v in prop.items()}\n",
" if 'const' in prop:\n",
" assert set(prop.keys()) == {'const'}\n",
" return f'{name} = ' + repr(prop['const'])\n",
" if not prop:\n",
" return f\"{name} : Any\"\n",
" if 'type' not in prop and 'anyOf' in prop:\n",
" print(prop)\n",
" return f\"{name}: TBD\"# \"{_union_value({'anyOf':prop['anyOf']}, current_root)}\"\n",
" if 'type' not in prop :\n",
" return '!!!'+str(prop)\n",
" if prop['type'] == 'integer':\n",
" prop.pop('type')\n",
" prop.pop('minimum', None)\n",
" prop.pop('maximum', None)\n",
" prop.pop('description', None)\n",
" assert not prop, prop\n",
" return f'{name} : int'\n",
" if prop['type'] == 'boolean':\n",
" prop.pop('type')\n",
" prop.pop('description', None)\n",
" assert not prop, prop\n",
" return f'{name} : bool'\n",
" if prop['type'] == 'string':\n",
" prop.pop('type')\n",
" prop.pop('description', None)\n",
" prop.pop('enum', None)\n",
" assert not prop, prop\n",
" return f'{name} : str'\n",
" \n",
" if prop['type'] == 'array':\n",
" prop.pop('type')\n",
" items = prop.pop('items')\n",
" prop.pop('description', None)\n",
" assert not prop, prop\n",
" if list(items.keys()) == ['$ref']:\n",
" nx, sch = resolve(schemas, items['$ref'], current_root)\n",
" return f'{name} : List[{nx}]'\n",
" \n",
" return f'{name} : List[{items}]'\n",
"\n",
" \n",
" \n",
" return f\"?{prop.get('type', prop)}\"\n",
"\n",
"\n",
"def _union_value(v, root):\n",
" assert list(v.keys()) == ['anyOf']\n",
" subs = ','.join([resolve(schemas, x['$ref'], root)[0] for x in v['anyOf']])\n",
" return f'Union[{subs}]'\n",
" \n",
"for (root,k),v in defs.items():\n",
" print()\n",
" if v.get('type',None) != 'object':\n",
" print('We likely have a union')\n",
" print(f'{k} = {_union_value(v, root)}')\n",
" continue\n",
" print(f'class {k}:')\n",
" #v.pop('type')\n",
" v.pop('description', None)\n",
" properties = {}\n",
" allOf = v.get('allOf', [])\n",
" for it in allOf :\n",
" kys = set(it.keys()) - {'properties', 'additionalProperties', '$ref'}\n",
" if kys:\n",
" print('Refs ?',kys)\n",
" if it.get('$ref'):\n",
" pass\n",
" #print('Allof Ref:', it['$ref'], resolve(schemas, it['$ref'], root)[1])\n",
" properties.update(it.get('properties',{}))\n",
" properties.update(v.get('properties',{}))\n",
" for p_name,p_val in properties.items():\n",
" print(' ', _handle_prop(p_name, p_val, root))\n",
" #print(' # additionalProperties: ', it.get('additionalProperties', None))\n",
" #print(' # required: ', it.get('required', None))\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 104,
"id": "6c4fdcfd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'required': ['referenceType'],\n",
" 'properties': {'referenceType': {'description': 'explicitness of the reference:\\n`shortcut` - reference is implicit, identifier inferred\\n`collapsed` - reference explicit, identifier inferred\\n`full` - reference explicit, identifier explicit',\n",
" 'type': 'string',\n",
" 'enum': ['shortcut', 'collapsed', 'full']}}}"
]
},
"execution_count": 104,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"v"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "921bc6a4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "da08cd68",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment