Пример #1
0
def new_Pvariable(id_: ID, *args: SafeExpType) -> Pvariable:
    """Declare a new pointer variable in the code generation by calling it's constructor
    with the given arguments.

    :param id_: The ID used to declare the variable (also specifies the type).
    :param args: The values to pass to the constructor.

    :returns The new variable as a MockObj.
    """
    if args and isinstance(args[0], TemplateArguments):
        id_ = id_.copy()
        id_.type = id_.type.template(args[0])
        args = args[1:]
    rhs = id_.type.new(*args)
    return Pvariable(id_, rhs)
Пример #2
0
def variable(id_: ID, rhs: SafeExpType, type_: "MockObj" = None) -> "MockObj":
    """Declare a new variable, not pointer type, in the code generation.

    :param id_: The ID used to declare the variable.
    :param rhs: The expression to place on the right hand side of the assignment.
    :param type_: Manually define a type for the variable, only use this when it's not possible
      to do so during config validation phase (for example because of template arguments).

    :returns The new variable as a MockObj.
    """
    assert isinstance(id_, ID)
    rhs = safe_exp(rhs)
    obj = MockObj(id_, ".")
    if type_ is not None:
        id_.type = type_
    assignment = AssignmentExpression(id_.type, "", id_, rhs, obj)
    CORE.add(assignment)
    CORE.register_variable(id_, obj)
    return obj
Пример #3
0
def to_code(config):
    paren = yield cg.get_variable(config[CONF_WEB_SERVER_BASE_ID])

    var = cg.new_Pvariable(config[CONF_ID], paren)
    yield cg.register_component(var, config)

    if CONF_AUTH in config:
        username = config[CONF_AUTH][CONF_USERNAME]
        if username:
            cg.add(var.set_username(username))
            password = config[CONF_AUTH][CONF_PASSWORD]
            if password:
                cg.add(var.set_password(password))

    gz_io = BytesIO()
    with GzipFile(
        fileobj=gz_io,
        mode="wb",
        compresslevel=9,
        mtime=None
        if config[CONF_FORCE_UPDATE]
        else Path(CORE.config_path).stat().st_mtime,
    ) as f:
        f.write(bytes(_dump_config(), encoding="utf8"))

    gz_config = gz_io.getvalue()

    arr = cg.progmem_array(
        ID(
            "backup_data_43c31a8938664f3a97597d916cb5c2ba",
            is_declaration=True,
            type=cg.uint8,
        ),
        ArrayInitializer(*[o for o in gz_config]),
    )
    cg.add(var.set_config(arr, len(gz_config)))

    _LOGGER.info(f"Backup config will take: {len(gz_config)} bytes")