%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/optimization_features/
Upload File :
Create Path :
Current File : //opt/cloudlinux/venv/lib64/python3.11/site-packages/clwpos/optimization_features/configurations.py

# -*- coding: utf-8 -*-

# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2022 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT

# configurations.py - configuration helpers for AccelerateWP optimization features

from pathlib import Path
from typing import Optional, Dict, Tuple, Union

from clwpos import gettext as _
from clwpos.cl_wpos_exceptions import WposError
from clwpos.utils import check_domain, home_dir
from clwpos.wp_utils import WordpressError

from .features import Feature
from ..user.website_check import RollbackException


def to_interface_name(feature):
    return Feature(feature).to_interface_name()


def convert_features_dict_to_interface(features_dict):
    return {to_interface_name(feature): state for feature, state in features_dict.items()}


def convert_feature_list_to_interface(features_list):
    return [to_interface_name(feature) for feature in features_list]


class DocRootPath(str):
    """This class represent path to doc_root."""
    pass


class DomainName(str):
    """This class represent domain name."""
    pass


def disable_without_config_affecting(
        arg: Union[DocRootPath, DomainName], wp_path: str, *, module: str, domain=None
) -> Optional[WordpressError]:
    """
    Deactivate and delete specified wordpress module.
    :param arg: user's docroot or domain
    :param wp_path: path to user's wordpress directory
    :param module: module on which to perform disable operations
    :param domain: userdomain
    :return: error if error was happened else None
    """
    if isinstance(arg, DomainName):
        doc_root = check_domain(arg)[-1]
        domain = arg
    elif isinstance(arg, DocRootPath):
        doc_root = Path(home_dir(), arg)
        if not domain:
            raise ValueError('Domain must be specified')

    abs_wp_path = str(Path(doc_root).joinpath(wp_path).absolute())
    last_error = None

    errors = Feature(module).disable(abs_wp_path, domain=domain, website=wp_path)
    if errors:
        last_error = errors[-1]

    return last_error


def enable_without_config_affecting(
        arg: Union[DocRootPath, DomainName], wp_path: str, *,
        module: str, domain=None, ignore_errors=False
) -> Tuple[bool, Dict[str, Union[str, dict]]]:
    """
    Install and activate specified wordpress module.
    :param arg: user's docroot or domain
    :param wp_path: path to user's wordpress directory
    :param module: module on which to perform enable operations
    :param domain: userdomain
    :param ignore_errors: if True, skip additional checks during enabling
    :return: tuple that consists of enabling status and details
    """
    if isinstance(arg, DomainName):
        __, doc_root = check_domain(arg)
        domain = arg
    elif isinstance(arg, DocRootPath):
        doc_root = Path(home_dir(), arg)
        if not domain:
            raise ValueError('Domain must be specified')
    else:
        raise ValueError("Invalid argument format")

    wp_path = wp_path.lstrip("/")
    abs_wp_path = str(Path(doc_root).joinpath(wp_path).absolute())

    # try to install plugin
    feature = Feature(module)
    try:
        feature.install(abs_wp_path)
    except WposError as e:
        return False, dict(
            message=_("WordPress plugin installation failed. "
                      "Try again and contact your system administrator if issue persists."),
            details=e.message,
            context=e.context
        )

    # try to activate plugin
    try:
        feature.enable(abs_wp_path, domain=domain, website=wp_path, skip_checkers=ignore_errors)
    except WposError as e:
        feature.disable(abs_wp_path, domain=domain, website=wp_path)
        if isinstance(e, RollbackException):
            return False, {
                'context': e.context,
                'result': e.message,
                'issues': e.errors
            }
        return False, dict(
            message=_("WordPress plugin activation failed. Changes were reverted and caching module is now disabled. "
                      "Try again and contact your system administrator if issue persists."),
            details=e.message,
            context=e.context
        )

    return True, {}

Zerion Mini Shell 1.0