[gnome-code-assistance] Add yaml backend based on yamllint



commit 18a7d6a0d1329338f7ed81fda5e4875bc075dd3b
Author: Elad Alfassa <elad fedoraproject org>
Date:   Fri Oct 20 21:26:49 2017 +0300

    Add yaml backend based on yamllint

 README                                             |    3 +
 backends/Makefile.am                               |    4 +
 backends/yaml/Makefile.am                          |   15 ++++
 backends/yaml/__init__.py                          |   85 ++++++++++++++++++++
 .../yaml/org.gnome.CodeAssist.v1.yaml.service.in   |    3 +
 backends/yaml/yaml.in                              |    7 ++
 configure.ac                                       |   35 ++++++++
 7 files changed, 152 insertions(+), 0 deletions(-)
---
diff --git a/README b/README
index 1f1f749..9c9e341 100644
--- a/README
+++ b/README
@@ -199,3 +199,6 @@ Run time dependencies : libvala >= 0.20, libgee-0.8
 
 ### Xml
 Dependencies : python, python-dbus
+
+### Yaml
+Dependencies : python, yamllint
diff --git a/backends/Makefile.am b/backends/Makefile.am
index 4d3e881..9e86c5d 100644
--- a/backends/Makefile.am
+++ b/backends/Makefile.am
@@ -50,4 +50,8 @@ if BACKENDS_JSON_ENABLE
 include backends/json/Makefile.am
 endif
 
+if BACKENDS_YAML_ENABLE
+include backends/yaml/Makefile.am
+endif
+
 GITIGNOREDEPS += backends/Makefile.am
diff --git a/backends/yaml/Makefile.am b/backends/yaml/Makefile.am
new file mode 100644
index 0000000..d01a7d4
--- /dev/null
+++ b/backends/yaml/Makefile.am
@@ -0,0 +1,15 @@
+yamlbackenddir = $(GCA_PYBACKENDS_DIR)/yaml
+yamlbackend_PYTHON =                           \
+       backends/yaml/__init__.py
+
+yamlbackendexecdir = $(GCA_BACKENDS_EXEC_DIR)
+yamlbackendexec_SCRIPTS = \
+       backends/yaml/yaml
+
+yamlbackendservicedir = $(datadir)/dbus-1/services
+yamlbackendservice_DATA = \
+       backends/yaml/org.gnome.CodeAssist.v1.yaml.service
+
+EXTRA_DIST += $(pythonbackendservice_DATA)
+
+GITIGNOREDEPS += backends/yaml/Makefile.am
diff --git a/backends/yaml/__init__.py b/backends/yaml/__init__.py
new file mode 100644
index 0000000..75881a4
--- /dev/null
+++ b/backends/yaml/__init__.py
@@ -0,0 +1,85 @@
+# gnome code assistance yaml backend
+# Copyright (C) 2017  Elad Alfassa <elad fedoraproject org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+import os
+import os.path
+
+from gnome.codeassistance import transport, types
+
+from yamllint.config import YamlLintConfig
+from yamllint.linter import PROBLEM_LEVELS
+from yamllint import linter
+
+
+def get_yamllint_config(doc_path):
+    """ Look for yamllint config files and return a YamlLintConfig object """
+
+    # try .yamlllint first
+    doc_dir = os.path.dirname(doc_path)
+    dotfile = os.path.join(doc_dir, '.yamllint')
+    if os.path.isfile(dotfile):
+        return YamlLintConfig(file=dotfile)
+
+    # try the global user config file second
+
+    if 'XDG_CONFIG_HOME' in os.environ:
+        configfile = os.path.join(os.environ['XDG_CONFIG_HOME'], 'yamllint', 'config')
+    else:
+        configfile = os.path.expanduser('~/.config/yamllint/config')
+
+    if os.path.isfile(configfile):
+        return YamlLintConfig(file=configfile)
+
+    # use default config if no config file exists
+    return YamlLintConfig('extends: default')
+
+
+class Service(transport.Service):
+    language = 'yaml'
+
+    def parse(self, doc, options):
+        doc.diagnostics = []
+
+        # load the yamllint config file, if exists
+        config = get_yamllint_config(doc.path)
+
+        with open(doc.data_path) as f:
+            for problem in linter.run(f, config, doc.data_path):
+                loc = types.SourceLocation(line=problem.line, column=problem.column)
+
+                severity = types.Diagnostic.Severity.INFO
+                if problem.level == 'warning':
+                    severity = types.Diagnostic.Severity.WARNING
+                elif problem.level == 'error':
+                    severity = types.Diagnostic.Severity.ERROR
+
+                doc.diagnostics.append(types.Diagnostic(severity=severity,
+                                                        locations=[loc.to_range()],
+                                                        message=problem.message))
+
+
+class Document(transport.Document, transport.Diagnostics):
+    pass
+
+
+def run():
+    transport.Transport(Service, Document).run()
+
+if __name__ == '__main__':
+    run()
+
+# ex:ts=4:et:
diff --git a/backends/yaml/org.gnome.CodeAssist.v1.yaml.service.in 
b/backends/yaml/org.gnome.CodeAssist.v1.yaml.service.in
new file mode 100644
index 0000000..73b154c
--- /dev/null
+++ b/backends/yaml/org.gnome.CodeAssist.v1.yaml.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.gnome.CodeAssist.v1.yaml
+Exec=@backendexecdir@/yaml --transport dbus
diff --git a/backends/yaml/yaml.in b/backends/yaml/yaml.in
new file mode 100644
index 0000000..af5ddfb
--- /dev/null
+++ b/backends/yaml/yaml.in
@@ -0,0 +1,7 @@
+#!/usr/bin/env python@PYTHON_VERSION_MAJOR@
+
+import sys
+sys.path.insert(0, '@GCA_PYBACKENDS_ROOT_EX@')
+
+from gnome.codeassistance import yaml
+yaml.run()
diff --git a/configure.ac b/configure.ac
index 9056227..4e88aa8 100644
--- a/configure.ac
+++ b/configure.ac
@@ -401,6 +401,38 @@ color_enable_var("$enable_xml", [enable_xml_msg])
 AM_CONDITIONAL(BACKENDS_XML_ENABLE, test "x$enable_xml" = "xyes")
 
 
+dnl ================================================================
+dnl yaml backend configuration
+dnl ================================================================
+AC_ARG_ENABLE([yaml],
+              AS_HELP_STRING([--enable-yaml],[enable yaml backend]),
+              [enable_yaml=$enableval],
+              [enable_yaml=auto])
+
+AC_MSG_CHECKING([yaml backend])
+
+if test "x$enable_yaml" = "xauto"; then
+       if test "x$python_found" = "xno"; then
+               AC_MSG_RESULT([no])
+               enable_yaml=no
+       else
+               AC_MSG_RESULT([yes])
+               enable_yaml=yes
+       fi
+elif test "x$enable_yaml" != "xno"; then
+       if test "x$python_found" = "xno"; then
+               AC_MSG_ERROR([python not found])
+       else
+               AC_MSG_RESULT([yes])
+               enable_yaml=yes
+       fi
+else
+       AC_MSG_RESULT([no])
+fi
+
+color_enable_var("$enable_yaml", [enable_yaml_msg])
+
+AM_CONDITIONAL(BACKENDS_YAML_ENABLE, test "x$enable_yaml" = "xyes")
 
 dnl ================================================================
 dnl vala backend configuration
@@ -629,6 +661,8 @@ backends/css/org.gnome.CodeAssist.v1.css.service
 backends/css/css
 backends/json/org.gnome.CodeAssist.v1.json.service
 backends/json/json
+backends/yaml/org.gnome.CodeAssist.v1.yaml.service
+backends/yaml/yaml
 data/org.gnome.codeassistance.gschema.xml
 tests/dbus.conf
 ])
@@ -655,5 +689,6 @@ Configuration:
                shell:          $enable_sh_msg
                css:            $enable_css_msg
                json:           $enable_json_msg
+               yaml:           $enable_yaml_msg
 "
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]