# Copyright 2017 Palantir Technologies, Inc. import logging import subprocess import re import string from pyls import _utils, lsp from confls import hookimpl from confls.helpers import SYSTEMD_COMPLETOR log = logging.getLogger(__name__) def setup(): s = subprocess.Popen("systemd --dump-configuration".split(), stdout=subprocess.PIPE) lines = s.stdout.read().decode("utf-8") args = lines.split() args.append(None) keys = re.findall(r"\[\w+\]", lines) indexes = [args.index(key) for key in keys] keys.append(None) indexes.append(len(args)) slices = {key:(args.index(key) + 1, args.index(keys[keys.index(key) + 1])) for key in keys[:-1]} data = {} for key, value in slices.items(): start, end = value[0], value[1] section_args = args[start:end] t = [] for section_arg in section_args: split = re.split(r"\=", section_arg) try: a = split[0] b = split[1] d = {} d['key'] = a d['val'] = b t.append(d) except: print(split) data[key] = t return data ARGS = setup() #DIRECTIVE = helper.SystemdDirective() def get_active_key(document, position): lines = list(document.lines)[0:position["line"]] lines.reverse() for line in lines: line = line.strip() if re.match(r"\[\w+\]", line): key = line return key return None @hookimpl def pyls_completions(config, document, position): """Get formatted completions for current code position""" settings = config.plugin_settings('systemd', document_path=document.path) key = get_active_key(document, position) line = document.lines[position["line"]] word = line[0:position['character']] print(word, key) try: if key is None or "[" in word: t = list(SYSTEMD_COMPLETOR.get_units()) completions = [_format_section(c) for c in t] return completions else: key = key.replace("[", "").replace("]","")#.lower() t = list(SYSTEMD_COMPLETOR.get_all(word, key)) completions = [_format_argument(c) for c in t] return completions except Exception as e: print(e) return None def _format_argument(d): completion = { 'label': "{}".format(d.name), 'detail': "Value type: {}".format(d.value), 'documentation': "Found in: " + " ".join(d.path), 'sortText': d.name, 'insertText': d.name, } return completion def _format_section(d): completion = { 'label': "{}".format(d), 'detail': "details about {}".format(d), 'documentation': "doc...", 'sortText': d, 'insertText': d, } return completion def _label(definition): sig = definition.get_signatures() if definition.type in ('function', 'method') and sig: params = ', '.join(param.name for param in sig[0].params) return '{}({})'.format(definition.name, params) return definition.name def _detail(definition): try: return definition.parent().full_name or '' except AttributeError: return definition.full_name or ''