Ejemplo n.º 1
0
def path_to_ns(path, stop_paths=None):
    if stop_paths is None:
        stop_paths = ['src', 'test']

    stop_paths = sorted(stop_paths, key=lambda x: x.count('/'))

    raw_path_list = None

    for stop_path in reversed(stop_paths):
        m = re.search(stop_path, path)
        if m:
            raw_path_list = path[m.start():].replace(
                "_", "-").split('/')[stop_path.count('/') + 1:]
            raw_path_list[-1] = raw_path_list[-1].split('.')[0]
            break

    if raw_path_list is None:
        log_debug("Previous check did not work. Attempting project.clj")

        # Look for project.clj
        path = path.replace("_", "-").split('/')[1:]
        path[-1] = path[-1].split('.')[0]
        for ix, _ in enumerate(path):
            if os.path.exists(os.path.join(*["/", *path[:ix], "project.clj"])):
                raw_path_list = path[ix + 1:]
                break

    if raw_path_list is None:
        log_warning("Have not found any viable path")
        return None
    else:
        log_debug("Found path list: {}", raw_path_list)
        return ".".join(raw_path_list)
Ejemplo n.º 2
0
def path_to_ns(path, stop_paths=None, base_files=None):
    log_debug("Supplied path is {}", str(path))

    if not path:
        log_debug("Possibly empty or scratch buffer. Skipping")
        return

    if stop_paths is None:
        stop_paths = ['src', 'test']

    if base_files is None:
        base_files = ['project.clj', 'deps.edn', 'build.boot']

    stop_paths = sorted(stop_paths, key=lambda x: x.count('/'))

    raw_path_list = None

    for stop_path in reversed(stop_paths):
        stop_path = "/{}/".format(stop_path)
        ix = path[::-1].find(stop_path[::-1])
        log_debug("Attempting reverse match: [{}: {}] -> {}",
                  stop_path[::-1], path[::-1], ix)
        if ix > 0:
            startpos = len(path) - ix
            raw_path_list = path[startpos:].replace("_", "-").split('/')
            raw_path_list = [x for x in raw_path_list if x]
            if len(raw_path_list) > 0:
                raw_path_list[-1] = raw_path_list[-1].split('.')[0]
                break

    if raw_path_list is None:
        log_debug("Previous check did not work. Attempting base files")

        # Look for project.clj
        path = path.replace("_", "-").split('/')[1:]
        path[-1] = path[-1].split('.')[0]
        for ix, _ in enumerate(path):
            for bf in base_files:
                if os.path.exists(os.path.join(*["/", *path[:ix], bf])):
                    raw_path_list = path[ix+1:]
                    break

    if not raw_path_list:
        log_warning("Have not found any viable path")
        return ""
    else:
        log_debug("Found path list: {}", raw_path_list)
        return ".".join(raw_path_list)
Ejemplo n.º 3
0
def format_payload(payload):
    if type(payload) == str:
        return [payload]

    ls = []
    try:
        msg_id = payload.get('id', '****')
        for k, v in payload.items():
            key = k.lower()
            if key not in {'ns', 'session', 'id', 'op'}:
                if type(v) == list:
                    log_debug('v is a list')
                    header, trailer = v[0], v[1:]
                elif type(v) == dict:
                    log_debug('v is a dict')
                    formatted = format_dict(v)
                    log_debug('Got {}', str(formatted))
                    if len(formatted) > 0:
                        header, *trailer = formatted
                    else:
                        header = []
                elif '\n' in v:
                    header, *trailer = v.split('\n')
                else:
                    header, trailer = v, []

                if not header:
                    next

                if header.isspace() or header is "":
                    header, trailer = trailer[0], trailer[1:]

                ls.append("[{}][{: <9}] => {}".format(
                    str(msg_id)[-4:], key.upper(), str(header)
                ).strip())

                for i in trailer:
                    ls.append("{: <20} {}".format("", str(i)))
    except e:
        log_error("Couldn't finish producing output: {}", str(e))
    finally:
        if len(ls) == 0:
            log_warning("Empty output for ls: {}", str(payload))
        return ls
Ejemplo n.º 4
0
    def print_doc(msg):
        outcome = {}
        lines = []
        for key, value in definition['data'].items():
            log_debug("Working with key {}", key)
            if 'default' in value:
                obj = msg.get(key, value['default'])
            else:
                obj = msg[key]

            if obj:
                if 'transform' in value:
                    this, *other = transform_meta(value['transform'])
                    obj = value['transform'](obj, *[outcome[i] for i in other])

                if 'prepend' in value:
                    prepend = value['prepend']
                    if type(obj) == list:
                        obj = [prepend, *obj]
                    else:
                        obj = '{} {}'.format(prepend, obj)

                if 'rename' in value:
                    key = value['rename']
                outcome[key] = obj

        log_debug("current data: {}", outcome)

        for key in definition['format']:
            if type(key) == list and lines[-1] != '':
                lines.append('')
            elif key in outcome:
                obj = outcome[key]
                if obj:
                    obj_type = type(obj)
                    if obj_type == str:
                        lines.append(obj)
                    elif obj_type == list:
                        lines = [*lines, *obj]
                    else:
                        log_warning('Unknown obj type, skipping.')
        return lines
Ejemplo n.º 5
0
def path_to_ns(nvim):
    path = nvim.funcs.expand("%:p:r").replace("_", "-").split('/')[1:]
    raw_path_list = None

    for ix, node in enumerate(reversed(path)):
        if node == 'src':
            raw_path_list = path[ix * -1:]
            break

    if raw_path_list is None:
        # Look for project.clj
        for ix, _ in enumerate(path):
            if os.path.exists(os.path.join(*["/", *path[:ix], "project.clj"])):
                raw_path_list = path[ix + 1:]
                break

    if raw_path_list is None:
        log_warning("Have not found any viable path")
    else:
        log_debug("Found path list: {}", raw_path_list)

    return ".".join(raw_path_list)