@@ -38,36 +38,77 @@ def get_sub(prop: dict):
3838 return "TList(Any())"
3939 if prop ["type" ] == "object" :
4040 return "Dict()"
41- if prop ["type" ] in ["anyOf" , "function" , "record" , "ref" ]:
41+ if prop ["type" ] == "record" :
42+ return "Dict()"
43+ if prop ["type" ] == "ref" and prop .get ("ref" , "" ).startswith ("RouteLocation" ):
44+ return "Any()"
45+ if prop ["type" ] == "allOf" and "string & {}" in prop .get ("text" , "" ):
46+ return "Unicode()"
47+ if prop ["type" ] == "anyOf" :
48+ sub = [get_sub (e ) for e in prop ["items" ] if get_sub (e ) is not None ]
49+ if "Any()" in sub :
50+ return "Any()"
51+ if len (set (sub )) == 1 :
52+ return sub [0 ]
53+ if sub :
54+ return f"Union([{ ', ' .join (sub )} ])"
55+ return "Any()"
56+ if prop ["type" ] in ["allOf" , "constructor" , "function" , "ref" , "UNSUPPORTED" ]:
4257 return None
4358 raise Exception (f"Unknown sub type { prop ['type' ]} " )
4459
4560
61+ def supports_record (prop : dict ):
62+ return "record<" in prop .get ("text" , "" ).casefold ()
63+
64+
65+ def with_default (traitlet : str ):
66+ if traitlet .startswith ("Union(" ):
67+ return traitlet [:- 1 ] + ", default_value=None, allow_none=True).tag(sync=True)\n "
68+ return traitlet .replace ("()" , "(default_value=None, allow_none=True).tag(sync=True)\n " )
69+
70+
4671def generate_traitlet (prop : dict ):
72+ mixed_string_union = (
73+ prop ["type" ] == "anyOf"
74+ and "string & {}" in prop ["text" ]
75+ and {item ["type" ] for item in prop ["items" ]}.issuperset ({"boolean" , "number" })
76+ )
4777 if (
48- "string & {}" in prop ["text" ]
78+ ( "string & {}" in prop ["text" ] and not mixed_string_union )
4979 or prop ["type" ] == "string"
5080 or prop ["type" ] == "ref"
5181 or prop ["type" ] == "function"
5282 ):
5383 return "Unicode(default_value=None, allow_none=True).tag(sync=True)\n "
5484 if prop ["type" ] == "anyOf" :
5585 sub = [get_sub (e ) for e in prop ["items" ] if get_sub (e ) is not None ]
86+ if "Any()" in sub :
87+ return "Any().tag(sync=True)\n "
88+ if mixed_string_union :
89+ sub .sort (key = lambda traitlet : traitlet != "Bool()" )
90+ sub = list (dict .fromkeys (sub ))
91+ if not sub :
92+ return "Any().tag(sync=True)\n "
5693 if len (set (sub )) == 1 :
5794 if sub [0 ] == "TList(Any())" :
5895 return "TList(Any(), default_value=None, allow_none=True).tag(sync=True)\n "
59- return sub [0 ]. replace ( "()" , "(default_value=None, allow_none=True).tag(sync=True) \n " )
96+ return with_default ( sub [0 ])
6097 return f"Union([{ ', ' .join (sub )} ], default_value=None, allow_none=True).tag(sync=True)\n "
6198 if prop ["type" ] == "boolean" :
6299 return "Bool(default_value=None, allow_none=True).tag(sync=True)\n "
63100 if prop ["type" ] == "array" :
101+ if supports_record (prop ):
102+ return "Union([TList(Any()), Dict()], default_value=None, allow_none=True).tag(sync=True)\n "
64103 return "TList(Any(), default_value=None, allow_none=True).tag(sync=True)\n "
65104 if prop ["type" ] == "object" :
66105 return "Dict(default_value=None, allow_none=True).tag(sync=True)\n "
67106 if prop ["type" ] == "number" :
68107 return "Float(default_value=None, allow_none=True).tag(sync=True)\n "
69108 if prop ["type" ] == "any" :
70109 return "Any().tag(sync=True)\n "
110+ if prop ["type" ] == "unknown" :
111+ return "Any().tag(sync=True)\n "
71112 raise Exception (f"Unknown type { prop ['type' ]} " )
72113
73114
@@ -82,7 +123,7 @@ class {name[1:]}(VuetifyWidget):
82123 data = json .load (f )
83124 props = data ["props" ]
84125 for prop_name , prop in sorted (props .items ()):
85- if prop ["type" ] in [ "record" , "unknown" ] :
126+ if prop ["type" ] == "record" :
86127 continue
87128
88129 code += dedent_with_offset (
@@ -152,6 +193,9 @@ def generate_js_class(name, path):
152193
153194 with open (path ) as f :
154195 data = json .load (f )
196+ component_name = (
197+ "IpyvuetifyDatePicker" if data ["fileName" ] == "VDatePicker" else data ["fileName" ]
198+ )
155199 props = data ["props" ]
156200 for prop_name , prop in props .items ():
157201 if prop ["type" ] in ["record" , "unknown" ]:
@@ -170,7 +214,7 @@ def generate_js_class(name, path):
170214 }}
171215
172216 getVueTag() {{ // eslint-disable-line class-methods-use-this
173- return "{ data [ "fileName" ] } ";
217+ return "{ component_name } ";
174218 }}
175219}}
176220
0 commit comments