示例#1
0
def parse_weight_str(context: PluginContext, weight_value) -> int:
    """For constructs like:

    - choice:
        probability: 60%
        pick: Closed Won

    Render and convert the 60% to just 60.
    """
    weight_str = context.evaluate(weight_value)
    if isinstance(weight_str, str):
        weight_str = weight_str.rstrip("%")
    return int(weight_str)
示例#2
0
    def __init__(
        self,
        output_stream: OutputStream,
        parse_result: ParseResult,
        globals: Globals,
        *,
        parent_application,
        options: Mapping = None,
        snowfakery_plugins: Optional[Mapping[str, callable]] = None,
        faker_providers: Sequence[object] = (),
        continuing=False,
    ):
        self.output_stream = output_stream
        self.options = options or {}
        self.faker_providers = faker_providers
        snowfakery_plugins = snowfakery_plugins or {}
        self.plugin_instances = {
            name: plugin(self) for name, plugin in snowfakery_plugins.items()
        }
        self.plugin_function_libraries = {
            name: plugin.custom_functions()
            for name, plugin in self.plugin_instances.items()
        }
        self.globals = globals
        self.continuing = continuing
        stop_table_name = parent_application.stopping_tablename
        if stop_table_name and stop_table_name not in parse_result.tables:
            raise DataGenNameError(
                f"No template creating {stop_table_name}",
            )

        # make a plugin context for our Faker stuff to act like a plugin
        self.faker_plugin_context = PluginContext(SnowfakeryPlugin(self))

        self.faker_template_libraries = {}

        # inject context into the standard functions
        standard_funcs_obj = StandardFuncs(self).custom_functions()
        self.standard_funcs = {
            name: getattr(standard_funcs_obj, name)
            for name in dir(standard_funcs_obj)
            if not name.startswith("_") and name != "context"
        }

        self.statements = parse_result.statements
        self.parent_application = parent_application
示例#3
0
def render_boolean(context: PluginContext, value: FieldDefinition) -> bool:
    val = context.evaluate(value)
    if isinstance(val, str):
        val = literal_eval(val)

    return bool(val)