<commit_before>import os
import time
import tempfile
import shutil
import pkg_resources
from PyQt4.QtCore import Qt, QUrl
from PyQt4.QtGui import (QApplication, QDialog, QPrinter, QIcon,
                         QPrintDialog, QFileDialog, QMenu)
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from Orange.widgets.settings import Setting
from Orange.widgets.io import PngFormat


class OWReport(OWWidget):
    name = "Report"
    save_dir = Setting("")
    report_url_pref = "file:///"
    report_temp_dir = tempfile.mkdtemp("", "Orange3_report")

    def __init__(self):
        super().__init__()
        # TODO - ko kliknes na webview, oznaci item
        self.widget_list_items = []
        self.widget_list = gui.listBox(
            self.controlArea, self,
            labels="widget_list_items", callback=self._reload,
            enableDragDrop=True, dragDropCallback=self._reload
        )
        self.widget_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.widget_list.customContextMenuRequested.connect(self._show_menu)

        self.save_button = gui.button(
            self.controlArea, self, "Save",
            callback=self._save_report, default=True
        )
        self.print_button = gui.button(
            self.controlArea, self, "Print", callback=self._print_report
        )

        self.report_view_items = {}
        self.report_view = gui.WebviewWidget(self.mainArea)
        frame = self.report_view.page().mainFrame()
        frame.setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAsNeeded)
        self.javascript = frame.evaluateJavaScript

        index_file = pkg_resources.resource_filename(__name__, "index.html")
        self.report_html_template = open(index_file, "r").read()

        self.setModal(False)

    def _reload(self):
        self._build_html()

    def _show_menu(self, pos):
        widget_list_menu = QMenu(self)
        widget_list_menu.addAction("Remove", self._remove_widget_item)
        widget_list_menu.addAction("Remove All", self._clear)
        widget_list_menu.popup(self.mapToGlobal(pos))

    def _clear(self):
        self.widget_list_items = []
        self.report_view_items = {}
        self._build_html()

    def _remove_widget_item(self):
        selected_row = self.widget_list.currentRow()
        if selected_row >= 0:
            items = self.widget_list_items
            selected_item = items.pop(selected_row)
            self.widget_list_items = items
            del self.report_view_items[selected_item]
            if selected_row < len(self.report_view_items):
                self.widget_list.setCurrentRow(selected_row)
            self._build_html()

    def _add_widget_item(self, widget):
        items = self.widget_list_items
        path = pkg_resources.resource_filename(widget.__module__, widget.icon)
        icon = QIcon(path)
        items.append((widget.name, icon))
        self.report_view_items[(widget.name, icon)] = widget.report_html
        self.widget_list_items = items

    def _build_html(self):
        n_widgets = len(self.widget_list_items)
        if not n_widgets:
            return

        selected_row = self.widget_list.currentRow()
        if selected_row < 0 and n_widgets:
            selected_row = n_widgets - 1
            self.widget_list.setCurrentRow(selected_row)

        html = self.report_html_template
        html += "<body>"
        for i, (item_name, item_icon) in enumerate(self.widget_list_items):
            html += "<div id='%s' class='%s'>%s</div>" % (
                id(item_icon),
                "selected" if i == selected_row else "normal",
                self.report_view_items[(item_name, item_icon)]
            )
        html += "</body></html>"
        self.report_view.setHtml(html, QUrl(self.report_url_pref))

        if selected_row < len(self.widget_list_items):
            self.javascript(
                "document.getElementById('%s').scrollIntoView();"
                % id(self.widget_list_items[selected_row][1]))

    def make_report(self, widget):
        self._add_widget_item(widget)
        self._build_html()

    @staticmethod
    def get_html_section(name):
        datetime = time.strftime("%a %b %d %y, %H:%M:%S")
        return "<h1>%s <span class='timestamp'>%s</h1>" % (name, datetime)

    @staticmethod
    def get_html_subsection(name):
        return "<h2>%s</h2>" % name

    @staticmethod
    def get_html_paragraph(items):
        return "<ul>" + "".join("<b>%s:</b> %s</br>" % i
                                for i in items) + "</ul>"

    @staticmethod
    def get_html_img(scene):
        filename = OWReport._get_unique_filename(OWReport.get_instance(),
                                                 "img", "png")
        writer = PngFormat()
        writer.write(filename, scene)
        return "<ul><img src='%s%s'/></ul>" % (OWReport.report_url_pref,
                                               filename)

    def _get_unique_filename(self, name, ext):
        for i in range(1000000):
            filename = os.path.join(self.report_temp_dir,
                                    "%s%f.%s" % (name, i, ext))
            if not os.path.exists(filename):
                return filename<commit_msg>OWReport: Add clip_string and clipped_list<commit_after>import os
import time
import tempfile
import shutil
import pkg_resources
from PyQt4.QtCore import Qt, QUrl
from PyQt4.QtGui import (QApplication, QDialog, QPrinter, QIcon,
                         QPrintDialog, QFileDialog, QMenu)
from Orange.widgets import gui
from Orange.widgets.widget import OWWidget
from Orange.widgets.settings import Setting
from Orange.widgets.io import PngFormat


class OWReport(OWWidget):
    name = "Report"
    save_dir = Setting("")
    report_url_pref = "file:///"
    report_temp_dir = tempfile.mkdtemp("", "Orange3_report")

    def __init__(self):
        super().__init__()
        # TODO - ko kliknes na webview, oznaci item
        self.widget_list_items = []
        self.widget_list = gui.listBox(
            self.controlArea, self,
            labels="widget_list_items", callback=self._reload,
            enableDragDrop=True, dragDropCallback=self._reload
        )
        self.widget_list.setContextMenuPolicy(Qt.CustomContextMenu)
        self.widget_list.customContextMenuRequested.connect(self._show_menu)

        self.save_button = gui.button(
            self.controlArea, self, "Save",
            callback=self._save_report, default=True
        )
        self.print_button = gui.button(
            self.controlArea, self, "Print", callback=self._print_report
        )

        self.report_view_items = {}
        self.report_view = gui.WebviewWidget(self.mainArea)
        frame = self.report_view.page().mainFrame()
        frame.setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAsNeeded)
        self.javascript = frame.evaluateJavaScript

        index_file = pkg_resources.resource_filename(__name__, "index.html")
        self.report_html_template = open(index_file, "r").read()

        self.setModal(False)

    def _reload(self):
        self._build_html()

    def _show_menu(self, pos):
        widget_list_menu = QMenu(self)
        widget_list_menu.addAction("Remove", self._remove_widget_item)
        widget_list_menu.addAction("Remove All", self._clear)
        widget_list_menu.popup(self.mapToGlobal(pos))

    def _clear(self):
        self.widget_list_items = []
        self.report_view_items = {}
        self._build_html()

    def _remove_widget_item(self):
        selected_row = self.widget_list.currentRow()
        if selected_row >= 0:
            items = self.widget_list_items
            selected_item = items.pop(selected_row)
            self.widget_list_items = items
            del self.report_view_items[selected_item]
            if selected_row < len(self.report_view_items):
                self.widget_list.setCurrentRow(selected_row)
            self._build_html()

    def _add_widget_item(self, widget):
        items = self.widget_list_items
        path = pkg_resources.resource_filename(widget.__module__, widget.icon)
        icon = QIcon(path)
        items.append((widget.name, icon))
        self.report_view_items[(widget.name, icon)] = widget.report_html
        self.widget_list_items = items

    def _build_html(self):
        n_widgets = len(self.widget_list_items)
        if not n_widgets:
            return

        selected_row = self.widget_list.currentRow()
        if selected_row < 0 and n_widgets:
            selected_row = n_widgets - 1
            self.widget_list.setCurrentRow(selected_row)

        html = self.report_html_template
        html += "<body>"
        for i, (item_name, item_icon) in enumerate(self.widget_list_items):
            html += "<div id='%s' class='%s'>%s</div>" % (
                id(item_icon),
                "selected" if i == selected_row else "normal",
                self.report_view_items[(item_name, item_icon)]
            )
        html += "</body></html>"
        self.report_view.setHtml(html, QUrl(self.report_url_pref))

        if selected_row < len(self.widget_list_items):
            self.javascript(
                "document.getElementById('%s').scrollIntoView();"
                % id(self.widget_list_items[selected_row][1]))

    def make_report(self, widget):
        self._add_widget_item(widget)
        self._build_html()

    @staticmethod
    def get_html_section(name):
        datetime = time.strftime("%a %b %d %y, %H:%M:%S")
        return "<h1>%s <span class='timestamp'>%s</h1>" % (name, datetime)

    @staticmethod
    def get_html_subsection(name):
        return "<h2>%s</h2>" % name

    @staticmethod
    def get_html_paragraph(items):
        return "<ul>" + "".join("<b>%s:</b> %s</br>" % i
                                for i in items) + "</ul>"

    @staticmethod
    def get_html_img(scene):
        filename = OWReport._get_unique_filename(OWReport.get_instance(),
                                                 "img", "png")
        writer = PngFormat()
        writer.write(filename, scene)
        return "<ul><img src='%s%s'/></ul>" % (OWReport.report_url_pref,
                                               filename)

    @staticmethod
    def clip_string(s, limit=1000, sep=None):
        if len(s) < limit:
            return s
        s = s[:limit - 3]
        if sep is None:
            return s
        sep_pos = s.rfind(sep)
        if sep_pos == -1:
            return s
        return s[:sep_pos + len(sep)] + "..."

    @staticmethod
    def clipped_list(s, limit=1000):
        return OWReport.clip_string(", ".join(s), limit, ", ")

    def _get_unique_filename(self, name, ext):
        for i in range(1000000):
            filename = os.path.join(self.report_temp_dir,
                                    "%s%f.%s" % (name, i, ext))
            if not os.path.exists(filename):
                return filename
{'fraction_non_alphanumeric': 0.11049066276089345, 'fraction_numerical': 0.004210911753936287, 'mean_word_length': 2.3106060606060606, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4916074', 'n_tokens_mistral': 3329, 'n_tokens_neox': 3208, 'n_words': 608}