Exemplo n.º 1
0
def get_linter(linter_name, callback):
    """tries to retrieve a linter and calls `callback` on it on success"""
    if linter_name in LINTERS:
        callback(LINTERS[linter_name])
        return

    if linter_name not in NEEDS_LICENSE:
        showError(i18nc('@info:status', 'No acceptable linter named %1!', linter_name))
        return

    license, objname, url = NEEDS_LICENSE[linter_name]
    cache_path = p.join(CACHE_DIR, linter_name + '.js')

    def success():
        """store newly created linter and “return” it"""
        LINTERS[linter_name] = JSModule(JS_ENGINE, cache_path, objname)
        callback(LINTERS[linter_name])

    if p.exists(cache_path):
        success()
        return

    # the user doesn’t have the file. ask to accept its license
    if not license_accepted(license):
        return

    download = KIO.file_copy(KUrl(url), KUrl.fromPath(cache_path))
    @download.result.connect
    def _call(job):
        if job.error():
            showError(i18nc('@info:status', 'Download failed'))
        else:
            success()
    download.start()
Exemplo n.º 2
0
def checkAll(doc=None, excludes=None, exclude_all=False):
    """Check the syntax, pep8 and pyflakes errors of the document"""
    python_utils_conf = kate.configuration.root.get('python_utils', {})
    if not (not doc or (is_mymetype_python(doc) and
                        not doc.isModified())):
        return
    is_called = not bool(doc)
    from python_checkers.parse_checker import parseCode
    excludes = excludes or []
    currentDoc = doc or kate.activeDocument()
    mark_iface = currentDoc.markInterface()
    clearMarksOfError(currentDoc, mark_iface)
    if not exclude_all:
        if not 'parseCode' in excludes and (is_called or python_utils_conf.get(_PARSECODE_CHECK_WHEN_SAVE, DEFAULT_PARSECODE_CHECK_WHEN_SAVE)):
            parseCode.f(currentDoc, refresh=False)
        if not 'checkPyflakes' in excludes and (is_called or python_utils_conf.get(_PYFLAKES_CHECK_WHEN_SAVE, DEFAULT_CHECK_PYFLAKES_WHEN_SAVE)):
            try:
                from python_checkers.pyflakes_checker import checkPyflakes
                checkPyflakes.f(currentDoc, refresh=False)
            except ImportError:
                pass
        if not 'checkPep8' in excludes and (is_called or python_utils_conf.get(_PEP8_CHECK_WHEN_SAVE, DEFAULT_CHECK_PEP8_WHEN_SAVE)):
            from python_checkers.pep8_checker import checkPep8
            checkPep8.f(currentDoc, refresh=False)
    if not doc and currentDoc.isModified() and not excludes:
        showError(i18n('You must save the file first'))
Exemplo n.º 3
0
def prettyXMLFormat():
    """Pretty format of a XML code"""
    # TODO Use decorators to apply constraints
    document = kate.activeDocument()
    view = document.activeView()
    try:
        encoding = 'utf-8'
        source = view.selectionText()
        m = encoding_pattern.match(source)
        if m:
            encoding = m.groups()[0]
        target = minidom.parseString(source.encode(encoding))
        unicode_escape = codecs.getdecoder('unicode_escape')
        indent = unicode_escape(
            kate.configuration.get(_INDENT_CFG, DEFAULT_INDENT))[0]
        newl = unicode_escape(kate.configuration.get(_NEWL_CFG,
                                                     DEFAULT_NEWL))[0]
        xml_pretty = target.toprettyxml(
            indent=indent, newl=newl, encoding=encoding).decode(encoding)
        xml_pretty = newl.join([
            line for line in xml_pretty.split(newl)
            if line.replace(' ', '').replace(indent, '')
        ])
        document.replaceText(view.selectionRange(), xml_pretty)
    except (ExpatError, LookupError) as e:
        showError(
            i18nc('@info:tooltip', 'The selected text is not valid XML: %1',
                  str(e)))
Exemplo n.º 4
0
  def enable(self, doc):
      if doc.url() == '':
        self.act.blockSignals(True)
        showError(i18n('Can\'t auto-reload unsaved file'))
        self.act.setChecked(False)
        self.act.blockSignals(False)
        return

      doc.setModifiedOnDiskWarning(False)
      doc.modifiedOnDisk.connect(doc.documentReload)
      doc.setProperty('AutoReload', True)
      
      showOk(i18n('Auto-Reload enabled'))
Exemplo n.º 5
0
    def enable(self, doc):
        if doc.url() == '':
            self.act.blockSignals(True)
            showError(i18n('Can\'t auto-reload unsaved file'))
            self.act.setChecked(False)
            self.act.blockSignals(False)
            return

        doc.setModifiedOnDiskWarning(False)
        doc.modifiedOnDisk.connect(doc.documentReload)
        doc.setProperty('AutoReload', True)

        showOk(i18n('Auto-Reload enabled'))
Exemplo n.º 6
0
 def treatmentException(self, e):
     if self.invocationType == KTextEditor.CodeCompletionModel.AutomaticInvocation:
         return
     f = e.filename or ""
     text = e.text
     line = e.lineno
     message = i18n("There was a syntax error in this file:")
     if f:
         message = i18nc("%1 is error message", "%1\n  * file: %2", message, f)
     if text:
         message = i18nc("%1 is error message", "%1\n  * text: %2", message, text)
     if line:
         message = i18nc("%1 is error message", "%1\n  * line: %2", message, line)
     showError(message)
Exemplo n.º 7
0
def saveFirst():
    showError(i18n('You must save the file first'))
Exemplo n.º 8
0
 def _call(job):
     if job.error():
         showError(i18nc('@info:status', 'Download failed'))
     else:
         success()