MCPcopy Create free account
hub / github.com/dot-agent/nextpy / format_prop

Function format_prop

nextpy/utils/format.py:312–374  ·  view source on GitHub ↗

Format a prop. Args: prop: The prop to format. Returns: The formatted prop to display within a tag. Raises: exceptions.InvalidStylePropError: If the style prop value is not a valid type. TypeError: If the prop is not valid.

(
    prop: Union[Var, EventChain, ComponentStyle, str],
)

Source from the content-addressed store, hash-verified

310 return switch_code
311
312def format_prop(
313 prop: Union[Var, EventChain, ComponentStyle, str],
314) -> Union[int, float, str]:
315 """Format a prop.
316
317 Args:
318 prop: The prop to format.
319
320 Returns:
321 The formatted prop to display within a tag.
322
323 Raises:
324 exceptions.InvalidStylePropError: If the style prop value is not a valid type.
325 TypeError: If the prop is not valid.
326 """
327 # import here to avoid circular import.
328 from nextpy.backend.event import EventChain
329
330 try:
331 # Handle var props.
332 if isinstance(prop, Var):
333 if not prop._var_is_local or prop._var_is_string:
334 return str(prop)
335 if types._issubclass(prop._var_type, str):
336 return format_string(prop._var_full_name)
337 prop = prop._var_full_name
338
339 # Handle event props.
340 elif isinstance(prop, EventChain):
341 sig = inspect.signature(prop.args_spec) # type: ignore
342 if sig.parameters:
343 arg_def = ",".join(f"_{p}" for p in sig.parameters)
344 arg_def = f"({arg_def})"
345 else:
346 # add a default argument for addEvents if none were specified in prop.args_spec
347 # used to trigger the preventDefault() on the event.
348 arg_def = "(_e)"
349
350 chain = ",".join([format_event(event) for event in prop.events])
351 event = f"addEvents([{chain}], {arg_def}, {json_dumps(prop.event_actions)})"
352 prop = f"{arg_def} => {event}"
353
354 # Handle other types.
355 elif isinstance(prop, str):
356 if is_wrapped(prop, "{"):
357 return prop
358 return json_dumps(prop)
359
360 # For dictionaries, convert any properties to strings.
361 elif isinstance(prop, dict):
362 prop = serializers.serialize_dict(prop) # type: ignore
363
364 else:
365 # Dump the prop as JSON.
366 prop = json_dumps(prop)
367 except exceptions.InvalidStylePropError:
368 raise
369 except TypeError as e:

Callers 1

format_propsFunction · 0.85

Calls 6

format_stringFunction · 0.85
format_eventFunction · 0.85
json_dumpsFunction · 0.85
is_wrappedFunction · 0.85
wrapFunction · 0.85
joinMethod · 0.80

Tested by

no test coverage detected