enums_to_js

Transpiler tools for PEP 435 style python enumeration classes.

class render_static.transpilers.enums_to_js.UnrecognizedBehavior[source]

Bases: Enum

Enumeration of behaviors when a value cannot be mapped to an enum instance:

THROW_EXCEPTION = 1

Throw a TypeError if the value cannot be mapped to an enum instance.

RETURN_NULL = 2

Return null if the value cannot be mapped to an enum instance.

RETURN_INPUT = 3

Return the input value if the value cannot be mapped to an enum instance.

class render_static.transpilers.enums_to_js.EnumTranspiler[source]

Bases: Transpiler

The base javascript transpiler for python PEP 435 Enums. Extend from this base class to write custom transpilers.

__init__(to_javascript=<function to_js>, overrides=None, **kwargs)
Parameters:
  • to_javascript (str | Callable) – A callable that accepts a python artifact and returns a transpiled object or primitive instantiation.

  • kwargs – A set of configuration parameters for the generator, see above.

  • overrides (Dict[str, OverrideNode] | None)

Return type:

None

classmethod __new__(*args, **kwargs)
class render_static.transpilers.enums_to_js.EnumClassWriter[source]

Bases: EnumTranspiler

A PEP 435 transpiler that generates ES6 style classes in the style of https://github.com/rauschma/enumify

__init__(class_name='{}', on_unrecognized=UnrecognizedBehavior.THROW_EXCEPTION, export=False, include_properties=True, symmetric_properties=False, exclude_properties=None, class_properties=True, to_string=True, isymmetric_properties=None, **kwargs)[source]
Parameters:
  • class_name (str) – A pattern to use to generate class names. This should be a string that will be formatted with the class name of each enum. The default string ‘{}’ will resolve to the python class name.

  • on_unrecognized (str | UnrecognizedBehavior) – If the given value cannot be mapped to an enum instance, either “THROW_EXCEPTION”, “RETURN_NULL”, or “RETURN_INPUT”. See render_static.transpilers.enums_to_js.UnrecognizedBehavior.

  • export (bool) – If true the classes will be exported - Default: False

  • include_properties (bool | Collection[str]) – If true, any python properties present on the enums will be included in the transpiled javascript enums. May also be an iterable of property names to include. value will always be included.

  • symmetric_properties (bool | Collection[str]) – If true, properties that the enums may be instantiated from will be automatically determined and included in the get() function. If False (default), enums will not be instantiable from properties. May also be an iterable of property names to treat as symmetric.

  • exclude_properties (Collection[str] | None) – Exclude this list of properties. Only useful if include_properties is True

  • class_properties (bool | Collection[str]) – If true, include all Django classproperties as static members on the transpiled Enum class. May also be an iterable of specific property names to include.

  • to_string (bool | str) – If true (default) include a toString() method that returns a string representation of the enum. If a non-empty string, use that string as the name of the property to return from toString().

  • isymmetric_properties (Collection[str] | bool | None) – If provided, case insensitive symmetric properties will be limited to those listed. If not provided, case insensitive properties will be dynamically determined. Provide an empty list to disable case insensitive properties.

  • kwargs – additional kwargs for the base transpiler classes.

Return type:

None

classmethod __new__(*args, **kwargs)
property EnumClassWriter.context

The template render context passed to overrides. In addition to render_static.transpilers.Transpiler.context. This includes:

  • enum: The enum class being transpiled

  • class_name: The name of the transpiled class

  • properties: A list of property names of the enum to transpile

  • str_prop: The name of the string property of the enum

  • class_properties: A list of the class property names of the enum to transpile

  • symmetric_properties: The list of property names that the enum can be instantiated from

  • to_string: Boolean, True if the enum should have a toString() method