source: virtaal/trunk/fuentes/virtaal/controllers/checkscontroller.py @ 252

Last change on this file since 252 was 252, checked in by mabarracus, 5 years ago

Added patch to remove unit test, required by Pilar

File size: 9.2 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3#
4# Copyright 2010-2011 Zuza Software Foundation
5#
6# This file is part of Virtaal.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program; if not, see <http://www.gnu.org/licenses/>.
20
21import logging
22from gobject import SIGNAL_RUN_FIRST, timeout_add
23
24from virtaal.common import GObjectWrapper
25
26from basecontroller import BaseController
27
28check_names = {
29    'fuzzy': _(u"Fuzzy"),
30    'untranslated': _(u"Untranslated"),
31    'accelerators': _(u"Accelerators"),
32    'acronyms': _(u"Acronyms"),
33    'blank': _(u"Blank"),
34    'brackets': _(u"Brackets"),
35    'compendiumconflicts': _(u"Compendium conflict"),
36    'credits': _(u"Translator credits"),
37    'doublequoting': _(u"Double quotes"),
38    'doublespacing': _(u"Double spaces"),
39    'doublewords': _(u"Repeated word"),
40    'emails': _(u"E-mail"),
41    'endpunc': _(u"Ending punctuation"),
42    'endwhitespace': _(u"Ending whitespace"),
43    'escapes': _(u"Escapes"),
44    'filepaths': _(u"File paths"),
45    'functions': _(u"Functions"),
46    'gconf': _(u"GConf values"),
47    'kdecomments': _(u"Old KDE comment"),
48    'long': _(u"Long"),
49    'musttranslatewords': _(u"Must translate words"),
50    'newlines': _(u"Newlines"),
51    'nplurals': _(u"Number of plurals"),
52    'notranslatewords': _(u"Don't translate words"),
53    'numbers': _(u"Numbers"),
54    'options': _(u"Options"),
55    'printf': _(u"printf()"),
56    'puncspacing': _(u"Punctuation spacing"),
57    'purepunc': _(u"Pure punctuation"),
58    'sentencecount': _(u"Number of sentences"),
59    'short': _(u"Short"),
60    'simplecaps': _(u"Simple capitalization"),
61    'simpleplurals': _(u"Simple plural(s)"),
62    'singlequoting': _(u"Single quotes"),
63    'spellcheck': _(u"Spelling"),
64    'startcaps': _(u"Starting capitalization"),
65    'startpunc': _(u"Starting punctuation"),
66    'startwhitespace': _(u"Starting whitespace"),
67    'tabs': _(u"Tabs"),
68    'unchanged': _(u"Unchanged"),
69    'urls': _(u"URLs"),
70    'validchars': _(u"Valid characters"),
71    'variables': _(u"Placeholders"),
72    'xmltags': _(u"XML tags"),
73
74# Consider:
75#  -  hassuggestion
76#  -  isreview
77}
78
79
80class ChecksController(BaseController):
81    """Controller for quality checks."""
82
83    __gtype_name__ = 'ChecksController'
84    __gsignals__ = {
85        'checker-set':  (SIGNAL_RUN_FIRST, None, (object,)),
86        'unit-checked': (SIGNAL_RUN_FIRST, None, (object, object, object))
87    }
88
89    CHECK_TIMEOUT = 500
90    """Time to wait before performing checks on the current unit."""
91
92    # INITIALIZERS #
93    def __init__(self, main_controller):
94        GObjectWrapper.__init__(self)
95
96        self.main_controller = main_controller
97        self.main_controller.checks_controller = self
98        self.store_controller = main_controller.store_controller
99
100        main_controller.store_controller.connect('store-loaded', self._on_store_loaded)
101        main_controller.unit_controller.connect('unit-modified', self._on_unit_modified)
102        if main_controller.lang_controller:
103            main_controller.lang_controller.connect('target-lang-changed', self._on_target_lang_changed)
104        else:
105            main_controller.connect('controller-registered', self._on_controller_registered)
106
107        self.code = None
108        self._checker = None
109        self._check_timer_active = False
110        self._checker_code_to_name = {
111              None: _('Default'),
112              "openoffice":  _('OpenOffice.org'),
113              "mozilla": _('Mozilla'),
114              "kde": _('KDE'),
115              "gnome": _('GNOME'),
116              "drupal": _('Drupal'),
117        }
118        self._checker_name_to_code = dict([(value, key) for (key, value) in self._checker_code_to_name.items()])
119        self._checker_info = None
120        self._checker_menu_items = {}
121        self._cursor_connection = ()
122        self.last_unit = None
123
124        self._projview = None
125        self._unitview = None
126
127        if self.store_controller.get_store():
128            # We are too late for the initial 'store-loaded'
129            self._on_store_loaded(self.store_controller)
130
131    # ACCESSORS #
132    def _get_checker_info(self):
133        if not self._checker_info:
134            from translate.filters import checks
135            self._checker_info = {
136                # XXX: Add other checkers below with a localisable string as key
137                #      (used on the GUI) and a checker class as the value.
138                None:    checks.StandardChecker,
139                'openoffice': checks.OpenOfficeChecker,
140                'mozilla':    checks.MozillaChecker,
141                'drupal':     checks.DrupalChecker,
142                'gnome':      checks.GnomeChecker,
143                'kde':        checks.KdeChecker,
144            }
145        return self._checker_info
146    checker_info = property(_get_checker_info)
147
148    def _get_projview(self):
149        from virtaal.views.checksprojview import ChecksProjectView
150        if self._projview is None:
151            self._projview = ChecksProjectView(self)
152            self._projview.show()
153        return self._projview
154    projview = property(_get_projview)
155
156    def _get_unitview(self):
157        from virtaal.views.checksunitview import ChecksUnitView
158        if self._unitview is None:
159            self._unitview = ChecksUnitView(self)
160            self._unitview.show()
161        return self._unitview
162    unitview = property(_get_unitview)
163
164    def get_checker(self):
165        return self._checker
166
167    def set_checker_by_name(self, name):
168        self.set_checker_by_code(self._checker_name_to_code.get(name, None))
169
170    def set_checker_by_code(self, code):
171        target_lang = self.main_controller.lang_controller.target_lang.code
172        if not target_lang:
173            target_lang = None
174        self._checker = self.checker_info.get(code, self.checker_info[None])()
175        self._checker.config.updatetargetlanguage(target_lang)
176
177        self.emit('checker-set', self.get_checker())
178        self.projview.set_checker_name(self._checker_code_to_name.get(code, self._checker_code_to_name[None]))
179        self.code = code
180        if self.main_controller.unit_controller.current_unit:
181            self.check_unit(self.main_controller.unit_controller.current_unit)
182
183
184    # METHODS #
185    def check_unit(self, unit):
186        #LliureX Says
187        return None
188        checker = self.get_checker()
189        if not checker:
190            logging.debug('No checker instantiated :(')
191            return
192        self.last_failures = checker.run_filters(unit)
193        if self.last_failures:
194            logging.debug('Failures: %s' % (self.last_failures))
195        self.unitview.update(self.last_failures)
196        self.emit('unit-checked', unit, checker, self.last_failures)
197        return self.last_failures
198
199    def _check_timer_expired(self, unit):
200        self._check_timer_active = False
201        if unit is not self.last_unit:
202            return
203        self.check_unit(unit)
204
205    def _start_check_timer(self):
206        if self._check_timer_active:
207            return
208        if not self.last_unit:
209            # haven't changed units yet, probably strange timing issue
210            return
211        self._check_timer_active = True
212        timeout_add(self.CHECK_TIMEOUT, self._check_timer_expired, self.last_unit)
213
214    def get_check_name(self, check):
215        """Return the human readable form of the given check name."""
216        name = check_names.get(check, None)
217        if not name and check.startswith('check-'):
218            check = check[len('check-'):]
219            name = check_names.get(check, None)
220        if not name:
221            name = check
222        return name
223
224
225    # EVENT HANDLERS #
226    def _on_controller_registered(self, main_controller, controller):
227        if controller is main_controller.lang_controller:
228            controller.connect('target-lang-changed', self._on_target_lang_changed)
229
230    def _on_cursor_changed(self, cursor):
231        self.last_unit = cursor.deref()
232        self.check_unit(self.last_unit)
233
234    def _on_target_lang_changed(self, lang_controller, langcode):
235        current_checker = self.get_checker()
236        if current_checker:
237           current_checker.config.updatetargetlanguage(langcode)
238           self.emit('checker-set', current_checker)
239           if self.last_unit:
240               self.check_unit(self.last_unit)
241
242    def _on_store_loaded(self, store_controller):
243        self.set_checker_by_code(store_controller.store._trans_store.getprojectstyle())
244        if self._cursor_connection:
245            widget, connect_id = self._cursor_connection
246            widget.disconnect(connect_id)
247        self._cursor_connection = (
248            store_controller.cursor,
249            store_controller.cursor.connect('cursor-changed', self._on_cursor_changed)
250        )
251        self._on_cursor_changed(store_controller.cursor)
252
253    def _on_unit_modified(self, unit_controller, unit):
254        self._start_check_timer()
Note: See TracBrowser for help on using the repository browser.