<commit_before># Copyright 2013 XCG Consulting (http://odoo.consulting)
# Copyright 2018 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import time
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
from odoo.tools.misc import find_in_path
from odoo.tools.safe_eval import safe_eval


logger = logging.getLogger(__name__)

try:
    from py3o.formats import Formats
except ImportError:
    logger.debug('Cannot import py3o.formats')

PY3O_CONVERSION_COMMAND_PARAMETER = "py3o.conversion_command"


class IrActionsReport(models.Model):
    """ Inherit from ir.actions.report to allow customizing the template
    file. The user cam chose a template from a list.
    The list is configurable in the configuration tab, see py3o_template.py
    """

    _inherit = 'ir.actions.report'

    @api.multi
    @api.constrains("py3o_filetype", "report_type")
    def _check_py3o_filetype(self):
        for report in self:
            if report.report_type == "py3o" and not report.py3o_filetype:
                raise ValidationError(_(
                    "Field 'Output Format' is required for Py3O report"))

    @api.model
    def _get_py3o_filetypes(self):
        formats = Formats()
        names = formats.get_known_format_names()
        selections = []
        for name in names:
            description = name
            if formats.get_format(name).native:
                description = description + " " + _("(Native)")
            selections.append((name, description))
        return selections

    report_type = fields.Selection(
        selection_add=[("py3o", "py3o")]
        )
    py3o_filetype = fields.Selection(
        selection="_get_py3o_filetypes",
        string="Output Format")
    is_py3o_native_format = fields.Boolean(
        compute='_compute_is_py3o_native_format'
    )
    py3o_template_id = fields.Many2one(
        'py3o.template',
        "Template")
    module = fields.Char(
        "Module",
        help="The implementer module that provides this report")
    py3o_template_fallback = fields.Char(
        "Fallback",
        size=128,
        help=(
            "If the user does not provide a template this will be used "
            "it should be a relative path to root of YOUR module "
            "or an absolute path on your server."
        ))
    report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
    py3o_multi_in_one = fields.Boolean(
        string='Multiple Records in a Single Report',
        help="If you execute a report on several records, "
        "by default Odoo will generate a ZIP file that contains as many "
        "files as selected records. If you enable this option, Odoo will "
        "generate instead a single report for the selected records.")
    lo_bin_path = fields.Char(
        string="Path to the libreoffice runtime",
        compute="_compute_lo_bin_path"
        )
    is_py3o_report_not_available = fields.Boolean(
        compute='_compute_py3o_report_not_available'
        )
    msg_py3o_report_not_available = fields.Char(
        compute='_compute_py3o_report_not_available'
        )

    @api.model
    def _register_hook(self):
        self._validate_reports()

    @api.model
    def _validate_reports(self):
        """Check if the existing py3o reports should work with the current
        installation.

        This method log a warning message into the logs for each report
        that should not work.
        """
        for report in self.search([("report_type", "=", "py3o")]):
            if report.is_py3o_report_not_available:
                logger.warning(report.msg_py3o_report_not_available)

    @api.model
    def _get_lo_bin(self):
        lo_bin = self.env['ir.config_parameter'].get_param(
            PY3O_CONVERSION_COMMAND_PARAMETER, 'libreoffice',
        )
        try:
            lo_bin = find_in_path(lo_bin)
        except IOError:
            lo_bin = None
        return lo_bin

    @api.depends("report_type", "py3o_filetype")<commit_msg>[FIX] report_py3o: Access to ir.config_parameter with sudo<commit_after># Copyright 2013 XCG Consulting (http://odoo.consulting)
# Copyright 2018 <NAME>/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging
import time
from odoo import api, fields, models, _
from odoo.exceptions import ValidationError
from odoo.tools.misc import find_in_path
from odoo.tools.safe_eval import safe_eval


logger = logging.getLogger(__name__)

try:
    from py3o.formats import Formats
except ImportError:
    logger.debug('Cannot import py3o.formats')

PY3O_CONVERSION_COMMAND_PARAMETER = "py3o.conversion_command"


class IrActionsReport(models.Model):
    """ Inherit from ir.actions.report to allow customizing the template
    file. The user cam chose a template from a list.
    The list is configurable in the configuration tab, see py3o_template.py
    """

    _inherit = 'ir.actions.report'

    @api.multi
    @api.constrains("py3o_filetype", "report_type")
    def _check_py3o_filetype(self):
        for report in self:
            if report.report_type == "py3o" and not report.py3o_filetype:
                raise ValidationError(_(
                    "Field 'Output Format' is required for Py3O report"))

    @api.model
    def _get_py3o_filetypes(self):
        formats = Formats()
        names = formats.get_known_format_names()
        selections = []
        for name in names:
            description = name
            if formats.get_format(name).native:
                description = description + " " + _("(Native)")
            selections.append((name, description))
        return selections

    report_type = fields.Selection(
        selection_add=[("py3o", "py3o")]
        )
    py3o_filetype = fields.Selection(
        selection="_get_py3o_filetypes",
        string="Output Format")
    is_py3o_native_format = fields.Boolean(
        compute='_compute_is_py3o_native_format'
    )
    py3o_template_id = fields.Many2one(
        'py3o.template',
        "Template")
    module = fields.Char(
        "Module",
        help="The implementer module that provides this report")
    py3o_template_fallback = fields.Char(
        "Fallback",
        size=128,
        help=(
            "If the user does not provide a template this will be used "
            "it should be a relative path to root of YOUR module "
            "or an absolute path on your server."
        ))
    report_type = fields.Selection(selection_add=[('py3o', "Py3o")])
    py3o_multi_in_one = fields.Boolean(
        string='Multiple Records in a Single Report',
        help="If you execute a report on several records, "
        "by default Odoo will generate a ZIP file that contains as many "
        "files as selected records. If you enable this option, Odoo will "
        "generate instead a single report for the selected records.")
    lo_bin_path = fields.Char(
        string="Path to the libreoffice runtime",
        compute="_compute_lo_bin_path"
        )
    is_py3o_report_not_available = fields.Boolean(
        compute='_compute_py3o_report_not_available'
        )
    msg_py3o_report_not_available = fields.Char(
        compute='_compute_py3o_report_not_available'
        )

    @api.model
    def _register_hook(self):
        self._validate_reports()

    @api.model
    def _validate_reports(self):
        """Check if the existing py3o reports should work with the current
        installation.

        This method log a warning message into the logs for each report
        that should not work.
        """
        for report in self.search([("report_type", "=", "py3o")]):
            if report.is_py3o_report_not_available:
                logger.warning(report.msg_py3o_report_not_available)

    @api.model
    def _get_lo_bin(self):
        lo_bin = self.env['ir.config_parameter'].sudo().get_param(
            PY3O_CONVERSION_COMMAND_PARAMETER, 'libreoffice',
        )
        try:
            lo_bin = find_in_path(lo_bin)
        except IOError:
            lo_bin = None
        return lo_bin

    @api.depends("report_type", "py3o_filetype")
{'fraction_non_alphanumeric': 0.08692986758214812, 'fraction_numerical': 0.011647866601275135, 'mean_word_length': 2.643144260830728, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4612460', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2410, 'n_words': 639}